Skip to content

Commit

Permalink
Fixed broken download due to site changes and added some future chang…
Browse files Browse the repository at this point in the history
…es protection (#52)
  • Loading branch information
Mor Sela authored Apr 8, 2017
1 parent 9536110 commit 41db7a0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
4 changes: 2 additions & 2 deletions addon.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.subtitles.torec"
name="Torec"
version="1.3.0"
version="1.3.1"
provider-name="slmosl">
<requires>
<import addon="xbmc.python" version="2.14.0"/>
Expand All @@ -13,7 +13,7 @@
<extension point="xbmc.addon.metadata">
<summary lang="en">Kodi/XBMC Torec Subtitles allows for downloading hebrew subtitles from the Israeli site Torec.net</summary>
<description lang="en">Search and download various hebrew subtitles from the Israeli subtitles site Torec.net.</description>
<source>https://github.com/slmosl/service.subtitles.Torec</source>
<source>https://github.com/XBMCil/service.subtitles.torec</source>
<email></email>
<license>GNU GENERAL PUBLIC LICENSE. Version 2, June 1991</license>
</extension>
Expand Down
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.3.1
- Fixed broken download due to site changes and added some future changes protection

1.3.0
- Added fake token generation support - download speed should much improve (@yosi-dediashvili)
- Improved Checking item's media type (@burekas7)
Expand Down
58 changes: 47 additions & 11 deletions resources/lib/TorecSubtitlesDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ def login(self):
return username in content

class TorecGuestTokenGenerator():
def __init__(self, sub_id):
def __init__(self, sub_id, handle_daylight_saving_skew):
self.sub_id = sub_id
self.handle_daylight_saving_skew = handle_daylight_saving_skew

def generate_ticket(self):
return self._gen_fake_encoded_ticket(self.sub_id, 90)

def _gen_plain_ticket(self, sub_id, secs_ago):
t = datetime.datetime.now() - datetime.timedelta(seconds=secs_ago)
if self.handle_daylight_saving_skew:
t -= datetime.timedelta(hours = 1)

st = t.strftime("%m/%d/%Y %-I:%M:%S %p")
st = re.sub("(^|/| )0", r"\1", st)
return "{}_sub{}".format(st, sub_id)
Expand All @@ -131,6 +135,14 @@ def _encode_ticket(self, plain_ticket):
for p, o in zip(plain_ticket, itertools.cycle("imet"))
)

def _decode_ticket(self, ticket):
split_ticket = re.findall('..', ticket)

return ''.join(
chr(int(p, 16) - ord(o))
for p, o in zip(split_ticket, itertools.cycle("imet"))
)

def _gen_fake_encoded_ticket(self, sub_id, secs_ago):
return self._encode_ticket(self._gen_plain_ticket(sub_id, secs_ago))

Expand All @@ -151,7 +163,7 @@ def __init__(self):
def _build_default_cookie(self, sub_id):
current_time = datetime.datetime.now().strftime("%m/%d/%Y+%I:%M:%S+%p")
return self.DEFAULT_COOKIE % {
"screen_width": 1440,
"screen_width": 1680,
"subId": sub_id,
"current_datetime": current_time
}
Expand Down Expand Up @@ -255,10 +267,7 @@ def _confirm_download_code(self, sub_id, option_id):
response = self.opener.open("%s/ajax/sub/t7/guest_dl_code.asp" % self.BASE_URL, urllib.urlencode(params))
response_data = response.read()

def get_download_link(self, sub_id, option_id):
self._confirm_download_code(sub_id, option_id)

guest_token = TorecGuestTokenGenerator(sub_id).generate_ticket()
def _try_get_download_link(self, sub_id, option_id, guest_token):
encoded_params = urllib.urlencode({
"sub_id": sub_id,
"code": option_id,
Expand All @@ -267,23 +276,50 @@ def get_download_link(self, sub_id, option_id):
"timewaited": 13
})

response = self.opener.open("%s/ajax/sub/t7/downloadun.asp" % self.BASE_URL, encoded_params)

download_link = response.read()
if download_link and "sdls.asp" in download_link:
return download_link
else:
return None

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

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/t7/downloadun.asp" % self.BASE_URL, encoded_params)
download_link = response.read()

if download_link and "sdls.asp" in download_link:
download_link = self._try_get_download_link(sub_id, option_id, guest_token)
if download_link:
break

xbmc.sleep(500)
waited_msec += 500

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

return download_link

def get_download_link(self, sub_id, option_id):
self._confirm_download_code(sub_id, option_id)

log(__name__, "trying to retrieve download link with skewed generated guest token")
generated_time_skewed_guest_token = TorecGuestTokenGenerator(sub_id, True).generate_ticket()
download_link = self._try_get_download_link(sub_id, option_id, generated_time_skewed_guest_token)
if download_link:
return download_link

log(__name__, "trying to retrieve download link with generated guest token")
generated_guest_token = TorecGuestTokenGenerator(sub_id, False).generate_ticket()
download_link = self._try_get_download_link(sub_id, option_id, generated_guest_token)
if download_link:
return download_link

log(__name__, "trying to retrieve download link with guest token request")
return self._get_download_link_with_regular_token(sub_id, option_id)


def download(self, download_link):
Expand Down

5 comments on commit 41db7a0

@burekas7
Copy link
Contributor

@burekas7 burekas7 commented on 41db7a0 Apr 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The download process still returns an error with version 1.3.1.

@morsela
Copy link
Contributor

@morsela morsela commented on 41db7a0 Apr 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird, please add a relevant log.

@burekas7
Copy link
Contributor

@burekas7 burekas7 commented on 41db7a0 Apr 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure doesn't show in the simple log, so here is the relevant log from the debug log:

21:26:53 T:3080   DEBUG: ------ Window Init () ------
21:26:54 T:3080   DEBUG: ------ Window Init (Pointer.xml) ------
21:26:56 T:3080   DEBUG: CInputManager::ProcessMouse: trying mouse action leftclick
21:26:56 T:4104   DEBUG: XFILE::CPluginDirectory::StartScript - calling plugin Torec('plugin://service.subtitles.torec/','110','?action=download&sub_id=40027&option_id=B1C9C0D5DF81B1B4A297C0ADC1A3AB&filename=BRRip XviD MP3-RARBG')
21:26:56 T:4104   DEBUG: XFILE::CPluginDirectory::WaitOnScriptResult - waiting on the Torec (id=152) plugin...
21:26:56 T:12228   DEBUG: Thread LanguageInvoker start, auto delete: false
21:26:56 T:12228    INFO: initializing python engine.
21:26:56 T:12228   DEBUG: CPythonInvoker(152, C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py): start processing
21:26:57 T:12228   DEBUG: -->Python Interpreter Initialized<--
21:26:57 T:12228   DEBUG: CPythonInvoker(152, C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py): the source file to load is "C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py"
21:26:57 T:12228   DEBUG: CPythonInvoker(152, C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py): setting the Python path to C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec;C:\Users\aUser\AppData\Roaming\Kodi\addons\script.common.plugin.cache\lib;C:\Users\aUser\AppData\Roaming\Kodi\addons\script.module.beautifulsoup4\lib;C:\Program Files (x86)\Kodi\system\python\DLLs;C:\Program Files (x86)\Kodi\system\python\Lib;C:\Program Files (x86)\Kodi\python27.zip;C:\Program Files (x86)\Kodi\system\python\lib\plat-win;C:\Program Files (x86)\Kodi\system\python\lib\lib-tk;C:\Program Files (x86)\Kodi;C:\Program Files (x86)\Kodi\system\python;C:\Program Files (x86)\Kodi\system\python\lib\site-packages
21:26:57 T:12228   DEBUG: CPythonInvoker(152, C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py): entering source directory C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec
21:26:57 T:12228   DEBUG: CPythonInvoker(152, C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py): instantiating addon using automatically obtained id of "service.subtitles.torec" dependent on version 2.14.0 of the xbmc.python api
21:26:58 T:12228   DEBUG: ### [TorecSubtitlesDownloader] - trying to retrieve download link with skewed generated guest token
21:26:58 T:12228   DEBUG: ### [__main__] - failed to connect to service for subtitle download Invalid format string
21:26:58 T:12228    INFO: CPythonInvoker(152, C:\Users\aUser\AppData\Roaming\Kodi\addons\service.subtitles.torec\service.py): script successfully run
21:26:58 T:4104   DEBUG: XFILE::CPluginDirectory::WaitOnScriptResult- plugin returned successfully
21:26:58 T:3080   DEBUG: ------ Window Init (DialogKaiToast.xml) ------
21:26:58 T:12228    INFO: Python script stopped

@matanby
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slmosl
TorecSubtitlesDownloader.py:128, remove the "-" char at pos 27:
st = t.strftime("%m/%d/%Y %-I:%M:%S %p")

@burekas7
Copy link
Contributor

@burekas7 burekas7 commented on 41db7a0 Apr 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matanby @slmosl
I did what matan said and now the download is ok.
Thanks Matan.

I made a PR:
#54

Please sign in to comment.