This repository has been archived by the owner on May 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_connection.py
138 lines (105 loc) · 4.45 KB
/
api_connection.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
import http.client
import urllib.parse
import json
import logging.config
import logging
from constants import ConstantsToken
from errors import FilmNotFound
class APIMethods():
def __init__(self):
self.URL_ADDRES = "api.themoviedb.org"
self.API_KEY = ConstantsToken.TMBD_API_TOKEN
self.EXPORT_DATA = {
"page": "1",
"language": "ru",
"api_key": self.API_KEY
}
self.connection_api = http.client.HTTPSConnection(self.URL_ADDRES)
self.image_size = "w300"
self.logger = logging.getLogger("console_logger")
def urlencode_to_send(self, data):
return urllib.parse.urlencode(data)
def send_request(self, where_to_send, type_request="GET", payload={}):
self.connection_api.request(type_request, where_to_send, payload)
self.logger.debug("NEW REQUESTS " + where_to_send)
result = self.connection_api.getresponse().read().decode("utf-8")
result_json = json.loads(result)
return result_json
def get_movie_by_title(self, title):
current_data = self.EXPORT_DATA.copy()
current_data["query"] = title
formated = self.urlencode_to_send(current_data)
curent_url = "/3/search/movie?" + formated
answer = self.send_request(curent_url)
if len(answer["results"]) > 0:
return answer
else:
self.logger.debug("FILM '" + title + "' NOT FOUND")
raise FilmNotFound
def get_most_popular(self):
current_data = self.EXPORT_DATA.copy()
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/movie/popular?" + formated_data
answer = self.send_request(current_url)
return answer
def get_similar_movie(self, id):
current_data = self.EXPORT_DATA.copy()
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/movie/" + str(id) + "/similar?" + formated_data
answer = self.send_request(current_url)
return answer
def get_now_playing(self):
current_data = self.EXPORT_DATA.copy()
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/movie/now_playing?" + formated_data
answer = self.send_request(current_url)
return answer
def get_movie_upcoming(self):
current_data = self.EXPORT_DATA.copy()
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/movie/upcoming?" + formated_data
ans = self.send_request(current_url)
return ans
def get_image_url(self, path):
base_url = "https://image.tmdb.org/t/p/"
size = self.image_size
current_url = base_url + size + path
return current_url
def get_movie_by_params(self, **params):
current_data = self.EXPORT_DATA.copy()
for key in params:
current_data[key] = params[key]
print(current_data)
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/discover/movie?" + formated_data
ans = self.send_request(current_url)
return ans
def get_genres(self):
# Можно сделать иначе. Не нужно постоянно делать запрос, поскольку это будет занимать время
# А жанры и их id вроде как не меняются
current_data = self.EXPORT_DATA.copy()
del current_data["page"]
formated_data = self.urlencode_to_send(current_data)
print(formated_data)
current_url = "/3/genre/movie/list?" + formated_data
ans = self.send_request(current_url)
return ans
def get_movies_by_genre(self):
# Можно использовать get_movie_by_params
current_data = self.EXPORT_DATA.copy()
# TODO: Сформировать нормальную ссылку
pass
def get_top_rated(self):
current_data = self.EXPORT_DATA.copy()
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/movie/top_rated?" + formated_data
ans = self.send_request(current_url)
return ans
def get_film_by_id(self, film_id):
current_data = self.EXPORT_DATA.copy()
formated_data = self.urlencode_to_send(current_data)
current_url = "/3/movie/" + str(film_id) + "?" + formated_data
ans = self.send_request(current_url)
return ans
if __name__ == "__main__":
pass