-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathysapi.py
119 lines (92 loc) · 3.6 KB
/
ysapi.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
#
# Copyright (C) 2012 Tommy Winther
# http://tommy.winther.nu
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this Program; see the file LICENSE.txt. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
#
# https://docs.google.com/document/d/1_rs5BXklnLqGS6g6eAjevVHsPafv4PXDCi_dAM2b7G0/edit?pli=1
#
import cookielib
import urllib2
try:
import json
except:
import simplejson as json
API_URL = 'http://api.yousee.tv/rest'
API_KEY = 'HCN2BMuByjWnrBF4rUncEfFBMXDumku7nfT3CMnn'
AREA_TVGUIDE = 'tvguide'
class YouSeeApi(object):
COOKIE_JAR = cookielib.LWPCookieJar()
def __init__(self):
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor(self.COOKIE_JAR)))
def _invoke(self, area, function, params=dict()):
url = API_URL + '/' + area + '/' + function
for key, value in params.items():
url += '/' + key + '/' + str(value)
url += '/format/json'
print 'Invoking URL: ' + url
r = urllib2.Request(url, headers = {'X-API-KEY' : API_KEY})
u = urllib2.urlopen(r)
data = u.read()
u.close()
return json.loads(data)
class YouSeeTVGuideApi(YouSeeApi):
def channelsInCategory(self, category):
channels = self.channels()
for channel in channels:
if channel['name'] == category:
return channel['channels']
return None
def channels(self):
"""
Returns complete channel list ordered in channel packages.
Note: the channel package "Mine Kanaler" contains the default channels a user should have in her favorites, until overwritten by the user herself.
"""
return self._invoke(AREA_TVGUIDE, 'channels')
def categories(self):
"""
Returns complete list of categories
"""
return self._invoke(AREA_TVGUIDE, 'categories')
def programs(self, channelId= None, offset = None, tvdate = None):
"""
Returns program list
@param:channel_id (optional)
@param: offset (optional) default -1 (yesterday)
@param: tvdate (optional) format: yyyy-mm-dd (overrides offset)
@type: tvdate datetime.datetime
"""
params = dict()
if channelId is not None:
params['channel_id'] = channelId
if tvdate is not None:
params['tvdate'] = tvdate.strftime('%Y-%m-%d')
elif offset is not None:
params['offset'] = offset
return self._invoke(AREA_TVGUIDE, 'programs', params)
if __name__ == '__main__':
api = YouSeeTVGuideApi()
data = api.channels()
entries = dict()
for channels in data:
for channel in channels['channels']:
if not entries.has_key(channel['name']):
entries[channel['name']] = 'plugin://plugin.video.yousee.tv/?channel=%s' % channel['id']
for e in sorted(entries.keys()):
print e + '=' + entries[e]
#s = simplejson.dumps(json, sort_keys=True, indent=' ')
#print '\n'.join([l.rstrip() for l in s.splitlines()])