-
Notifications
You must be signed in to change notification settings - Fork 1
/
trakt_discord.py
executable file
·90 lines (81 loc) · 3.42 KB
/
trakt_discord.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
#!/usr/bin/env python3
# irbyjm 20180929
from sys import argv
from trakt.users import User
import time
from calendar import timegm
import libs.rpc as rpc
import os
import signal
def signal_handler(sig, frame):
runtime = round((time.time() - start)/60/60, 2)
print('\n', time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ': Ctrl+C pressed; exiting after', runtime, 'hours')
try:
rpc_obj.close()
except:
pass
exit(0)
signal.signal(signal.SIGINT, signal_handler)
start = time.time()
if len(argv) != 3:
print("Usage: ./trakt_discord.py [username] [Discord_client_ID]")
else:
username = argv[1]
trakt_connected = False
while not trakt_connected:
try:
my = User(username)
trakt_connected = True
print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": Successfully connected to Trakt")
except Exception:
print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": Trakt Connection Failure (1)")
time.sleep(15)
client_id = argv[2]
# force this script to live on an island and catch the errors
# I don't want to see a command prompt when I'm expecting this to keep going
# Spit an error for OSError: [Errno 107] Transport endpoint is not connecte
# Discord has to be running before this is--duh
rpc_obj = rpc.DiscordIpcClient.for_platform(client_id)
while True:
try:
watching = my.watching
if watching:
timestamp = int(timegm(time.strptime(watching.started_at, "%Y-%m-%dT%H:%M:%S.000Z")))
activity = {
"timestamps": {
"start": timestamp
},
"assets": {
"small_text": "Playing",
"small_image": "play",
"large_text": "Trakt",
"large_image": "trakt"
}
}
if watching.media_type == "episodes":
details = watching.show
state = "".join(("S", str(watching.season), "E", str(watching.episode), " (", watching.title , ")"))
activity["details"] = details
activity["state"] = state
print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": Trakt: playing", details, state)
else:
details = "".join((watching.title, " (", str(watching.year), ")"))
activity["details"] = details
print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": Trakt: playing", details)
try:
rpc_obj.set_activity(activity)
# print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": DEBUG : Sending data to Discord")
except:
rpc_obj = rpc.DiscordIpcClient.for_platform(client_id)
else:
try:
# Figure out if this is actually sending Trakt something
# If it's not actually sending packets to Trakt
# Then I don't particularly care
print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": Trakt: not playing")
rpc_obj.close()
except:
pass
except:
print(time.strftime("%Y-%m-%dT%H:%M:%S.000%Z"), ": Trakt Connection Failure (2)")
time.sleep(15)