[ImgsedBridge] Fix and improvements (#3710)

* [ImgsedBridge] Fix and improvements

- Display an error if the user doesn't select at least an content type
  to display
- Unsplit the regular expression to make the URL of imgsed.com work too
- Remove the "hour part" of the publication date : the website shows
  only the number of days if the content is older than one day

* [ImgsedBridge] Fix and improvements

Fix syntax

* [ImgsedBridge] Fix and improvements

- Fix TEST_DETECT_PARAMETERS
- change detectParameters regular expression to match more instagram.com
  URLs

* [ImgsedBridge] Fix and improvements

- Fix date parsing for interval 'a day'

* lint

---------

Co-authored-by: Dag <me@dvikan.no>
This commit is contained in:
sysadminstory 2023-10-01 19:00:13 +02:00 committed by GitHub
parent 7273a05f02
commit 0c92cf32d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 4 deletions

View File

@ -36,6 +36,12 @@ class ImgsedBridge extends BridgeAbstract
], ],
] ]
]; ];
const TEST_DETECT_PARAMETERS = [
'https://www.instagram.com/instagram/' => ['context' => 'Username', 'u' => 'instagram', 'post' => 'on', 'story' => 'on', 'tagged' => 'on'],
'https://instagram.com/instagram/' => ['context' => 'Username', 'u' => 'instagram', 'post' => 'on', 'story' => 'on', 'tagged' => 'on'],
'https://imgsed.com/instagram/' => ['context' => 'Username', 'u' => 'instagram', 'post' => 'on', 'story' => 'on', 'tagged' => 'on'],
'https://www.imgsed.com/instagram/' => ['context' => 'Username', 'u' => 'instagram', 'post' => 'on', 'story' => 'on', 'tagged' => 'on'],
];
public function getURI() public function getURI()
{ {
@ -213,9 +219,16 @@ HTML,
{ {
$date = date_create(); $date = date_create();
$dateString = str_replace(' ago', '', $content); $dateString = str_replace(' ago', '', $content);
// Special case : 'a day' is not a valid interval in PHP, so replace it with it's PHP equivalenbt : '1 day'
if ($dateString == 'a day') {
$dateString = '1 day';
}
$relativeDate = date_interval_create_from_date_string($dateString); $relativeDate = date_interval_create_from_date_string($dateString);
if ($relativeDate) { if ($relativeDate) {
date_sub($date, $relativeDate); date_sub($date, $relativeDate);
// As the relative interval has the precision of a day for date older than 24 hours, we can remove the hour of the date, as it is not relevant
date_time_set($date, 0, 0, 0, 0);
} else { } else {
$this->logger->info(sprintf('Unable to parse date string: %s', $dateString)); $this->logger->info(sprintf('Unable to parse date string: %s', $dateString));
} }
@ -244,7 +257,13 @@ HTML,
if ($this->getInput('tagged')) { if ($this->getInput('tagged')) {
$types[] = 'Tags'; $types[] = 'Tags';
} }
// If no content type is selected, this bridge does nothing, so we return an error
if (count($types) == 0) {
returnClientError('You must select at least one of the content type : Post, Stories or Tags !');
}
$typesText = $types[0] ?? ''; $typesText = $types[0] ?? '';
if (count($types) > 1) { if (count($types) > 1) {
for ($i = 1; $i < count($types) - 1; $i++) { for ($i = 1; $i < count($types) - 1; $i++) {
$typesText .= ', ' . $types[$i]; $typesText .= ', ' . $types[$i];
@ -262,10 +281,9 @@ HTML,
$params = [ $params = [
'post' => 'on', 'post' => 'on',
'story' => 'on', 'story' => 'on',
'tagged' => 'on' 'tagged' => 'on',
]; ];
$regex = '/^http(s|):\/\/((www\.|)(instagram.com)\/([a-zA-Z0-9_\.]{1,30})\/(reels\/|tagged\/|) $regex = '/^http(s|):\/\/((www\.|)(instagram.com)\/([a-zA-Z0-9_\.]{1,30})(\/reels\/|\/tagged\/|\/|)|(www\.|)(imgsed.com)\/(stories\/|tagged\/|)([a-zA-Z0-9_\.]{1,30})\/)/';
|(www\.|)(imgsed.com)\/(stories\/|tagged\/|)([a-zA-Z0-9_\.]{1,30})\/)/';
if (preg_match($regex, $url, $matches) > 0) { if (preg_match($regex, $url, $matches) > 0) {
$params['context'] = 'Username'; $params['context'] = 'Username';
// Extract detected domain using the regex // Extract detected domain using the regex
@ -273,7 +291,7 @@ HTML,
if ($domain == 'imgsed.com') { if ($domain == 'imgsed.com') {
$params['u'] = $matches[10]; $params['u'] = $matches[10];
return $params; return $params;
} else if ($domain == 'instagram.com') { } elseif ($domain == 'instagram.com') {
$params['u'] = $matches[5]; $params['u'] = $matches[5];
return $params; return $params;
} else { } else {