forked from allu77/TVScraper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TVShowScraper.php
200 lines (163 loc) · 6.19 KB
/
TVShowScraper.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
<?php
abstract class TVShowScraper {
protected $user;
protected $pass;
protected $tvdb;
protected $logger;
public function setLogger($logger) {
$this->logger = $logger;
}
public function setLogFile($logFile, $severity = LOGGER_DEBUG) {
$this->logger = new Logger($logFile, $severity);
}
protected function log($msg, $severity = LOGGER_DEBUG) {
if ($this->logger) {
$this->logger->log($msg, $severity);
}
}
protected function error($msg) {
if ($this->logger) $this->logger->error($msg);
return FALSE;
}
public function __construct($tvdb) {
$this->tvdb = $tvdb;
}
public function runScraper($scraperId, $showOnlyNew = false, $saveResults = false) {
$scraper = $this->tvdb->getScraper($scraperId);
if (isset($scraper['season'])) {
$ret = $this->runScraperSeason($scraper, $showOnlyNew, $saveResults);
if ($ret != FALSE && sizeof($ret) > 0 && $saveResults) {
$this->tvdb->resetBestFilesForSeason($scraper['season']);
}
return $ret;
} else if (isset($scraper['tvshow'])) {
return $this->runScraperTVShow($scraper, $showOnlyNew, $saveResults);
}
}
protected function submitSeasonCandidates($scraperData, $candidateLinks, $showOnlyNew = false, $saveResults = false) {
$showData = $this->tvdb->getTVShow($scraperData['tvshow']);
$showSeasons = $this->tvdb->getTVShowSeasons($scraperData['tvshow']);
$latestComplete = NULL;
foreach ($showSeasons as $s) {
if (isset($s['status']) && $s['status'] == 'complete' && ($latestComplete == NULL || $latestComplete < $s['n'])) {
$latestComplete = $s['n'];
$this->log("Found complete season $latestComplete");
}
}
$res = array();
foreach ($candidateLinks as $link) {
$this->log("Evaulating link " . $link['uri']);
if (! $showOnlyNew) {
$res[] = $link;
} else {
$keepCandidate = TRUE;
if ($latestComplete != NULL && isset($link['n']) && $link['n'] != "") {
if ($link['n'] <= $latestComplete) {
$this->log("Season is older than latest complete. Skipping.");
$keepCandidate = FALSE;
}
}
if ($keepCandidate && isset($showData['res']) && $showData['res'] != 'any' && isset($link['res'])) {
$this->log("Candidate resolution = " . $link['res']);
if (! checkResolution($showData['res'], $link['res'])) {
$this->log("Undesired resolution, skipping...");
$keepCandidate = FALSE;
}
}
if ($keepCandidate && isset($showData['lang']) && isset($link['lang']) && sizeof($link['lang'] > 0)) {
$this->log("Candidate language = " . $link['lang']);
$keepCandidate = in_array($showData['lang'], $link['lang']);
}
if ($keepCandidate) {
$previouslyScraped = $this->tvdb->getScrapedSeasonFromUri($scraperData['id'], $link['uri'], isset($link['n']) ? $link['n'] : NULL);
$keepCandidate = $previouslyScraped == NULL ? TRUE : FALSE;
}
if ($keepCandidate) {
if ($saveResults) {
$this->log("New season, adding...");
$p = array(
'uri' => $link['uri'],
'n' => $link['n']
);
if (isset($scraperData['notify']) && $scraperData['notify'] == "1") $p['tbn'] = '1';
$newId = $this->tvdb->addScrapedSeason($scraperData['id'], $p);
if (isset($scraperData['autoAdd']) && $scraperData['autoAdd'] == "1" && $n > 0) {
$this->tvdb->createSeasonScraperFromScraped($newId);
}
}
$res[] = $link;
}
}
}
return $res;
}
protected function submitEpisodeCandidates($scraperData, $candidateLinks, $showOnlyNew = false, $saveResults = false) {
$seasonData = $this->tvdb->getSeason($scraperData['season']);
$res = array();
for ($j = 0; $j < sizeof($candidateLinks); $j++) {
$link = $candidateLinks[$j]['link'];
$this->log("Evaluating candidate $j: $link");
$fileNameParts = FALSE;
if (isset($candidateLinks[$j]['type']) && $candidateLinks[$j]['type'] == 'torrent') {
if (isset($candidateLinks[$j]['episode']) && isset($candidateLinks[$j]['season'])) {
$fileNameParts['season'] = $candidateLinks[$j]['season'];
$fileNameParts['episode'] = $candidateLinks[$j]['episode'];
}
} else {
$linkParts = parseED2KURI($link);
if ($linkParts === FALSE) {
$this->log("Invalid link $link found");
} else {
$fileNameParts = parseEpisodeFileName($linkParts['fileName']);
}
}
if ($fileNameParts === FALSE) {
$this->log("Couldn't guess season and episode from $link. Skipping.");
} else {
if ($seasonData['n'] != $fileNameParts['season']) {
$this->log("Season index different than expected (expected " . $seasonData['n'] . ", got " . $fileNameParts['season']);
} else {
if (! $showOnlyNew) {
$res[] = $link;
} else {
$episode = $this->tvdb->getEpisodeFromIndex($seasonData['tvshow'], $fileNameParts['season'], $fileNameParts['episode']);
if ($episode == FALSE && $saveResults) {
$this->log("Creating new episode");
$episodeId = $this->tvdb->addEpisode($scraperData['season'], Array('n'=>$fileNameParts['episode']));
$episode = $this->tvdb->getEpisode($episodeId);
}
$addFile = TRUE;
if ($episode != FALSE) {
$candidates = $this->tvdb->getFilesForEpisode($episode['id']);
foreach ($candidates as $fileData) {
if ($fileData['uri'] == $link && isset($fileData['scraper']) && $fileData['scraper'] == $scraperData['id']) {
$this->log("A file with the same URI and scraper already exists. Skipping...");
$addFile = FALSE;
break;
}
}
}
if ($addFile) {
if ($saveResults) {
$this->log("Creating new file");
$this->tvdb->addFile($seasonData['tvshow'], Array(
'uri' => $link,
'season' => $scraperData['season'],
'episode' => $episode['id'],
'scraper' => $scraperData['id'],
'pubDate' => $candidateLinks[$j]['pubDate'],
'type' => isset($candidateLinks[$j]['type']) ? $candidateLinks[$j]['type'] : 'ed2k'
));
}
$res[] = $link;
}
}
}
}
}
return $res;
}
abstract protected function runScraperSeason($scraper, $showOnlyNew = false, $saveResults = false);
abstract protected function runScraperTVShow($scraper, $showOnlyNew = false, $saveResults = false);
}
?>