-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvpassport.py
More file actions
57 lines (46 loc) · 1.91 KB
/
tvpassport.py
File metadata and controls
57 lines (46 loc) · 1.91 KB
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
import logging
import urllib.request
import urllib.parse
import re
from . import HTMLView
import homeassistant.core as ha
_LOGGER = logging.getLogger(__name__)
class tvpassport(HTMLView):
url = '/my_api/tvpassport/{path:.*}'
name = 'my_api:tvpassport'
requires_auth = False
@ha.callback
def get(self, request, path):
data = {'timezone': 'Asia/Yekaterinburg'}
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request('https://www.tvpassport.com/my-passport/dashboard/save_timezone', data=data)
resp = urllib.request.urlopen(req)
cisession = None
for c in resp.info().get_all('Set-Cookie'):
m = re.match(r'cisession=(.*?);', c)
if m: cisession = m[1]
status = 200
try:
req = urllib.request.Request('https://www.tvpassport.com/tv-listings/stations/' + path)
if cisession:
req.add_header('Cookie', 'cisession=' + cisession)
resp = urllib.request.urlopen(req)
d = resp.read().decode('utf-8')
m = re.search(r'<title>TV Schedule for (.*?) | TV Passport</title>', d)
station = m[1]
m = re.search(r'<option value="([^"]*)" selected>', d)
timezone = m[1]
msg = '<html><head><title>{}</title></head><body>'.format(station)
msg = msg + '<table>'
for i in re.findall(r'<div id="itemheader.*?>', d):
sched = {}
for type, value in re.findall(r'([^ "=]+)="(.*?)"', i):
if value:
sched[type] = value
msg = msg + '<tr><td>{}<td>{}'.format(sched['data-st'], sched['data-showName'])
msg = msg + '</table>'
msg = msg + '</body></html>'
except:
msg = '<html><body>Error</body></html>'
status = 404
return self.html(msg, status)