Completed
Push — master ( 45893c...373238 )
by Angus
02:39
created

Tracker_Admin_Model::updateCustom()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 41
Code Lines 29

Duplication

Lines 11
Ratio 26.83 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 8
nop 0
dl 11
loc 41
ccs 0
cts 0
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
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 112
	public function __construct() {
5 112
		parent::__construct();
6 112
	}
7
8
9
	/**
10
	 * Checks for any titles that haven't updated in 16 hours and updates them.
11
	 * This is ran every 6 hours via a cron job.
12
	 */
13
	public function updateLatestChapters() {
14
		$query = $this->db
15
			->select('
16
				tracker_titles.id,
17
				tracker_titles.title,
18
				tracker_titles.title_url,
19
				tracker_titles.status,
20
				tracker_sites.site,
21
				tracker_sites.site_class,
22
				tracker_sites.status,
23
				tracker_titles.latest_chapter,
24
				tracker_titles.last_updated,
25
				from_unixtime(MAX(auth_users.last_login)) AS timestamp
26
			')
27
			->from('tracker_titles')
28
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
29
			->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
30
			->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
31
			->where('tracker_sites.status', 'enabled')
32
			->where('tracker_chapters.active', 'Y') //CHECK: Does this apply BEFORE the GROUP BY/HAVING is done?
33
			//Check if title is marked as on-going...
34
			->where('(tracker_titles.status = 0', NULL, FALSE) //TODO: Each title should have specific interval time?
35
			//Then check if it's NULL (only occurs for new series)
36
			->where('(`latest_chapter` = NULL', NULL, FALSE)
37
			//Or if it hasn't updated within the past 12 hours AND isn't MangaFox
38
			//FIXME: We <really> shouldn't have to specify specific sites here. A DB column would probably be better.
39
			->or_where('(NOT tracker_sites.site_class = "MangaFox" AND `last_checked` < DATE_SUB(NOW(), INTERVAL 12 HOUR))', NULL, FALSE)
40
			//Or if it hasn't updated within the past 12 hours AND isn't MangaFox
41
			//FIXME: See above.
42
			->or_where('`last_checked` < DATE_SUB(NOW(), INTERVAL 36 HOUR)))', NULL, FALSE)
43
			//Check if title is marked as complete...
44
			->or_where('(tracker_titles.status = 1', NULL, FALSE)
45
			//Then check if it hasn't updated within the past week
46
			->where('`last_checked` < DATE_SUB(NOW(), INTERVAL 1 WEEK))', NULL, FALSE)
47
			//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally.
48
			->group_by('tracker_titles.id')
49
			->having('timestamp IS NOT NULL')
50
			->having('timestamp > DATE_SUB(NOW(), INTERVAL 120 HOUR)')
51
			->order_by('tracker_titles.title', 'ASC')
52
			->get();
53
54
		if($query->num_rows() > 0) {
55
			foreach ($query->result() as $row) {
56
				print "> {$row->title} <{$row->site_class}>"; //Print this prior to doing anything so we can more easily find out if something went wrong
57
				$titleData = $this->sites->{$row->site_class}->getTitleData($row->title_url);
58
				if(is_array($titleData) && !is_null($titleData['latest_chapter'])) {
59
					//FIXME: "At the moment" we don't seem to be doing anything with TitleData['last_updated'].
60
					//       Should we even use this? Y/N
61 View Code Duplication
					if($this->Tracker->title->updateByID((int) $row->id, $titleData['latest_chapter'])) {
62
						//Make sure last_checked is always updated on successful run.
63
						//CHECK: Is there a reason we aren't just doing this in updateByID?
64
						$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
65
						         ->where('id', $row->id)
66
						         ->update('tracker_titles');
67
68
						print " - ({$titleData['latest_chapter']})\n";
69
					}
70
				} else {
71
					log_message('error', "{$row->title} failed to update successfully");
72
					print " - FAILED TO PARSE\n";
73
				}
74
			}
75
		}
76
	}
77
78
	public function updateCustom() {
79
		$query = $this->db->select('*')
80
		                  ->from('tracker_sites')
81
		                  ->where('status', 'enabled')
82
		                  ->get();
83
84
		$sites = $query->result_array();
85
		foreach ($sites as $site) {
86
			if($titleDataList = $this->sites->{$site['site_class']}->doCustomUpdate()) {
87
				foreach ($titleDataList as $titleURL => $titleData) {
88
					print "> {$titleData['title']} <{$site['site_class']}>"; //Print this prior to doing anything so we can more easily find out if something went wrong
89
					if(is_array($titleData) && !is_null($titleData['latest_chapter'])) {
90
						if($dbTitleData = $this->Tracker->title->getID($titleURL, (int) $site['id'], FALSE, TRUE)) {
91
							if($this->sites->{$site['site_class']}->doCustomCheck($dbTitleData['latest_chapter'], $titleData['latest_chapter'])) {
92
								$titleID = $dbTitleData['id'];
93 View Code Duplication
								if($this->Tracker->title->updateByID((int) $titleID, $titleData['latest_chapter'])) {
94
									//Make sure last_checked is always updated on successful run.
95
									//CHECK: Is there a reason we aren't just doing this in updateByID?
96
									$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
97
									         ->where('id', $titleID)
98
									         ->update('tracker_titles');
99
100
									print " - ({$titleData['latest_chapter']})\n";
101
								} else {
102
									print " - Title doesn't exist? ($titleID)\n";
103
								}
104
							} else {
105
								print " - Failed Check.\n";
106
							}
107
						} else {
108
							log_message('error', "{$titleData['title']} || Title does not exist in DB??");
109
							print " - Possibly diff language than in DB? ($titleURL)\n";
110
						}
111
					} else {
112
						log_message('error', "{$titleData['title']} failed to custom update successfully");
113
						print " - FAILED TO PARSE\n";
114
					}
115
				}
116
			}
117
		}
118
	}
119
120
	public function getNextUpdateTime() : string {
121
		$temp_now = new DateTime();
122
		$temp_now->setTimezone(new DateTimeZone('America/New_York'));
123
		$temp_now_formatted = $temp_now->format('Y-m-d H:i:s');
124
125
		//NOTE: PHP Bug: DateTime:diff doesn't play nice with setTimezone, so we need to create another DT object
126
		$now         = new DateTime($temp_now_formatted);
127
		$future_date = new DateTime($temp_now_formatted);
128
		$now_hour    = (int) $now->format('H');
129
		if($now_hour < 4) {
130
			//Time until 4am
131
			$future_date->setTime(4, 00);
132
		} elseif($now_hour < 8) {
133
			//Time until 8am
134
			$future_date->setTime(8, 00);
135
		} elseif($now_hour < 12) {
136
			//Time until 12pm
137
			$future_date->setTime(12, 00);
138
		} elseif($now_hour < 16) {
139
			//Time until 4pm
140
			$future_date->setTime(16, 00);
141
		} elseif($now_hour < 20) {
142
			//Time until 8pm
143
			$future_date->setTime(20, 00);
144
		} else {
145
			//Time until 12am
146
			$future_date->setTime(00, 00);
147
			$future_date->add(new DateInterval('P1D'));
148
		}
149
150
		$interval = $future_date->diff($now);
151
		return $interval->format("%H:%I:%S");
152
	}
153
}
154