-
Notifications
You must be signed in to change notification settings - Fork 4
/
kodi.py
119 lines (101 loc) · 5.31 KB
/
kodi.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
import requests
from requests.exceptions import ConnectionError
from settings import Settings
from urlhelper import UrlHelper
class Kodi():
player_id = None
username = None
password = None
url_helper = None
parent_params = None
def __init__(self, username, password, ip_address, port):
self.url_helper = UrlHelper(ip_address, port)
self.ip_address = ip_address
self.port = port
self.username = username
self.password = password
self.parent_params = {}
def Connect(self):
try:
# TODO Save settings based on response
response = requests.get(self.url_helper.prepare_url_without_param('Player.GetActivePlayers'), auth=(self.username, self.password))
settings = Settings()
settings.Save({'ip_address' : self.ip_address, 'port' : self.port, 'username' : self.username, 'password' : self.password})
return True
except ConnectionError as conn_error:
print(conn_error)
return False
def Handshake(self):
try:
self.player_id = self.GetActivePlayers()
# print(self.player_id)
if self.player_id is None:
currentPlaying = 'Nothing is playing'
else:
currentPlaying = self.PlayerGetItem()
return currentPlaying
except ConnectionError as conn_error:
# print(conn_error)
return False
def GetActivePlayers(self):
response = requests.get(self.url_helper.prepare_url_without_param('Player.GetActivePlayers'), auth=(self.username, self.password))
response = response.json()['result']
# check if something is playing
if len(response) == 0:
return None
else:
return response[0]['playerid']
def PlayerGetItem(self):
params = self.url_helper.prepare_param(self.parent_params, {'name': 'playerid', 'value': self.player_id})
response = requests.get(self.url_helper.prepare_url_with_param('Player.GetItem', params), auth=(self.username, self.password))
self.parent_params = {}
response = response.json()
return response['result']['item']['label']
def InputBack(self):
response = requests.get(self.url_helper.prepare_url_without_param('Input.Back'), auth=(self.username, self.password))
self.ParseResponse(response)
def InputLeft(self):
response = requests.get(self.url_helper.prepare_url_without_param('Input.Left'), auth=(self.username, self.password))
self.ParseResponse(response)
def InputRight(self):
response = requests.get(self.url_helper.prepare_url_without_param('Input.Right'), auth=(self.username, self.password))
self.ParseResponse(response)
def InputSelect(self):
response = requests.get(self.url_helper.prepare_url_without_param('Input.Select'), auth=(self.username, self.password))
self.ParseResponse(response)
def InputUp(self):
response = requests.get(self.url_helper.prepare_url_without_param('Input.Up'), auth=(self.username, self.password))
self.ParseResponse(response)
def InputDown(self):
response = requests.get(self.url_helper.prepare_url_without_param('Input.Down'), auth=(self.username, self.password))
self.ParseResponse(response)
def PlayPause(self):
params = self.url_helper.prepare_param(self.parent_params, {'name': 'playerid', 'value': self.player_id})
response = requests.get(self.url_helper.prepare_url_with_param('Player.PlayPause', params), auth=(self.username, self.password))
self.parent_params = {}
self.ParseResponse(response)
def Stop(self):
params = self.url_helper.prepare_param(self.parent_params, {'name': 'playerid', 'value': self.player_id})
response = requests.get(self.url_helper.prepare_url_with_param('Player.Stop', params), auth=(self.username, self.password))
self.parent_params = {}
self.ParseResponse(response)
def Previous(self):
parent_params = self.url_helper.prepare_param(self.parent_params, {'name': 'playerid', 'value': self.player_id})
parent_params = self.url_helper.prepare_param(self.parent_params, {'name': 'to', 'value': 'previous'})
response = requests.get(self.url_helper.prepare_url_with_param('Player.GoTo', parent_params), auth=(self.username, self.password))
self.parent_params = {}
self.ParseResponse(response)
def Next(self):
parent_params = self.url_helper.prepare_param(self.parent_params, {'name': 'playerid', 'value': self.player_id})
parent_params = self.url_helper.prepare_param(self.parent_params, {'name': 'to', 'value': 'next'})
response = requests.get(self.url_helper.prepare_url_with_param('Player.GoTo', parent_params), auth=(self.username, self.password))
self.parent_params = {}
self.ParseResponse(response)
def SetVolume(self, vol_type):
params = self.url_helper.prepare_param(self.parent_params, {'name': 'volume', 'value': vol_type})
response = requests.get(self.url_helper.prepare_url_with_param('Application.SetVolume', params), auth=(self.username, self.password))
self.parent_params = {}
self.ParseResponse(response)
def ParseResponse(self, response):
response = response.json()
# print(response)