python-ox/ox/web/opensubtitles.py

42 lines
1.5 KiB
Python
Raw Permalink Normal View History

2010-07-07 23:25:57 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
from ox.cache import read_url
from ox import find_re, strip_tags
2012-09-09 17:28:11 +00:00
from ox.iso import langCode2To3, langTo3Code
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def find_subtitles(imdb, parts = 1, language = "eng"):
2018-01-14 15:47:15 +00:00
import feedparser
2010-07-07 23:25:57 +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)
data = read_url(url)
2010-07-07 23:25:57 +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:
opensubtitleId = find_re(data, '/en/subtitles/(.*?)/')
2010-07-07 23:25:57 +00:00
return opensubtitleId
2012-08-15 15:15:40 +00:00
def download_subtitle(opensubtitle_id):
2010-07-07 23:25:57 +00:00
srts = {}
data = read_url('http://www.opensubtitles.org/en/subtitles/%s' % opensubtitle_id)
2010-07-07 23:25:57 +00:00
reg_exp = 'href="(/en/download/file/.*?)">(.*?)</a>'
for f in re.compile(reg_exp, re.DOTALL).findall(data):
name = strip_tags(f[1]).split('\n')[0]
2010-07-07 23:25:57 +00:00
url = "http://www.opensubtitles.com%s" % f[0]
srts[name] = read_url(url, unicode=True)
2010-07-07 23:25:57 +00:00
return srts