[opensub] encoding corrections and test fix

This commit is contained in:
Julien Veyssier 2013-03-10 23:59:04 +01:00
commit 98235d7719
2 changed files with 17 additions and 16 deletions

View file

@ -100,26 +100,26 @@ class SubtitlePage(BasePage):
father = self.parser.select(self.document.getroot(),'a#app_link',1).getparent() father = self.parser.select(self.document.getroot(),'a#app_link',1).getparent()
a = self.parser.select(father,'a')[1] a = self.parser.select(father,'a')[1]
id = a.attrib.get('href','').split('/')[-1] id = a.attrib.get('href','').split('/')[-1]
url = "http://www.opensubtitles.org/subtitleserve/sub/%s"%id url = unicode('http://www.opensubtitles.org/subtitleserve/sub/%s'%id)
link = self.parser.select(self.document.getroot(),'link[rel=bookmark]',1) link = self.parser.select(self.document.getroot(),'link[rel=bookmark]',1)
title = link.attrib.get('title','') title = unicode(link.attrib.get('title',''))
nb_cd = int(title.lower().split('cd')[0].split()[-1]) nb_cd = int(title.lower().split('cd')[0].split()[-1])
lang = title.split('(')[1].split(')')[0] lang = unicode(title.split('(')[1].split(')')[0])
file_names = self.parser.select(self.document.getroot(),"img[title~=filename]") file_names = self.parser.select(self.document.getroot(),"img[title~=filename]")
if len(file_names) > 0: if len(file_names) > 0:
file_name = file_names[0].getparent().text_content() file_name = file_names[0].getparent().text_content()
file_name = " ".join(file_name.split()) file_name = ' '.join(file_name.split())
desc = u"files :" desc = u'files :'
for f in file_names: for f in file_names:
desc_line = f.getparent().text_content() desc_line = f.getparent().text_content()
desc += "\n"+" ".join(desc_line.split()) desc += '\n'+' '.join(desc_line.split())
name = "%s (%s)"%(title,file_name) name = unicode('%s (%s)'%(title,file_name))
subtitle = Subtitle(id,name) subtitle = Subtitle(id,name)
subtitle.url = url subtitle.url = url
for lshort,llong in LANGUAGE_CONV.items(): for lshort,llong in LANGUAGE_CONV.items():
if lang == llong: if lang == llong:
lang = lshort lang = unicode(lshort)
break break
subtitle.language = lang subtitle.language = lang
subtitle.nb_cd = nb_cd subtitle.nb_cd = nb_cd

View file

@ -19,20 +19,21 @@
from weboob.tools.test import BackendTest from weboob.tools.test import BackendTest
import urllib
from random import choice from random import choice
class OpensubtitlesTest(BackendTest): class OpensubtitlesTest(BackendTest):
BACKEND = 'opensubtitles' BACKEND = 'opensubtitles'
def test_subtitle(self): def test_subtitle(self):
subtitles = list(self.backend.iter_subtitles('fr','spiderman')) lsub = []
assert (len(subtitles) > 0) subtitles = self.backend.iter_subtitles('fr','spiderman')
for subtitle in subtitles: for i in range(5):
path, qs = urllib.splitquery(subtitle.url) subtitle = subtitles.next()
assert path.endswith('.zip') lsub.append(subtitle)
assert subtitle.url.startswith('http')
assert (len(lsub) > 0)
# get the file of a random sub # get the file of a random sub
if len(subtitles): if len(lsub):
subtitle = choice(subtitles) subtitle = choice(lsub)
self.backend.get_subtitle_file(subtitle.id) self.backend.get_subtitle_file(subtitle.id)