-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrobble.py
141 lines (124 loc) · 3.6 KB
/
scrobble.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import json
import sys
import time
import requests
from dotenv import load_dotenv
from get_ids import search_for_media_ratingkey
from utilities import get_from_env, log
load_dotenv()
args = sys.argv[1:]
NUMBER_OF_ARGS = len(args)
log(f"args:\n{args}\n")
log(f"NUMBER_OF_ARGS: {NUMBER_OF_ARGS}\n")
i = 0
while i < NUMBER_OF_ARGS:
current_arg = args[i]
if (
current_arg.startswith("-")
and i + 1 < NUMBER_OF_ARGS
and not args[i + 1].startswith("-")
): # if it isn't a show -s is empty followed by -M, so we go to the next arg -M otherwise we would go to the title string
pass
else:
i += 1
continue
# log(f"{i}: {current_arg}")
arg = args[i + 1]
match current_arg:
case "-m":
MEDIA_TYPE = arg
case "-s":
SHOW_NAME = arg
case "-M":
MOVIE_NAME = arg
case "-y":
MEDIA_YEAR = arg
case "-t":
TVDB_ID = int(arg)
case "-i":
IMDB_ID = arg
case "-r":
RATINGKEY = arg
case "-S":
SEASON = int(arg)
case "-E":
EPISODE = int(arg)
case "-P":
PROGRESS = float(arg)
case "-a":
ACTION = arg
case "-PlexUser":
PLEX_USER = arg
i += 2
if "MEDIA_YEAR" not in locals():
MEDIA_YEAR = 0
if MEDIA_YEAR == "":
MEDIA_YEAR = 0
body = ""
log(f"media_type: {MEDIA_TYPE}")
if MEDIA_TYPE == "movie":
body = {
"movie": {
"title": f"{MOVIE_NAME}",
"year": f"{MEDIA_YEAR}",
"ids": {"imdb": f"{IMDB_ID}"},
}
}
elif MEDIA_TYPE in ["show", "episode"]:
if get_from_env("PLEX_BASEURL") and search_for_media_ratingkey(
MEDIA_TYPE, RATINGKEY
): # this is slower, but it's more reliable if season/episode database is not the same one as Trakt.tv
ids = search_for_media_ratingkey(MEDIA_TYPE, RATINGKEY)
body = {"episode": {"ids": ids}}
else:
body = {
"show": {
"title": f"{SHOW_NAME}",
"year": MEDIA_YEAR,
"ids": {"tvdb": TVDB_ID},
},
"episode": {"season": SEASON, "number": EPISODE},
}
else:
log("Invalid media type")
sys.exit(1)
body["progress"] = PROGRESS
body["app_version"] = "1.0"
body["app_date"] = time.strftime("%Y-%m-%d")
URI = f"https://api.trakt.tv/scrobble/{ACTION}"
HEADERS = {
"Content-Type": "application/json",
"Authorization": f"Bearer {get_from_env('access_token', PLEX_USER)}",
"trakt-api-version": "2",
"trakt-api-key": get_from_env("client_id", PLEX_USER),
}
body = json.dumps(body)
log(body)
log(URI)
r = requests.post(URI, headers=HEADERS, data=body)
log(f"{r.status_code} | {r.text}")
if r.status_code == 404:
if MEDIA_TYPE == "movie":
ids = search_for_media_ratingkey(MEDIA_TYPE, RATINGKEY)
if ids:
body = {
"movie": {
"ids": ids,
}
}
body["progress"] = PROGRESS
body["app_version"] = "1.0"
body["app_date"] = time.strftime("%Y-%m-%d")
body = json.dumps(body)
r = requests.post(URI, headers=HEADERS, data=body)
else:
msg = "404, movie or episode not found on Trakt.tv. If this is incorrect, please report it on github."
log(msg)
print(msg)
sys.exit(1)
while r.status_code == 429:
time.sleep(5)
r = requests.post(URI, headers=HEADERS, data=body)
log(f"{r.status_code} | {r.text}")
print(r.status_code)
sys.exit(0)