Skip to content

Commit

Permalink
now using xbmc sleep functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
morsela committed Oct 23, 2016
1 parent a8d6fe4 commit e6a9aed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 0 additions & 1 deletion resources/lib/SubtitleHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

def log(module, msg):
xbmc.log((u"### [%s] - %s" % (module, msg,)).encode('utf-8'), level=xbmc.LOGDEBUG)
print (u"### [%s] - %s" % (module, msg,)).encode('utf-8')

def normalize_string(str):
return unicodedata.normalize(
Expand Down
29 changes: 17 additions & 12 deletions resources/lib/TorecSubtitlesDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import cookielib
import datetime
import re
import time
import urllib
import urllib2
import zlib
import json

import bs4

import xbmc
from SubtitleHelper import log

class SubtitleOption(object):
Expand Down Expand Up @@ -109,6 +109,8 @@ def login(self):
return username in content

class TorecSubtitlesDownloader(FirefoxURLHandler):
MAXIMUM_WAIT_TIME_MSEC = 13 * 1000

DEFAULT_SEPERATOR = " "
BASE_URL = "http://www.xn--9dbf0cd.net"
SUBTITLE_PATH = "sub.asp?sub_id="
Expand Down Expand Up @@ -191,27 +193,30 @@ def _request_subtitle(self, sub_id):
return response.read()

def get_download_link(self, sub_id, option_id):
guest_token = self._request_subtitle(sub_id)

download_link = None
for i in xrange(1, 15):
params = {
guest_token = self._request_subtitle(sub_id)
encoded_params = urllib.urlencode({
"sub_id": sub_id,
"code": option_id,
"sh": "yes",
"guest": guest_token,
"timewaited": 13
}
})

response = self.opener.open("%s/ajax/sub/downloadun.asp" % self.BASE_URL, urllib.urlencode(params))
download_link = None
waited_msec = 0.0

# Torec website may delay download up to 13 seconds
while (not xbmc.abortRequested) and (waited_msec < self.MAXIMUM_WAIT_TIME_MSEC):
response = self.opener.open("%s/ajax/sub/downloadun.asp" % self.BASE_URL, encoded_params)
download_link = response.read()

if download_link:
break

time.sleep(1)

log(__name__, "received link after sleeping %d seconds" % i)

xbmc.sleep(500)
waited_msec += 500

log(__name__, "received link after sleeping %f seconds" % (waited_msec / 1000.0))

return download_link

Expand Down
10 changes: 10 additions & 0 deletions tests/xbmc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import time

LOGDEBUG = 0
abortRequested = False

def log(message, level):
print(message)

def sleep(msec):
time.sleep(msec / 1000.0)
3 changes: 3 additions & 0 deletions tests/xbmcaddon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Addon(object):
def getAddonInfo(self, type):
pass

0 comments on commit e6a9aed

Please sign in to comment.