1 # -*- coding: utf-8 -*-
5 from datetime import datetime
10 from parseHtml import Parser
11 import parseCorrection
13 from PySide.QtCore import Qt
14 from PySide.QtDeclarative import QDeclarativeView
15 from BeautifulSoup import BeautifulSoup
17 def QMLModel(overview, details):
18 # Mapping from the "overview" data structure to a "plain" data
19 # structure to be used as model for the qml listview
24 'date': item['date'].strftime('%d.%m.%Y') if item['date'] else u'Fußweg',
25 'duration': item['duration'].strftime('%H:%M'),
26 'price': item['price'],
27 'change': item['change'],
28 'details': details[i],
31 if len(item['time']) == 2 and all(x is not None for x in item['time']):
33 'time_from': item['time'][0].strftime('%H:%M'),
34 'time_to': item['time'][1].strftime('%H:%M'),
37 d.update({'time_from': '-', 'time_to': '-'})
46 def __init__(self, origin, destination, origin_type='stop', destination_type='stop', parent=None):
48 self.destination = destination
49 self.origin_type = origin_type
50 self.destination_type = destination_type
55 def get_html(self, dtime=None):
57 dtime = datetime.now()
58 #FIXME replace with logger
59 print "get_html (%s:%s:%s)" % tuple(dtime.timetuple())[3:6]
60 return urllib2.urlopen('%s?%s' % (settings.action, self.get_parameter(dtime)))
62 def open_browser(self, dtime=datetime.now()):
63 webbrowser.open('%s?%s' % (settings.action, self.get_parameter(dtime)))
65 def open_qml(self, dtime=None):
67 dtime = datetime.now()
68 #FIXME replace with logger
69 print "open_qml (%s:%s:%s)" % tuple(dtime.timetuple())[3:6]
70 html = self.get_html(dtime)
71 if BeautifulSoup(html).find('form', {'id': 'form_fahrplanauskunft'}):
72 cor = parseCorrection.Parser(html)
74 # TODO: Show selection
77 # TODO: Show selection
80 self.qml_model = QMLModel(p.overview, p.details)
81 self.view = QDeclarativeView(self.parent)
82 self.view.setWindowTitle('Search results')
83 self.view.setWindowFlags(Qt.Window)
84 # quick & dirty workaround
86 self.view.setAttribute(Qt.WA_Maemo5StackedWindow)
89 self.view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
90 self.view.setSource('ui/Overview.qml')
91 self.view.rootObject().setProperty('model', self.qml_model)
94 def get_datetime(self, dtime):
95 return (dtime.strftime('%d.%m.%Y'), dtime.strftime('%H:%M'))
97 def get_parameter(self, dtime):
98 date, time = self.get_datetime(dtime)
100 post = {'language': 'de',
103 'execInst': 'normal',
105 'anySigWhenPerfectNoOtherMatches': 1,
106 'itdLPxx_locationServerActive': '',
107 'locationServerActive': 0,
108 'typeInfo_origin': 'invalid',
109 'placeState_origin': 'empty',
110 'placeInfo_origin': 'invalid',
111 'place_origin': 'Wien',
112 'type_origin': self.origin_type, # stop/address/poi
113 'nameState_origin': 'empty',
114 'nameInfo_origin': 'invalid',
115 'anyType_origin': '',
116 'name_origin': self.origin,
117 'typeInfo_destination': 'invalid',
118 'placeState_destination': 'empty',
119 'placeInfo_destination': 'invalid',
120 'place_destination': 'Wien',
121 'type_destination': self.destination_type, # stop/address/poi
122 'nameState_destination': 'empty',
123 'nameInfo_destination': 'invalid',
124 'anyType_destination': '',
125 'name_destination': self.destination,
126 'itdTripDateTimeDepArr': 'dep',
127 'itdDateDayMonthYear': date, # DD.MM.YYYY
128 'itdTime': time, # HH:MM
129 'submitbutton': 'SUCHEN'
132 params = urllib.urlencode(post)