summaryrefslogtreecommitdiff
path: root/scraper/thetvdbscraper/tvdbmirrors.c
blob: 2c063ecc7b8e647c3e34a5aae3450814de781aa2 (plain)
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
#include "tvdbmirrors.h"

using namespace std;

cTVDBMirrors::cTVDBMirrors(void) {
}

cTVDBMirrors::~cTVDBMirrors() {
}

string cTVDBMirrors::GetMirrorXML(void) {
    if (xmlmirrors.size() == 0)
        return "";
    int randMirror = rand() % xmlmirrors.size();
    return xmlmirrors[randMirror];
}
string cTVDBMirrors::GetMirrorBanner(void) {
    if (bannermirrors.size() == 0)
        return "";
    int randMirror = rand() % bannermirrors.size();
    return bannermirrors[randMirror] + "/banners/";
}

string cTVDBMirrors::GetMirrorZip(void) {
    if (zipmirrors.size() == 0)
        return "";
    int randMirror = rand() % zipmirrors.size();
    return zipmirrors[randMirror];
}

bool cTVDBMirrors::ParseXML(string xml) {
    xmlDoc *doc = xmlReadMemory(xml.c_str(), xml.size(), "noname.xml", NULL, 0);
    if (doc == NULL)
        return false;
    //Root Element has to be <Mirrors>
    xmlNode *node = NULL;
    node = xmlDocGetRootElement(doc);
    if (!(node && !xmlStrcmp(node->name, (const xmlChar *)"Mirrors"))) {
        xmlFreeDoc(doc);
        return false;
    }
    //Loop through <Mirror>
    node = node->children;
    xmlNode *cur_node = NULL;
    bool ok = false;
    for (cur_node = node; cur_node; cur_node = cur_node->next) {
        if ((cur_node->type == XML_ELEMENT_NODE) && !xmlStrcmp(cur_node->name, (const xmlChar *)"Mirror")) {
            ok = ReadEntry(doc, cur_node->children);
        }
    }
    xmlFreeDoc(doc);
    return ok;
}

bool cTVDBMirrors::ReadEntry(xmlDoc *doc, xmlNode *node) {
    xmlNode *cur_node = NULL;
    xmlChar *node_content;
    string path = "";
    int typemask = 0;
    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 *)"mirrorpath")) {
                path = (const char *)node_content;
            } else if (!xmlStrcmp(cur_node->name, (const xmlChar *)"typemask")) {
                typemask = atoi((const char *)node_content);
            }
            xmlFree(node_content);
        }
    }
    return CreateMirror(path, typemask);
}

bool cTVDBMirrors::CreateMirror(string path, int typemask) {
    if (path.size() < 1)
        return false;
    if (typemask < 1 || typemask > 7)
        return false;
    if (typemask & 1)
       xmlmirrors.push_back(path); 
    if (typemask & 2)
       bannermirrors.push_back(path); 
    if (typemask & 4)
       zipmirrors.push_back(path);
    return true;
}