summaryrefslogtreecommitdiff
path: root/PlexServer.cpp
blob: 8e498051602c016407457aed32f4be7f2ca028f0 (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
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
#include <vdr/tools.h>
#include "PlexServer.h"
#include "Config.h"
#include "plex.h"

namespace plexclient {

    PlexServer::PlexServer(std::string ip, int port) {
        Poco::URI uri;
        uri.setHost(ip);
        uri.setPort(port);
        uri.setScheme("http");
        m_uri = uri.toString();
        Offline = false;
    }

    PlexServer::PlexServer(std::string data, std::string ip) {
        ParseData(data, ip);
    }

    PlexServer::PlexServer(std::string uri, std::string name, std::string uuid, std::string accessToken, bool owned,
                           bool local) {
        m_sServerName = name;
        m_sUuid = uuid;
        m_nOwned = owned;
        m_bLocal = local;
        m_authToken = accessToken;
        Offline = false;
        m_uri = uri;
    }

    PlexServer::~PlexServer() {

    }

    void PlexServer::ParseData(std::string data, std::string ip) {
        int port = 0;
        std::istringstream f(data);
        std::string s;
        Offline = false;
        while (std::getline(f, s)) {
            int pos = s.find(':');
            if (pos > 0) {
                std::string name = Poco::trim(s.substr(0, pos));
                std::string val = Poco::trim(s.substr(pos + 1));
                if (name == "Content-Type") {
                    m_sContentType = val;
                } else if (name == "Resource-Identifier") {
                    m_sUuid = val;
                } else if (name == "Name") {
                    m_sServerName = val;
                } else if (name == "Port") {
                    port = atoi(val.c_str());
                } else if (name == "Updated-At") {
                    m_nUpdated = atol(val.c_str());
                } else if (name == "Version") {
                    m_sVersion = val;
                }
            }
        }

        m_bLocal = true;

        Poco::URI uri;

        uri.setHost(ip);
        uri.setPort(port);
        uri.setScheme("http");

        m_uri = uri.toString();
    }

    std::string PlexServer::GetHost() {
        Poco::URI uri(m_uri);
        return uri.getHost();
    }

    int PlexServer::GetPort() {
        Poco::URI uri(m_uri);
        return uri.getPort();
    }

    std::shared_ptr<Poco::Net::HTTPClientSession> PlexServer::GetClientSession() {
        Poco::URI uri(m_uri);
        std::shared_ptr<Poco::Net::HTTPClientSession> pHttpSession = nullptr;
        if (uri.getScheme().find("https") != std::string::npos) {
            pHttpSession = std::make_shared<Poco::Net::HTTPSClientSession>(uri.getHost(), uri.getPort());
        }
        else {
            pHttpSession = std::make_shared<Poco::Net::HTTPClientSession>(uri.getHost(), uri.getPort());
        }

        //pHttpSession->setTimeout(Poco::Timespan(5, 0)); // set 5 seconds Timeout
        return pHttpSession;
    }

    std::shared_ptr<Poco::Net::HTTPClientSession> PlexServer::MakeRequest(bool &ok, std::string path,
                                                                          const std::map<std::string, std::string> &queryParameters) {
        Poco::URI uri(path);
        // Create a request with an optional query
        if (queryParameters.size()) {
            for (auto const &pair : queryParameters) {
                // addQueryParameter does the encode already
                uri.addQueryParameter(pair.first, pair.second);
            }
        }
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uri.getPathAndQuery(),
                                       Poco::Net::HTTPMessage::HTTP_1_1);

        request.add("User-Agent",
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17");
        request.add("X-Plex-Client-Identifier", Config::GetInstance().GetUUID());
        request.add("X-Plex-Device", "PC");
        request.add("X-Plex-Device-Name", Config::GetInstance().GetHostname());
        request.add("X-Plex-Language", Config::GetInstance().GetLanguage());
        request.add("X-Plex-Model", "Linux");
        request.add("X-Plex-Platform", "VDR");
        request.add("X-Plex-Product", "plex for vdr");
        request.add("X-Plex-Provides", "player");
        request.add("X-Plex-Version", VERSION);

        if (Config::GetInstance().UsePlexAccount && !GetAuthToken().empty()) {
            // Add PlexToken to Header
            request.add("X-Plex-Token", GetAuthToken());
        }
        auto cSession = GetClientSession();
        ok = true;
        try {
            cSession->sendRequest(request);
        } catch (Poco::TimeoutException &exc) {
            esyslog("[plex] Timeout: %s", path.c_str());
            ok = false;
        } catch (Poco::Exception &exc) {
            esyslog("[plex] Oops Exception: %s", exc.displayText().c_str());
            ok = false;
        }
        return cSession;
    }

    std::string PlexServer::GetUri() {
        return m_uri;
    }

}