-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathtasks.py
More file actions
146 lines (109 loc) · 4.51 KB
/
tasks.py
File metadata and controls
146 lines (109 loc) · 4.51 KB
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
139
140
141
142
143
144
145
146
import logging
from celery import shared_task
from django.contrib.auth import get_user_model
import events
from app.mixins import disable_fetch_releases
from app.models import MediaTypes
from app.templatetags import app_tags
from integrations.imports import (
anilist,
goodreads,
helpers,
hltb,
imdb,
kitsu,
mal,
simkl,
steam,
trakt,
watcharr,
yamtrack,
)
logger = logging.getLogger(__name__)
ERROR_TITLE = "\n\n\n Couldn't import the following media: \n\n"
def format_media_type_display(count, media_type):
"""Format media type display with proper pluralization."""
if count == 0:
return None
if count == 1:
return f"{count} {dict(MediaTypes.choices).get(media_type, media_type)}"
return f"{count} {app_tags.media_type_readable_plural(media_type)}"
def format_import_message(imported_counts, warning_messages=None):
"""Format the import result message based on counts and warnings."""
parts = [
format_media_type_display(count, media_type)
for media_type, count in imported_counts.items()
]
parts = [p for p in parts if p is not None]
if not parts:
info_message = "No media was imported."
else:
info_message = f"Imported {helpers.join_with_commas_and(parts)}."
if warning_messages:
return f"{info_message} {ERROR_TITLE} {warning_messages}"
return info_message
def import_media(importer_func, identifier, user_id, mode, oauth_username=None):
"""Handle the import process for different media services."""
user = get_user_model().objects.get(id=user_id)
with disable_fetch_releases():
if oauth_username is None:
imported_counts, warnings = importer_func(
identifier,
user,
mode,
)
else:
imported_counts, warnings = importer_func(
identifier,
user,
mode,
username=oauth_username,
)
events.tasks.reload_calendar.delay()
return format_import_message(imported_counts, warnings)
@shared_task(name="Import from Trakt")
def import_trakt(user_id, mode, token=None, username=None):
"""Celery task for importing media data from Trakt.
Can import using either OAuth (token provided) or public username.
"""
return import_media(trakt.importer, token, user_id, mode, username)
@shared_task(name="Import from SIMKL")
def import_simkl(token, user_id, mode, username=None): # noqa: ARG001
"""Celery task for importing media data from SIMKL."""
return import_media(simkl.importer, token, user_id, mode)
@shared_task(name="Import from MyAnimeList")
def import_mal(username, user_id, mode):
"""Celery task for importing anime and manga data from MyAnimeList."""
return import_media(mal.importer, username, user_id, mode)
@shared_task(name="Import from AniList")
def import_anilist(user_id, mode, token=None, username=None):
"""Celery task for importing media data from AniList."""
return import_media(anilist.importer, token, user_id, mode, username)
@shared_task(name="Import from Kitsu")
def import_kitsu(username, user_id, mode):
"""Celery task for importing anime and manga data from Kitsu."""
return import_media(kitsu.importer, username, user_id, mode)
@shared_task(name="Import from Yamtrack")
def import_yamtrack(file, user_id, mode):
"""Celery task for importing media data from Yamtrack."""
return import_media(yamtrack.importer, file, user_id, mode)
@shared_task(name="Import from HowLongToBeat")
def import_hltb(file, user_id, mode):
"""Celery task for importing media data from HowLongToBeat."""
return import_media(hltb.importer, file, user_id, mode)
@shared_task(name="Import from Steam")
def import_steam(username, user_id, mode):
"""Celery task for importing game data from Steam."""
return import_media(steam.importer, username, user_id, mode)
@shared_task(name="Import from IMDB")
def import_imdb(file, user_id, mode):
"""Celery task for importing media data from IMDB."""
return import_media(imdb.importer, file, user_id, mode)
@shared_task(name="Import from GoodReads")
def import_goodreads(file, user_id, mode):
"""Celery task for importing media data from GoodReads."""
return import_media(goodreads.importer, file, user_id, mode)
@shared_task(name="Import from Watcharr")
def import_watcharr(file, user_id, mode):
"""Celery task for importing the Watcharr JSON export."""
return import_media(watcharr.importer, file, user_id, mode)