diff options
author | chriszero <zerov83@gmail.com> | 2015-02-15 22:17:20 +0100 |
---|---|---|
committer | chriszero <zerov83@gmail.com> | 2015-02-15 22:17:20 +0100 |
commit | 6255bbe09625828312e08835bb30d5822d600e46 (patch) | |
tree | 6217475a4f3a9fa075a181f7972f0c99db387a7f | |
parent | e412257abf8de4576a180351d24ff07589fa630c (diff) | |
download | vdr-plugin-plex-6255bbe09625828312e08835bb30d5822d600e46.tar.gz vdr-plugin-plex-6255bbe09625828312e08835bb30d5822d600e46.tar.bz2 |
Faster response.
Modified Plexservice for future additions.
-rw-r--r-- | Directory.cpp | 4 | ||||
-rw-r--r-- | Directory.h | 1 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | Plexservice.cpp | 64 | ||||
-rw-r--r-- | Plexservice.h | 18 | ||||
-rw-r--r-- | cPlexOsdItem.cpp | 4 | ||||
-rw-r--r-- | cPlexOsdItem.h | 1 | ||||
-rw-r--r-- | plex.cpp | 44 | ||||
-rw-r--r-- | plex.h | 3 | ||||
-rw-r--r-- | user.h | 2 |
10 files changed, 62 insertions, 82 deletions
diff --git a/Directory.cpp b/Directory.cpp index 46f2d54..64e2ddf 100644 --- a/Directory.cpp +++ b/Directory.cpp @@ -7,7 +7,6 @@ namespace plexclient Directory::Directory(Poco::XML::Node* pNode, MediaContainer* parent) { if(Poco::icompare(pNode->nodeName(), "Directory") == 0) { - m_pParent = parent; Poco::XML::AutoPtr<Poco::XML::NamedNodeMap> pAttribs = pNode->attributes(); m_bAllowSync = GetNodeValueAsBool(pAttribs->getNamedItem("allowSync")); @@ -28,13 +27,14 @@ Directory::Directory(Poco::XML::Node* pNode, MediaContainer* parent) pAttribs->release(); } + if(m_sTitle2.empty()) m_sTitle2 = parent->m_sTitle2; } std::string Directory::GetTitle() { switch(m_eType) { case SEASON: - return Poco::format("%s - Staffel %d", m_pParent->m_sTitle2, m_iIndex); + return Poco::format("%s - Staffel %d", m_sTitle2, m_iIndex); default: return m_sTitle; } diff --git a/Directory.h b/Directory.h index 8638565..19bd17f 100644 --- a/Directory.h +++ b/Directory.h @@ -47,7 +47,6 @@ public: Poco::Timestamp m_tCreatedAt; std::string m_sKey; MediaType m_eType; - MediaContainer* m_pParent; std::string GetTitle(); }; @@ -39,7 +39,8 @@ TMPDIR ?= /tmp export CFLAGS = $(call PKGCFG,cflags) export CXXFLAGS = $(call PKGCFG,cxxflags) -##CXXFLAGS += -std=gnu++0x -Wunused-variable -Wunused-parameter +CXXFLAGS += -std=gnu++0x +#CXXFLAGS += -std=c++11 ifeq ($(CFLAGS),) $(error CFLAGS not set) diff --git a/Plexservice.cpp b/Plexservice.cpp index 7a1633e..b8d41b7 100644 --- a/Plexservice.cpp +++ b/Plexservice.cpp @@ -1,6 +1,7 @@ #include "Plexservice.h" #include "PlexHelper.h" +#include <memory> namespace plexclient { @@ -36,18 +37,19 @@ Poco::Net::HTTPClientSession* Plexservice::GetHttpSession(bool createNew) std::string Plexservice::GetMyPlexToken() { - // Syncronize - Poco::Mutex::ScopedLock lock(m_mutex); + static bool done; + static std::string myToken; //todo: cache token in file or db - if(&m_sToken != 0 || m_sToken.empty()) { + if(!done || myToken.empty()) { + std::cout << "Get Token" << std::endl; std::stringstream ss; Poco::Base64Encoder b64(ss); b64 << Config::GetInstance().GetUsername() << ":" << Config::GetInstance().GetPassword(); b64.close(); - m_sToken = ss.str(); + 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...?! @@ -58,22 +60,27 @@ std::string Plexservice::GetMyPlexToken() 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", m_sToken)); + request.add("Authorization", Poco::format("Basic %s", tempToken)); plexSession.sendRequest(request); Poco::Net::HTTPResponse response; std::istream &rs = plexSession.receiveResponse(response); - // parse the XML Response - user *u = new user(&rs); - m_sToken = u->authenticationToken; + 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.detachSocket(); } catch (Poco::Exception &exc) { esyslog("[plex]Exception in %s s%", __func__, exc.displayText().c_str() ); } } - return m_sToken; + return myToken; } void Plexservice::Authenticate() @@ -104,23 +111,22 @@ PlexServer* Plexservice::GetServer() return pServer; } -MediaContainer* Plexservice::GetAllSections() -{ - return GetSection(""); -} - -MediaContainer* Plexservice::GetSection(std::string section) +std::shared_ptr<MediaContainer> Plexservice::GetSection(std::string section, bool putOnStack) { - std::string token = GetMyPlexToken(); if(GetHttpSession()) { Poco::Net::HTTPRequest *pRequest; + std::string uri; if(section[0]=='/') { // Full URI? - pRequest = CreateRequest(Poco::format("%s?X-Plex-Token=%s", section, token)); - } else if(false == section.empty()) { - pRequest = CreateRequest(Poco::format("/library/sections/%s?X-Plex-Token=%s", section, token)); + uri = section; } else { - pRequest = CreateRequest("/library/sections/?X-Plex-Token=" + token); + uri = Poco::format("%s/%s", m_vUriStack.top(), section);// m_vUriStack.top() + "/" + section; + } + + pRequest = CreateRequest(uri); + if(putOnStack) { + m_vUriStack.push(uri); + std::cout << "Get: " << uri << std::endl; } m_pPlexSession->sendRequest(*pRequest); @@ -129,7 +135,7 @@ MediaContainer* Plexservice::GetSection(std::string section) dsyslog("[plex] URI: %s[s%]", m_pPlexSession->getHost().c_str(), pRequest->getURI().c_str()); - MediaContainer* pAllsections = new MediaContainer(&rs, *pServer); + std::shared_ptr<MediaContainer> pAllsections(new MediaContainer(&rs, *pServer)); //Poco::StreamCopier::copyStream(rs, std::cout); delete pRequest; return pAllsections; @@ -140,12 +146,28 @@ MediaContainer* Plexservice::GetSection(std::string section) } } +std::shared_ptr<MediaContainer> Plexservice::GetLastSection() +{ + if(m_vUriStack.size() > 1) { + // discard last one + m_vUriStack.pop(); + std::string uri = m_vUriStack.top(); + std::cout << "Pop: " << uri << std::endl; + return GetSection(uri, false); + } + return NULL; +} + Poco::Net::HTTPRequest* Plexservice::CreateRequest(std::string path) { Poco::Net::HTTPRequest *pRequest = new Poco::Net::HTTPRequest(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1); PlexHelper::AddHttpHeader(*pRequest); + // Add PlexToken to Header + std::string token = GetMyPlexToken(); + if(!token.empty()) + pRequest->add("X-Plex-Token", token); return pRequest; } diff --git a/Plexservice.h b/Plexservice.h index cccf17b..af7275d 100644 --- a/Plexservice.h +++ b/Plexservice.h @@ -7,7 +7,7 @@ #include <sstream> #include <iostream> #include <string> -#include <vector> +#include <stack> #include <stdio.h> #include <stdlib.h> #include <memory> @@ -43,28 +43,24 @@ public: ~Plexservice(); void DisplaySections(); - MediaContainer* GetAllSections(); - MediaContainer* GetSection(std::string section); + std::shared_ptr<MediaContainer> GetSection(std::string section, bool putOnStack = true); + std::shared_ptr<MediaContainer> GetLastSection(); void GetAuthDetails(); - std::string GetMyPlexToken(); void Authenticate(); - //void DiscoverFirstServer(); + PlexServer* GetServer(); static std::string GetUniversalTranscodeUrl(Video* video, int offset = 0, PlexServer* server = 0); - + static std::string GetMyPlexToken(); static MediaContainer GetMediaContainer(std::string fullUrl); static std::string encode(std::string message); private: Poco::Mutex m_mutex; - // Never Access m_sToken directly! => possible race condition - std::string m_sToken; - - std::string USERAGENT; - Poco::Net::HTTPClientSession *m_pPlexSession; PlexServer *pServer; + std::stack<std::string> m_vUriStack; + Poco::Net::HTTPClientSession* GetHttpSession(bool createNew = false); Poco::Net::HTTPRequest* CreateRequest(std::string path); }; diff --git a/cPlexOsdItem.cpp b/cPlexOsdItem.cpp index 3d0684e..bfc6898 100644 --- a/cPlexOsdItem.cpp +++ b/cPlexOsdItem.cpp @@ -43,7 +43,3 @@ plexclient::Directory* cPlexOsdItem::GetAttachedDirectory() { plexclient::PlexServer* cPlexOsdItem::GetAttachedServer() { return pserver; } - -cPlexOsdItem::~cPlexOsdItem() -{ -} diff --git a/cPlexOsdItem.h b/cPlexOsdItem.h index 3395cb0..9aba337 100644 --- a/cPlexOsdItem.h +++ b/cPlexOsdItem.h @@ -23,7 +23,6 @@ public: cPlexOsdItem(const char* title, plexclient::PlexServer* server); cPlexOsdItem(const char* title, plexclient::Video* obj); cPlexOsdItem(const char* title, plexclient::Directory* obj); - ~cPlexOsdItem(); plexclient::Video* GetAttachedVideo(); plexclient::Directory* GetAttachedDirectory(); plexclient::PlexServer* GetAttachedServer(); @@ -29,7 +29,8 @@ cPlexBrowser::cPlexBrowser(const char *title, plexclient::PlexServer* pServ) :cO dsyslog("[plex]%s:\n", __FUNCTION__); pService = new plexclient::Plexservice(pServ); pService->Authenticate(); - pCont = pService->GetAllSections(); + pCont = pService->GetSection("/library/sections"); + //pChannels = pService->GetSection("/video"); SetMenuCategory(mcRecording); CreateMenu(); } @@ -100,28 +101,11 @@ eOSState cPlexBrowser::ProcessKey(eKeys key) eOSState cPlexBrowser::LevelUp() { - if(m_vStack.size() <= 1) { - m_vStack.clear(); + pCont = pService->GetLastSection(); + if(!pCont) { ShowBrowser = 0; return osEnd; } - - m_vStack.pop_back(); - - std::string uri; - for(unsigned int i = 0; i < m_vStack.size(); i++) { - if(m_vStack[i][0]=='/') { - uri = m_vStack[i]; - continue; - } - uri += m_vStack[i]; - if(i+1 < m_vStack.size()) { - uri += "/"; - } - } - - pCont = pService->GetSection(uri); - CreateMenu(); return osContinue; } @@ -137,25 +121,9 @@ eOSState cPlexBrowser::ProcessSelected() return osEnd; } - if(item->IsDir()) { plexclient::Directory* pDir = item->GetAttachedDirectory(); - - m_vStack.push_back(pDir->m_sKey); - std::string uri; - for(unsigned int i = 0; i < m_vStack.size(); i++) { - if(m_vStack[i][0]=='/') { - uri = m_vStack[i]; - continue; - } - uri += m_vStack[i]; - if(i+1 < m_vStack.size()) { - uri += "/"; - } - } - std::cout << "m_sSection: " << uri << std::endl; - - pCont = pService->GetSection(uri); + pCont = pService->GetSection(pDir->m_sKey); CreateMenu(); return osContinue; @@ -169,7 +137,7 @@ eOSState cPlexBrowser::ProcessSelected() cPlexInfo::cPlexInfo(plexclient::Video* video) : cOsdMenu(video->GetTitle().c_str()) { cOsdMenu::Display(); - + Add(new cOsdItem(video->m_sSummary.c_str())); } @@ -22,6 +22,7 @@ #include <vector> #include <iterator> #include <algorithm> +#include <memory> /// vdr-plugin version number. @@ -43,7 +44,7 @@ class cPlexBrowser :public cOsdMenu { private: plexclient::Plexservice* pService; - plexclient::MediaContainer *pCont; + std::shared_ptr<plexclient::MediaContainer> pCont; std::vector<plexclient::Video> *v_Vid; std::vector<plexclient::Directory> *v_Dir; std::vector<std::string> m_vStack; @@ -28,8 +28,6 @@ class user public: user(std::istream *response); - ~user(); - std::string authenticationToken; protected: |