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
|
#include "../../lib/curl.h"
#include "thetvdbscraper.h"
using namespace std;
cTVDBScraper::cTVDBScraper(string language) {
apiKey = "E9DBB94CA50832ED";
baseURL = "thetvdb.com";
this->language = language;
mirrors = NULL;
xmlInitParser();
}
cTVDBScraper::~cTVDBScraper() {
if (mirrors)
delete mirrors;
}
cTVDBSeries *cTVDBScraper::ScrapInitial(string seriesName) {
cTVDBSeries *series = NULL;
int seriesID = ReadSeries(seriesName);
if (seriesID) {
series = new cTVDBSeries(seriesID, language, apiKey, mirrors);
}
return series;
}
cTVDBSeries *cTVDBScraper::GetSeries(int seriesID) {
return new cTVDBSeries(seriesID, language, apiKey, mirrors);
}
cTVDBEpisode *cTVDBScraper::GetEpisode(int episodeID) {
return new cTVDBEpisode(episodeID, language, apiKey, mirrors);
}
bool cTVDBScraper::Connect(void) {
stringstream url;
url << baseURL << "/api/" << apiKey << "/mirrors.xml";
string mirrorsXML;
bool ok = false;
if (curl.GetUrl(url.str().c_str(), &mirrorsXML)) {
mirrors = new cTVDBMirrors();
ok = mirrors->ParseXML(mirrorsXML);
}
return ok;
}
int cTVDBScraper::GetServerTime(void) {
string url = "http://thetvdb.com/api/Updates.php?type=none";
string serverTimeXML;
if (!curl.GetUrl(url.c_str(), &serverTimeXML)) {
return 0;
}
xmlDoc *doc = SetXMLDoc(serverTimeXML);
if (doc == NULL)
return 0;
//Root Element has to be <Items>
xmlNode *node = NULL;
node = xmlDocGetRootElement(doc);
if (!(node && !xmlStrcmp(node->name, (const xmlChar *)"Items"))) {
xmlFreeDoc(doc);
return 0;
}
node = node->children;
xmlNode *cur_node = NULL;
for (cur_node = node; cur_node; cur_node = cur_node->next) {
if ((cur_node->type == XML_ELEMENT_NODE) && !xmlStrcmp(cur_node->name, (const xmlChar *)"Time")) {
node = cur_node;
break;
} else {
node = NULL;
}
}
if (!node) {
xmlFreeDoc(doc);
return 0;
}
xmlChar *node_content = xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
int serverTime = atoi((const char *)node_content);
xmlFree(node_content);
xmlFreeDoc(doc);
return serverTime;
}
bool cTVDBScraper::GetUpdatedSeriesandEpisodes(set<int> *updatedSeries, set<int> *updatedEpisodes, int lastScrap) {
stringstream url;
url << "http://thetvdb.com/api/Updates.php?type=all&time=" << lastScrap;
string updatedXML;
if (!curl.GetUrl(url.str().c_str(), &updatedXML)) {
return false;
}
xmlDoc *doc = SetXMLDoc(updatedXML);
if (doc == NULL)
return false;
//Root Element has to be <Items>
xmlNode *node = NULL;
node = xmlDocGetRootElement(doc);
if (!(node && !xmlStrcmp(node->name, (const xmlChar *)"Items"))) {
xmlFreeDoc(doc);
return false;
}
xmlNode *cur_node = node->children;
for (; cur_node; cur_node = cur_node->next) {
if ((cur_node->type == XML_ELEMENT_NODE) && !xmlStrcmp(cur_node->name, (const xmlChar *)"Series")) {
xmlChar *node_content = xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
int seriesId = atoi((const char *)node_content);
xmlFree(node_content);
updatedSeries->insert(seriesId);
} else if ((cur_node->type == XML_ELEMENT_NODE) && !xmlStrcmp(cur_node->name, (const xmlChar *)"Episode")) {
xmlChar *node_content = xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
int episodeId = atoi((const char *)node_content);
xmlFree(node_content);
updatedEpisodes->insert(episodeId);
}
}
xmlFreeDoc(doc);
return true;
}
int cTVDBScraper::ReadSeries(string seriesName) {
int seriesID = 0;
stringstream url;
string seriesXML;
char* escUrl = curl.EscapeUrl(seriesName.c_str());
url << mirrors->GetMirrorXML() << "/api/GetSeries.php?seriesname=" << escUrl << "&language=" << language.c_str();
curl.Free(escUrl);
if (curl.GetUrl(url.str().c_str(), &seriesXML))
seriesID = ParseXML(seriesXML);
return seriesID;
}
int cTVDBScraper::ParseXML(string xml) {
int seriesID = 0;
xmlDoc *doc = SetXMLDoc(xml);
if (doc == NULL)
return seriesID;
//Root Element has to be <Data>
xmlNode *node = NULL;
node = xmlDocGetRootElement(doc);
if (!(node && !xmlStrcmp(node->name, (const xmlChar *)"Data"))) {
xmlFreeDoc(doc);
return seriesID;
}
//Searching for <Series>
node = node->children;
xmlNode *cur_node = NULL;
for (cur_node = node; cur_node; cur_node = cur_node->next) {
if ((cur_node->type == XML_ELEMENT_NODE) && !xmlStrcmp(cur_node->name, (const xmlChar *)"Series")) {
node = cur_node;
break;
} else {
node = NULL;
}
}
if (!node) {
xmlFreeDoc(doc);
return seriesID;
}
//now read the first series
node = node->children;
xmlChar *node_content;
for (cur_node = node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
node_content = xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
if (!node_content)
continue;
if (!xmlStrcmp(cur_node->name, (const xmlChar *)"seriesid")) {
seriesID = atoi((const char *)node_content);
xmlFree(node_content);
break;
}
xmlFree(node_content);
}
}
xmlFreeDoc(doc);
return seriesID;
}
xmlDoc *cTVDBScraper::SetXMLDoc(string xml) {
xmlDoc *doc = xmlReadMemory(xml.c_str(), xml.size(), "noname.xml", NULL, 0);
return doc;
}
|