Skip to content

Commit

Permalink
[service.subtitles.torec] 1.1.1 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
morsela authored Oct 22, 2016
1 parent 14b9910 commit a8d6fe4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion 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.1.0"
version="1.1.1"
provider-name="slmosl">
<requires>
<import addon="xbmc.python" version="2.1.0"/>
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.1.1
- some minor bug fixes

1.1.0
- updated the addon to support Torec website version 7.0

Expand Down
66 changes: 36 additions & 30 deletions resources/lib/TorecSubtitlesDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ def __init__(self, data):

def fetch_url(self, season_number, episode_number):
subtitle_soup = bs4.BeautifulSoup(self.data, "html.parser")
episode_options = subtitle_soup(
"div", {
'id': 'tabs4-season%s' % season_number
}
)[0].findAll("a")
season_div = subtitle_soup("div", { 'id': 'tabs4-season%s' % season_number })
if not season_div:
return None

episode_options = season_div[0].findAll("a")
episode_option = next((episode_option for episode_option in episode_options if (episode_option.contents[0] == u'פרק %s' % episode_number)), None)
if not episode_option:
return None
Expand Down Expand Up @@ -86,8 +85,7 @@ def __init__(self):
self.opener.addheaders = [
(
'User-agent', (
'Mozilla/4.0 (compatible; MSIE 6.0; '
'Windows NT 5.2; .NET CLR 1.1.4322)'
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
),

)
Expand Down Expand Up @@ -130,21 +128,6 @@ def _build_default_cookie(self, sub_id):
"current_datetime": current_time
}

def _get_user_auth(self, subw_text):
return re.search(r"userAuth='(.*)';", subw_text).group(1)

def _get_time_waited(self, subw_text):
return re.search(r"seconds\s+=\s+(\d+);", subw_text).group(1)

def _request_subtitle(self, sub_id):
params = {
"sub_id" : sub_id,
"s" : 1440
}

response = self.opener.open("%s/ajax/sub/guest_time.asp" % self.BASE_URL, urllib.urlencode(params))
return response.read()

def _fetch_main_url(self, name):
log(__name__, "fetching main url for name %s" % name)
search_response = self.opener.open(
Expand Down Expand Up @@ -198,18 +181,41 @@ def search_movie(self, movie_name):

return self._fetch_subtitles_options(main_url)

def get_download_link(self, sub_id, option_id):
params = {
"sub_id": sub_id,
"code": option_id,
"sh": "yes",
"guest": self._request_subtitle(sub_id),
"timewaited": 9
def _request_subtitle(self, sub_id):
params = {
"sub_id" : sub_id,
"s" : 1440
}

response = self.opener.open("%s/ajax/sub/downloadun.asp" % self.BASE_URL, urllib.urlencode(params))
response = self.opener.open("%s/ajax/sub/guest_time.asp" % self.BASE_URL, urllib.urlencode(params))
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 = {
"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 = response.read()

if download_link:
break

time.sleep(1)

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

return download_link


def download(self, download_link):
if not download_link:
log(__name__, "no download link found")
Expand Down
2 changes: 1 addition & 1 deletion tests/movie_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _assert_subtitle_data(self, subtitleData, fileName):
temp.flush()

out = convert_to_utf(temp.name)
print temp.read()

self.assertIsNotNone(temp.read())
break

Expand Down
18 changes: 15 additions & 3 deletions tests/tvshow_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@ def test_search_inexisting_tvshow(self):
options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode'])
self.assertIsNone(options)

def test_search_existing_tvshow_with_no_subtitles(self):
item = {
'tvshow': 'Cowboy Bebop',
'season': '1',
'episode': '1'
}

options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode'])
self.assertIsNone(options)

def test_download_tvshow_sanity(self):
item = self._create_test_valid_item()
options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode'])

option = options[0]
option = options[-1]
page_id = option.sub_id
subtitle_id = option.option_id

result = self.downloader.get_download_link(page_id, subtitle_id)
subtitleData, fileName = self.downloader.download(result)
download_link = self.downloader.get_download_link(page_id, subtitle_id)
self.assertTrue(len(download_link) != 0)

subtitleData, fileName = self.downloader.download(download_link)
self.assertIsNotNone(subtitleData)

self._assert_subtitle_data(subtitleData, fileName)
Expand Down

0 comments on commit a8d6fe4

Please sign in to comment.