# Path: htpc/proxy.py
# def get_image(url, height=None, width=None, opacity=100, auth=None, headers=None):
#     """ Load image form cache if possible, else download. Resize if needed """
#     opacity = float(opacity)
#     logger = logging.getLogger('htpc.proxy')
# 
#     # Create image directory if it doesnt exist
#     imgdir = os.path.join(htpc.DATADIR, 'images/')
#     if not os.path.exists(imgdir):
#         logger.debug("Creating image directory at " + imgdir)
#         os.makedirs(imgdir)
# 
#     # Create a hash of the path to use as filename
#     imghash = hashlib.md5(url).hexdigest()
# 
#     # Set filename and path
#     image = os.path.join(imgdir, imghash)
# 
#     # If there is no local copy of the original
#     if not os.path.isfile(image):
#         logger.debug("No local image found for " + image + ". Downloading")
#         download_image(url, image, auth, headers)
# 
#     # Check if resize is needed
#     if (height and width) or (opacity < 100):
# 
#         if PIL:
#             # Set filename for resized file
#             resized = image + '_w' + width + '_h' + height + '_o' + str(opacity)
# 
#             # If there is no local resized copy
#             if not os.path.isfile(resized):
#                 resize_image(image, height, width, opacity, resized)
# 
#             # Serve the resized file
#             image = resized
#         else:
#             logger.error("Can't resize when PIL is missing on system!")
#             if (opacity < 100):
#                 image = os.path.join(htpc.RUNDIR, 'interfaces/default/img/fff_20.png')
# 
#     # Load file from disk
#     imagetype = imghdr.what(image)
#     if imagetype is not None:
#         return serve_file(path=image, content_type='image/' + imagetype)


import cherrypy
import htpc
import json
import logging
import hashlib
from htpc.proxy import get_image
from urllib2 import urlopen
from cherrypy.lib.auth2 import require
            self.logger.error("Unable to connect to couchpotato")
            self.logger.debug("connection-URL: " + url)
            return

    @cherrypy.expose()
    @require()
    @cherrypy.tools.json_out()
    def getapikey(self, couchpotato_username, couchpotato_password, couchpotato_host, couchpotato_port, couchpotato_apikey, couchpotato_basepath, couchpotato_ssl=False, **kwargs):
        self.logger.debug("Testing connectivity to couchpotato")
        if couchpotato_password and couchpotato_username != '':
            couchpotato_password = hashlib.md5(couchpotato_password).hexdigest()
            couchpotato_username = hashlib.md5(couchpotato_username).hexdigest()

        getkey = 'getkey/?p=%s&u=%s' % (couchpotato_password, couchpotato_username)

        if not(couchpotato_basepath.endswith('/')):
            couchpotato_basepath += "/"

        ssl = 's' if couchpotato_ssl else ''
        url = 'http' + ssl + '://' + couchpotato_host + ':' + couchpotato_port + couchpotato_basepath + getkey
        try:
            return json.loads(urlopen(url, timeout=10).read())
        except:
            self.logger.error("Unable to connect to couchpotato")
            self.logger.debug("connection-URL: " + url)
            return json.loads(urlopen(url, timeout=10).read())

    @cherrypy.expose()
    @require()
    def GetImage(self, url, h=None, w=None, o=100):

# Path: htpc/proxy.py
# def get_image(url, height=None, width=None, opacity=100, auth=None, headers=None):
#     """ Load image form cache if possible, else download. Resize if needed """
#     opacity = float(opacity)
#     logger = logging.getLogger('htpc.proxy')
# 
#     # Create image directory if it doesnt exist
#     imgdir = os.path.join(htpc.DATADIR, 'images/')
#     if not os.path.exists(imgdir):
#         logger.debug("Creating image directory at " + imgdir)
#         os.makedirs(imgdir)
# 
#     # Create a hash of the path to use as filename
#     imghash = hashlib.md5(url).hexdigest()
# 
#     # Set filename and path
#     image = os.path.join(imgdir, imghash)
# 
#     # If there is no local copy of the original
#     if not os.path.isfile(image):
#         logger.debug("No local image found for " + image + ". Downloading")
#         download_image(url, image, auth, headers)
# 
#     # Check if resize is needed
#     if (height and width) or (opacity < 100):
# 
#         if PIL:
#             # Set filename for resized file
#             resized = image + '_w' + width + '_h' + height + '_o' + str(opacity)
# 
#             # If there is no local resized copy
#             if not os.path.isfile(resized):
#                 resize_image(image, height, width, opacity, resized)
# 
#             # Serve the resized file
#             image = resized
#         else:
#             logger.error("Can't resize when PIL is missing on system!")
#             if (opacity < 100):
#                 image = os.path.join(htpc.RUNDIR, 'interfaces/default/img/fff_20.png')
# 
#     # Load file from disk
#     imagetype = imghdr.what(image)
#     if imagetype is not None:
#         return serve_file(path=image, content_type='image/' + imagetype)


# Path: modules/couchpotato.py
import cherrypy
import htpc
import json
import logging
import hashlib
from htpc.proxy import get_image
from urllib2 import urlopen
from cherrypy.lib.auth2 import require

            self.logger.error("Unable to connect to couchpotato")
            self.logger.debug("connection-URL: " + url)
            return

    @cherrypy.expose()
    @require()
    @cherrypy.tools.json_out()
    def getapikey(self, couchpotato_username, couchpotato_password, couchpotato_host, couchpotato_port, couchpotato_apikey, couchpotato_basepath, couchpotato_ssl=False, **kwargs):
        self.logger.debug("Testing connectivity to couchpotato")
        if couchpotato_password and couchpotato_username != '':
            couchpotato_password = hashlib.md5(couchpotato_password).hexdigest()
            couchpotato_username = hashlib.md5(couchpotato_username).hexdigest()

        getkey = 'getkey/?p=%s&u=%s' % (couchpotato_password, couchpotato_username)

        if not(couchpotato_basepath.endswith('/')):
            couchpotato_basepath += "/"

        ssl = 's' if couchpotato_ssl else ''
        url = 'http' + ssl + '://' + couchpotato_host + ':' + couchpotato_port + couchpotato_basepath + getkey
        try:
            return json.loads(urlopen(url, timeout=10).read())
        except:
            self.logger.error("Unable to connect to couchpotato")
            self.logger.debug("connection-URL: " + url)
            return json.loads(urlopen(url, timeout=10).read())

    @cherrypy.expose()
    @require()
    def GetImage(self, url, h=None, w=None, o=100):

        return get_image(url, h, w, o)