/
omdb.py
51 lines (41 loc) · 1.28 KB
/
omdb.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
import requests
import json
from movie import Movie
from datetime import datetime
import os
import locale
from dotenv import load_dotenv
load_dotenv()
omdb_api_key = os.environ['OMDB_API_KEY']
class Omdb:
def __init__(self):
self.host = 'http://www.omdbapi.com'
def omdb_get_movie(self, id):
movie_id = id
r = requests.get(f"http://www.omdbapi.com/?i={movie_id}&apikey={omdb_api_key}")
data = r.json()
imdb_id = data['imdbID']
original_title = data['Title']
title = data['Title']
release_date = datetime.strptime(data['Released'], '%d %b %Y').date()
duration = None
score = data['imdbRating']
rating = data['Rated']
if rating == "PG-13":
rating = "-12"
elif rating in ["PG", "G", "R"]:
rating = "TP"
synopsis = data['Plot']
#print(imdb_id)
#print(title)
#print(original_title)
#print(release_date)
#print(duration)
#print(rating)
#print(score)
movie = Movie(title, original_title, duration, release_date, rating)
movie.imdb_id = imdb_id
movie.score = score
movie.synopsis = synopsis
return movie
Omdb().omdb_get_movie('tt4520988')