-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.py
executable file
·269 lines (242 loc) · 11 KB
/
default.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xbmc
import xbmcplugin
import xbmcaddon
import xbmcgui
import urllib
import urllib2
import sys
import os
import re
import csv
import json
from xml.dom import Node;
from xml.dom import minidom;
try:
import StorageServer
except:
import storageserverdummy as StorageServer
cache = StorageServer.StorageServer("plugin.program.wienerlinien", 999999)
version = "0.1.2"
plugin = "WienerLinien-" + version
author = "sofaking"
pluginhandle = int(sys.argv[1])
settings = xbmcaddon.Addon(id='plugin.program.wienerlinien')
#CSV Urls
stations_url = "http://data.wien.gv.at/csv/wienerlinien-ogd-haltestellen.csv"
lines_url = "http://data.wien.gv.at/csv/wienerlinien-ogd-linien.csv"
positions_url = "http://data.wien.gv.at/csv/wienerlinien-ogd-steige.csv"
basepath = settings.getAddonInfo('path')
resourcespath = xbmc.translatePath( settings.getAddonInfo('profile')).decode("utf-8")
if not os.path.exists(os.path.dirname(resourcespath)):
os.makedirs(os.path.dirname(resourcespath))
translation = settings.getLocalizedString
defaultbackdrop = os.path.join(basepath,"fanart.jpg")
stationpath = os.path.join(resourcespath, "stations.csv")
linepath = os.path.join(resourcespath, "lines.csv")
positionpath = os.path.join(resourcespath, "positions.csv")
#GET CSVs
urllib.urlretrieve(stations_url, stationpath)
urllib.urlretrieve(lines_url, linepath)
urllib.urlretrieve(positions_url, positionpath)
def getMainMenu():
addDirectory((translation(30000)).encode("utf-8"),"","searchStation")
addDirectory((translation(30001)).encode("utf-8"),"","getFavorites")
addDirectory((translation(30002)).encode("utf-8"),"","getMonitor")
addDirectory((translation(30003)).encode("utf-8"),"","getFailures")
xbmcplugin.setContent(pluginhandle,'episodes')
xbmcplugin.endOfDirectory(pluginhandle)
def addDirectory(title,desc,mode,id=''):
parameters = {"title" : title,"mode" : mode,"id":id}
u = sys.argv[0] + '?' + urllib.urlencode(parameters)
liz=xbmcgui.ListItem(label=title,label2=desc,iconImage=os.path.join(basepath,"icon.png"))
liz.setProperty('IsPlayable', 'false')
ok=xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz,isFolder=True)
return ok
def parameters_string_to_dict(parameters):
paramDict = {}
if parameters:
paramPairs = parameters[1:].split("&")
for paramsPair in paramPairs:
paramSplits = paramsPair.split('=')
if (len(paramSplits)) == 2:
paramDict[paramSplits[0]] = paramSplits[1]
return paramDict
def getMonitor(search=''):
dictReader = csv.DictReader(open(stationpath, 'rb'), fieldnames = ['HALTESTELLEN_ID', 'TYP','DIVA', 'NAME', 'GEMEINDE', 'GEMEINDE_I'], delimiter = ';', quotechar = '"')
for row in dictReader:
if(row['NAME'] != "NAME"):
if search.lower() in row['NAME'].lower():
parameters = {"title" : row['NAME'],"id" : row['HALTESTELLEN_ID'],"mode" : "getStationInfos"}
u = sys.argv[0] + '?' + urllib.urlencode(parameters)
liz=xbmcgui.ListItem(row['NAME'],iconImage=os.path.join(basepath,"icon.png"))
liz.setProperty('IsPlayable', 'false')
xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz,isFolder=True)
xbmcplugin.setContent(pluginhandle,'episodes')
xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_LABEL )
xbmcplugin.endOfDirectory(pluginhandle)
def getStationPositions(haltestellen_id):
dictReader = csv.DictReader(open(positionpath, 'rb'), fieldnames = ['STEIG_ID', 'FK_LINIEN_ID','FK_HALTESTELLEN_ID', 'RICHTUNG', 'REIHENFOLGE', 'RBL_NUMMER','BEREICH','STEIG', 'STEIG_WGS84_LAT', 'STEIG_WGS84_LON', 'STAND'], delimiter = ';', quotechar = '"')
haltestellen_list =[]
rbl_str = ""
for row in dictReader:
if row['FK_HALTESTELLEN_ID'] == haltestellen_id:
if findStationDuplicate(haltestellen_list,row['RBL_NUMMER']):
haltestellen_list.append(row['RBL_NUMMER'])
for rbl in haltestellen_list:
if rbl != '':
if ":" in rbl:
rbl_array = rbl.split(":")
for rbl_item in rbl_array:
rbl_str += "rbl=%s&" % rbl_item
else:
rbl_str += "rbl=%s&" % rbl
if rbl_str != '':
json_url = "http://www.wienerlinien.at/ogd_realtime/monitor?%ssender=Bb6juvg7Qe" % rbl_str
getJsonMessage(json_url)
else:
notFound()
def notFound():
parameters = {"title" : translation(30004),"mode" : "notFound"}
u = sys.argv[0] + '?' + urllib.urlencode(parameters)
liz=xbmcgui.ListItem((translation(30004)).encode("utf-8"),iconImage=os.path.join(basepath,"icon.png"))
liz.setProperty('IsPlayable', 'false')
xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz,isFolder=True)
xbmcplugin.setContent(pluginhandle,'episodes')
xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_LABEL )
xbmcplugin.endOfDirectory(pluginhandle)
def searchStation():
addDirectory((translation(30005)).encode("utf-8"),"","searchStationResult")
cache.table_name = "searchhistory"
some_dict = cache.get("searches").split("|")
for str in reversed(some_dict):
if str != '':
addDirectory(str,"","searchStationHistory",str)
xbmcplugin.setContent(pluginhandle,'episodes')
xbmcplugin.endOfDirectory(pluginhandle)
def searchStationResult():
keyboard = xbmc.Keyboard('')
keyboard.doModal()
if (keyboard.isConfirmed()):
cache.table_name = "searchhistory"
keyboard_in = keyboard.getText()
some_dict = cache.get("searches") + "|"+keyboard_in
cache.set("searches",some_dict);
getMonitor(keyboard_in)
xbmcplugin.setContent(pluginhandle,'episodes')
xbmcplugin.endOfDirectory(pluginhandle)
def findStationDuplicate(list,id):
for item in list:
if item == id:
return False
return True
def getJsonMessage(url):
url = urllib.unquote_plus(url)
print "URL:%s" % url
parameters = {"title" : (translation(30006)).encode("utf-8"),"mode" : "refreshStations" , "id" : url}
u = sys.argv[0] + '?' + urllib.urlencode(parameters)
liz=xbmcgui.ListItem(label=(translation(30006)).encode("utf-8"), label2="",iconImage=os.path.join(basepath,"icon.png"))
liz.setProperty('IsPlayable', 'false')
xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz,isFolder=True)
json_response = urllib2.urlopen(url).read()
json_dict = json.loads(json_response)
for column in json_dict['data']['monitors']:
for row in column['lines']:
departure_str = ""
jam_str = ""
for departures in row['departures']['departure']:
try:
departure_str += "%s min | " % departures['departureTime']['countdown']
name = row['name']
towards = row['towards']
if row['trafficjam']:
jam_str = (translation(30007)).encode("utf-8")
title = "%s | %s | %s | %s" % (name.encode('utf-8'),towards.encode('utf-8'),departure_str,jam_str)
parameters = {"title" : title,"mode" : "refreshStations","id": url}
u = sys.argv[0] + '?' + urllib.urlencode(parameters)
liz=xbmcgui.ListItem(label=title, label2=departure_str,iconImage=os.path.join(basepath,"icon.png"))
liz.setProperty('IsPlayable', 'false')
xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz,isFolder=True)
except:
pass
xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_LABEL )
xbmcplugin.endOfDirectory(pluginhandle)
def getFavorites():
xbmc.executebuiltin('XBMC.ActivateWindow(favourites)')
def getJsonFailureMessage():
url = "http://www.wienerlinien.at/ogd_realtime/trafficInfoList?sender=Bb6juvg7Qe"
dictReader = csv.DictReader(open(positionpath, 'rb'), fieldnames = ['STEIG_ID', 'FK_LINIEN_ID','FK_HALTESTELLEN_ID', 'RICHTUNG', 'REIHENFOLGE', 'RBL_NUMMER','BEREICH','STEIG', 'STEIG_WGS84_LAT', 'STEIG_WGS84_LON', 'STAND'], delimiter = ';', quotechar = '"')
json_response = urllib2.urlopen(url).read()
json_dict = json.loads(json_response)
for column in json_dict['data']['trafficInfos']:
desc = ' | '
try:
title = column["title"].replace("\n"," | ").encode('utf-8')
except:
title = ''
try:
desc += column["description"].encode('utf-8')
except:
desc += (translation(30008)).encode("utf-8")
try:
reason = column['attributes']["reason"].encode('utf-8')
desc += "| %s |" % reason
except:
pass
relatedlines = ''
try:
for line in column['attributes']["relatedLines"]:
relatedlines += "[%s] " % line
desc += "| %s |" % relatedlines
except:
try:
for line in column["relatedLines"]:
desc += " [Linie %s] " % line
except:
pass
try:
station = column['attributes']["station"].encode('utf-8')
desc += "[ %s ]" % station
except:
station = ''
try:
status = column['attributes']["status"].encode('utf-8')
desc += "| %s: %s |" % ((translation(30009)).encode("utf-8"),status)
except:
status = ''
try:
duration = "%s bis &s" % (column['attributes']["ausVon"].encode('utf-8'),column['attributes']["ausBis"].encode('utf-8'))
desc += "| %s: %s |" % ((translation(30010)).encode("utf-8"),duration)
except:
duration = ""
title += desc.replace("\n"," | ")
parameters = {"title" : title,"mode" : "getFailures"}
u = sys.argv[0] + '?' + urllib.urlencode(parameters)
liz2=xbmcgui.ListItem(label=title, label2=desc,iconImage=os.path.join(basepath,"icon.png"))
liz2.setProperty('IsPlayable', 'false')
xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz2,isFolder=True)
xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_LABEL )
xbmcplugin.endOfDirectory(pluginhandle)
params=parameters_string_to_dict(sys.argv[2])
mode=params.get('mode')
id=params.get('id')
xbmcplugin.setPluginFanart(int(sys.argv[1]), defaultbackdrop, color2='0xFFFF3300')
if mode =='getMonitor':
getMonitor()
elif mode == 'getStationInfos':
getStationPositions(id)
elif mode == 'refreshStations':
getJsonMessage(id)
elif mode == 'getFavorites':
getFavorites();
elif mode == 'searchStation':
searchStation()
elif mode == 'searchStationResult':
searchStationResult()
elif mode == 'searchStationHistory':
getMonitor(id)
elif mode == 'getFailures':
getJsonFailureMessage()
else:
getMainMenu()