summaryrefslogtreecommitdiff
path: root/thetvdbscraper/tvdbseries.c
blob: b679e5ec8ba0fb5a683440d7effd71ec592c0a10 (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
89
90
91
92
#include <string>
#include <sstream>
#include <vector>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "tvdbseries.h"

using namespace std;

cTVDBSeries::cTVDBSeries(string xml) {
    doc = NULL;
    SetXMLDoc(xml);
    seriesID = 0;
    name = "";
    banner = "";
    overview = "";
    imbdid = "";
}

cTVDBSeries::~cTVDBSeries() {
    xmlFreeDoc(doc);
}

void cTVDBSeries::SetXMLDoc(string xml) {
    xmlInitParser();
    doc = xmlReadMemory(xml.c_str(), strlen(xml.c_str()), "noname.xml", NULL, 0);
}

void cTVDBSeries::ParseXML(void) {
    if (doc == NULL)
        return;
    //Root Element has to be <Data>
    xmlNode *node = NULL;
    node = xmlDocGetRootElement(doc);
    if (!(node && !xmlStrcmp(node->name, (const xmlChar *)"Data")))
        return;
    //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)
        return;
    //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);
            } else if (!xmlStrcmp(cur_node->name, (const xmlChar *)"SeriesName")) {
                name = (const char *)node_content;
            } else if (!xmlStrcmp(cur_node->name, (const xmlChar *)"Overview")) {
                overview = (const char *)node_content;
            } else if (!xmlStrcmp(cur_node->name, (const xmlChar *)"banner")) {
                banner = (const char *)node_content;
            } else if (!xmlStrcmp(cur_node->name, (const xmlChar *)"IMDB_ID")) {
                imbdid = (const char *)node_content;
            }
            xmlFree(node_content);
        }
    }
}

void cTVDBSeries::StoreDB(cTVScraperDB *db) {
    db->InsertSeries(seriesID, name, overview);
}

void cTVDBSeries::StoreBanner(string baseUrl, string destDir) {
    if (banner.size() == 0)
        return;
    stringstream strUrl;
    strUrl << baseUrl << banner;
    string url = strUrl.str();
    stringstream fullPath;
    fullPath << destDir << "banner.jpg";
    string path = fullPath.str();
    CurlGetUrlFile(url.c_str(), path.c_str());
}

void cTVDBSeries::Dump() {
    esyslog("tvscraper: series %s, id: %d, overview %s, imdb %s", name.c_str(), seriesID, overview.c_str(), imbdid.c_str());
}