summaryrefslogtreecommitdiff
path: root/scraper/themoviedbscraper/themoviedbscraper.c
blob: b18782ac463ea3d8760d7dae6784f27aa62b76e4 (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
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <jansson.h>

#include "../../lib/curl.h"
#include "../../tools/stringhelpers.h"
#include "../../epgdconfig.h"
#include "themoviedbscraper.h"

using namespace std;

cMovieDBScraper::cMovieDBScraper(string language)
{
   apiKey = EpgdConfig.scrapMovieDbApiKey; // "abb01b5a277b9c2c60ec0302d83c5ee9";
   this->language = language;
   baseURL = "api.themoviedb.org/3";
   posterSize = "w500";
   backdropSize = "w1280";
   actorthumbSize = "h632";
}

cMovieDBScraper::~cMovieDBScraper() {
}

cMovieDbMovie *cMovieDBScraper::Scrap(string movieName, string year) {
    int movieID = SearchMovie(movieName, year);
    if (movieID < 1) {
        return NULL;
    }
    cMovieDbMovie *movie = ReadMovie(movieID);
    if (!movie)
        return NULL;
    return movie;
}

bool cMovieDBScraper::Connect(void) {
    stringstream url;
    url << baseURL << "/configuration?api_key=" << apiKey;
    string configJSON;
    if (curl.GetUrl(url.str().c_str(), &configJSON)) {
        return parseJSON(configJSON);
    }
    return false;
}

bool cMovieDBScraper::parseJSON(string jsonString) {
    cJsonLoader root(jsonString.c_str());
    if (!root.isObject()) {
        return false;
    }
    json_t *images = root.objectByName("images");
    if(!json_is_object(images)) {
        return false;
    }

    json_t *imgUrl;
    imgUrl = json_object_get(images, "base_url");
    if(!json_is_string(imgUrl)) {
        return false;
    }
    imageUrl = json_string_value(imgUrl);
    return true;
}

int cMovieDBScraper::SearchMovie(string movieName, string year) {
    stringstream url;
    string movieJSON;
    int movieID = -1;
    char* escUrl = curl.EscapeUrl(movieName.c_str());

    url << baseURL << "/search/movie?api_key=" << apiKey << "&query=" << escUrl << "&language=" << language.c_str();
    if (year.size() > 0)
        url << "&year=" << year.c_str();
    curl.Free(escUrl);

    if (curl.GetUrl(url.str().c_str(), &movieJSON)) {
        cMovieDbMovie movie(movieJSON);
        movieID = movie.ParseJSONForMovieId(movieName);
    }

    return movieID;
}

cMovieDbMovie *cMovieDBScraper::ReadMovie(int movieID) {
    stringstream url;
    url << baseURL << "/movie/" << movieID << "?api_key=" << apiKey << "&language=" << language.c_str();
    string movieJSON;
    cMovieDbMovie *movie = NULL;
    if (curl.GetUrl(url.str().c_str(), &movieJSON)) {
        movie = new cMovieDbMovie(movieJSON);
        movie->id = movieID;
        movie->SetBaseUrl(baseURL);
        movie->SetApiKey(apiKey);
        movie->SetPosterBaseUrl(imageUrl + posterSize);
        movie->SetBackdropBaseUrl(imageUrl + backdropSize);
        movie->SetActorBaseUrl(imageUrl + actorthumbSize);
        movie->ParseJSON();
    }
    return movie;
}