-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCuckooNest.php
216 lines (216 loc) · 8.11 KB
/
CuckooNest.php
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
<?php
require_once "vendor/autoload.php";
error_reporting(E_ALL ^ E_DEPRECATED);//for hQuery
use duzun\hQuery;
class CuckooNest{
public $target_base_url = "https://nitter.net/";
public $status = array();
public $page;
public $issues = [];
public $basename_directory;
private $hquery_cache_path = "../../cache/";
private $hquery_cache_expires = 3600 * 2; //couple of hours
private $status_path = "./status.json";
private $images_path = "./images/";
private $existing = array('posts'=>array(), 'images'=>array());
function __construct($current_directory=__DIR__){
chdir($current_directory);
$this->basename_directory = basename($current_directory);
if(!class_exists('hQuery')){ return $this->issue("missing hQuery class"); }
hQuery::$cache_path = $this->hquery_cache_path;
hQuery::$cache_expires = $this->hquery_cache_expires;
$this->loadStatus();
$this->checkExistingPosts();
$this->checkExistingImages();
$this->getPage();
$this->getProfileFromPage();
$this->getPostsFromPage();
$this->saveStatus();
$this->toJSON();
}
function loadStatus(){
if(!file_exists($this->status_path)){
$this->status = array(
'username'=>$this->basename_directory,
'fullname'=>"", 'avatar'=>"",
'posts'=>[],
'success'=>true
);
$this->saveStatus(); //to check if PHP can write to this folder
}else{
$status_json = file_get_contents($this->status_path);
if(!$status_json){ return $this->issue("could not open ".$this->status_path); }
$this->status = json_decode($status_json, true);
}
return $this->status;
}
function saveStatus(){
$status_json = json_encode($this->status, true);
$saved = file_put_contents($this->status_path, $status_json, LOCK_EX);
if(!$saved){
return $this->issue("could not save to ".$this->status_path);
}
return $saved;
}
function checkExistingPosts(){
foreach($this->status['posts'] as $post){
$this->existing['posts'][$post['identifier']] = true;
}
}
function checkExistingImages(){
if(!is_dir($this->images_path)){
$created = mkdir($this->images_path);
if(!$created){
return $this->issue("could not create ".$this->images_path);
}
return true;
}
foreach(glob($this->images_path."*.*") as $path){
$image = pathinfo($path);
if(!$image['filename'] || !$image['extension']){ continue; }
$this->existing['images'][$image['filename']] = $image['extension'];
}
}
function issue($message){
$this->issues[] = $message;
return false;
}
function exitIfAnyIssuesFound(){
if($this->issues){ $this->toJSON(); }
}
function toJSON(){
header("Content-Type: application/json");
if($this->issues){
exit(json_encode(array('success'=>false, 'issues'=>$this->issues), true));
}
exit(json_encode($this->status, true));
}
function getPage(){
if(!$this->status['username']){ return $this->issue("status has no username"); }
$url = $this->target_base_url.$this->status['username'];
$page = hQuery::fromUrl($url, [
"Accept" => "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8",
"User-Agent" => "CuckooNest"
]);
if(!$page){
return $this->issue("No 200 response from ".$url);
}
$this->page = $page;
return $this->page;
}
function findElements($selectors, $target=false, $optionals=[]){
$target = $target ? $target : $this->page;
$elements = array();
foreach($selectors as $name=>$selector){
$found = $target->find($selector);
if(!$found && !in_array($name, $optionals)){
$this->issue("found no {$name} element with {$selector} selector");
}else{
$elements[$name] = $found;
}
}
$this->exitIfAnyIssuesFound();
return $elements;
}
function getImageURL($url){
if(!$url){ return false; }
$filename = crc32($url);
$path = "images/{$filename}.jpg";
if(isset($this->existing['images'][$filename])){
return $path;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$fp = fopen($path, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_exec($ch);
if(curl_errno($ch)){
$error_msg = curl_error($ch);
return $this->issue("could not download image from {$url}: {$error_msg}");
}
curl_close($ch);
fclose($fp);
return $path;
}
function getProfileFromPage(){
$page = $this->findElements(array(
'fullname'=>'.profile-card-fullname',
'username'=>'.profile-card-username',
'avatar'=>'.profile-card-avatar'
));
$username = str_replace('@', '', $page['username']->text());
if(strtolower($username) != strtolower($this->status['username'])){
return $this->issue("requested {$this->status['username']} and response is for {$username}");
}
$this->status['fullname'] = $page['fullname']->text();
$this->status['avatar'] = $this->getImageURL($page['avatar']->attr('href'));
return $this->status;
}
function getPostsFromPage(){
$page = $this->findElements(array('items'=>'.timeline-item'));
foreach($page['items'] as $item){
$elements = $this->findElements(array(
'link'=>'.tweet-link',
'retweet'=>'.retweet-header',
'date'=>'.tweet-date',
'content'=>'.tweet-content',
'still_images'=>'.still-image img',
'fullname'=>'.tweet-header .fullname',
'avatar'=>'.tweet-header .avatar.round',
), $item, ['retweet', 'still_images']);
$identifier = crc32($elements['link']->attr('href'));
if(isset($this->existing['posts'][$identifier])){
continue;
}
$post = array(
'identifier'=>$identifier,
'repost'=>!!$elements['retweet'],
'html'=>strip_tags($elements['content']->html(), '<a>'),
'date'=>$elements['date']->attr('title'),
'images'=>[]
);
if($elements['still_images']){
foreach($elements['still_images'] as $image){
array_push($post['images'], $this->getImageURL($image->attr('src')));
}
}
if($post['repost']){
$post['fullname'] = $elements['fullname']->text();
$post['avatar'] = $this->getImageURL($elements['avatar']->attr('src'));
}
array_push($this->status['posts'], $post);
}
return $this->status;
}
static function generateEndpoints($usernames=array()){
if(!in_array(__FILE__, get_included_files())){
return false;
}
$index_php = "
<?php
require_once '../../CuckooNest.php';
\$cn = new CuckooNest(__DIR__);
";
foreach(['nests', 'cache'] as $foldername){
$folder = __DIR__."/{$foldername}";
if(is_dir($folder)){ continue; }
mkdir($folder);
}
foreach($usernames as $username){
$folder = __DIR__."/nests/{$username}";
$path = $folder."/index.php";
if(is_dir($folder)){
echo "\nalready created folder for ".$username;
continue;
}
mkdir($folder);
file_put_contents($path, $index_php);
echo "\ncreated folder for ".$username;
}
echo PHP_EOL;
}
}
if(basename(__FILE__) == "CuckooNest.php" && $argv && count($argv) > 1){
CuckooNest::generateEndpoints(array_slice($argv, 1));
}