|
| 1 | +<?php |
| 2 | + |
| 3 | +class GreatFonBridge extends BridgeAbstract |
| 4 | +{ |
| 5 | + const MAINTAINER = 'sysadminstory'; |
| 6 | + const NAME = 'GreatFon Bridge'; |
| 7 | + const URI = 'https://greatfon.com/'; |
| 8 | + const INSTAGRAMURI = 'https://www.instagram.com/'; |
| 9 | + const CACHE_TIMEOUT = 3600; // 1h |
| 10 | + const DESCRIPTION = 'Returns GreatFon (Instagram viewer) content by user'; |
| 11 | + |
| 12 | + const PARAMETERS = [ |
| 13 | + 'Username' => [ |
| 14 | + 'u' => [ |
| 15 | + 'name' => 'username', |
| 16 | + 'type' => 'text', |
| 17 | + 'title' => 'Instagram username you want to follow', |
| 18 | + 'exampleValue' => 'aesoprockwins', |
| 19 | + 'required' => true, |
| 20 | + ], |
| 21 | + ] |
| 22 | + ]; |
| 23 | + const TEST_DETECT_PARAMETERS = [ |
| 24 | + 'https://www.instagram.com/instagram/' => ['context' => 'Username', 'u' => 'instagram'], |
| 25 | + 'https://instagram.com/instagram/' => ['context' => 'Username', 'u' => 'instagram'], |
| 26 | + 'https://greatfon.com/v/instagram' => ['context' => 'Username', 'u' => 'instagram'], |
| 27 | + 'https://www.greatfon.com/v/instagram' => ['context' => 'Username', 'u' => 'instagram'], |
| 28 | + ]; |
| 29 | + |
| 30 | + public function collectData() |
| 31 | + { |
| 32 | + $username = $this->getInput('u'); |
| 33 | + $html = getSimpleHTMLDOMCached(self::URI . '/v/' . $username); |
| 34 | + $html = defaultLinkTo($html, self::URI); |
| 35 | + |
| 36 | + foreach ($html->find('div[class*=content__item]') as $post) { |
| 37 | + // Skip the ads |
| 38 | + if (!str_contains($post->class, 'ads')) { |
| 39 | + $url = $post->find('a[href^=https://greatfon.com/c/]', 0)->href; |
| 40 | + $date = $this->parseDate($post->find('div[class=content__time-text]', 0)->plaintext); |
| 41 | + $description = $post->find('img', 0)->alt; |
| 42 | + $imageUrl = $post->find('img', 0)->src; |
| 43 | + $author = $username; |
| 44 | + $uid = $url; |
| 45 | + $title = 'Post - ' . $username . ' - ' . $this->descriptionToTitle($description); |
| 46 | + |
| 47 | + // Checking post type |
| 48 | + $isVideo = (bool) $post->find('div[class=content__camera]', 0); |
| 49 | + $videoNote = $isVideo ? '<p><i>(video)</i></p>' : ''; |
| 50 | + |
| 51 | + $this->items[] = [ |
| 52 | + 'uri' => $url, |
| 53 | + 'author' => $author, |
| 54 | + 'timestamp' => $date, |
| 55 | + 'title' => $title, |
| 56 | + 'thumbnail' => $imageUrl, |
| 57 | + 'enclosures' => [$imageUrl], |
| 58 | + 'content' => <<<HTML |
| 59 | +<a href="{$url}"> |
| 60 | + <img loading="lazy" src="{$imageUrl}" alt="{$description}"/> |
| 61 | +</a> |
| 62 | +{$videoNote} |
| 63 | +<p>{$description}<p> |
| 64 | +HTML, |
| 65 | + 'uid' => $uid |
| 66 | + ]; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private function parseDate($content) |
| 72 | + { |
| 73 | + // Parse date, and transform the date into a timetamp, even in a case of a relative date |
| 74 | + $date = date_create(); |
| 75 | + |
| 76 | + // Content trimmed to be sure that the "article" is at the beginning of the string and remove "ago" to make it a valid PHP date interval |
| 77 | + $dateString = trim(str_replace(' ago', '', $content)); |
| 78 | + |
| 79 | + // Replace the article "an" or "a" by the number "1" to be a valid PHP date interval |
| 80 | + $dateString = preg_replace('/^((an|a) )/m', '1 ', $dateString); |
| 81 | + |
| 82 | + $relativeDate = date_interval_create_from_date_string($dateString); |
| 83 | + if ($relativeDate) { |
| 84 | + date_sub($date, $relativeDate); |
| 85 | + // 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 |
| 86 | + date_time_set($date, 0, 0, 0, 0); |
| 87 | + } else { |
| 88 | + $this->logger->info(sprintf('Unable to parse date string: %s', $dateString)); |
| 89 | + } |
| 90 | + return date_format($date, 'r'); |
| 91 | + } |
| 92 | + |
| 93 | + public function getURI() |
| 94 | + { |
| 95 | + if (!is_null($this->getInput('u'))) { |
| 96 | + return urljoin(self::URI, '/v/' . $this->getInput('u')); |
| 97 | + } |
| 98 | + |
| 99 | + return parent::getURI(); |
| 100 | + } |
| 101 | + |
| 102 | + public function getIcon() |
| 103 | + { |
| 104 | + return static::URI . '/images/favicon-hub-3ede543aa6d1225e8dc016ccff6879c8.ico?vsn=d'; |
| 105 | + } |
| 106 | + |
| 107 | + private function descriptionToTitle($description) |
| 108 | + { |
| 109 | + return strlen($description) > 60 ? mb_substr($description, 0, 57) . '...' : $description; |
| 110 | + } |
| 111 | + |
| 112 | + public function getName() |
| 113 | + { |
| 114 | + if (!is_null($this->getInput('u'))) { |
| 115 | + return 'Username ' . $this->getInput('u') . ' - GreatFon Bridge'; |
| 116 | + } |
| 117 | + return parent::getName(); |
| 118 | + } |
| 119 | + |
| 120 | + public function detectParameters($url) |
| 121 | + { |
| 122 | + $regex = '/^http(s|):\/\/((www\.|)(instagram.com)\/([a-zA-Z0-9_\.]{1,30})(\/reels\/|\/tagged\/|\/|)|(www\.|)(greatfon.com)\/v\/([a-zA-Z0-9_\.]{1,30}))/'; |
| 123 | + if (preg_match($regex, $url, $matches) > 0) { |
| 124 | + $params['context'] = 'Username'; |
| 125 | + // Extract detected domain using the regex |
| 126 | + $domain = $matches[8] ?? $matches[4]; |
| 127 | + if ($domain == 'greatfon.com') { |
| 128 | + $params['u'] = $matches[9]; |
| 129 | + return $params; |
| 130 | + } elseif ($domain == 'instagram.com') { |
| 131 | + $params['u'] = $matches[5]; |
| 132 | + return $params; |
| 133 | + } else { |
| 134 | + return null; |
| 135 | + } |
| 136 | + } else { |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments