-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_storage.py
126 lines (107 loc) · 4.71 KB
/
data_storage.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
from pymongo import MongoClient
import os
from dotenv import load_dotenv
from media_finder import MediaFinder
# Load environment variables from either a .env file(dev) or from the environment(production)
load_dotenv()
# URL of the database
client = MongoClient(os.getenv("DATABASE_URL"))
db = client.test_media_data
collection = db.media_data
# MediaFinder Object
media_finder = MediaFinder()
def update_all_info(code):
"""Gets all required data from the TMDB API and updates the information in the database."""
# If the code supplied is correct, update the database.
if code == os.getenv("UPDATE_VERIFICATION_CODE"):
# pymongo transaction
with client.start_session() as session:
# start the transaction
with session.start_transaction():
main_home = db.media_data.update_one(
{"page": "main_home"},
{"$set": {"content": get_main_home_info()}},
upsert=True,
)
movie_home = db.media_data.update_one(
{"page": "movie_home"},
{"$set": {"content": get_movie_home_info()}},
upsert=True,
)
tv_home = db.media_data.update_one(
{"page": "tv_home"},
{"$set": {"content": get_tv_home_info()}},
upsert=True,
)
return (
main_home.matched_count + movie_home.matched_count + tv_home.matched_count
)
else:
return None
def load_info(page_name):
"""Return required data from the DB."""
result = db.media_data.find_one({"page": page_name})
return result["content"]
def get_main_home_info():
info_dict = {
"trending_movie_week": media_finder.info_by_category(
category_before="trending", media_type="movie", category_after="week"
),
"trending_shows_week": media_finder.info_by_category(
category_before="trending", media_type="tv", category_after="week"
),
"spotlight": media_finder.info_by_category(
category_before="trending", media_type="all", category_after="day"
),
"documentary_movies": media_finder.discover_by_genre("movie", "Documentary"),
"crime_shows": media_finder.discover_by_genre("tv", "Crime"),
"war_politics_shows": media_finder.discover_by_genre("tv", "War & Politics"),
"family_movies": media_finder.discover_by_genre("movie", "Family"),
}
return info_dict
def get_tv_home_info():
info_dict = {
"shows_on_the_air": media_finder.info_by_category(
category_after="on_the_air", media_type="tv"
),
"shows_airing_today": media_finder.info_by_category(
category_after="airing_today", media_type="tv"
),
"popular_shows": media_finder.info_by_category(
category_after="popular", media_type="tv"
),
"top_rated_shows": media_finder.info_by_category(
category_after="top_rated", media_type="tv"
),
"documentary_shows": media_finder.discover_by_genre("tv", "Documentary"),
"animated_shows": media_finder.discover_by_genre("tv", "Animation"),
"action_adventure_shows": media_finder.discover_by_genre(
"tv", "Action & Adventure"
),
"kids_shows": media_finder.discover_by_genre("tv", "Kids"),
"mystery_shows": media_finder.discover_by_genre("tv", "Mystery"),
"reality_shows": media_finder.discover_by_genre("tv", "Reality"),
}
return info_dict
def get_movie_home_info():
info_dict = {
"now_playing_movies": media_finder.info_by_category(
category_after="now_playing", media_type="movie"
),
"upcoming_movies": media_finder.info_by_category(
category_after="upcoming", media_type="movie"
),
"top_rated_movies": media_finder.info_by_category(
category_after="top_rated", media_type="movie"
),
"popular_movies": media_finder.info_by_category(
category_after="popular", media_type="movie"
),
"documentary_movies": media_finder.discover_by_genre("movie", "Documentary"),
"comedy_movies": media_finder.discover_by_genre("movie", "Comedy"),
"mystery_movies": media_finder.discover_by_genre("movie", "Mystery"),
"western_movies": media_finder.discover_by_genre("movie", "Western"),
"drama_movies": media_finder.discover_by_genre("movie", "Drama"),
"crime_movies": media_finder.discover_by_genre("movie", "Crime"),
}
return info_dict