2008-06-01 13:15:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-06-19 09:47:02 +00:00
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2008-06-01 13:15:14 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
import feedparser
|
2009-10-12 15:18:59 +00:00
|
|
|
from oxlib.cache import readUrl, readUrlUnicode
|
|
|
|
from oxlib import findRe, stripTags
|
|
|
|
from oxlib import langCode2To3, langTo3Code
|
2008-06-01 13:15:14 +00:00
|
|
|
|
|
|
|
def findSubtitlesByImdb(imdb, parts = 1, language = "eng"):
|
2008-06-19 09:47:02 +00:00
|
|
|
if len(language) == 2:
|
|
|
|
language = langCode2To3(language)
|
|
|
|
elif len(language) != 3:
|
|
|
|
language = langTo3Code(language)
|
|
|
|
url = "http://www.opensubtitles.org/en/search/"
|
|
|
|
if language:
|
|
|
|
url += "sublanguageid-%s/" % language
|
|
|
|
url += "subsumcd-%s/subformat-srt/imdbid-%s/rss_2_00" % (parts, imdb)
|
2009-10-12 11:47:43 +00:00
|
|
|
data = readUrl(url)
|
2008-06-19 09:47:02 +00:00
|
|
|
if "title>opensubtitles.com - search results</title" in data:
|
|
|
|
fd = feedparser.parse(data)
|
|
|
|
opensubtitleId = None
|
|
|
|
if fd.entries:
|
|
|
|
link = fd.entries[0]['links'][0]['href']
|
|
|
|
opensubtitleId = re.compile('subtitles/(.*?)/').findall(link)
|
|
|
|
if opensubtitleId:
|
|
|
|
opensubtitleId = opensubtitleId[0]
|
|
|
|
else:
|
2009-10-12 15:18:59 +00:00
|
|
|
opensubtitleId = findRe(data, '/en/subtitles/(.*?)/')
|
2008-06-19 09:47:02 +00:00
|
|
|
return opensubtitleId
|
2008-06-01 13:15:14 +00:00
|
|
|
|
|
|
|
def downloadSubtitleById(opensubtitle_id):
|
2008-06-19 09:47:02 +00:00
|
|
|
srts = {}
|
2009-10-12 11:47:43 +00:00
|
|
|
data = readUrl('http://www.opensubtitles.org/en/subtitles/%s' % opensubtitle_id)
|
2008-06-19 09:47:02 +00:00
|
|
|
reg_exp = 'href="(/en/download/file/.*?)">(.*?)</a>'
|
|
|
|
for f in re.compile(reg_exp, re.DOTALL).findall(data):
|
2009-10-12 15:18:59 +00:00
|
|
|
name = stripTags(f[1]).split('\n')[0]
|
2008-06-19 09:47:02 +00:00
|
|
|
url = "http://www.opensubtitles.com%s" % f[0]
|
2009-10-12 11:47:43 +00:00
|
|
|
srts[name] = readUrlUnicode(url)
|
2008-06-19 09:47:02 +00:00
|
|
|
return srts
|
|
|
|
|