1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
<?php
class ImgsedBridge extends BridgeAbstract
{
const MAINTAINER = 'sysadminstory';
const NAME = 'Imgsed Bridge';
const URI = 'https://imgsed.com/';
const INSTAGRAMURI = 'https://www.instagram.com/';
const CACHE_TIMEOUT = 3600; // 1h
const DESCRIPTION = 'Returns Imgsed (Instagram viewer) content by user';
const PARAMETERS = [
'Username' => [
'u' => [
'name' => 'username',
'type' => 'text',
'title' => 'Instagram username you want to follow',
'exampleValue' => 'aesoprockwins',
'required' => true,
],
'post' => [
'name' => 'posts',
'type' => 'checkbox',
'title' => 'Show posts for this Instagram user',
'defaultValue' => 'checked',
],
'story' => [
'name' => 'stories',
'type' => 'checkbox',
'title' => 'Show stories for this Instagram user',
],
'tagged' => [
'name' => 'tagged',
'type' => 'checkbox',
'title' => 'Show tagged post for this Instagram user',
],
]
];
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 collectData()
{
$username = $this->getInput('u');
try {
// Check if the user exist
$html = getSimpleHTMLDOMCached(self::URI . $username . '/');
if ($this->getInput('post')) {
$this->collectPosts();
}
if ($this->getInput('story')) {
$this->collectStories();
}
if ($this->getInput('tagged')) {
$this->collectTaggeds();
}
} catch (HttpException $e) {
throw new \Exception(sprintf('Unable to find user `%s`', $username));
}
}
private function collectPosts()
{
$username = $this->getInput('u');
$html = getSimpleHTMLDOMCached(self::URI . $username . '/');
$html = defaultLinkTo($html, self::URI);
foreach ($html->find('div[class=item]') as $post) {
$url = $post->find('a', 0)->href;
$instagramURL = $this->convertURLToInstagram($url);
$date = $this->parseDate($post->find('div[class=time]', 0)->plaintext);
$description = $post->find('img', 0)->alt;
$imageUrl = $post->find('img', 0)->src;
// Sometimes, there is some lazy image instead of the real URL
if ($imageUrl == 'https://imgsed.com/img/lazy.jpg') {
$imageUrl = $post->find('img', 0)->getAttribute('data-src');
}
$download = $post->find('a[class=download]', 0)->href;
$author = $username;
$uid = $post->find('a', 0)->href;
$title = 'Post - ' . $username . ' - ' . $this->descriptionToTitle($description);
// Checking post type
$isVideo = (bool) $post->find('i[class=video]', 0);
$videoNote = $isVideo ? '<p><i>(video)</i></p>' : '';
$isMoreContent = (bool) $post->find('svg', 0);
$moreContentNote = $isMoreContent ? '<p><i>(multiple images and/or videos)</i></p>' : '';
$this->items[] = [
'uri' => $url,
'author' => $author,
'timestamp' => $date,
'title' => $title,
'thumbnail' => $imageUrl,
'enclosures' => [$imageUrl, $download],
'content' => <<<HTML
<a href="{$url}">
<img loading="lazy" src="{$imageUrl}" alt="{$description}"/>
</a>
{$videoNote}
{$moreContentNote}
<p>{$description}<p>
<p><a href="{$download}">Download</a></p>
<p><a href="{$instagramURL}">Display on Instagram</a></p>
HTML,
'uid' => $uid
];
}
}
private function collectStories()
{
try {
$username = $this->getInput('u');
$html = getSimpleHTMLDOMCached(self::URI . 'api/media/?name=' . $username);
$json = Json::decode($html);
foreach ($json as $post) {
$url = $post['src'];
$imageUrl = $post['thumb'];
$download = $url;
$author = $username;
$uid = $url;
$title = 'Story - ' . $username;
$this->items[] = [
'uri' => $url,
'author' => $author,
'title' => $title,
'thumbnail' => $imageUrl,
'enclosures' => [$imageUrl, $download],
'content' => <<<HTML
<a href="{$url}">
<img loading="lazy" src="{$imageUrl}" alt="story"/>
</a>
<p><a href="{$download}">Download</a></p>
HTML,
'uid' => $uid
];
}
} catch (Exception $e) {
// If it fails, it's because there are no stories, so don't do anything
}
}
private function collectTaggeds()
{
$username = $this->getInput('u');
try {
$html = getSimpleHTMLDOMCached(self::URI . 'tagged/' . $username . '/');
$html = defaultLinkTo($html, self::URI);
foreach ($html->find('div[class=item]') as $post) {
$url = $post->find('a', 1)->href;
$instagramURL = $this->convertURLToInstagram($url);
$fromURL = $post->find('div[class=username]', 0)->find('a', 0)->href;
$fromUsername = $post->find('div[class=username]', 0)->plaintext;
$date = $this->parseDate($post->find('div[class=time]', 0)->plaintext);
$description = $post->find('img', 0)->alt;
$imageUrl = $post->find('img', 0)->src;
$download = $post->find('a[class=download]', 0)->href;
$author = $fromUsername;
$uid = $post->find('a', 0)->href;
$title = 'Tagged - ' . $fromUsername . ' - ' . $this->descriptionToTitle($description);
// Checking post type
$isVideo = (bool) $post->find('i[class=video]', 0);
$videoNote = $isVideo ? '<p><i>(video)</i></p>' : '';
$isMoreContent = (bool) $post->find('svg', 0);
$moreContentNote = $isMoreContent ? '<p><i>(multiple images and/or videos)</i></p>' : '';
$this->items[] = [
'uri' => $url,
'author' => $author,
'timestamp' => $date,
'title' => $title,
'thumbnail' => $imageUrl,
'enclosures' => [$imageUrl, $download],
'content' => <<<HTML
<a href="{$url}">
<img loading="lazy" src="{$imageUrl}" alt="{$description}"/>
</a>
{$videoNote}
{$moreContentNote}
<p>From <a href="{$fromURL}">{$fromUsername}</a></p>
<p>{$description}<p>
<p><a href="{$download}">Download</a></p>
<p><a href="{$instagramURL}">Display on Instagram</a></p>
HTML,
'uid' => $uid
];
}
} catch (Exception $e) {
// If it fails, it's because the account was not tagged
}
}
private function parseDate($content)
{
// Parse date, and transform the date into a timetamp, even in a case of a relative date
$date = date_create();
// 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
$dateString = trim(str_replace(' ago', '', $content));
// Replace the article "an" or "a" by the number "1" to be a valid PHP date interval
$dateString = preg_replace('/^((an|a) )/m', '1 ', $dateString);
$relativeDate = date_interval_create_from_date_string($dateString);
if ($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 {
$this->logger->info(sprintf('Unable to parse date string: %s', $dateString));
}
return date_format($date, 'r');
}
public function getURI()
{
if (!is_null($this->getInput('u'))) {
return urljoin(self::URI, '/' . $this->getInput('u') . '/');
}
return parent::getURI();
}
private function convertURLToInstagram($url)
{
return str_replace(self::URI, self::INSTAGRAMURI, $url);
}
private function descriptionToTitle($description)
{
return strlen($description) > 60 ? mb_substr($description, 0, 57) . '...' : $description;
}
public function getName()
{
if (!is_null($this->getInput('u'))) {
$types = [];
if ($this->getInput('post')) {
$types[] = 'Posts';
}
if ($this->getInput('story')) {
$types[] = 'Stories';
}
if ($this->getInput('tagged')) {
$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] ?? '';
if (count($types) > 1) {
for ($i = 1; $i < count($types) - 1; $i++) {
$typesText .= ', ' . $types[$i];
}
$typesText .= ' & ' . $types[$i];
}
return 'Username ' . $this->getInput('u') . ' - ' . $typesText . ' - Imgsed Bridge';
}
return parent::getName();
}
public function detectParameters($url)
{
$params = [
'post' => 'on',
'story' => 'on',
'tagged' => 'on',
];
$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})\/)/';
if (preg_match($regex, $url, $matches) > 0) {
$params['context'] = 'Username';
// Extract detected domain using the regex
$domain = $matches[8] ?? $matches[4];
if ($domain == 'imgsed.com') {
$params['u'] = $matches[10];
return $params;
} elseif ($domain == 'instagram.com') {
$params['u'] = $matches[5];
return $params;
} else {
return null;
}
} else {
return null;
}
}
}
|