From f316287b6aea15e8e05a88bee75ddd622fab8670 Mon Sep 17 00:00:00 2001 From: N3MIS15 Date: Sat, 6 Oct 2012 21:18:49 +1000 Subject: [PATCH] Add option to cache sickbeard images --- maraschino/modules.py | 8 +++++++- modules/sickbeard.py | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/maraschino/modules.py b/maraschino/modules.py index 73bd7005..8d3ec7ac 100644 --- a/maraschino/modules.py +++ b/maraschino/modules.py @@ -617,7 +617,13 @@ 'value': '0', 'description': 'Show air date', 'type': 'bool', - }, + }, + { + 'key': 'sickbeard_cache_img', + 'value': '0', + 'description': 'Cache images', + 'type': 'bool', + }, ] }, { diff --git a/modules/sickbeard.py b/modules/sickbeard.py index e7cd122e..d07331f5 100644 --- a/modules/sickbeard.py +++ b/modules/sickbeard.py @@ -2,12 +2,14 @@ import urllib2 import base64 import StringIO +import os +import time from maraschino import app from maraschino.tools import * +from threading import Thread import maraschino - def sickbeard_http(): if get_setting_value('sickbeard_https') == '1': return 'https://' @@ -199,7 +201,10 @@ def history(limit): # returns a link with the path to the required image from SB def get_pic(tvdb, style='banner'): - return '%s/sickbeard/get_%s/%s' % (maraschino.WEBROOT, style, tvdb) + if get_setting_value('sickbeard_cache_img') == '1': + return cache_sickbeard_image(tvdb, style) + else: + return '%s/sickbeard/get_%s/%s' % (maraschino.WEBROOT, style, tvdb) @app.route('/sickbeard/get_ep_info////') @@ -325,6 +330,36 @@ def get_poster(tvdbid): return send_file(img, mimetype='image/jpeg') +def cache_sickbeard_image(tvdbid, img_type): + cache_dir = os.path.join(maraschino.DATA_DIR, 'cache', 'sickbeard') + create_dir(cache_dir) + + file_path = os.path.join(cache_dir, '%s.%s.jpg' % (tvdbid, img_type)) + + if not os.path.exists(file_path): + image_path = '%s/?cmd=show.get%s&tvdbid=%s' % (sickbeard_url(), img_type, tvdbid) + Thread(target=download_image, args=(image_path, file_path)).start() + maraschino.THREADS.append(len(maraschino.THREADS) + 1) + + while maraschino.THREADS: + time.sleep(1) + + return sickbeard_image_file(tvdbid, img_type) + + +def sickbeard_image_file(tvdbid, img_type): + cache_dir = os.path.join(maraschino.DATA_DIR, 'cache', 'sickbeard') + + filepath = os.path.join(cache_dir, '%s.%s.jpg' % (tvdbid, img_type)) + if os.name == 'nt': + system = 'win' + else: + system = 'unix' + filepath = filepath[1:] + + return '%s/cache/image_file/%s/%s' % (maraschino.WEBROOT, system, filepath) + + @app.route('/sickbeard/log//') def log(level): params = '/?cmd=logs&min_level=%s' % level