summaryrefslogtreecommitdiff
path: root/Plexservice.cpp
diff options
context:
space:
mode:
authorchriszero <zerov83@gmail.com>2015-12-15 22:15:35 +0100
committerchriszero <zerov83@gmail.com>2015-12-15 22:15:35 +0100
commit7b64bf5062f3eb6ddff5d7606e44367bd5077e4c (patch)
tree258cbbc0d4edbe3bcabf58c6dee495d19384ed14 /Plexservice.cpp
parent135ed5d0ce1613f70f4b2ddcb9e8bca721ffca9e (diff)
downloadvdr-plugin-plex-7b64bf5062f3eb6ddff5d7606e44367bd5077e4c.tar.gz
vdr-plugin-plex-7b64bf5062f3eb6ddff5d7606e44367bd5077e4c.tar.bz2
Added SSL support, started support for remote
resources (servers). Browsing remote servers is working. plex.tv login is mandatory.
Diffstat (limited to 'Plexservice.cpp')
-rw-r--r--Plexservice.cpp102
1 files changed, 88 insertions, 14 deletions
diff --git a/Plexservice.cpp b/Plexservice.cpp
index bae8ed2..3e08613 100644
--- a/Plexservice.cpp
+++ b/Plexservice.cpp
@@ -53,6 +53,7 @@ std::string Plexservice::GetMyPlexToken()
// parse the XML Response
user u(&rs);
myToken = u.authenticationToken;
+ isyslog("[plex] plex.tv login successfull.");
} else {
esyslog("[plex] plex.tv Login failed, check you creditials.");
}
@@ -74,7 +75,7 @@ void Plexservice::Authenticate()
std::string token = GetMyPlexToken();
auto pRequest = CreateRequest("/?X-Plex-Token=" + token);
- Poco::Net::HTTPClientSession session(pServer->GetIpAdress(), pServer->GetPort());
+ Poco::Net::HTTPClientSession session(pServer->GetHost(), pServer->GetPort());
session.sendRequest(*pRequest);
Poco::Net::HTTPResponse response;
/*std::istream &rs = */
@@ -87,6 +88,37 @@ void Plexservice::Authenticate()
}
}
+void Plexservice::UpdateResources()
+{
+ // We must be autenticated
+ // https://plex.tv/api/resources?includeHttps=1
+ if(!Config::GetInstance().UsePlexAccount) {
+ isyslog("[plex] To access remote servers, please login with your plex.tv account.");
+ return; // Plugin is used without plex.tv login
+ }
+ isyslog("[plex] Updating remote resources...");
+
+ std::shared_ptr<MediaContainer> pContainer = GetMediaContainer("https://plex.tv/api/resources?includeHttps=1");
+
+ for(std::vector<Device>::iterator d_it = pContainer->m_vDevices.begin(); d_it != pContainer->m_vDevices.end(); ++d_it) {
+
+ // check device is a server
+ if(d_it->m_sProvides.find("server") != std::string::npos) {
+ // is it a remote server?
+ if(d_it->m_bPublicAddressMatches == false) {
+ // pick remote connection
+ for(std::vector<Connection>::iterator c_it = d_it->m_vConnections.begin(); c_it != d_it->m_vConnections.end(); ++c_it) {
+ if(c_it->m_bLocal == false) {
+ dsyslog("[plex] Found remote server: %s", d_it->m_sName.c_str());
+ // a remote Server
+ plexgdm::GetInstance().AddServer(PlexServer(c_it->m_sUri, d_it->m_sName, d_it->m_sClientIdentifier, d_it->m_sAccessToken, d_it->m_bOwned, c_it->m_bLocal));
+ }
+ }
+ }
+ }
+ }
+}
+
PlexServer* Plexservice::GetServer()
{
return pServer;
@@ -106,17 +138,18 @@ std::shared_ptr<MediaContainer> Plexservice::GetSection(std::string section, boo
if(putOnStack) {
m_vUriStack.push(uri);
}
-
- Poco::Net::HTTPClientSession session(pServer->GetIpAdress(), pServer->GetPort());
- session.sendRequest(*pRequest);
+
+ dsyslog("[plex] URI: %s%s", pServer->GetUri().c_str(), uri.c_str());
+ //Poco::Net::HTTPClientSession session(pServer->GetHost(), pServer->GetPort());
+ //session.sendRequest(*pRequest);
+ pServer->GetClientSession()->sendRequest(*pRequest);
Poco::Net::HTTPResponse response;
- std::istream &rs = session.receiveResponse(response);
+ std::istream &rs = pServer->GetClientSession()->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();
+ //session.abort();
return pAllsections;
} catch (Poco::Net::NetException &exc) {
@@ -153,8 +186,13 @@ std::unique_ptr<Poco::Net::HTTPRequest> Plexservice::CreateRequest(std::string p
if(Config::GetInstance().UsePlexAccount) {
// Add PlexToken to Header
std::string token = GetMyPlexToken();
- if(!token.empty())
+ if(pServer && !pServer->GetAuthToken().empty()) {
+ pRequest->add("X-Plex-Token", pServer->GetAuthToken());
+ dsyslog("[plex] Using server access token");
+ }
+ else if(!token.empty())
pRequest->add("X-Plex-Token", token);
+ dsyslog("[plex] Using global access token");
}
return pRequest;
@@ -162,22 +200,58 @@ std::unique_ptr<Poco::Net::HTTPRequest> Plexservice::CreateRequest(std::string p
std::shared_ptr<MediaContainer> Plexservice::GetMediaContainer(std::string fullUrl)
{
+ Poco::Net::HTTPClientSession* pSession = NULL;
try {
Poco::URI fileuri(fullUrl);
+
+ // Check for https
+ 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");
+
+ if(fileuri.getScheme().find("https") != std::string::npos) {
+ pSession = new Poco::Net::HTTPSClientSession(fileuri.getHost(), fileuri.getPort(), context);
+ }
+ else {
+ pSession = new Poco::Net::HTTPClientSession(fileuri.getHost(), fileuri.getPort());
+ }
+
+ // > HTTPS
+
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, fileuri.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
PlexHelper::AddHttpHeader(request);
+
+ if(Config::GetInstance().UsePlexAccount) {
+ // Add PlexToken to Header
+ std::string token = GetMyPlexToken();
+ if(!token.empty())
+ request.add("X-Plex-Token", token);
+ }
- Poco::Net::HTTPClientSession session(fileuri.getHost(), fileuri.getPort());
- session.sendRequest(request);
+ pSession->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())));
+ std::istream &rs = pSession->receiveResponse(response);
+
+ //Poco::StreamCopier::copyStream(rs, std::cout);
+
+ std::shared_ptr<MediaContainer> pAllsections;
+
+ // omit creating a server when talking to plex.tv
+ if(fileuri.getHost().find("plex.tv") != std::string::npos) {
+ pAllsections = std::shared_ptr<MediaContainer>(new MediaContainer(&rs));
+ }
+ else {
+ pAllsections = std::shared_ptr<MediaContainer>(new MediaContainer(&rs, plexgdm::GetInstance().GetServer(fileuri.getHost(), fileuri.getPort())));
+ }
+
- session.abort();
+ pSession->abort();
+ delete pSession;
return pAllsections;
} catch (Poco::Net::NetException &exc) {
+ std::cout << exc.displayText() << std::endl;
+ delete pSession;
return 0;
}
}