forked from RSS-Bridge/rss-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadsBridge.php
More file actions
134 lines (118 loc) · 4.77 KB
/
ThreadsBridge.php
File metadata and controls
134 lines (118 loc) · 4.77 KB
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
<?php
class ThreadsBridge extends BridgeAbstract
{
const NAME = 'Threads';
const URI = 'https://www.threads.net/';
const DESCRIPTION = 'Say more with Threads — Instagram's new text app.';
const MAINTAINER = 'mdemoss';
const CACHE_TIMEOUT = 3600;
const PARAMETERS = [
'By username' => [
'u' => [
'name' => 'username',
'required' => true,
'exampleValue' => 'zuck',
'title' => 'Insert a user name'
],
'limit' => [
'name' => 'Limit',
'type' => 'number',
'required' => false,
'title' => 'Specify number of posts to fetch',
'defaultValue' => 5
]
]
];
protected $feedName = self::NAME;
public function getName()
{
return $this->feedName;
}
public function detectParameters($url)
{
// By username
$regex = '/^(https?:\/\/)?(www\.)?threads\.net\/(@)?([^\/?\n]+)/';
if (preg_match($regex, $url, $matches) > 0) {
$params['context'] = 'By username';
$params['u'] = urldecode($matches[3]);
return $params;
}
return null;
}
public function getURI()
{
return self::URI . '@' . $this->getInput('u');
}
// https://stackoverflow.com/a/3975706/421140
// Found this in FlaschenpostBridge, modified to return an array and take an object.
private function recursiveFind($haystack, $needle)
{
$found = [];
$iterator = new \RecursiveArrayIterator($haystack);
$recursive = new \RecursiveIteratorIterator(
$iterator,
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($recursive as $key => $value) {
if ($key === $needle) {
$found[] = $value;
}
}
return $found;
}
public function collectData()
{
$html = getSimpleHTMLDOMCached($this->getURI(), static::CACHE_TIMEOUT);
$jsonBlobs = $html->find('script[type="application/json"]');
$gatheredPosts = [];
$limit = $this->getInput('limit');
foreach ($jsonBlobs as $jsonBlob) {
// The structure of the JSON document is likely to change, but we're looking for "post" objects
foreach ($this->recursiveFind(json_decode($jsonBlob->innertext), 'post') as $post) {
if (!is_object($post) && !is_array($post)) {
continue;
}
$post = (array)$post;
if (!isset($post['code'])) {
continue;
}
$candidateCode = $post['code'];
// code should be like CzZk4-USq1O or Cy3m1VnRiwP or Cywjyrdv9T6 or CzZk4-USq1O
if (grapheme_strlen($candidateCode) == 11 and !isset($gatheredPosts[$candidateCode])) {
$gatheredPosts[$candidateCode] = [
'code' => $candidateCode,
'taken_at' => $post['taken_at'] ?? null,
];
if (count($gatheredPosts) >= $limit) {
break 2;
}
}
}
}
$this->feedName = html_entity_decode($html->find('meta[property=og:title]', 0)->content);
// todo: meta[property=og:description] could populate the feed description
foreach ($gatheredPosts as $postData) {
$item = [];
// post URL is like: https://www.threads.net/@zuck/post/Czrr520PZfh
$item['uri'] = $this->getURI() . '/post/' . $postData['code'];
$articleHtml = getSimpleHTMLDOMCached($item['uri'], 15778800); // cache time: six months
// Relying on meta tags ought to be more reliable.
if ($articleHtml->find('meta[property=og:type]', 0)->content != 'article') {
continue;
}
$item['title'] = $articleHtml->find('meta[property=og:description]', 0)->content;
$item['content'] = $articleHtml->find('meta[property=og:description]', 0)->content;
$item['author'] = html_entity_decode($articleHtml->find('meta[property=og:title]', 0)->content);
$imageUrl = $articleHtml->find('meta[property=og:image]', 0);
if ($imageUrl) {
$item['enclosures'][] = html_entity_decode($imageUrl->content);
}
// todo: parse hashtags out of content for $item['categories']
// Extract timestamp from profile JSON data (taken_at is a Unix timestamp)
if (isset($postData['taken_at']) && $postData['taken_at']) {
$item['timestamp'] = $postData['taken_at'];
}
$this->items[] = $item;
}
}
}