From aa2266bd9764c4a857a1107661405ab2c08da93d Mon Sep 17 00:00:00 2001 From: faush01 Date: Thu, 9 Oct 2014 20:03:31 +1100 Subject: [PATCH] remove requests, use downloadUrl instead --- .gitignore | 215 +++++++++++++++++++++++++++++++++ MBCon.pyproj | 45 +++++++ addon.xml | 1 - default.py | 17 +-- media.sln | 22 ++++ resources/lib/DownloadUtils.py | 45 ++++--- service.py | 1 - 7 files changed, 315 insertions(+), 31 deletions(-) create mode 100644 .gitignore create mode 100644 MBCon.pyproj create mode 100644 media.sln diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9d6bd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,215 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg diff --git a/MBCon.pyproj b/MBCon.pyproj new file mode 100644 index 0000000..277b495 --- /dev/null +++ b/MBCon.pyproj @@ -0,0 +1,45 @@ + + + + Debug + 2.0 + {32210f10-ebc2-4c61-8d61-dfc843cf5f99} + + default.py + + . + . + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/addon.xml b/addon.xml index 3efa25a..db152c0 100644 --- a/addon.xml +++ b/addon.xml @@ -4,7 +4,6 @@ version="0.0.1" provider-name=""> - diff --git a/default.py b/default.py index 094f5e1..325cb5b 100644 --- a/default.py +++ b/default.py @@ -36,7 +36,6 @@ import inspect import base64 import random import datetime -import requests from urlparse import urlparse import cProfile import pstats @@ -279,29 +278,25 @@ def getCollections(detailsString): return collections def markWatched (url): - authHeaders = downloadUtils.getAuthHeader() - resp = requests.post(url, data='', headers=authHeaders) + downloadUtils.downloadUrl(url, postBody="", type="POST") WINDOW = xbmcgui.Window( 10000 ) WINDOW.setProperty("force_data_reload", "true") xbmc.executebuiltin("Container.Refresh") def markUnwatched (url): - authHeaders = downloadUtils.getAuthHeader() - resp = requests.delete(url, data='', headers=authHeaders) + downloadUtils.downloadUrl(url, type="DELETE") WINDOW = xbmcgui.Window( 10000 ) WINDOW.setProperty("force_data_reload", "true") xbmc.executebuiltin("Container.Refresh") def markFavorite (url): - authHeaders = downloadUtils.getAuthHeader() - resp = requests.post(url, data='', headers=authHeaders) + downloadUtils.downloadUrl(url, postBody="", type="POST") WINDOW = xbmcgui.Window( 10000 ) WINDOW.setProperty("force_data_reload", "true") xbmc.executebuiltin("Container.Refresh") def unmarkFavorite (url): - authHeaders = downloadUtils.getAuthHeader() - resp = requests.delete(url, data='', headers=authHeaders) + downloadUtils.downloadUrl(url, type="DELETE") WINDOW = xbmcgui.Window( 10000 ) WINDOW.setProperty("force_data_reload", "true") xbmc.executebuiltin("Container.Refresh") @@ -345,9 +340,7 @@ def delete (url): printDebug('Deleting via URL: ' + url) progress = xbmcgui.DialogProgress() progress.create(__language__(30052), __language__(30053)) - authHeaders = downloadUtils.getAuthHeader() - resp = requests.delete(url, data='', headers=authHeaders) - deleteSleep=0 + downloadUtils.downloadUrl(url, type="DELETE") progress.close() xbmc.executebuiltin("Container.Refresh") diff --git a/media.sln b/media.sln new file mode 100644 index 0000000..33f0478 --- /dev/null +++ b/media.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30723.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "MBCon", "MBCon.pyproj", "{32210F10-EBC2-4C61-8D61-DFC843CF5F99}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {32210F10-EBC2-4C61-8D61-DFC843CF5F99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {32210F10-EBC2-4C61-8D61-DFC843CF5F99}.Debug|Any CPU.Build.0 = Debug|Any CPU + {32210F10-EBC2-4C61-8D61-DFC843CF5F99}.Release|Any CPU.ActiveCfg = Release|Any CPU + {32210F10-EBC2-4C61-8D61-DFC843CF5F99}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/resources/lib/DownloadUtils.py b/resources/lib/DownloadUtils.py index 82ae797..ed97d6a 100644 --- a/resources/lib/DownloadUtils.py +++ b/resources/lib/DownloadUtils.py @@ -4,7 +4,6 @@ import xbmcaddon import urllib import urllib2 import httplib -import requests import hashlib import StringIO import gzip @@ -175,13 +174,18 @@ class DownloadUtils(): headers = {'Accept-encoding': 'gzip', 'Authorization' : authString} sha1 = hashlib.sha1(self.addonSettings.getSetting('password')) - resp = requests.post(url, data={'password':sha1.hexdigest(),'Username':self.addonSettings.getSetting('username')}, headers=headers) - code = str(resp.status_code) - result = resp.json() - - accessToken = result.get("AccessToken") - - if int(code) >= 200 and int(code) < 300 and accessToken != None: + messageData = "username=" + self.addonSettings.getSetting('username') + "&password=" + sha1.hexdigest() + + resp = self.downloadUrl(url, postBody=messageData, type="POST", authenticate=False) + + accessToken = None + try: + result = json.loads(resp) + accessToken = result.get("AccessToken") + except: + pass + + if(accessToken != None): self.logMsg("User Authenticated : " + accessToken) WINDOW.setProperty("AccessToken", accessToken) return accessToken @@ -190,19 +194,22 @@ class DownloadUtils(): WINDOW.setProperty("AccessToken", "") return "" - def getAuthHeader(self): + def getAuthHeader(self, authenticate): clientInfo = ClientInformation() txt_mac = clientInfo.getMachineId() version = clientInfo.getVersion() - userid = self.getUserId() - deviceName = self.addonSettings.getSetting('deviceName') deviceName = deviceName.replace("\"", "_") - authString = "MediaBrowser UserId=\"" + userid + "\",Client=\"XBMC\",Device=\"" + deviceName + "\",DeviceId=\"" + txt_mac + "\",Version=\"" + version + "\"" + #userid = self.getUserId() + #authString = "MediaBrowser UserId=\"" + userid + "\",Client=\"XBMC\",Device=\"" + deviceName + "\",DeviceId=\"" + txt_mac + "\",Version=\"" + version + "\"" + authString = "MediaBrowser Client=\"XBMC\",Device=\"" + deviceName + "\",DeviceId=\"" + txt_mac + "\",Version=\"" + version + "\"" headers = {"Accept-encoding": "gzip", "Accept-Charset" : "UTF-8,*", "Authorization" : authString} + if(authenticate == False): + return headers + authToken = self.authenticate() if(authToken != ""): headers["X-MediaBrowser-Token"] = authToken @@ -210,7 +217,7 @@ class DownloadUtils(): xbmc.log("MBCon Authentication Header : " + str(headers)) return headers - def downloadUrl(self, url, suppress=False, type="GET", popup=0, authenticate=True): + def downloadUrl(self, url, suppress=False, postBody=None, type="GET", popup=0, authenticate=True): self.logMsg("== ENTER: getURL ==") link = "" try: @@ -229,12 +236,16 @@ class DownloadUtils(): self.logMsg("urlPath = "+str(urlPath), level=2) conn = httplib.HTTPConnection(server, timeout=20) - head = {} - if(authenticate): - head = self.getAuthHeader() + head = self.getAuthHeader(authenticate) self.logMsg("HEADERS : " + str(head), level=1) - conn.request(method=type, url=urlPath, headers=head) + if(postBody != None): + head["Content-Type"] = "application/x-www-form-urlencoded" + self.logMsg("POST DATA : " + postBody) + conn.request(method=type, url=urlPath, body=postBody, headers=head) + else: + conn.request(method=type, url=urlPath, headers=head) + data = conn.getresponse() self.logMsg("GET URL HEADERS : " + str(data.getheaders()), level=2) diff --git a/service.py b/service.py index 605973b..886e1db 100644 --- a/service.py +++ b/service.py @@ -5,7 +5,6 @@ import urllib import httplib import os import time -import requests import socket import threading