forked from skandi/xbmc-addon-teleboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teleboy.py
216 lines (179 loc) · 6.81 KB
/
teleboy.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os, re, sys, base64
import cookielib, urllib, urllib2
import xbmcgui, xbmcplugin, xbmcaddon
from mindmade import *
import simplejson
from BeautifulSoup import BeautifulSoup
__author__ = "Andreas Wetzel"
__copyright__ = "Copyright 2011-2015, mindmade.org"
__credits__ = [ "Roman Haefeli", "Francois Marbot" ]
__maintainer__ = "Andreas Wetzel"
__email__ = "[email protected]"
#
# constants definition
############################################
PLUGINID = "plugin.video.teleboy"
MODE_PLAY = "play"
PARAMETER_KEY_MODE = "mode"
PARAMETER_KEY_STATION = "station"
PARAMETER_KEY_USERID = "userid"
TB_URL = "http://www.teleboy.ch"
IMG_URL = "http://media.cinergy.ch"
API_URL = "http://tv.api.teleboy.ch"
API_KEY = base64.b64decode( "ZjBlN2JkZmI4MjJmYTg4YzBjN2ExM2Y3NTJhN2U4ZDVjMzc1N2ExM2Y3NTdhMTNmOWMwYzdhMTNmN2RmYjgyMg==")
COOKIE_FILE = xbmc.translatePath( "special://home/addons/" + PLUGINID + "/resources/cookie.dat")
pluginhandle = int(sys.argv[1])
settings = xbmcaddon.Addon( id=PLUGINID)
cookies = cookielib.LWPCookieJar( COOKIE_FILE)
def ensure_login():
global cookies
opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookies))
urllib2.install_opener( opener)
try:
cookies.revert( ignore_discard=True)
for c in cookies:
if c.name == "cinergy_s":
return True
except IOError:
pass
cookies.clear()
fetchHttp( TB_URL + "/watchlist")
log( "logging in...")
login = settings.getSetting( id="login")
password = settings.getSetting( id="password")
url = TB_URL + "/login_check"
args = { "login": login,
"password": password,
"keep_login": "1",
"x": "14", "y": "7" }
reply = fetchHttp( url, args, post=True);
if "Falsche Eingaben" in reply or "Anmeldung war nicht erfolgreich" in reply:
log( "login failure")
log( reply)
notify( "Login Failure!", "Please set your login/password in the addon settings")
xbmcplugin.endOfDirectory( handle=pluginhandle, succeeded=False)
return False
res = cookies.save( ignore_discard=True)
log( "login ok")
return True
def fetchHttpWithCookies( url, args={}, hdrs={}, post=False):
if ensure_login():
html = fetchHttp( url, args, hdrs, post)
if "Bitte melde dich neu an" in html:
os.unlink( xbmc.translatePath( COOKIE_FILE));
if not ensure_login():
return "";
html = fetchHttp( url, args, hdrs, post)
return html
return ""
def get_stationLogoURL( station):
return IMG_URL + "/t_station/%d/logo_s_big1.gif" % int(station)
def get_videoJson( sid, user_id):
# get session key from cookie
global cookies
cookies.revert( ignore_discard=True)
session_cookie = ""
for c in cookies:
if c.name == "cinergy_s":
session_cookie = c.value
break
if (session_cookie == ""):
notify( "Session cookie not found!", "Please set your login/password in the addon settings")
return False
url = API_URL + "/users/%s/stream/live/%s" % (user_id, sid)
hdrs = { "x-teleboy-apikey": API_KEY,
"x-teleboy-session": session_cookie }
ans = fetchHttpWithCookies( url, { "alternative": "false" }, hdrs)
return simplejson.loads( ans)
############
# TEMP
############
def parameters_string_to_dict( parameters):
''' Convert parameters encoded in a URL to a dict. '''
paramDict = {}
if parameters:
paramPairs = parameters[1:].split("&")
for paramsPair in paramPairs:
paramSplits = paramsPair.split('=')
if (len(paramSplits)) == 2:
paramDict[paramSplits[0]] = urllib.unquote( paramSplits[1])
return paramDict
def addDirectoryItem( name, params={}, image="", total=0):
'''Add a list item to the XBMC UI.'''
img = "DefaultVideo.png"
if image != "": img = image
name = htmldecode( name)
li = xbmcgui.ListItem( name, iconImage=img, thumbnailImage=image)
li.setProperty( "Video", "true")
params_encoded = dict()
for k in params.keys():
params_encoded[k] = params[k].encode( "utf-8")
url = sys.argv[0] + '?' + urllib.urlencode( params_encoded)
return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li, isFolder = False, totalItems=total)
###########
# END TEMP
###########
def show_main():
content = fetchHttpWithCookies( TB_URL + "/tv/live_tv.php")
soup = BeautifulSoup( content)
# extract user id
user_id = ""
lines = content.split( '\n')
for line in lines:
if "id: " in line:
dummy, uid = line.split( ": ")
user_id = uid[:-1]
log( "user id: " + user_id)
break;
table = soup.find( "table", "show-listing")
if not table: return
for tr in table.findAll( "tr"):
a = tr.find( "a", "playIcon")
if a:
try:
id = int( a["data-stationid"])
channel = htmldecode( tr.find( "td", "station").find( "img")["alt"])
details = tr.find( "td", "show-details")
if (details.find( "a")):
show = htmldecode( details.find( "a")["title"])
else:
show = details.text
img = get_stationLogoURL( id)
label = channel + ": " + show
p = tr.find( "p", "listing-info");
if p:
desc = p.text
log( desc)
if desc.endswith( " | "): desc = desc[:-13]
label = label + " (" + desc + ")"
addDirectoryItem( label, { PARAMETER_KEY_STATION: str(id),
PARAMETER_KEY_MODE: MODE_PLAY,
PARAMETER_KEY_USERID: user_id }, img)
except Exception as e:
log( "Exception: " + str(e))
log( "HTML(show): " + str( tr))
xbmcplugin.endOfDirectory( handle=pluginhandle, succeeded=True)
#
# xbmc entry point
############################################
sayHi()
params = parameters_string_to_dict(sys.argv[2])
mode = params.get(PARAMETER_KEY_MODE, "0")
# depending on the mode, call the appropriate function to build the UI.
if not sys.argv[2]:
# new start
ok = show_main()
elif mode == MODE_PLAY:
station = params[PARAMETER_KEY_STATION]
user_id = params[PARAMETER_KEY_USERID]
json = get_videoJson( station, user_id)
if not json:
exit( 1)
title = json["data"]["epg"]["current"]["title"]
url = json["data"]["stream"]["url"]
if not url: exit( 1)
img = get_stationLogoURL( station)
li = xbmcgui.ListItem( title, iconImage=img, thumbnailImage=img)
li.setProperty( "IsPlayable", "true")
li.setProperty( "Video", "true")
xbmc.Player().play( url, li)