-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmigrate.php
More file actions
186 lines (147 loc) · 5.35 KB
/
migrate.php
File metadata and controls
186 lines (147 loc) · 5.35 KB
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
<?
require_once('TVShowScraperDB.php');
require_once('TVShowScraperDBSQLite.php');
require_once('Logger.php');
require_once('config.php');
ini_set('display_errors', 1);
$logger = new Logger('log/migrate.log', LOGGER_DEBUG);
$db = new TVShowScraperDBSQLite(DB_FILE);
$db->setLogger($logger);
$db->beginTransaction();
$xml = new TVShowScraperDB(LIB_FILE);
$xml->setLogger($logger);
$tvShows = $xml->getAllTVShows();
foreach ($tvShows as $tvShow) {
print "TV Show: " . $tvShow['title'] . "... ";
$dbTVShow = $db->addTVShow(array(
'title' => $tvShow['title'],
'alternateTitle' => $tvShow['alternateTitle'],
'lang' => $tvShow['lang'],
'nativeLang' => $tvShow['nativeLang'],
'res' => $tvShow['res']
));
if ($dbTVShow === FALSE) die($logger->errmsg());
print "Done!\n";
$scrapersMap = array();
$scrapers = $xml->getTVShowScrapers($tvShow['id']);
print "\n\tTV Show Scrapers:\n";
foreach ($scrapers as $scraper) {
print "\t" . $scraper['source'] . " - " . $scraper['uri'] . "... ";
$scraperId = $scraper['id'];
unset($scraper['id']);
unset($scraper['tvshow']);
$dbScraper = $db->addScraper($dbTVShow['id'], 'tvShow', $scraper);
if ($dbScraper === FALSE) die($logger->errmsg());
print "Done!\n";
$scrapersMap[$scraperId] = $dbScraper['id'];
}
$scrapedSeasons = $xml->getScrapedSeasons($tvShow['id']);
print "\n\tScraped Seasons:\n";
foreach ($scrapedSeasons as $scrapedSeason) {
if (! isset($scrapersMap[$scrapedSeason['scraper']])) die("Unknows scraper for scrapedSeason\n");
$dbScapedSeasonId = $scrapersMap[$scrapedSeason['scraper']];
print "\t" . $scrapedSeason['n'] . ". ". $scrapedSeason['source'] . " - " . $scrapedSeason['uri'] . "... ";
unset($scrapedSeason['id']);
unset($scrapedSeason['scraper']);
unset($scrapedSeason['source']);
$dbScrapedSeason = $db->addScrapedSeason($dbScapedSeasonId, $scrapedSeason);
if ($dbScrapedSeason === FALSE) die($logger->errmsg());
print "Done!\n";
}
$seasons = $xml->getTVShowSeasons($tvShow['id']);
print "\n\tSeasons:\n";
foreach ($seasons as $season) {
print "\t" . $season['n'] . ". ". $season['status'] . "... ";
$dbSeason = $db->addSeason($dbTVShow['id'], array(
'n' => $season['n'],
'status' => $season['status']
));
if ($dbSeason === FALSE) die($logger->errmsg());
print "Done!\n";
$scrapersMap = array();
print "\t\tSeason Scrapers:\n";
$scrapers = $xml->getSeasonScrapers($season['id']);
foreach ($scrapers as $scraper) {
print "\t\t" . $scraper['source'] . " - " . $scraper['uri'] . "... ";
$scraperId = $scraper['id'];
unset($scraper['id']);
unset($scraper['season']);
$dbScraper = $db->addScraper($dbSeason['id'], 'season', $scraper);
if ($dbScraper === FALSE) die($logger->errmsg());
print "Done!\n";
$scrapersMap[$scraperId] = $dbScraper['id'];
}
$stickyFiles = array();
$episodesMap = array();
print "\t\tEpisodes:\n";
$episodes = $xml->getSeasonEpisodes($season['id']);
foreach ($episodes as $episode) {
print "\t\t" . $episode['n'] . ". " . (isset($episode['bestSticky']) ? "has bestSticky" : "" ) . "... ";
$xmlStickyFile = isset($episode['bestSticky']) ? $episode['bestFile'] : null ;
$xmlEpisodeId = $episode['id'];
unset($episode['bestSticky']);
unset($episode['bestFile']);
unset($episode['season']);
unset($episode['id']);
$dbEpisode = $db->addEpisode($dbSeason['id'], $episode);
if ($dbEpisode === FALSE) die($logger->errmsg());
print "Done!\n";
$episodesMap[$xmlEpisodeId] = $dbEpisode['id'];
if ($xmlStickyFile != null) {
$stickyFiles[$xmlStickyFile] = $dbEpisode['id'];
} else {
$xml->resetEpisodeBestFile($xmlEpisodeId);
}
}
$filesMap = array();
print "\t\tFiles:\n";
$fileIds = $xml->getFilesForSeason($season['id']);
foreach ($fileIds as $fileId) {
$file = $xml->getFile($fileId);
print "\t\t" . $file['uri'] . "... ";
if (!isset($file['scraper']) || ! isset($scrapersMap[$file['scraper']])) {
print "Orphan, skipping.\n";
continue;
}
$xmlFileId = $file['id'];
$xmlEpisodeId = $file['episode'];
$file['episode'] = $episodesMap[$file['episode']];
$file['scraper'] = $scrapersMap[$file['scraper']];
if (!isset($file['type'])) $file['type'] = 'ed2k';
unset($file['id']);
unset($file['season']);
unset($file['episode']);
$dbFile = $db->addFile($episodesMap[$xmlEpisodeId], $file);
if ($dbFile === FALSE) die($logger->errmsg());
print "Done!\n";
if (isset($stickyFiles[$xmlFileId])) {
print "\t\t\tThis file sticks to an episode... ";
if (! $db->setEpisode($stickyFiles[$xmlFileId], array(
'bestFile' => $dbFile['id'],
'bestSticky' => 1
))) {
die ($logger->errmsg());
}
print " Done!\n";
}
$filesMap[$xmlFileId] = $dbFile['id'];
}
print "\t\t\tChecking that best files match:\n";
foreach ($episodesMap as $xmlEpisodeId => $dbEpisodeId) {
$xmlBestFile = $xml->getBestFileForEpisode($xmlEpisodeId);
$dbBestFile = $db->getBestFileForEpisode($dbEpisodeId);
print "\t\t\tChecking episode id $dbEpisodeId... ";
if (($xmlBestFile == null && $dbBestFile == null) || ($filesMap[$xmlBestFile['id']] == $dbBestFile['id'])) {
print "Matches!\n";
} else {
print "NO MATCH!!\n";
var_dump($xml->getFile($xmlBestFile['id']));
var_dump($db->getFile($dbBestFile['id']));
print "\n\n\nSaving anyway, chec resuts!\n";
$db->save();
die();
}
}
}
}
$db->save();