1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-08-13 16:53:54 -04:00

Fixed IndexError: list index out of range issue

This commit is contained in:
echel0n 2014-08-09 01:43:02 +00:00
parent c4105db242
commit 1136e5c833

View File

@ -1,143 +1,145 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2012 Nicolas Wack <wackou@gmail.com> # Copyright 2012 Nicolas Wack <wackou@gmail.com>
# #
# This file is part of subliminal. # This file is part of subliminal.
# #
# subliminal is free software; you can redistribute it and/or modify it under # subliminal is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by # the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or # the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# subliminal is distributed in the hope that it will be useful, # subliminal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details. # GNU Lesser General Public License for more details.
# #
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with subliminal. If not, see <http://www.gnu.org/licenses/>. # along with subliminal. If not, see <http://www.gnu.org/licenses/>.
from . import ServiceBase from . import ServiceBase
from ..cache import cachedmethod from ..cache import cachedmethod
from ..language import language_set, Language from ..language import language_set, Language
from ..subtitles import get_subtitle_path, ResultSubtitle from ..subtitles import get_subtitle_path, ResultSubtitle
from ..utils import get_keywords from ..utils import get_keywords
from ..videos import Episode from ..videos import Episode
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import logging import logging
import re import re
logger = logging.getLogger("subliminal") logger = logging.getLogger("subliminal")
def match(pattern, string): def match(pattern, string):
try: try:
return re.search(pattern, string).group(1) return re.search(pattern, string).group(1)
except AttributeError: except AttributeError:
logger.debug(u'Could not match %r on %r' % (pattern, string)) logger.debug(u'Could not match %r on %r' % (pattern, string))
return None return None
class TvSubtitles(ServiceBase): class TvSubtitles(ServiceBase):
server_url = 'http://www.tvsubtitles.net' server_url = 'http://www.tvsubtitles.net'
site_url = 'http://www.tvsubtitles.net' site_url = 'http://www.tvsubtitles.net'
api_based = False api_based = False
languages = language_set(['ar', 'bg', 'cs', 'da', 'de', 'el', 'en', 'es', 'fi', 'fr', 'hu', languages = language_set(['ar', 'bg', 'cs', 'da', 'de', 'el', 'en', 'es', 'fi', 'fr', 'hu',
'it', 'ja', 'ko', 'nl', 'pl', 'pt', 'ro', 'ru', 'sv', 'tr', 'uk', 'it', 'ja', 'ko', 'nl', 'pl', 'pt', 'ro', 'ru', 'sv', 'tr', 'uk',
'zh', 'pb']) 'zh', 'pb'])
#TODO: Find more exceptions #TODO: Find more exceptions
language_map = {'gr': Language('gre'), 'cz': Language('cze'), 'ua': Language('ukr'), language_map = {'gr': Language('gre'), 'cz': Language('cze'), 'ua': Language('ukr'),
'cn': Language('chi'), 'br': Language('pob')} 'cn': Language('chi'), 'br': Language('pob')}
videos = [Episode] videos = [Episode]
require_video = False require_video = False
required_features = ['permissive'] required_features = ['permissive']
@cachedmethod @cachedmethod
def get_likely_series_id(self, name): def get_likely_series_id(self, name):
r = self.session.post('%s/search.php' % self.server_url, data={'q': name}) r = self.session.post('%s/search.php' % self.server_url, data={'q': name})
soup = BeautifulSoup(r.content, self.required_features) soup = BeautifulSoup(r.content, self.required_features)
maindiv = soup.find('div', 'left') maindiv = soup.find('div', 'left')
results = [] results = []
for elem in maindiv.find_all('li'): for elem in maindiv.find_all('li'):
sid = int(match('tvshow-([0-9]+)\.html', elem.a['href'])) sid = int(match('tvshow-([0-9]+)\.html', elem.a['href']))
show_name = match('(.*) \(', elem.a.text) show_name = match('(.*) \(', elem.a.text)
results.append((show_name, sid)) results.append((show_name, sid))
#TODO: pick up the best one in a smart way
result = results[0] if len(results):
return result[1] #TODO: pick up the best one in a smart way
result = results[0]
@cachedmethod return result[1]
def get_episode_id(self, series_id, season, number):
"""Get the TvSubtitles id for the given episode. Raises KeyError if none @cachedmethod
could be found.""" def get_episode_id(self, series_id, season, number):
# download the page of the season, contains ids for all episodes """Get the TvSubtitles id for the given episode. Raises KeyError if none
episode_id = None could be found."""
r = self.session.get('%s/tvshow-%d-%d.html' % (self.server_url, series_id, season)) # download the page of the season, contains ids for all episodes
soup = BeautifulSoup(r.content, self.required_features) episode_id = None
table = soup.find('table', id='table5') r = self.session.get('%s/tvshow-%d-%d.html' % (self.server_url, series_id, season))
for row in table.find_all('tr'): soup = BeautifulSoup(r.content, self.required_features)
cells = row.find_all('td') table = soup.find('table', id='table5')
if not cells: for row in table.find_all('tr'):
continue cells = row.find_all('td')
episode_number = match('x([0-9]+)', cells[0].text) if not cells:
if not episode_number: continue
continue episode_number = match('x([0-9]+)', cells[0].text)
episode_number = int(episode_number) if not episode_number:
episode_id = int(match('episode-([0-9]+)', cells[1].a['href'])) continue
# we could just return the id of the queried episode, but as we episode_number = int(episode_number)
# already downloaded the whole page we might as well fill in the episode_id = int(match('episode-([0-9]+)', cells[1].a['href']))
# information for all the episodes of the season # we could just return the id of the queried episode, but as we
self.cache_for(self.get_episode_id, args=(series_id, season, episode_number), result=episode_id) # already downloaded the whole page we might as well fill in the
# raises KeyError if not found # information for all the episodes of the season
return self.cached_value(self.get_episode_id, args=(series_id, season, number)) self.cache_for(self.get_episode_id, args=(series_id, season, episode_number), result=episode_id)
# raises KeyError if not found
# Do not cache this method in order to always check for the most recent return self.cached_value(self.get_episode_id, args=(series_id, season, number))
# subtitles
def get_sub_ids(self, episode_id): # Do not cache this method in order to always check for the most recent
subids = [] # subtitles
r = self.session.get('%s/episode-%d.html' % (self.server_url, episode_id)) def get_sub_ids(self, episode_id):
epsoup = BeautifulSoup(r.content, self.required_features) subids = []
for subdiv in epsoup.find_all('a'): r = self.session.get('%s/episode-%d.html' % (self.server_url, episode_id))
if 'href' not in subdiv.attrs or not subdiv['href'].startswith('/subtitle'): epsoup = BeautifulSoup(r.content, self.required_features)
continue for subdiv in epsoup.find_all('a'):
subid = int(match('([0-9]+)', subdiv['href'])) if 'href' not in subdiv.attrs or not subdiv['href'].startswith('/subtitle'):
lang = self.get_language(match('flags/(.*).gif', subdiv.img['src'])) continue
result = {'subid': subid, 'language': lang} subid = int(match('([0-9]+)', subdiv['href']))
for p in subdiv.find_all('p'): lang = self.get_language(match('flags/(.*).gif', subdiv.img['src']))
if 'alt' in p.attrs and p['alt'] == 'rip': result = {'subid': subid, 'language': lang}
result['rip'] = p.text.strip() for p in subdiv.find_all('p'):
if 'alt' in p.attrs and p['alt'] == 'release': if 'alt' in p.attrs and p['alt'] == 'rip':
result['release'] = p.text.strip() result['rip'] = p.text.strip()
subids.append(result) if 'alt' in p.attrs and p['alt'] == 'release':
return subids result['release'] = p.text.strip()
subids.append(result)
def list_checked(self, video, languages): return subids
return self.query(video.path or video.release, languages, get_keywords(video.guess), video.series, video.season, video.episode)
def list_checked(self, video, languages):
def query(self, filepath, languages, keywords, series, season, episode): return self.query(video.path or video.release, languages, get_keywords(video.guess), video.series, video.season, video.episode)
logger.debug(u'Getting subtitles for %s season %d episode %d with languages %r' % (series, season, episode, languages))
self.init_cache() def query(self, filepath, languages, keywords, series, season, episode):
sid = self.get_likely_series_id(series.lower()) logger.debug(u'Getting subtitles for %s season %d episode %d with languages %r' % (series, season, episode, languages))
try: self.init_cache()
ep_id = self.get_episode_id(sid, season, episode) sid = self.get_likely_series_id(series.lower())
except KeyError: try:
logger.debug(u'Could not find episode id for %s season %d episode %d' % (series, season, episode)) ep_id = self.get_episode_id(sid, season, episode)
return [] except KeyError:
subids = self.get_sub_ids(ep_id) logger.debug(u'Could not find episode id for %s season %d episode %d' % (series, season, episode))
# filter the subtitles with our queried languages return []
subtitles = [] subids = self.get_sub_ids(ep_id)
for subid in subids: # filter the subtitles with our queried languages
language = subid['language'] subtitles = []
if language not in languages: for subid in subids:
continue language = subid['language']
path = get_subtitle_path(filepath, language, self.config.multi) if language not in languages:
subtitle = ResultSubtitle(path, language, self.__class__.__name__.lower(), '%s/download-%d.html' % (self.server_url, subid['subid']), continue
keywords=[subid['rip'], subid['release']]) path = get_subtitle_path(filepath, language, self.config.multi)
subtitles.append(subtitle) subtitle = ResultSubtitle(path, language, self.__class__.__name__.lower(), '%s/download-%d.html' % (self.server_url, subid['subid']),
return subtitles keywords=[subid['rip'], subid['release']])
subtitles.append(subtitle)
def download(self, subtitle): return subtitles
self.download_zip_file(subtitle.link, subtitle.path)
return subtitle def download(self, subtitle):
self.download_zip_file(subtitle.link, subtitle.path)
return subtitle
Service = TvSubtitles
Service = TvSubtitles