-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.py
46 lines (39 loc) · 1.24 KB
/
api.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
from flask import Flask, request
from torrent_crawler.search import Search, SearchQuery
from torrent_crawler.constants import Constants
import json
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
@app.route('/movies', methods=['GET'])
def salvador():
search_string = request.args.get('search')
order_by = request.args.get('order') or Constants.order_by[0]
genre = request.args.get('genre') or Constants.genre[0]
quality = request.args.get('quality') or Constants.quality[0]
rating = 0
search_query = SearchQuery(search_string, quality, genre, rating, order_by)
search = Search(search_query, True)
movies = search.start(search_query)
result = [json.loads(movie.to_json()) for movie in movies]
count = len(result)
if count == 0:
message = 'No movies found'
elif count == 1:
message = 'Only 1 movie found'
else:
message = 'Total {count} movies found'.format(count=count)
data = {
'count': count,
'message': message,
'result': result
}
response = app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
return response
if __name__ == '__main__':
app.run()