add opensubtitles
This commit is contained in:
parent
65283843bf
commit
66fe37b4ce
1 changed files with 45 additions and 0 deletions
45
ox/opensubtitles.py
Normal file
45
ox/opensubtitles.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vi:si:et:sw=2:sts=2:ts=2
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
import feedparser
|
||||||
|
from oxutils.cache import getUrl, getUrlUnicode
|
||||||
|
import oxutils
|
||||||
|
from oxutils.lang import langCode2To3, langTo3Code
|
||||||
|
|
||||||
|
def findSubtitlesByImdb(imdb, parts = 1, language = "eng"):
|
||||||
|
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)
|
||||||
|
data = getUrl(url)
|
||||||
|
if "title>opensubtitles.com - search results</title" in data:
|
||||||
|
fd = feedparser.parse(data)
|
||||||
|
opensubtitleId = None
|
||||||
|
print url
|
||||||
|
if fd.entries:
|
||||||
|
link = fd.entries[0]['links'][0]['href']
|
||||||
|
print link
|
||||||
|
opensubtitleId = re.compile('subtitles/(.*?)/').findall(link)
|
||||||
|
if opensubtitleId:
|
||||||
|
opensubtitleId = opensubtitleId[0]
|
||||||
|
else:
|
||||||
|
opensubtitleId = oxutils.findRe(data, '/en/subtitles/(.*?)/')
|
||||||
|
return opensubtitleId
|
||||||
|
|
||||||
|
def downloadSubtitleById(opensubtitle_id):
|
||||||
|
srts = {}
|
||||||
|
data = getUrl('http://www.opensubtitles.org/en/subtitles/%s' % opensubtitle_id)
|
||||||
|
reg_exp = 'href="(/en/download/file/.*?)">(.*?)</a>'
|
||||||
|
for f in re.compile(reg_exp, re.DOTALL).findall(data):
|
||||||
|
name = oxutils.stripTags(f[1]).split('\n')[0]
|
||||||
|
url = "http://www.opensubtitles.com%s" % f[0]
|
||||||
|
srts[name] = getUrlUnicode(url)
|
||||||
|
return srts
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue