1
|
|
|
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed'); |
2
|
|
|
|
3
|
|
|
class Tracker_Admin_Model extends Tracker_Base_Model { |
4
|
96 |
|
public function __construct() { |
5
|
96 |
|
parent::__construct(); |
6
|
96 |
|
} |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Checks for any series that haven't updated in 16 hours and updates them. |
10
|
|
|
* This is ran every 4 hours via a cron job. |
11
|
|
|
*/ |
12
|
|
|
public function updateLatestChapters() { |
13
|
|
|
// region $query = Get all titles ready to update; |
14
|
|
|
// @formatter:off |
15
|
|
|
$query = $this->db |
16
|
|
|
->select(' |
17
|
|
|
tracker_titles.id as title_id, |
18
|
|
|
tracker_titles.title, |
19
|
|
|
tracker_titles.title_url, |
20
|
|
|
tracker_titles.status, |
21
|
|
|
tracker_sites.site, |
22
|
|
|
tracker_sites.site_class, |
23
|
|
|
tracker_sites.status, |
24
|
|
|
tracker_titles.latest_chapter, |
25
|
|
|
tracker_titles.last_updated, tracker_titles.last_checked, |
26
|
|
|
from_unixtime(MAX(auth_users.last_login)) AS timestamp |
27
|
|
|
') |
28
|
|
|
->from('tracker_titles') |
29
|
|
|
->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') |
30
|
|
|
->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left') |
31
|
|
|
->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left') |
32
|
|
|
->where('tracker_sites.status', 'enabled') |
33
|
|
|
->group_start() //region |
34
|
|
|
->group_start() //region |
35
|
|
|
//Check if title is marked as on-going... |
36
|
|
|
->where('tracker_titles.status', 0) |
37
|
|
|
//AND matches one of where queries below |
38
|
|
|
->group_start() //region |
39
|
|
|
//Then check if it's NULL (only occurs for new series) |
40
|
|
|
//->where('latest_chapter', NULL) //NOTE: This isn't needed anymore?? |
41
|
|
|
//OR if it hasn't updated within the past 12 hours AND isn't a custom update site |
42
|
|
|
->group_start() //region |
43
|
|
|
->where('tracker_sites.use_custom', 'N') |
44
|
|
|
->where('last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)') |
45
|
|
|
->group_end() //endregion |
46
|
|
|
//OR it is a custom update site, has more than one follower and hasn't updated within the past 72 hours. |
47
|
|
|
->or_group_start() //region |
48
|
|
|
->where('tracker_titles.id IN ( |
49
|
|
|
SELECT title_id |
50
|
|
|
FROM tracker_chapters |
51
|
|
|
GROUP BY title_id |
52
|
|
|
HAVING COUNT(title_id) > 1 |
53
|
|
|
)', NULL, FALSE) |
54
|
|
|
->where('last_checked < DATE_SUB(NOW(), INTERVAL 72 HOUR)') |
55
|
|
|
->group_end() //endregion |
56
|
|
|
//OR it is a custom update site and hasn't updated within the past 120 hours (5 days) |
57
|
|
|
->or_where('last_checked < DATE_SUB(NOW(), INTERVAL 120 HOUR)') |
58
|
|
|
->group_end() //endregion |
59
|
|
|
->group_end() //endregion |
60
|
|
|
->or_group_start() //region |
61
|
|
|
//Check if title is marked as complete... |
62
|
|
|
->where('tracker_titles.status', 1) |
63
|
|
|
//Then check if it hasn't updated within the past week |
64
|
|
|
->where('last_checked < DATE_SUB(NOW(), INTERVAL 1 WEEK)') |
65
|
|
|
->group_end() //endregion |
66
|
|
|
->group_end() //endregion |
67
|
|
|
//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally. |
68
|
|
|
->group_by('tracker_titles.id, tracker_chapters.active') |
69
|
|
|
//Check if the series is actually being tracked by someone |
70
|
|
|
->having('timestamp IS NOT NULL') |
71
|
|
|
//AND if it's currently marked as active by the user |
72
|
|
|
->having('tracker_chapters.active', 'Y') |
73
|
|
|
//AND if they have been active in the last 120 hours (5 days) |
74
|
|
|
->having('timestamp > DATE_SUB(NOW(), INTERVAL 120 HOUR)') |
75
|
|
|
->order_by('tracker_titles.last_checked', 'ASC'); |
76
|
|
|
// endregion |
77
|
|
|
$query = $query->get(); |
78
|
|
|
|
79
|
|
|
if($query->num_rows() > 0) { |
80
|
|
|
$hardRateLimit = 500; //This is to avoid any possible IP bans by cache breaking again. |
81
|
|
|
$siteRateLimits = []; |
82
|
|
|
foreach ($query->result() as $row) { |
83
|
|
|
if(!array_key_exists($row->site_class,$siteRateLimits)) { |
84
|
|
|
$siteRateLimits[$row->site_class] = 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$siteRateLimits[$row->site_class]++; |
88
|
|
|
if($siteRateLimits[$row->site_class] > $hardRateLimit) continue; |
89
|
|
|
|
90
|
|
|
usleep(500000); // Delay requests by .5 seconds. |
91
|
|
|
if($siteRateLimits[$row->site_class] > 25) { |
92
|
|
|
// We're doing lots of requests to a single site, so add additional delay. |
93
|
|
|
usleep(500000); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->handleUpdate($row); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Intended to be only used as a quick way to update all series on a site after a bug. |
103
|
|
|
* |
104
|
|
|
* @param string $site |
105
|
|
|
* @param null|string $last_checked |
106
|
|
|
*/ |
107
|
|
|
public function updateAllTitlesBySite(string $site, ?string $last_checked = NULL) { |
108
|
|
|
// region $query = Get all titles by $site; |
109
|
|
|
// @formatter:off |
110
|
|
|
$query = $this->db |
111
|
|
|
->select(' |
112
|
|
|
tracker_titles.id as title_id, |
113
|
|
|
tracker_titles.title, |
114
|
|
|
tracker_titles.title_url, |
115
|
|
|
tracker_titles.status, |
116
|
|
|
tracker_sites.site, |
117
|
|
|
tracker_sites.site_class, |
118
|
|
|
tracker_sites.status, |
119
|
|
|
tracker_titles.latest_chapter, |
120
|
|
|
tracker_titles.last_updated, |
121
|
|
|
from_unixtime(MAX(auth_users.last_login)) AS timestamp |
122
|
|
|
') |
123
|
|
|
->from('tracker_titles') |
124
|
|
|
->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') |
125
|
|
|
->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left') |
126
|
|
|
->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left') |
127
|
|
|
->where('tracker_sites.status', 'enabled') |
128
|
|
|
->where('tracker_sites.site_class', $site) |
129
|
|
|
->group_start() //region |
130
|
|
|
//Check if title is marked as on-going... |
131
|
|
|
->where('tracker_titles.status', 0) |
132
|
|
|
//Check if title is marked as complete... |
133
|
|
|
->or_where('tracker_titles.status', 1) |
134
|
|
|
->group_end() //endregion |
135
|
|
|
//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally. |
136
|
|
|
->group_by('tracker_titles.id, tracker_chapters.active') |
137
|
|
|
//Check if the series is actually being tracked by someone |
138
|
|
|
->having('timestamp IS NOT NULL') |
139
|
|
|
//AND if it's currently marked as active by the user |
140
|
|
|
->having('tracker_chapters.active', 'Y') |
141
|
|
|
//AND if they have been active in the last 120 hours (5 days) |
142
|
|
|
->having('timestamp > DATE_SUB(NOW(), INTERVAL 120 HOUR)') |
143
|
|
|
->order_by('tracker_titles.last_checked', 'ASC'); |
144
|
|
|
// @formatter:on |
145
|
|
|
if(!is_null($last_checked)) { |
146
|
|
|
$query = $query->where('tracker_titles.last_checked >', $last_checked); |
147
|
|
|
} |
148
|
|
|
// endregion |
149
|
|
|
$query = $query->get(); |
150
|
|
|
|
151
|
|
|
if($query->num_rows() > 0) { |
152
|
|
|
foreach ($query->result() as $row) { |
153
|
|
|
$this->handleUpdate($row); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function handleUpdate(object $row) : void { |
159
|
|
|
/** @var Base_Site_Model $site */ |
160
|
|
|
$site = $this->sites->{$row->site_class}; |
161
|
|
|
|
162
|
|
|
print "> {$row->title} <{$row->site_class} - {$row->title_url}> | <{$row->title_id}>"; //Print this prior to doing anything so we can more easily find out if something went wrong |
163
|
|
|
|
164
|
|
|
$updateData = $site->handleBatchUpdate($row->title_url); |
165
|
|
|
if(!$updateData['limited']) { |
166
|
|
|
$titleData = $updateData['titleData']; |
167
|
|
|
if(is_array($titleData)) { |
168
|
|
|
if(array_keys_exist(['title', 'latest_chapter', 'last_updated'], $titleData)) { |
169
|
|
|
// Standard update. |
170
|
|
|
|
171
|
|
|
if($this->Tracker->title->updateTitleDataByID((int) $row->title_id, $titleData)) { |
172
|
|
|
print " - ({$titleData['latest_chapter']})\n"; |
173
|
|
|
} else { |
174
|
|
|
log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update."); |
|
|
|
|
175
|
|
|
|
176
|
|
|
print " - Something went wrong?\n"; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
else if(array_key_exists('status', $titleData)) { |
180
|
|
|
// Series has probably been deleted. |
181
|
|
|
|
182
|
|
|
if($this->Tracker->title->updateTitleDataByID((int) $row->title_id, $titleData)) { |
183
|
|
|
print " - Status has changed ({$titleData['status']})\n"; |
184
|
|
|
} else { |
185
|
|
|
log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update."); |
|
|
|
|
186
|
|
|
|
187
|
|
|
print " - Something went wrong?\n"; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
else if($site->canHaveNoChapters) { |
191
|
|
|
// Previous statements failed, however site can have no chapters. |
192
|
|
|
if($this->Tracker->title->updateTitleDataByID((int) $row->title_id, ['latest_chapter' => NULL])) { |
193
|
|
|
print " - (No chapters found?)\n"; |
194
|
|
|
} else { |
195
|
|
|
log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update."); |
|
|
|
|
196
|
|
|
|
197
|
|
|
print " - Something went wrong?\n"; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
else { |
201
|
|
|
log_message('error', 'handleUpdate failed due to invalid titleData info?'); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
else { |
205
|
|
|
// If TitleData does not exist, either something has broken, or we've set up the site wrong. |
206
|
|
|
|
207
|
|
|
//TODO: We should have some way to handle this in the site models. |
208
|
|
|
if($row->site_class !== 'MangaKakarot') { |
209
|
|
|
log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update."); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
$this->Tracker->title->updateFailedChecksByID((int) $row->title_id); |
212
|
|
|
|
213
|
|
|
print " - FAILED TO PARSE\n"; |
214
|
|
|
} |
215
|
|
|
} else { |
216
|
|
|
// Rate limited, do nothing. |
217
|
|
|
print " - Rate Limited!\n"; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Checks for any sites which support custom updating (usually via following lists) and updates them. |
223
|
|
|
* This is run hourly. |
224
|
|
|
*/ |
225
|
|
|
public function updateCustom() { |
226
|
|
|
$query = $this->db->select('*') |
227
|
|
|
->from('tracker_sites') |
228
|
|
|
->where('tracker_sites.status', 'enabled') |
229
|
|
|
->where('tracker_sites.use_custom', 'Y') |
230
|
|
|
->get(); |
231
|
|
|
|
232
|
|
|
$sites = $query->result_array(); |
233
|
|
|
foreach ($sites as $site) { |
234
|
|
|
$siteClass = $this->sites->{$site['site_class']}; |
235
|
|
|
if($titleDataList = $siteClass->doCustomUpdate()) { |
236
|
|
|
foreach ($titleDataList as $titleURL => $titleData) { |
237
|
|
|
$titleURL = (string) $titleURL; //Number only keys get converted to int for some reason, so we need to fix that. |
238
|
|
|
print "> {$titleData['title']} <{$site['site_class']}>"; //Print this prior to doing anything so we can more easily find out if something went wrong |
239
|
|
|
if(is_array($titleData) && !is_null($titleData['latest_chapter'])) { |
240
|
|
|
if($dbTitleData = $this->Tracker->title->getID($titleURL, (int) $site['id'], FALSE, TRUE)) { |
241
|
|
|
if($this->sites->{$site['site_class']}->doCustomCheck($dbTitleData['latest_chapter'], $titleData['latest_chapter'])) { |
242
|
|
|
$titleID = $dbTitleData['id']; |
243
|
|
|
if($this->Tracker->title->updateByID((int) $titleID, $titleData['latest_chapter'])) { |
244
|
|
|
//Make sure last_checked is always updated on successful run. |
245
|
|
|
//CHECK: Is there a reason we aren't just doing this in updateByID? |
246
|
|
|
$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE) |
247
|
|
|
->where('id', $titleID) |
248
|
|
|
->update('tracker_titles'); |
249
|
|
|
|
250
|
|
|
print " - ({$titleData['latest_chapter']})\n"; |
251
|
|
|
} else { |
252
|
|
|
print " - Title doesn't exist? ($titleID)\n"; |
253
|
|
|
} |
254
|
|
|
} else { |
255
|
|
|
print " - Failed Check (DB: '{$dbTitleData['latest_chapter']}' || UPDATE: '{$titleData['latest_chapter']}')\n"; |
256
|
|
|
} |
257
|
|
|
} else { |
258
|
|
|
if($siteClass->customType === 1) { |
259
|
|
|
//We only need to log if following page is missing title, not latest releases |
260
|
|
|
log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} || Title does not exist in DB??"); |
|
|
|
|
261
|
|
|
print " - Title doesn't currently exist in DB? Maybe different language or title stub change? ($titleURL)\n"; |
262
|
|
|
} else { |
263
|
|
|
print " - Title isn't currently tracked.\n"; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} else { |
267
|
|
|
log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} failed to custom update successfully"); |
|
|
|
|
268
|
|
|
print " - FAILED TO PARSE\n"; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function refollowCustom() { |
276
|
|
|
$query = $this->db->select('tracker_titles.id, tracker_titles.title_url, tracker_sites.site_class') |
277
|
|
|
->from('tracker_titles') |
278
|
|
|
->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') |
279
|
|
|
->where('tracker_titles.followed','N') |
280
|
|
|
->where('tracker_titles !=', '255') |
281
|
|
|
->where('tracker_sites.status', 'enabled') |
282
|
|
|
->where('tracker_sites.use_custom', 'Y') |
283
|
|
|
->get(); |
284
|
|
|
|
285
|
|
|
if($query->num_rows() > 0) { |
286
|
|
|
foreach($query->result() as $row) { |
287
|
|
|
$titleData = $this->Tracker->sites->{$row->site_class}->getTitleData($row->title_url, TRUE); |
288
|
|
|
|
289
|
|
|
if($titleData) { |
290
|
|
|
$titleData = array_intersect_key($titleData, array_flip(['followed'])); |
291
|
|
|
|
292
|
|
|
if(!empty($titleData)) { |
293
|
|
|
$this->db->set($titleData) |
294
|
|
|
->where('id', $row->id) |
295
|
|
|
->update('tracker_titles'); |
296
|
|
|
|
297
|
|
|
print "> {$row->site_class}:{$row->id}:{$row->title_url} FOLLOWED\n"; |
298
|
|
|
} else { |
299
|
|
|
print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO FOLLOWED)\n"; |
300
|
|
|
} |
301
|
|
|
} else { |
302
|
|
|
log_message('error', "getTitleData failed for: {$row->site_class} | {$row->title_url}"); |
|
|
|
|
303
|
|
|
print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO TITLEDATA)\n"; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function incrementRequests() : void { |
310
|
|
|
$temp_now = new DateTime(); |
311
|
|
|
$temp_now->setTimezone(new DateTimeZone('America/New_York')); |
312
|
|
|
$date = $temp_now->format('Y-m-d'); |
313
|
|
|
|
314
|
|
|
$query = $this->db->select('1') |
315
|
|
|
->from('site_stats') |
316
|
|
|
->where('date', $date) |
317
|
|
|
->get(); |
318
|
|
|
|
319
|
|
|
if($query->num_rows() > 0) { |
320
|
|
|
$this->db->set('total_requests', 'total_requests+1', FALSE) |
321
|
|
|
->where('date', $date) |
322
|
|
|
->update('site_stats'); |
323
|
|
|
} else { |
324
|
|
|
$this->db->insert('site_stats', [ |
325
|
|
|
'date' => $date, |
326
|
|
|
'total_requests' => 1 |
327
|
|
|
]); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function getNextUpdateTime(string $format = "%H:%I:%S") : string { |
332
|
|
|
$temp_now = new DateTime(); |
333
|
|
|
$temp_now->setTimezone(new DateTimeZone('America/New_York')); |
334
|
|
|
$temp_now_formatted = $temp_now->format('Y-m-d H:i:s'); |
335
|
|
|
|
336
|
|
|
//NOTE: PHP Bug: DateTime:diff doesn't play nice with setTimezone, so we need to create another DT object |
337
|
|
|
$now = new DateTime($temp_now_formatted); |
338
|
|
|
$future_date = new DateTime($temp_now_formatted); |
339
|
|
|
$now_hour = (int) $now->format('H'); |
340
|
|
|
if($now_hour < 4) { |
341
|
|
|
//Time until 4am |
342
|
|
|
$future_date->setTime(4, 00); |
343
|
|
|
} elseif($now_hour < 8) { |
344
|
|
|
//Time until 8am |
345
|
|
|
$future_date->setTime(8, 00); |
346
|
|
|
} elseif($now_hour < 12) { |
347
|
|
|
//Time until 12pm |
348
|
|
|
$future_date->setTime(12, 00); |
349
|
|
|
} elseif($now_hour < 16) { |
350
|
|
|
//Time until 4pm |
351
|
|
|
$future_date->setTime(16, 00); |
352
|
|
|
} elseif($now_hour < 20) { |
353
|
|
|
//Time until 8pm |
354
|
|
|
$future_date->setTime(20, 00); |
355
|
|
|
} else { |
356
|
|
|
//Time until 12am |
357
|
|
|
$future_date->setTime(00, 00); |
358
|
|
|
$future_date->add(new DateInterval('P1D')); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$interval = $future_date->diff($now); |
362
|
|
|
return $interval->format($format); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|