-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoyago.py
executable file
·106 lines (90 loc) · 3.28 KB
/
toyago.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
# -*- coding: utf-8 -*-
import client
import xmlCommon
#import threading
#from datetime import datetime, timedelta
#import time
import datasource
import control
apiUrl = 'https://api-go.toya.net.pl/toyago/index.php'
class GetInstance:
def __init__(self, devId, user, passw, token, auth):
self.deviceId = devId
self.user = user
self.passw = passw
self.token = token
self.xmlReq = xmlCommon.Request()
self.xmlResp = xmlCommon.Response()
if auth:
self.authenticate(True)
def authenticate(self, valid):
#import control
isvalid = self.setVersion()
# print('Token valid: ' + str(isvalid) + ' prev: ' + str(valid))
if not isvalid:
authReq = self.xmlReq.auth(self.deviceId, self.user, self.passw)
authResp = client.request(apiUrl, authReq)
token = self.xmlResp.parseToken(authResp)
if 'Wrong' in token:
raise Exception('Wrong Credentials')
self.token = token
control.setSetting('toya_go_token', token)
return self.authenticate(False)
elif not isvalid and not valid:
raise Exception('Login failed')
else:
return valid
# Deprecated
def auth(self):
authReq = self.xmlReq.auth(self.deviceId, self.user, self.passw)
authResp = client.request(apiUrl, authReq)
token = self.xmlResp.parseToken(authResp)
self.token = token
self.setVersion()
def setVersion(self):
verReq = self.xmlReq.version(self.deviceId, '2.0', self.token)
verResp = client.request(apiUrl, verReq)
isvalid = self.xmlResp.parseVersion(verResp)
return isvalid
def channels(self, cids):
channelsReq = self.xmlReq.getChannels(self.token, self.deviceId)
channelsResp = client.request(apiUrl, channelsReq)
channels = self.xmlResp.parseChannels(channelsResp, cids)
return channels
def epg(self, cids, epg):
epgStr = self.xmlReq.getEPG(self.token, self.deviceId, cids)
if control.debugAddon:
control.logError(str(epgStr))
epgResp = client.request(apiUrl, epgStr).decode('utf-8')
if control.debugAddon:
control.logError(str(epgResp))
self.xmlResp.parseEPG(epgResp, epg)
# Deprecated
def channel(self, cid, number, parent):
cid2 = str(cid).replace('1111', '')
channelReq = self.xmlReq.getChannel(self.token, self.deviceId, cid2, number, parent)
channelResp = client.request(apiUrl, channelReq)
return channelResp
def updatechannels(self):
table = 'channel'
db = datasource.Create('toyago')
db.drop(table)
colums = ['url text']
db.create(table, colums)
self.authenticate(True)
cids = []
channels = self.channels(cids)
values = []
for ch in channels:
values.append((ch.name, ch.source))
print(str(values.__len__()))
db.saveall(table, values, 2)
db.commit()
db.close()
def loadChannels(self):
table = 'channel'
db = datasource.Create('toyago')
channels = db.findall(table)
for channel in channels:
print(channel[0] + ' ' + channel[1])
db.close()