-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
89 lines (72 loc) · 3.63 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from flask import Flask
from flask import render_template, request, json, Response
import opensubtitles
import requests
app = Flask(__name__)
app.config.from_pyfile("settings.cfg")
all_languages = {'Arabic': 'ara', 'Bulgarian': 'bul', 'Dutch': 'dut', 'English': 'eng', 'French': 'fre',
'German': 'ger',
'Greek': 'ell', 'Hungarian': 'hun', 'Italian': 'ita', 'Polish': 'pol', 'Portuguese': 'por',
'Portuguese_BR': 'pob', 'Romanian': 'rum', 'Russian': 'rus', 'Spanish': 'spa', 'Turkish': 'tur'}
def _get_imdb_data(imdb_id):
imdb_id = "tt" + "%07d" % int(imdb_id)
payload = {"i": imdb_id, "apikey": app.config['OMDB_API_KEY']}
r = requests.get("http://www.omdbapi.com", params=payload)
imdb_data = r.json()
return imdb_data
def _get_tmdb_data(imdb_id):
imdb_id = "tt" + "%07d" % int(imdb_id)
payload = {"external_source": "imdb_id", "api_key": app.config['MOVIEDB_API_KEY']}
r = requests.get("http://api.themoviedb.org/3/find/%s" % imdb_id, params=payload)
return r.json()
def _get_poster_url_from_tmdb(tmdb_data, width):
image_path = None
for key, value in tmdb_data.items():
for data in value:
if 'poster_path' or 'still_path' in data:
image_path = data.get('poster_path') or data.get('still_path')
if not image_path:
return None
tmdb_image_url = "http://image.tmdb.org/t/p/w%s/%s" % (width, image_path)
return tmdb_image_url
def _stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
rv.disable_buffering()
return rv
@app.route("/", methods=['GET'])
@app.route("/results", methods=['POST'])
def index():
selected_language = request.cookies.get('language') or all_languages['English']
if request.method == "POST":
form_data = json.loads(request.form['data'])
language = request.form['language'] # language user selected
data = form_data['data']
token = opensubtitles.login(api_key=app.config['OPENSUBTITLES_API_KEY']).get('token') # Create session
def files(data):
for i, fileData in enumerate(data):
hash = fileData['hash']
file_name = fileData['fileName']
file_size = fileData['fileSize']
search_data = [{'moviehash': hash, 'moviebytesize': file_size, 'sublanguageid': language}]
sub_data = opensubtitles.search_sub(search_data, token)
if sub_data:
for imdb in sub_data:
imdb_data = _get_imdb_data(imdb[0]['IDMovieImdb'])
tmdb_data = _get_tmdb_data(imdb[0]['IDMovieImdb'])
imdb[0]['poster_tmdb'] = _get_poster_url_from_tmdb(tmdb_data, width=300)
imdb[0].update(imdb_data)
result = {'file': file_name, 'data': sub_data}
else:
result = {'file': file_name, 'error': "Could not find subtitle"}
yield (result, i + 1)
opensubtitles.logout(token) # End session
resp = Response(
_stream_template("index.html", files=files(data), total_files=len(data), languages=all_languages,
selected_language=language))
if selected_language != language:
resp.set_cookie('language', language) # Store language in cookie if it is different than default
return resp
return render_template("index.html", total_files=0, files=[], languages=all_languages,
selected_language=selected_language)