rework tmdb class

This commit is contained in:
TangoCash
2019-03-03 22:20:23 +01:00
committed by Thilo Graf
parent a153f44baa
commit 0174e6971f
5 changed files with 47 additions and 148 deletions

View File

@@ -50,7 +50,6 @@
#include <gui/moviebrowser/mb.h>
#include <gui/movieplayer.h>
#include <gui/pictureviewer.h>
#include <gui/tmdb.h>
#include <driver/record.h>
#include <driver/fontrenderer.h>
@@ -59,8 +58,6 @@
#include <eitd/sectionsd.h>
#include <timerdclient/timerdclient.h>
#define TMDB_COVER "/tmp/tmdb.jpg"
extern CPictureViewer * g_PicViewer;
#define ICON_LARGE_WIDTH 26
@@ -125,7 +122,6 @@ CEpgData::CEpgData()
{
bigFonts = false;
frameBuffer = CFrameBuffer::getInstance();
tmdb_active = false;
mp_movie_info = NULL;
header = NULL;
Bottombox = NULL;
@@ -135,6 +131,8 @@ CEpgData::CEpgData()
imdb = CIMDB::getInstance();
imdb_active = false;
tmdb = cTmdb::getInstance();
tmdb_active = false;
movie_filename.clear();
}
@@ -244,7 +242,7 @@ void CEpgData::processTextToArray(std::string text, int screening, bool has_cove
void CEpgData::showText(int startPos, int ypos, bool has_cover, bool fullClear)
{
Font* font = g_Font[SNeutrinoSettings::FONT_TYPE_EPG_INFO2];
std::string cover = "/tmp/tmdb.jpg"; //todo: maybe add a getCover()-function to tmdb class
std::string cover = tmdb->getCover();
int cover_max_width = ox/4; //25%
int cover_max_height = sb-(2*OFFSET_INNER_MID);
int cover_width = 0;
@@ -1164,7 +1162,7 @@ int CEpgData::show(const t_channel_id channel_id, uint64_t a_id, time_t* a_start
{
showPos = 0;
if (!tmdb_active) {
cTmdb* tmdb = new cTmdb(epgData.title);
tmdb->setTitle(epgData.title);
if ((tmdb->getResults() > 0) && (!tmdb->getDescription().empty())) {
epgText_saved = epgText;
epgText.clear();
@@ -1181,7 +1179,6 @@ int CEpgData::show(const t_channel_id channel_id, uint64_t a_id, time_t* a_start
} else {
ShowMsg(LOCALE_MESSAGEBOX_INFO, LOCALE_EPGVIEWER_NODETAILED, CMsgBox::mbrOk , CMsgBox::mbrOk);
}
delete tmdb;
} else {
epgText = epgText_saved;
textCount = epgText.size();
@@ -1386,7 +1383,9 @@ void CEpgData::hide()
imdb_active = false;
imdb->cleanup();
remove(TMDB_COVER);
// tmdb
tmdb_active = false;
tmdb->cleanup();
}
void CEpgData::GetEPGData(const t_channel_id channel_id, uint64_t id, time_t* startzeit, bool clear )

View File

@@ -36,6 +36,7 @@
#include <system/settings.h>
#include <gui/imdb.h>
#include <gui/tmdb.h>
#include <driver/movieinfo.h>
#include "widget/menue.h"
#include "widget/navibar.h"
@@ -53,6 +54,7 @@ class CEpgData
CChannelEventList followlist;
CEPGData epgData;
CIMDB *imdb;
cTmdb *tmdb;
CComponentsHeader *header;
CNaviBar *Bottombox;

View File

@@ -2068,7 +2068,8 @@ bool CMovieBrowser::onButtonPressMainFrame(neutrino_msg_t msg)
extension = "." + extension;
str_replace(extension, ".jpg", cover_file);
printf("TMDB: %s : %s\n",m_movieSelectionHandler->file.Name.c_str(),cover_file.c_str());
cTmdb* tmdb = new cTmdb(m_movieSelectionHandler->epgTitle);
cTmdb* tmdb = cTmdb::getInstance();
tmdb->setTitle(m_movieSelectionHandler->epgTitle);
if ((tmdb->getResults() > 0) && (tmdb->hasCover())) {
if (!cover_file.empty())
if (tmdb->getSmallCover(cover_file))

View File

@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 TangoCash
Copyright (C) 2015,2018 TangoCash
License: GPLv2
@@ -34,7 +34,6 @@
#include <string>
#include "system/settings.h"
#include "system/helpers.h"
#include <system/helpers-json.h>
#include "system/set_threadname.h"
#include "gui/widget/hintbox.h"
@@ -46,23 +45,32 @@
#include "tmdb.h"
#if LIBCURL_VERSION_NUM < 0x071507
#include <curl/types.h>
#endif
#define URL_TIMEOUT 60
#define TMDB_COVER "/tmp/tmdb.jpg"
cTmdb::cTmdb(std::string epgtitle)
cTmdb* cTmdb::getInstance()
{
minfo.epgtitle = epgtitle;
curl_handle = curl_easy_init();
static cTmdb* tmdb = NULL;
if(!tmdb)
tmdb = new cTmdb();
return tmdb;
}
cTmdb::cTmdb()
{
#ifdef TMDB_API_KEY
key = TMDB_API_KEY;
#else
key = g_settings.tmdb_api_key;
#endif
}
cTmdb::~cTmdb()
{
cleanup();
}
void cTmdb::setTitle(std::string epgtitle)
{
memset(&minfo, 0, sizeof(minfo));
minfo.epgtitle = epgtitle;
CHintBox hintbox(LOCALE_MESSAGEBOX_INFO, g_Locale->getText(LOCALE_TMDB_READ_DATA));
hintbox.paint();
@@ -75,121 +83,6 @@ cTmdb::cTmdb(std::string epgtitle)
hintbox.hide();
}
cTmdb::~cTmdb()
{
curl_easy_cleanup(curl_handle);
}
size_t cTmdb::CurlWriteToString(void *ptr, size_t size, size_t nmemb, void *data)
{
if (size * nmemb > 0) {
std::string* pStr = (std::string*) data;
pStr->append((char*) ptr, nmemb);
}
return size*nmemb;
}
std::string cTmdb::decodeUrl(std::string url)
{
char * str = curl_easy_unescape(curl_handle, url.c_str(), 0, NULL);
if (str)
url = str;
curl_free(str);
return url;
}
std::string cTmdb::encodeUrl(std::string txt)
{
char * str = curl_easy_escape(curl_handle, txt.c_str(), txt.length());
if (str)
txt = str;
curl_free(str);
return txt;
}
bool cTmdb::getUrl(std::string &url, std::string &answer, CURL *_curl_handle)
{
printf("[TMDB]: %s\n",__func__);
if (!_curl_handle)
_curl_handle = curl_handle;
curl_easy_setopt(_curl_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(_curl_handle, CURLOPT_WRITEFUNCTION, &cTmdb::CurlWriteToString);
curl_easy_setopt(_curl_handle, CURLOPT_FILE, (void *)&answer);
curl_easy_setopt(_curl_handle, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(_curl_handle, CURLOPT_TIMEOUT, URL_TIMEOUT);
curl_easy_setopt(_curl_handle, CURLOPT_NOSIGNAL, (long)1);
curl_easy_setopt(_curl_handle, CURLOPT_SSL_VERIFYPEER, false);
if (!g_settings.softupdate_proxyserver.empty()) {
curl_easy_setopt(_curl_handle, CURLOPT_PROXY, g_settings.softupdate_proxyserver.c_str());
if (!g_settings.softupdate_proxyusername.empty()) {
std::string tmp = g_settings.softupdate_proxyusername + ":" + g_settings.softupdate_proxypassword;
curl_easy_setopt(_curl_handle, CURLOPT_PROXYUSERPWD, tmp.c_str());
}
}
char cerror[CURL_ERROR_SIZE] = {0};
curl_easy_setopt(_curl_handle, CURLOPT_ERRORBUFFER, cerror);
printf("try to get [%s] ...\n", url.c_str());
CURLcode httpres = curl_easy_perform(_curl_handle);
printf("http: res %d size %d\n", httpres, (int)answer.size());
if (httpres != 0 || answer.empty()) {
printf("error: %s\n", cerror);
return false;
}
return true;
}
bool cTmdb::DownloadUrl(std::string url, std::string file, CURL *_curl_handle)
{
if (!_curl_handle)
_curl_handle = curl_handle;
FILE * fp = fopen(file.c_str(), "wb");
if (fp == NULL) {
perror(file.c_str());
return false;
}
curl_easy_setopt(_curl_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(_curl_handle, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(_curl_handle, CURLOPT_FILE, fp);
curl_easy_setopt(_curl_handle, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(_curl_handle, CURLOPT_TIMEOUT, URL_TIMEOUT);
curl_easy_setopt(_curl_handle, CURLOPT_NOSIGNAL, (long)1);
curl_easy_setopt(_curl_handle, CURLOPT_SSL_VERIFYPEER, false);
if (!g_settings.softupdate_proxyserver.empty()) {
curl_easy_setopt(_curl_handle, CURLOPT_PROXY, g_settings.softupdate_proxyserver.c_str());
if (!g_settings.softupdate_proxyusername.empty()) {
std::string tmp = g_settings.softupdate_proxyusername + ":" + g_settings.softupdate_proxypassword;
curl_easy_setopt(_curl_handle, CURLOPT_PROXYUSERPWD, tmp.c_str());
}
}
char cerror[CURL_ERROR_SIZE] = {0};
curl_easy_setopt(_curl_handle, CURLOPT_ERRORBUFFER, cerror);
printf("try to get [%s] ...\n", url.c_str());
CURLcode httpres = curl_easy_perform(_curl_handle);
double dsize;
curl_easy_getinfo(_curl_handle, CURLINFO_SIZE_DOWNLOAD, &dsize);
fclose(fp);
printf("http: res %d size %g.\n", httpres, dsize);
if (httpres != 0) {
printf("curl error: %s\n", cerror);
unlink(file.c_str());
return false;
}
return true;
}
bool cTmdb::GetMovieDetails(std::string lang)
{
printf("[TMDB]: %s\n",__func__);
@@ -292,3 +185,9 @@ std::string cTmdb::CreateEPGText()
epgtext += (std::string)g_Locale->getText(LOCALE_EPGEXTENDED_ACTORS)+":\n"+ minfo.cast+"\n";
return epgtext;
}
void cTmdb::cleanup()
{
if (access(TMDB_COVER, F_OK) == 0)
unlink(TMDB_COVER);
}

View File

@@ -20,10 +20,10 @@
#ifndef __TMDB__
#define __TMDB__
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include "system/helpers.h"
#define TMDB_COVER "/tmp/tmdb.jpg"
typedef struct {
std::string epgtitle;
@@ -47,20 +47,16 @@ typedef struct {
class cTmdb
{
private:
CURL *curl_handle;
tmdbinfo minfo;
static size_t CurlWriteToString(void *ptr, size_t size, size_t nmemb, void *data);
std::string encodeUrl(std::string txt);
std::string decodeUrl(std::string url);
std::string key; // tmdb api key
bool getUrl(std::string &url, std::string &answer, CURL *_curl_handle = NULL);
bool DownloadUrl(std::string url, std::string file, CURL *_curl_handle = NULL);
bool GetMovieDetails(std::string lang);
public:
cTmdb(std::string epgtitle);
cTmdb();
~cTmdb();
static cTmdb* getInstance();
void setTitle(std::string epgtitle);
std::string CreateEPGText();
std::string getTitle() { return minfo.epgtitle;}
@@ -69,11 +65,13 @@ class cTmdb
std::string getDescription() { return minfo.overview;}
std::string getVote() { return minfo.vote_average;}
std::string getCast() { return minfo.cast;}
std::string getCover() { return TMDB_COVER;}
bool hasCover() { return !minfo.poster_path.empty();}
bool getBigCover(std::string cover) { return DownloadUrl("http://image.tmdb.org/t/p/w342" + minfo.poster_path, cover);}
bool getSmallCover(std::string cover) { return DownloadUrl("http://image.tmdb.org/t/p/w185" + minfo.poster_path, cover);}
bool getBigCover(std::string cover) { return downloadUrl("http://image.tmdb.org/t/p/w342" + minfo.poster_path, cover);}
bool getSmallCover(std::string cover) { return downloadUrl("http://image.tmdb.org/t/p/w185" + minfo.poster_path, cover);}
int getResults() { return minfo.result;}
int getStars() { return (int) (atof(minfo.vote_average.c_str())+0.5);}
void cleanup();
};
#endif