-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (76 loc) · 3.82 KB
/
main.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
import requests
import time
from pypresence import Presence
from datetime import datetime, timedelta
client_id = '535883147112611880'
RPC = Presence(client_id) # Initialize the client class
RPC.connect() # Start the handshake loop
port = 8080 # By default the port is 8080 if you have changed it specify here
url = f"http://localhost:{port}/jsonrpc"
infourl = "?request={%20%22jsonrpc%22:%20%222.0%22,%20%22method%22:%20%22Player.GetItem%22,%20%22params%22:%20{%20%22properties%22:%20[%20%22title%22,%20%22season%22,%20%22episode%22,%20%22duration%22,%20%22showtitle%22,%20%22tvshowid%22],%20%22playerid%22:%201%20},%20%22id%22:%20%22VideoGetItem%22%20}"
lengthurl = "?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetProperties%22,%22params%22:{%22playerid%22:1,%22properties%22:[%22speed%22,%22time%22,%22totaltime%22]},%22id%22:%221%22}"
result = None
def loading(string):
for x in range(0, 6):
b = string + "." * x
print(b, end="\r")
time.sleep(1)
def set_rp(info, length):
info = info['result']['item']
length = length['result']
start_time = (datetime.now() - timedelta(hours=length["time"]['hours'], minutes=length["time"]['minutes'], seconds=length["time"]['seconds']))
end_time = (start_time + timedelta(hours=length['totaltime']['hours'], minutes=length['totaltime']['minutes'], seconds=length['totaltime']['seconds'])).timestamp()
start_time = start_time.timestamp()
if info['type'] == 'movie':
if length['speed'] == 0:
RPC.update(details=str(info['title']),
state="Paused...",
large_image='kodi_dark',
large_text='Kodi Rich Prescence by Alphex#0977',
small_image='pause',
small_text='Paused')
else:
RPC.update(details=str(info['title']),
start=start_time,
end=end_time,
large_image='kodi_dark',
large_text='Kodi Rich Prescence by Alphex#0977',
small_image='play',
small_text='Playing')
if info['type'] == "episode":
state_info = 'Season ' + str(info['season'])+' Episode ' + str(info['episode'])+' : ' + str(info['title'])
if length['speed'] == 0:
RPC.update(state=state_info,
details=str(info['showtitle']),
start=start_time,
end=end_time,
large_image='kodi_dark',
large_text='Kodi Rich Prescence by Alphex#0977',
small_image='pause',
small_text='Paused')
else:
RPC.update(state=state_info,
details=str(info['showtitle']),
start=start_time,
end=end_time,
large_image='kodi_dark',
large_text='Kodi Rich Prescence by Alphex#0977',
small_image='play',
small_text='Playing')
if info['type'] == 'unknown':
RPC.update(state="Not watching anything",
details="IDLE",
large_image='kodi_dark',
large_text='Kodi Rich Prescence by Alphex#0977',
small_image='pause',
small_text='Nothing Playing')
time.sleep(15)
while True:
try:
info = requests.get(url+infourl).json()
length = requests.get(url+lengthurl).json()
set_rp(info, length)
except requests.exceptions.RequestException:
print("Cant connect to Kodi web interface. Are you sure its running? Is the web interface on?")
loading("Trying again in 5 seconds")
pass