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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "Plexservice.h"
#include "PlexHelper.h"
#include "plexgdm.h"
#include <memory>
#include <Poco/Net/NetException.h>
namespace plexclient
{
Plexservice::Plexservice(PlexServer *server)
{
pServer = server;
}
Plexservice::Plexservice(PlexServer *server, std::string startUri)
{
pServer = server;
StartUri = startUri;
}
std::string Plexservice::GetMyPlexToken()
{
static bool done;
static std::string myToken;
//todo: cache token in file or db
if(!done) {
std::stringstream ss;
Poco::Base64Encoder b64(ss);
b64 << Config::GetInstance().GetUsername() << ":" << Config::GetInstance().GetPassword();
b64.close();
std::string tempToken = ss.str();
Poco::Net::Context::Ptr context = new Poco::Net::Context(
Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE, // VERIFY_NONE...?!
9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
try {
Poco::Net::HTTPSClientSession plexSession("plex.tv", 443, context);
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/users/sign_in.xml", Poco::Net::HTTPMessage::HTTP_1_1);
PlexHelper::AddHttpHeader(request);
request.add("Authorization", Poco::format("Basic %s", tempToken));
plexSession.sendRequest(request);
Poco::Net::HTTPResponse response;
std::istream &rs = plexSession.receiveResponse(response);
if(response.getStatus() == 201) {
// parse the XML Response
user u(&rs);
myToken = u.authenticationToken;
} else {
esyslog("[plex] plex.tv Login failed, check you creditials.");
}
done = true;
plexSession.abort();
} catch (Poco::Exception &exc) {
esyslog("[plex]Exception in %s s%", __func__, exc.displayText().c_str() );
done = true;
}
}
return myToken;
}
void Plexservice::Authenticate()
{
if(!Config::GetInstance().UsePlexAccount) return;
try {
std::string token = GetMyPlexToken();
auto pRequest = CreateRequest("/?X-Plex-Token=" + token);
Poco::Net::HTTPClientSession session(pServer->GetIpAdress(), pServer->GetPort());
session.sendRequest(*pRequest);
Poco::Net::HTTPResponse response;
/*std::istream &rs = */
session.receiveResponse(response);
session.abort();
// TODO: process response
//Poco::StreamCopier::copyStream(rs, std::cout);
} catch (Poco::Exception &exc) {
esyslog("[plex]Exception in %s s%", __func__, exc.displayText().c_str() );
}
}
PlexServer* Plexservice::GetServer()
{
return pServer;
}
std::shared_ptr<MediaContainer> Plexservice::GetSection(std::string section, bool putOnStack)
{
try {
std::string uri;
if(section[0]=='/') { // Full URI?
uri = section;
} else {
uri = Poco::format("%s/%s", m_vUriStack.top(), section);
}
auto pRequest = CreateRequest(uri);
if(putOnStack) {
m_vUriStack.push(uri);
}
Poco::Net::HTTPClientSession session(pServer->GetIpAdress(), pServer->GetPort());
session.sendRequest(*pRequest);
Poco::Net::HTTPResponse response;
std::istream &rs = session.receiveResponse(response);
dsyslog("[plex] URI: http://%s:%d%s", pServer->GetIpAdress().c_str(), pServer->GetPort(), uri.c_str());
std::shared_ptr<MediaContainer> pAllsections(new MediaContainer(&rs, pServer));
session.abort();
return pAllsections;
} catch (Poco::Net::NetException &exc) {
pServer->Offline = true;
return 0;
}
}
std::shared_ptr<MediaContainer> Plexservice::GetLastSection(bool current)
{
if(m_vUriStack.size() > 1) {
if(!current) {
// discard last one
m_vUriStack.pop();
}
std::string uri = m_vUriStack.top();
return GetSection(uri, false);
}
return NULL;
}
bool Plexservice::IsRoot()
{
return m_vUriStack.size() <= 1;
}
std::unique_ptr<Poco::Net::HTTPRequest> Plexservice::CreateRequest(std::string path)
{
std::unique_ptr<Poco::Net::HTTPRequest> pRequest = std::unique_ptr<Poco::Net::HTTPRequest>(new Poco::Net::HTTPRequest(Poco::Net::HTTPRequest::HTTP_GET,
path, Poco::Net::HTTPMessage::HTTP_1_1));
PlexHelper::AddHttpHeader(*pRequest);
if(Config::GetInstance().UsePlexAccount) {
// Add PlexToken to Header
std::string token = GetMyPlexToken();
if(!token.empty())
pRequest->add("X-Plex-Token", token);
}
return pRequest;
}
std::shared_ptr<MediaContainer> Plexservice::GetMediaContainer(std::string fullUrl)
{
try {
Poco::URI fileuri(fullUrl);
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, fileuri.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
PlexHelper::AddHttpHeader(request);
Poco::Net::HTTPClientSession session(fileuri.getHost(), fileuri.getPort());
session.sendRequest(request);
Poco::Net::HTTPResponse response;
std::istream &rs = session.receiveResponse(response);
std::shared_ptr<MediaContainer> pAllsections(new MediaContainer(&rs, plexgdm::GetInstance().GetServer(fileuri.getHost(), fileuri.getPort())));
session.abort();
return pAllsections;
} catch (Poco::Net::NetException &exc) {
return 0;
}
}
std::string Plexservice::encode(std::string message)
{
std::string temp;
Poco::URI::encode(message, " !\"#$%&'()*+,/:;=?@[]", temp);
return temp;
}
std::string Plexservice::GetUniversalTranscodeUrl(Video* video, int offset, PlexServer* server)
{
PlexServer* pSrv = server ? server : video->m_pServer;
std::stringstream params;
//params << "/video/:/transcode/universal/start.m3u8?";
params << "/video/:/transcode/universal/start?";
params << "path=" << encode(pSrv->GetUri() + video->m_sKey);
params << "&mediaIndex=0";
params << "&partIndex=0";
params << "&protocol=hls";
//params << "&protocol=dash";
params << "&offset=" << offset;
params << "&fastSeek=1";
params << "&directPlay=0";
params << "&directStream=1";
params << "&maxVideoBitrate=20000";
//params << "&subtitles=burn";
//params << "&subtitleSize=90";
//params << "&skipSubtitles=1";
//params << "&audioBoost=100";
params << "&videoResolution=1920x1080";
params << "&videoQuality=100";
params << "&session=" << encode(Config::GetInstance().GetUUID()); // TODO: generate Random SessionID
params << "&X-Plex-Client-Identifier=" << encode(Config::GetInstance().GetUUID());
params << "&X-Plex-Device=PC";
params << "&X-Plex-Device-Name=" << encode(Config::GetInstance().GetHostname());
params << "&X-Plex-Language=" << Config::GetInstance().GetLanguage();
params << "&X-Plex-Model=Linux";
params << "&X-Plex-Platform=" << encode("VDR Plex Plugin");;
params << "&X-Plex-Product=" << encode("VDR Plex Plugin");
params << "&X-Plex-Provides=player";
//std::cout << pSrv->GetUri() << params.str() << std::endl;
return pSrv->GetUri() + params.str();
}
}
|