3
3
import logging
4
4
import asyncio
5
5
6
- from app .models import TV , Movie
7
- from app .utils import helpers
6
+ from app .models import TV , Season , Movie
7
+ from app .utils import helpers , metadata
8
8
9
9
TMDB_API = config ("TMDB_API" , default = "" )
10
10
logger = logging .getLogger (__name__ )
@@ -40,13 +40,13 @@ def import_tmdb(user, request_token):
40
40
- Rated movies
41
41
- Watchlist movies
42
42
- Rated TV shows
43
- It currently doesn't import watchlist TV shows because current tv models
44
- doesn't have a status field, shows status are tracked per season.
43
+ - Watchlist TV shows
45
44
46
- It doesn 't track dates because TMDB API doesn't provide that information .
45
+ It can 't track dates because not provided by TMDB API .
47
46
"""
48
47
session_id = get_session_id (request_token )
49
48
tv_images_to_download = []
49
+ season_images_to_download = []
50
50
movies_images_to_download = []
51
51
52
52
# MOVIES
@@ -68,11 +68,22 @@ def import_tmdb(user, request_token):
68
68
tv_images , bulk_tv = process_media_list (tv_rated_url , "tv" , "Completed" , user )
69
69
tv_images_to_download .extend (tv_images )
70
70
71
+ # TVs
72
+ tv_watchlist_url = f"https://api.themoviedb.org/3/account/{ user } /watchlist/tv?api_key={ TMDB_API } &session_id={ session_id } "
73
+ # add first season of each media as watchlist
74
+ season_images , bulk_seasons = process_media_list (
75
+ tv_watchlist_url , "season" , "Planning" , user
76
+ )
77
+ season_images_to_download .extend (season_images )
78
+
71
79
asyncio .run (helpers .images_downloader (tv_images_to_download , "tv" ))
72
80
asyncio .run (helpers .images_downloader (movies_images_to_download , "movie" ))
81
+ asyncio .run (helpers .images_downloader (season_images_to_download , "season" ))
73
82
74
83
TV .objects .bulk_create (bulk_tv )
75
84
Movie .objects .bulk_create (bulk_movies )
85
+ print (bulk_seasons )
86
+ Season .objects .bulk_create (bulk_seasons )
76
87
77
88
78
89
def process_media_list (url , media_type , status , user ):
@@ -98,16 +109,22 @@ def process_media_list(url, media_type, status, user):
98
109
next_page += 1
99
110
100
111
for media in data ["results" ]:
101
- if media_type == "tv" :
112
+ if media_type == "tv" or media_type == "season" :
102
113
media ["title" ] = media ["name" ]
103
114
104
- if media_mapping ["model" ].objects .filter (
105
- media_id = media ["id" ], user = user
106
- ).exists ():
107
- logger .info (
115
+ if (
116
+ media_mapping ["model" ]
117
+ .objects .filter (media_id = media ["id" ], user = user )
118
+ .exists ()
119
+ ):
120
+ logger .warning (
108
121
f"{ media_type .capitalize ()} : { media ['title' ]} ({ media ['id' ]} ) already exists, skipping..."
109
122
)
110
123
else :
124
+ # if tv watchlist, get image of first season
125
+ if media_type == "season" :
126
+ media ["poster_path" ] = metadata .season (media ["id" ], season_number = 1 ).get ("poster_path" )
127
+
111
128
if media ["poster_path" ]:
112
129
# poster_path e.g: "/aFmqXViWzIKm.jpg", remove the first slash
113
130
image = media ["poster_path" ][1 :]
@@ -127,10 +144,16 @@ def process_media_list(url, media_type, status, user):
127
144
"user" : user ,
128
145
"notes" : "" ,
129
146
}
147
+
130
148
if media_type == "movie" :
131
149
media_params ["status" ] = status
132
150
media_params ["end_date" ] = None
133
151
152
+ # if tv watchlist, add first season as planning
153
+ if media_type == "season" :
154
+ media_params ["status" ] = "Planning"
155
+ media_params ["season_number" ] = 1
156
+
134
157
bulk_media .append (media_mapping ["model" ](** media_params ))
135
158
136
159
logger .info (
0 commit comments