Skip to content

Commit

Permalink
Get proxy settings from kodi and use them for our requests
Browse files Browse the repository at this point in the history
  • Loading branch information
razzeee committed May 4, 2015
1 parent 736ba33 commit 3eab97a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion traktapi.py
Expand Up @@ -2,9 +2,10 @@
#
import xbmcaddon
import logging
import os
from trakt import Trakt, ClientError, ServerError
from trakt.objects import Movie, Show
from utilities import getSetting, setSetting, findMovieMatchInList, findShowMatchInList, findEpisodeMatchInList, findSeasonMatchInList, notification, getString, createError
from utilities import getSetting, setSetting, findMovieMatchInList, findShowMatchInList, findEpisodeMatchInList, findSeasonMatchInList, notification, getString, createError, checkAndConfigureProxy
from sys import version_info

if version_info >= (2, 7):
Expand All @@ -25,6 +26,13 @@ class traktAPI(object):
def __init__(self):
logger.debug("Initializing.")

proxyURL = checkAndConfigureProxy()
if proxyURL:
if proxyURL.startswith('http://'):
os.environ['HTTP_PROXY'] = proxyURL
elif proxyURL.startswith('https://'):
os.environ['HTTPS_PROXY'] = proxyURL

# Get user login data
self.__pin = getSetting('PIN')
if getSetting('authorization'):
Expand Down
24 changes: 24 additions & 0 deletions utilities.py
Expand Up @@ -43,6 +43,8 @@

REGEX_YEAR = '^(.+) \((\d{4})\)$'

REGEX_URL = '(^https?://)(.+)'

def notification(header, message, time=5000, icon=__addon__.getAddonInfo('icon')):
xbmc.executebuiltin("XBMC.Notification(%s,%s,%i,%s)" % (header, message, time, icon))

Expand Down Expand Up @@ -452,3 +454,25 @@ def createError(ex):
"-->End of Python script error report<--"
)
return template.format(type(ex).__name__, ex.args, traceback.format_exc())

def checkAndConfigureProxy():
proxyActive = kodiJsonRequest({'jsonrpc': '2.0', "method":"Settings.GetSettingValue", "params":{"setting":"network.usehttpproxy"}, 'id': 1})['value']
proxyType = kodiJsonRequest({'jsonrpc': '2.0', "method":"Settings.GetSettingValue", "params":{"setting":"network.httpproxytype"}, 'id': 1})['value']

if proxyActive and proxyType == 0: # PROXY_HTTP
proxyURL = kodiJsonRequest({'jsonrpc': '2.0', "method":"Settings.GetSettingValue", "params":{"setting":"network.httpproxyserver"}, 'id': 1})['value']
proxyPort = unicode(kodiJsonRequest({'jsonrpc': '2.0', "method":"Settings.GetSettingValue", "params":{"setting":"network.httpproxyport"}, 'id': 1})['value'])
proxyUsername = kodiJsonRequest({'jsonrpc': '2.0', "method":"Settings.GetSettingValue", "params":{"setting":"network.httpproxyusername"}, 'id': 1})['value']
proxyPassword = kodiJsonRequest({'jsonrpc': '2.0', "method":"Settings.GetSettingValue", "params":{"setting":"network.httpproxypassword"}, 'id': 1})['value']

if proxyUsername and proxyPassword and proxyURL and proxyPort:
regexUrl = re.compile(REGEX_URL)
matchURL = regexUrl.search(proxyURL)
if matchURL:
return matchURL.group(1) + proxyUsername + ':' + proxyPassword + '@' + matchURL.group(2) + ':' + proxyPort
else:
None
elif proxyURL and proxyPort:
return proxyURL + ':' + proxyPort
else:
return None

0 comments on commit 3eab97a

Please sign in to comment.