-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweet2json.php
More file actions
142 lines (121 loc) · 4.76 KB
/
tweet2json.php
File metadata and controls
142 lines (121 loc) · 4.76 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
135
136
137
138
139
140
141
142
<?php
libxml_use_internal_errors(true);
class Tweet2JSON
{
private function innerHTML(\DOMElement $element): string
{
$doc = $element->ownerDocument;
$html = "";
foreach ($element->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
}
public function getJSON(string $twitterName): array
{
// Get raw JSON from Twitter API
$rawJSONContent = json_decode(
file_get_contents(
"https://cdn.syndication.twimg.com/timeline/profile?screen_name=$twitterName"
),
true
);
// Make sure content is valid, else return empty array
if (
!boolval($rawJSONContent) ||
$rawJSONContent["headers"]["status"] !== 200
) {
return [];
}
// Get raw HTML
$rawHTMLContent = $rawJSONContent["body"];
// Instantiate DOMDocument
$doc = new DOMDocument();
// Load raw HTML
$doc->loadHTML($rawHTMLContent);
// Get 'ol' html tags
$tweetNodes = $doc->getElementsByTagName("ol")[0]->childNodes ?? [];
$tweets = [];
foreach ($tweetNodes as $tweetNode) {
if (get_class($tweetNode) === "DOMText") {
continue;
}
// Get timestamp
($time = $tweetNode->getElementsByTagName("time")[0] ?? null) &&
($time = $time->getAttribute("datetime") ?? null);
($temp = new DOMDocument()) &&
$temp->loadHTML($doc->saveHTML($tweetNode));
$xpath = new DomXpath($temp);
// Get raw text
$text =
$xpath->query('//p[@class="timeline-Tweet-text"]')->item(0)
->nodeValue ?? null;
// Like Tweet URL (Twitter)
($likeTweetURL =
$xpath->query('//a[@data-scribe="element:heart"]')->item(0) ??
null) && ($likeTweetURL = $likeTweetURL->getAttribute("href"));
// Share Tweet URL (Twitter)
($shareTweetURL_twitter =
$xpath->query('//a[@data-scribe="element:twitter"]')->item(0) ??
null) &&
($shareTweetURL_twitter = $shareTweetURL_twitter->getAttribute(
"href"
));
// Share Tweet URL (Facebook)
($shareTweetURL_facebook =
$xpath
->query('//a[@data-scribe="element:facebook"]')
->item(0) ?? null) &&
($shareTweetURL_facebook = $shareTweetURL_facebook->getAttribute(
"href"
));
// Share Tweet URL (LinkedIn)
($shareTweetURL_linkedin =
$xpath
->query('//a[@data-scribe="element:linkedin"]')
->item(0) ?? null) &&
($shareTweetURL_linkedin = $shareTweetURL_linkedin->getAttribute(
"href"
));
// Share Tweet URL (Tumblr)
($shareTweetURL_tumblr =
$xpath->query('//a[@data-scribe="element:tumblr"]')->item(0) ??
null) &&
($shareTweetURL_tumblr = $shareTweetURL_tumblr->getAttribute(
"href"
));
// Images
$imageURLs = [];
$imageNodes = $xpath->query("//img[@data-image]");
foreach ($imageNodes as $imageNode) {
if ($imageNode->getAttribute("alt") === "Embedded video") {
$imageURLs[] =
$imageNode->getAttribute("data-image") . ".png"; // Thumbnail URL
$imageURLs[] =
str_replace(
"https://pbs.twimg.com/tweet_video_thumb",
"https://video.twimg.com/tweet_video",
$imageNode->getAttribute("data-image")
) . ".mp4"; // Video URL
} else {
$imageURLs[] =
$imageNode->getAttribute("data-image") .
"." .
$imageNode->getAttribute("data-image-format"); // Image URL
}
}
$tweets[] = [
"timestamp" => $time,
"text" => $text,
"like-tweet-url" => $likeTweetURL,
"media" => $imageURLs,
"share-tweet-url-twitter" => $shareTweetURL_twitter,
"share-tweet-url-facebook" => $shareTweetURL_facebook,
"share-tweet-url-linkedin" => $shareTweetURL_linkedin,
"share-tweet-url-tumblr" => $shareTweetURL_tumblr,
];
}
return $tweets;
}
}
?>