-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
146 lines (127 loc) · 6.22 KB
/
logic.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from movies import Movies
from genres import Genres
from dateutil import parser
from datetime import datetime, date
import requests
import random
class Api:
def __init__(self):
self.currMovies = 702625
self.g = Genres()
self.m = Movies()
self.imgURI = 'https://image.tmdb.org/t/p/w220_and_h330_face'
self._apiKey = 'cab215b672637a6d24409a56bcd457c2'
self._baseUri = 'https://api.themoviedb.org/'
pass
def getGenres(self):
endpoint = '3/genre/movie/list'
genres = requests.get(url=self._baseUri+endpoint, params=dict(api_key=self._apiKey,))
d = genres.json()
[self._saveGenres(g) for g in d['genres']]
def _saveGenres(self, genre):
if not self.g.exists(genre['id']):
self.g.saveGenre(genreId=genre['id'], genreName=genre['name'])
def _getGenreWithID(self, id):
return self.g.getGenreName(id)
def getMovies(self):
result = self.makeRequest()
l = 'popularity.asc, popularity.desc, release_date.asc, release_date.desc, revenue.asc, revenue.desc, primary_release_date.asc, primary_release_date.desc, original_title.asc, original_title.desc, vote_average.asc, vote_average.desc, vote_count.asc, vote_count.desc'
totalPages = result['total_pages']
print(f'going to download info for {result["total_results"]} movies :)))\n\n')
[self._saveMovies(m) for m in result['results']]
for page in range(totalPages):
print(f'current page: {page} \n\n')
for c in l.split(', '):
result = self.makeRequest(endpoint= '3/discover/movie', page=page+1, c=c)
[self._saveMovies(m) for m in result['results']]
result = self.makeRequest(endpoint= '3/discover/tv')
totalPages = result['total_pages']
for page in range(totalPages):
print(f'current page: {page} \n\n')
for c in l.split(', '):
result = self.makeRequest(endpoint= '3/discover/tv',page=page+1, c=c)
[self._saveMovies(m) for m in result['results']]
def findMovie(self, search='', popularity=-1, genres='', adult=False, limit=10):
print(search, adult, genres, popularity, limit)
popularity = int(popularity)
array = self.m.getList(search=search, popularity=popularity, genres=genres, isAdult=adult,)
final = []
if array == []:
final.append(dict(
imgPath='https://scontent.ftia4-1.fna.fbcdn.net/v/t1.0-9/31358061_1486482608130033_6889977130264821760_o.jpg?_nc_cat=108&_nc_sid=09cbfe&_nc_ohc=LTfoIC-71R0AX8GBsPb&_nc_ht=scontent.ftia4-1.fna&oh=38837f4d06399f89709401838678f389&oe=5EDD08ED',
title='No movies with your preferences!',
description=f'You searched for "{search}". \n We are sorry but we coldn\'t find movies with that name on the categories you chose, so we are ofering you some random suggestions',
bday='',
language='Sorry',
genres=genres,
popularity=popularity,
)
)
array = self.m.getList(isAdult=adult,)
while len(final) < limit:
final.append(array.pop(random.randint(0, len(array))))
elif len(array) > limit:
while len(final) < limit:
try:
final.append(array.pop(random.randint(0, len(array))))
except Exception:
break
else:
final = array
return final
def makeRequest(self, endpoint= '3/discover/movie', page=1, c='popularity.desc'):
genres = requests.get(
url=self._baseUri+endpoint,
params=dict(
api_key=self._apiKey,
include_adult=True,
sort_by=c,
page=page
))
return genres.json()
def getSavedMovies(self):
[print(l, '\n') for l in self.m.getAllMovies() ]
return self.m.getAllMovies()
def _saveMovies(self, movie):
try:
popularity = movie['popularity']
if movie['poster_path']:
posterPath = self.imgURI + movie['poster_path']
else:
posterPath = 'https://scontent.ftia4-1.fna.fbcdn.net/v/t1.0-9/31358061_1486482608130033_6889977130264821760_o.jpg?_nc_cat=108&_nc_sid=09cbfe&_nc_ohc=LTfoIC-71R0AX8GBsPb&_nc_ht=scontent.ftia4-1.fna&oh=38837f4d06399f89709401838678f389&oe=5EDD08ED'
movieId = movie['id']
isAdult = movie['adult']
language = movie['original_language']
genres = []
[genres.append(self._getGenreWithID(id)) for id in movie['genre_ids']]
title = movie['title']
description = movie['overview']
bday = movie['release_date']
votes = movie['vote_count']
bday = parser.parse(bday)
averageVote = movie['vote_average']
if not self.m.exists(movieId):
self.m.saveMovie(
movieId=movieId,
movieTitle=title,
description=description,
popularity=popularity,
posterPath=posterPath,
isAdult=isAdult,
language=language,
genre=''.join(str(e+' ') for e in genres),
bday=bday,
votes=votes,
averageVote=averageVote
)
print(f'\tsaved movie with id: {movieId} \t title: {title}')
return f'\tsaved movie with id: {movieId} \t title: {title}'
else:
print(f'\talready saved movie with id: {movieId} \t title: {title}')
return f'\talready saved movie with id: {movieId} \t title: {title}'
except Exception:
print('smth went wrong but we good')
return False
if __name__ == "__main__":
a = Api()
[print(e) for e in a.findMovie(adult=True, popularity=10)]