Skip to content

Commit 8b07628

Browse files
committed
allow import from TMDB TV watchlist
the watchlist will be added as Season 1 for each media
1 parent f95ad6f commit 8b07628

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/app/utils/imports/tmdb.py

+33-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import logging
44
import asyncio
55

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
88

99
TMDB_API = config("TMDB_API", default="")
1010
logger = logging.getLogger(__name__)
@@ -40,13 +40,13 @@ def import_tmdb(user, request_token):
4040
- Rated movies
4141
- Watchlist movies
4242
- 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
4544
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.
4746
"""
4847
session_id = get_session_id(request_token)
4948
tv_images_to_download = []
49+
season_images_to_download = []
5050
movies_images_to_download = []
5151

5252
# MOVIES
@@ -68,11 +68,22 @@ def import_tmdb(user, request_token):
6868
tv_images, bulk_tv = process_media_list(tv_rated_url, "tv", "Completed", user)
6969
tv_images_to_download.extend(tv_images)
7070

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+
7179
asyncio.run(helpers.images_downloader(tv_images_to_download, "tv"))
7280
asyncio.run(helpers.images_downloader(movies_images_to_download, "movie"))
81+
asyncio.run(helpers.images_downloader(season_images_to_download, "season"))
7382

7483
TV.objects.bulk_create(bulk_tv)
7584
Movie.objects.bulk_create(bulk_movies)
85+
print(bulk_seasons)
86+
Season.objects.bulk_create(bulk_seasons)
7687

7788

7889
def process_media_list(url, media_type, status, user):
@@ -98,16 +109,22 @@ def process_media_list(url, media_type, status, user):
98109
next_page += 1
99110

100111
for media in data["results"]:
101-
if media_type == "tv":
112+
if media_type == "tv" or media_type == "season":
102113
media["title"] = media["name"]
103114

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(
108121
f"{media_type.capitalize()}: {media['title']} ({media['id']}) already exists, skipping..."
109122
)
110123
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+
111128
if media["poster_path"]:
112129
# poster_path e.g: "/aFmqXViWzIKm.jpg", remove the first slash
113130
image = media["poster_path"][1:]
@@ -127,10 +144,16 @@ def process_media_list(url, media_type, status, user):
127144
"user": user,
128145
"notes": "",
129146
}
147+
130148
if media_type == "movie":
131149
media_params["status"] = status
132150
media_params["end_date"] = None
133151

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+
134157
bulk_media.append(media_mapping["model"](**media_params))
135158

136159
logger.info(

src/app/utils/metadata.py

-15
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@ def get_media_metadata(media_type, media_id):
2020
return media_metadata
2121

2222

23-
def get_season_metadata_from_tv(season_number, seasons_metadata):
24-
"""
25-
Return the metadata for the selected season from the tv show metadata
26-
"""
27-
# when there are specials episodes, they are on season 0,
28-
# so offset everything by 1
29-
if seasons_metadata[0]["season_number"] == 0:
30-
offset = 0
31-
else:
32-
offset = 1
33-
34-
# get the selected season from the metadata
35-
return seasons_metadata[season_number - offset]
36-
37-
3823
def anime_manga(media_type, media_id):
3924
"""
4025
Return the metadata for the selected anime or manga

0 commit comments

Comments
 (0)