-
Notifications
You must be signed in to change notification settings - Fork 0
/
furaffinity.class.php
332 lines (311 loc) · 9.64 KB
/
furaffinity.class.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/*
* FurAffinity API PHP
*
* PHP version 5.4.45
*
* @package FurAffinity-API-PHP
* @author d1KdaT <i@d1kdat.ru>
* @version 1.0.0
* @license MIT License
* @link https://github.com/d1KdaT/FurAffinity-API-PHP
*/
/*
* Two classes to describe errors in requests
*/
class TimeOutException extends Exception { }
class BadRespondException extends Exception { }
class FurAffinityAPI
{
/*
* @var string
*/
private $username;
/*
* @var string
*/
private $cookies;
/*
* Create an object with the user's cookies: "a" and "b" from FurAffinity
*
* @param array(username, a, b)
*
* @throw \RuntimeException - cURL is not installed
* @throw \InvalidArgumentException - lost one or more of the parameters
*/
public function __construct($settings)
{
if(!function_exists('curl_init'))
{
throw new RuntimeException("cURL is needed, see: http://curl.haxx.se/docs/install.html");
}
if(!isset($settings['username']) || !isset($settings['a']) || !isset($settings['b']))
{
throw new InvalidArgumentException("Lost one or more of the settings parameters");
}
$cookie = array();
$this->username = $settings['username'];
foreach($settings as $k => $v)
{
if($k == "username")
{
$this->username = $settings['username'];
}
else
{
$cookie[$k] = $v;
}
}
foreach($cookie as $k => $v)
{
$this->cookies[] = $k."=".$v;
}
$this->cookies = implode("; ", $this->cookies);
}
/*
* Get information about submission
*
* @param int
*
* @return mixed [array(title, author, username, file) or bool]
*/
public function getById($id)
{
$return = false;
$response = $this->curl("http://www.furaffinity.net/view/".$id."/");
if($response)
{
if(preg_match("/by \<a href\=\"\/user\/(.+)\/\"\>(.+)\<\/a\>/ui", $response, $match) || preg_match("/<a href=\"\/user\/(.+)\/\"><h2>(.+)<\/h2><\/a>/ui", $response, $match))
{
$return['author'] = $match[2];
$return['username'] = $match[1];
}
if($return && (preg_match("/\<b\>\<a href\=\"(.+)\"\>Download\<\/a\>\<\/b\>/ui", $response, $match) || preg_match("/<a class=\"button section-button\" href=\"(.+)\">Download<\/a>/ui", $response, $match)))
{
if(preg_match("/^\/\//",$match[1])) $match[1] = "http:".$match[1];
$return['file'] = $match[1];
}
if($return && preg_match("/<meta property=\"og:title\" content=\"(.+) by ".preg_quote($return['author'])."\" \/>/ui", $response, $match))
{
$return['title'] = $match[1];
}
return $return;
}
else
{
return false;
}
}
/*
* Get user watchlist
*
* @param string
*
* @return mixed [array(array(username, screen_name)) or bool]
*/
public function getWatchlist($username = null)
{
if($username == null)
{
$username = $this->username;
}
$return = false;
$i = 1;
while(true)
{
$response = $this->curl("http://www.furaffinity.net/watchlist/by/".$username."/".$i."/");
if(preg_match_all("/<a href=\"\/user\/(.+)\/\" target=\"_blank\"><span class=\"artist_name\">(.+)<\/span><\/a>/ui", $response, $match) || preg_match_all("/<a href=\"\/user\/(.+)\/\" target=\"_blank\">(.+)<\/a>/ui", $response, $match))
{
if(count($match[1]) > 0)
{
foreach($match[1] as $k => $v)
{
$return[] = array(
"username" => $v,
"screen_name" => $match[2][$k]);
}
}
else
{
break;
}
}
else
{
break;
}
$i++;
}
return $return;
}
/*
* @param string
*
* @throw \TimeOutException - Server didn't respond
*
* @return bool
*/
public function checkUserExist($username = null)
{
if($username == null)
{
$username = $this->username;
}
$return = false;
$response = $this->curl("http://www.furaffinity.net/user/".$username."/");
if(!$response)
{
throw new TimeOutException("The server didn't respond for a certain time");
}
if($response && !preg_match("/<font face=\'Verdana\' size=1>This user cannot be found\.<br\/>/ui", $response))
{
$return = true;
}
return $return;
}
/*
* Check (setting username == authorized username)
*
* @throw \TimeOutException - Server didn't respond
* @throw \BadRespondException - Failed to load the content (CloudFlare DDoS Protection)
*
* @return bool
*/
public function checkLogIn()
{
$return = false;
$response = $this->curl("http://www.furaffinity.net/submit/");
if(!$response)
{
throw new TimeOutException("The server didn't respond for a certain time");
}
if($response && (preg_match("/<a id=\"my-username\" href=\"\/user\/(.+)\/\">~(.+)<\/a>/ui", $response, $match) || preg_match("/<a id=\"my-username\" class=\"top-heading hideonmobile\" href=\"\/user\/(.+)\/\">~(.+)<span class=\"hideondesktop\">/ui", $response, $match)))
{
if($match[1] == $this->username)
{
$return = true;
}
}
elseif($response && !preg_match("/Fur Affinity/ui", $response))
{
throw new BadRespondException("The content is not loaded");
}
return $return;
}
/*
* @param string
*
* @return int [0, 1 or 2 (0 - bad respond, 1 - success, 2 - already)]
*/
public function addWatch($username)
{
$return = 0;
$response = $this->curl("http://www.furaffinity.net/user/".$username."/");
if(preg_match("/<b><a href=\"\/watch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\">\+Watch<\/a><\/b>/ui", $response, $match) || preg_match("/<a href=\"\/watch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\"><div class=\"button userpage\-button green hideonmobile\">\+Watch<\/div><\/a>/ui", $response, $match))
{
$response = $this->curl("http://www.furaffinity.net/watch/".$username."/?key=".$match[1]);
if(preg_match("/".preg_quote($username)." has been added to your watch list\!/ui", $response))
{
$return = 1;
}
}
elseif(preg_match("/<b><a href=\"\/unwatch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\">\-Watch<\/a><\/b>/ui", $response) || preg_match("/<a href=\"\/unwatch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\"><div class=\"button userpage\-button green hideonmobile\">\-Watch<\/div><\/a>/ui", $response))
{
$return = 2;
}
return $return;
}
/*
* @param string
*
* @return int [0, 1 or 2 (0 - bad respond, 1 - success, 2 - already)]
*/
public function removeWatch($username)
{
$return = 0;
$response = $this->curl("http://www.furaffinity.net/user/".$username."/");
if(preg_match("/<b><a href=\"\/unwatch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\">\-Watch<\/a><\/b>/ui", $response, $match) || preg_match("/<a href=\"\/unwatch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\"><div class=\"button userpage\-button green hideonmobile\">\-Watch<\/div><\/a>/ui", $response, $match))
{
$response = $this->curl("http://www.furaffinity.net/unwatch/".$username."/?key=".$match[1]);
if(preg_match("/".preg_quote($username)." has been removed from your watch list\!/ui", $response))
{
$return = 1;
}
}
elseif(preg_match("/<b><a href=\"\/watch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\">\+Watch<\/a><\/b>/ui", $response) || preg_match("/<a href=\"\/watch\/".preg_quote($username)."\/\?key=([A-Za-z0-9]+)\"><div class=\"button userpage\-button green hideonmobile\">\+Watch<\/div><\/a>/ui", $response))
{
$return = 2;
}
return $return;
}
/*
* @param int
*
* @return int [0, 1 or 2 (0 - bad respond, 1 - success, 2 - already)]
*/
public function addFavorite($id)
{
$return = 0;
$response = $this->curl("http://www.furaffinity.net/view/".$id."/");
if(preg_match("/<b><a href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\+Add to Favorites<\/a><\/b>/ui", $response, $match) || preg_match("/<a class=\"button section-button\" href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\+Fav<\/a>/ui", $response, $match))
{
$response = $this->curl("http://www.furaffinity.net/fav/".$id."/?key=".$match[1]);
if($response)
{
$return = 1;
}
}
elseif(preg_match("/<b><a href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\-Remove from Favorites<\/a><\/b>/ui", $response) || preg_match("/<a class=\"button section-button\" href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\-Favs<\/a>/ui", $response))
{
$return = 2;
}
return $return;
}
/*
* @param int
*
* @return int [0, 1 or 2 (0 - bad respond, 1 - success, 2 - already)]
*/
public function removeFavorite($id)
{
$return = 0;
$response = $this->curl("http://www.furaffinity.net/view/".$id."/");
if(preg_match("/<b><a href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\-Remove from Favorites<\/a><\/b>/ui", $response, $match) || preg_match("/<a class=\"button section-button\" href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\-Favs<\/a>/ui", $response, $match))
{
$response = $this->curl("http://www.furaffinity.net/fav/".$id."/?key=".$match[1]);
if($response)
{
$return = 1;
}
}
elseif(preg_match("/<b><a href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\+Add to Favorites<\/a><\/b>/ui", $response) || preg_match("/<a class=\"button section-button\" href=\"\/fav\/".preg_quote($id)."\/\?key=([A-Za-z0-9]+)\">\+Fav<\/a>/ui", $response))
{
$return = 2;
}
return $return;
}
/*
* @param string
*
* @return mixed (string or bool)
*/
private function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)");
curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
$result = curl_exec($ch);
if (!$result)
{
return false;
}
curl_close($ch);
return $result;
}
}