Skip to content

Commit 160e5d3

Browse files
committed
add OpenSubtitle2
1 parent cbb52a7 commit 160e5d3

File tree

19 files changed

+393
-2
lines changed

19 files changed

+393
-2
lines changed

configure.ac

+11
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ Makefile
1111
plugin/Makefile
1212
plugin/seekers/Makefile
1313
plugin/seekers/Edna/Makefile
14+
plugin/seekers/Elsubtitle/Makefile
15+
plugin/seekers/Indexsubtitle/Makefile
1416
plugin/seekers/Itasa/Makefile
17+
plugin/seekers/Moviesubtitles/Makefile
18+
plugin/seekers/Moviesubtitles2/Makefile
19+
plugin/seekers/MySubs/Makefile
1520
plugin/seekers/OpenSubtitles/Makefile
21+
plugin/seekers/OpenSubtitles2/Makefile
1622
plugin/seekers/Podnapisi/Makefile
1723
plugin/seekers/SerialZone/Makefile
24+
plugin/seekers/Subdl/Makefile
1825
plugin/seekers/Subscene/Makefile
1926
plugin/seekers/SubtitlesGR/Makefile
27+
plugin/seekers/Subsyts/Makefile
28+
plugin/seekers/Subtitlecat/Makefile
29+
plugin/seekers/Subtitlesmora/Makefile
30+
plugin/seekers/Subtitlist/Makefile
2031
plugin/seekers/Titlovi/Makefile
2132
plugin/seekers/Titulky/Makefile
2233
plugin/parsers/Makefile

plugin/seekers/Elsubtitle/Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Elsubtitle
2+
install_PYTHON = __init__.py service.py ElsubtitleUtilities.py
3+
install_DATA = logo.png
4+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Indexsubtitle
2+
install_PYTHON = __init__.py service.py IndexsubtitleUtilities.py
3+
install_DATA = logo.png
4+

plugin/seekers/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SUBDIRS = Edna Itasa OpenSubtitles Podnapisi SerialZone Subscene SubtitlesGR Titlovi Titulky
1+
SUBDIRS = Edna Elsubtitle Indexsubtitle Itasa Moviesubtitles Moviesubtitles2 MySubs OpenSubtitles OpenSubtitles2 Podnapisi SerialZone Subdl Subscene Subsyts Subtitlecat SubtitlesGR Subtitlesmora Subtitlist Titlovi Titulky
22

33
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers
44
install_PYTHON = __init__.py seeker.py utilities.py xbmc_subtitles.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Moviesubtitles
2+
install_PYTHON = __init__.py service.py MoviesubtitlesUtilities.py
3+
install_DATA = logo.png
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Moviesubtitles2
2+
install_PYTHON = __init__.py service.py Moviesubtitles2Utilities.py
3+
install_DATA = logo.png
4+

plugin/seekers/MySubs/Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/MySubs
2+
install_PYTHON = __init__.py service.py MySubsUtilities.py
3+
install_DATA = logo.png
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/OpenSubtitles2
2+
install_PYTHON = __init__.py service.py OpenSubtitles2Utilities.py
3+
install_DATA = logo.png
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
import requests
4+
from ..utilities import log as _log
5+
6+
import six
7+
8+
9+
ses = requests.Session()
10+
11+
LANGUAGES = (
12+
("Albanian", "29", "sq", "alb", "0", 30201),
13+
("Arabic", "12", "ar", "ara", "1", 30202),
14+
("Belarusian", "0", "hy", "arm", "2", 30203),
15+
("Bosnian", "10", "bs", "bos", "3", 30204),
16+
("Bulgarian", "33", "bg", "bul", "4", 30205),
17+
("Catalan", "53", "ca", "cat", "5", 30206),
18+
("Chinese", "17", "zh", "chi", "6", 30207),
19+
("Croatian", "38", "hr", "hrv", "7", 30208),
20+
("Czech", "7", "cs", "cze", "8", 30209),
21+
("Danish", "24", "da", "dan", "9", 30210),
22+
("Dutch", "23", "nl", "dut", "10", 30211),
23+
("English", "2", "en", "eng", "11", 30212),
24+
("Estonian", "20", "et", "est", "12", 30213),
25+
("Persian", "52", "fa", "per", "13", 30247),
26+
("Finnish", "31", "fi", "fin", "14", 30214),
27+
("French", "8", "fr", "fre", "15", 30215),
28+
("German", "5", "de", "ger", "16", 30216),
29+
("Greek", "16", "el", "ell", "17", 30217),
30+
("Hebrew", "22", "he", "heb", "18", 30218),
31+
("Hindi", "42", "hi", "hin", "19", 30219),
32+
("Hungarian", "15", "hu", "hun", "20", 30220),
33+
("Icelandic", "6", "is", "ice", "21", 30221),
34+
("Indonesian", "0", "id", "ind", "22", 30222),
35+
("Italian", "9", "it", "ita", "23", 30224),
36+
("Japanese", "11", "ja", "jpn", "24", 30225),
37+
("Korean", "4", "ko", "kor", "25", 30226),
38+
("Latvian", "21", "lv", "lav", "26", 30227),
39+
("Lithuanian", "0", "lt", "lit", "27", 30228),
40+
("Macedonian", "35", "mk", "mac", "28", 30229),
41+
("Malay", "0", "ms", "may", "29", 30248),
42+
("Norwegian", "3", "no", "nor", "30", 30230),
43+
("Polish", "26", "pl", "pol", "31", 30232),
44+
("Portuguese", "32", "pt", "por", "32", 30233),
45+
("PortugueseBrazil", "48", "pb", "pob", "33", 30234),
46+
("Romanian", "13", "ro", "rum", "34", 30235),
47+
("Russian", "27", "ru", "rus", "35", 30236),
48+
("Serbian", "36", "sr", "scc", "36", 30237),
49+
("Slovak", "37", "sk", "slo", "37", 30238),
50+
("Slovenian", "1", "sl", "slv", "38", 30239),
51+
("Spanish", "28", "es", "spa", "39", 30240),
52+
("Swedish", "25", "sv", "swe", "40", 30242),
53+
("Thai", "0", "th", "tha", "41", 30243),
54+
("Turkish", "30", "tr", "tur", "42", 30244),
55+
("Ukrainian", "46", "uk", "ukr", "43", 30245),
56+
("Vietnamese", "51", "vi", "vie", "44", 30246),
57+
("BosnianLatin", "10", "bs", "bos", "100", 30204),
58+
("Farsi", "52", "fa", "per", "13", 30247),
59+
("English (US)", "2", "en", "eng", "100", 30212),
60+
("English (UK)", "2", "en", "eng", "100", 30212),
61+
("Portuguese (Brazilian)", "48", "pt-br", "pob", "100", 30234),
62+
("Portuguese (Brazil)", "48", "pb", "pob", "33", 30234),
63+
("Portuguese-BR", "48", "pb", "pob", "33", 30234),
64+
("Brazilian", "48", "pb", "pob", "33", 30234),
65+
("Español (Latinoamérica)", "28", "es", "spa", "100", 30240),
66+
("Español (España)", "28", "es", "spa", "100", 30240),
67+
("Spanish (Latin America)", "28", "es", "spa", "100", 30240),
68+
("Español", "28", "es", "spa", "100", 30240),
69+
("SerbianLatin", "36", "sr", "scc", "100", 30237),
70+
("Spanish (Spain)", "28", "es", "spa", "100", 30240),
71+
("Chinese (Traditional)", "17", "zh", "chi", "100", 30207),
72+
("Chinese (Simplified)", "17", "zh", "chi", "100", 30207))
73+
74+
opensubtitles2_languages = {
75+
'Chinese BG code': 'Chinese',
76+
'Brazillian Portuguese': 'Portuguese (Brazil)',
77+
'Serbian': 'SerbianLatin',
78+
'Ukranian': 'Ukrainian',
79+
'Farsi/Persian': 'Persian'
80+
}
81+
82+
83+
def get_language_info(language):
84+
if language in opensubtitles2_languages:
85+
language = opensubtitles2_languages[language]
86+
87+
for lang in LANGUAGES:
88+
if lang[0] == language:
89+
return {'name': lang[0], '2et': lang[2], '3et': lang[3]}
90+
91+
92+
def log(module, msg):
93+
_log(module, msg.encode('utf-8'))
94+
95+
96+
def geturl(url1, headers=None, params=None):
97+
try:
98+
res = ses.get(url1, headers=headers, verify=False, timeout=5)
99+
print('res.status_code', res.status_code)
100+
if res.status_code == 200:
101+
return six.ensure_str(res.content)
102+
e = res.raise_for_status()
103+
print(('Download error', e))
104+
return ''
105+
except requests.exceptions.RequestException as e:
106+
print(('Download error', str(e)))
107+
return ''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from __future__ import absolute_import
2+
# Dummy file to make this directory a package.
3+
from . import service as opensubtitles2
13.3 KB
Loading
+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# -*- coding: UTF-8 -*-
2+
from __future__ import absolute_import
3+
4+
import os
5+
import re
6+
import requests
7+
#import warnings
8+
#from requests.packages.urllib3.exceptions import InsecureRequestWarning
9+
from .OpenSubtitles2Utilities import get_language_info
10+
#warnings.simplefilter('ignore', InsecureRequestWarning)
11+
import os.path
12+
from urllib.request import HTTPCookieProcessor, build_opener, install_opener, Request, urlopen
13+
from urllib.parse import urlencode
14+
from ..utilities import languageTranslate, log, getFileSize
15+
from ..seeker import SubtitlesDownloadError, SubtitlesErrors
16+
17+
HDR = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:109.0) Gecko/20100101 Firefox/115.0',
18+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
19+
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
20+
'Content-Type': 'text/html; charset=UTF-8',
21+
'Host': 'www.opensubtitles.com',
22+
'Referer': 'https://www.opensubtitles.com',
23+
'Upgrade-Insecure-Requests': '1',
24+
'Connection': 'keep-alive',
25+
'Accept-Encoding': 'gzip, deflate'} # , deflate'}
26+
27+
s = requests.Session()
28+
29+
main_url = "https://www.opensubtitles.com"
30+
debug_pretext = "opensubtitles.com"
31+
32+
33+
def get_url(url, referer=None):
34+
if referer is None:
35+
headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'}
36+
else:
37+
headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0', 'Referer': referer}
38+
req = Request(url, None, headers)
39+
response = urlopen(req)
40+
content = response.read().decode('utf-8')
41+
response.close()
42+
content = content.replace('\n', '')
43+
return content
44+
45+
46+
def get_rating(downloads):
47+
rating = int(downloads)
48+
if (rating < 50):
49+
rating = 1
50+
elif (rating >= 50 and rating < 100):
51+
rating = 2
52+
elif (rating >= 100 and rating < 150):
53+
rating = 3
54+
elif (rating >= 150 and rating < 200):
55+
rating = 4
56+
elif (rating >= 200 and rating < 250):
57+
rating = 5
58+
elif (rating >= 250 and rating < 300):
59+
rating = 6
60+
elif (rating >= 300 and rating < 350):
61+
rating = 7
62+
elif (rating >= 350 and rating < 400):
63+
rating = 8
64+
elif (rating >= 400 and rating < 450):
65+
rating = 9
66+
elif (rating >= 450):
67+
rating = 10
68+
return rating
69+
70+
71+
def search_subtitles(file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack): # standard input
72+
languagefound = lang1
73+
language_info = get_language_info(languagefound)
74+
language_info1 = language_info['name']
75+
language_info2 = language_info['2et']
76+
language_info3 = language_info['3et']
77+
78+
subtitles_list = []
79+
msg = ""
80+
81+
if len(tvshow) == 0 and year: # Movie
82+
searchstring = "%s (%s)" % (title, year)
83+
elif len(tvshow) > 0 and title == tvshow: # Movie not in Library
84+
searchstring = "%s (%#02d%#02d)" % (tvshow, int(season), int(episode))
85+
elif len(tvshow) > 0: # TVShow
86+
searchstring = "%s S%#02dE%#02d" % (tvshow, int(season), int(episode))
87+
else:
88+
searchstring = title
89+
log(__name__, "%s Search string = %s" % (debug_pretext, searchstring))
90+
get_subtitles_list(searchstring, language_info2, language_info1, subtitles_list)
91+
return subtitles_list, "", msg # standard output
92+
93+
94+
def download_subtitles(subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): # standard input
95+
language = subtitles_list[pos]["language_name"]
96+
id = subtitles_list[pos]["id"]
97+
#id = re.compile('(.+?.+?)/').findall(id)[-1]
98+
link = '%s%s' % (main_url, id)
99+
#print(link)
100+
content = get_url(link, referer=main_url)
101+
#print(content)
102+
regx = "https:(.+?).srt"
103+
try:
104+
downloadlink = 'https:' + re.findall(regx, content, re.M | re.I)[0] + '.srt'
105+
except:
106+
pass
107+
print(downloadlink)
108+
if downloadlink:
109+
log(__name__, "%s Downloadlink: %s " % (debug_pretext, downloadlink))
110+
viewstate = 0
111+
previouspage = 0
112+
subtitleid = 0
113+
typeid = "zip"
114+
filmid = 0
115+
#postparams = { '__EVENTTARGET': 's$lc$bcr$downloadLink', '__EVENTARGUMENT': '' , '__VIEWSTATE': viewstate, '__PREVIOUSPAGE': previouspage, 'subtitleId': subtitleid, 'typeId': typeid, 'filmId': filmid}
116+
postparams = urlencode({'__EVENTTARGET': 's$lc$bcr$downloadLink', '__EVENTARGUMENT': '', '__VIEWSTATE': viewstate, '__PREVIOUSPAGE': previouspage, 'subtitleId': subtitleid, 'typeId': typeid, 'filmId': filmid})
117+
#class MyOpener(urllib.FancyURLopener):
118+
#version = 'User-Agent=Mozilla/5.0 (Windows NT 6.1; rv:109.0) Gecko/20100101 Firefox/115.0'
119+
#my_urlopener = MyOpener()
120+
#my_urlopener.addheader('Referer', url)
121+
log(__name__, "%s Fetching subtitles using url with referer header '%s' and post parameters '%s'" % (debug_pretext, downloadlink, postparams))
122+
#response = my_urlopener.open(downloadlink, postparams)
123+
response = s.get(downloadlink, data=postparams, headers=HDR, verify=False, allow_redirects=True)
124+
#print(response.content)
125+
local_tmp_file = zip_subs
126+
try:
127+
log(__name__, "%s Saving subtitles to '%s'" % (debug_pretext, local_tmp_file))
128+
if not os.path.exists(tmp_sub_dir):
129+
os.makedirs(tmp_sub_dir)
130+
local_file_handle = open(local_tmp_file, 'wb')
131+
local_file_handle.write(response.content)
132+
local_file_handle.close()
133+
# Check archive type (rar/zip/else) through the file header (rar=Rar!, zip=PK) urllib3.request.urlencode
134+
myfile = open(local_tmp_file, "rb")
135+
myfile.seek(0)
136+
if (myfile.read(1).decode('utf-8') == 'R'):
137+
typeid = "rar"
138+
packed = True
139+
log(__name__, "Discovered RAR Archive")
140+
else:
141+
myfile.seek(0)
142+
if (myfile.read(1).decode('utf-8') == 'P'):
143+
typeid = "zip"
144+
packed = True
145+
log(__name__, "Discovered ZIP Archive")
146+
else:
147+
typeid = "srt"
148+
packed = False
149+
subs_file = local_tmp_file
150+
log(__name__, "Discovered a non-archive file")
151+
myfile.close()
152+
log(__name__, "%s Saving to %s" % (debug_pretext, local_tmp_file))
153+
except:
154+
log(__name__, "%s Failed to save subtitle to %s" % (debug_pretext, local_tmp_file))
155+
if packed:
156+
subs_file = typeid
157+
log(__name__, "%s Subtitles saved to '%s'" % (debug_pretext, local_tmp_file))
158+
return packed, language, subs_file # standard output
159+
160+
161+
def get_subtitles_list(searchstring, languageshort, languagelong, subtitles_list):
162+
search_string = searchstring.lower().replace(" ", "-").replace(":", "-")
163+
lang = languageshort
164+
print('search_string', search_string)
165+
url = '%s/fr/%s/search-all/q-%s/hearing_impaired-include/machine_translated-/trusted_sources-' % (main_url, lang, search_string)
166+
print('url', url)
167+
168+
try:
169+
log(__name__, "%s Getting url: %s" % (debug_pretext, url))
170+
content = get_url(url, referer=main_url)
171+
content = content.replace('amp;', '')
172+
#print(content)
173+
except:
174+
pass
175+
log(__name__, "%s Failed to get url:%s" % (debug_pretext, url))
176+
return
177+
try:
178+
log(__name__, "%s Getting '%s' subs ..." % (debug_pretext, languageshort))
179+
subtitles = re.compile('(<a href="/fr/subtitles/.+?</a><div.+?<i title="Direct download)').findall(content)
180+
##print('subtitles',subtitles)
181+
except:
182+
log(__name__, "%s Failed to get subtitles" % (debug_pretext))
183+
return
184+
for subtitle in subtitles:
185+
try:
186+
filename = re.compile('<a href=".+?">(.+?)</a><div').findall(subtitle)[0]
187+
#filename = filename.split("subtitles for")[-1]
188+
filename = filename.strip()
189+
#print(filename)
190+
id = re.compile('<a download.+?href="(.+?)"><i').findall(subtitle)[0]
191+
#print(id)
192+
try:
193+
downloads = re.compile('<a title="votes".+?href.+?>.+?</a>.+?href.+?>(.+?)</a>').findall(subtitle)[0]
194+
downloads = re.sub("\D", "", downloads)
195+
#print(downloads)
196+
except:
197+
pass
198+
try:
199+
rating = get_rating(downloads)
200+
#print(rating)
201+
except:
202+
rating = 0
203+
pass
204+
if not downloads == 0:
205+
log(__name__, "%s Subtitles found: %s (id = %s)" % (debug_pretext, filename, id))
206+
subtitles_list.append({'rating': str(rating), 'no_files': 1, 'filename': filename, 'sync': False, 'id': id, 'language_flag': 'flags/' + languageshort + '.gif', 'language_name': languagelong})
207+
except:
208+
pass
209+
return

plugin/seekers/Subdl/Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Subdl
2+
install_PYTHON = __init__.py service.py SubdlUtilities.py
3+
install_DATA = logo.png
4+

plugin/seekers/Subsyts/Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Subsyts
2+
install_PYTHON = __init__.py service.py SubsytsUtilities.py
3+
install_DATA = logo.png
4+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
installdir = $(libdir)/enigma2/python/Plugins/Extensions/SubsSupport/seekers/Subtitlecat
2+
install_PYTHON = __init__.py service.py SubtitlecatUtilities.py
3+
install_DATA = logo.png
4+

0 commit comments

Comments
 (0)