Add QML UI for displaying the overview
[pywienerlinien] / wlSearch.py
1 # -*- coding: utf-8 -*-
2
3 import urllib
4 import sys
5 from datetime import datetime
6 import settings
7 import webbrowser
8 import urllib2
9
10 from parseHtml import Parser
11
12 from PySide.QtDeclarative import QDeclarativeView
13
14 def QMLModel(overview):
15     # Mapping from the "overview" data structure to a "plain" data
16     # structure to be used as model for the qml listview
17     r = []
18     for item in overview:
19         d = {
20                 'date': item['date'].strftime('%d.%m.%Y') if item['date'] else u'Fußweg',
21                 'duration': item['duration'].strftime('%H:%M'),
22                 'price': item['price'],
23                 'change': item['change'],
24         }
25
26         if len(item['time']) == 2 and all(x is not None for x in item['time']):
27             d.update({
28                 'time_from': item['time'][0].strftime('%H:%M'),
29                 'time_to': item['time'][1].strftime('%H:%M'),
30             })
31         else:
32             d.update({'time_from': '-', 'time_to': '-'})
33
34         r.append(d)
35     return r
36
37
38 class Search:
39
40     def __init__(self, origin, destination, origin_type='stop', destination_type='stop'):
41         self.origin = origin
42         self.destination = destination
43         self.origin_type = origin_type
44         self.destination_type = destination_type
45         self.view = None
46         self.qml_model = None
47
48     def get_html(self, dtime=datetime.now()):
49         return urllib2.urlopen('%s?%s' % (settings.action, self.get_parameter(dtime)))
50
51     def open_browser(self, dtime=datetime.now()):
52         webbrowser.open('%s?%s' % (settings.action, self.get_parameter(dtime)))
53
54     def open_qml(self, dtime=datetime.now()):
55         p = Parser(self.get_html(dtime))
56         self.qml_model = QMLModel(p.overview)
57         self.view = QDeclarativeView()
58         self.view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
59         self.view.setSource('ui/Overview.qml')
60         self.view.rootObject().setProperty('model', self.qml_model)
61         self.view.show()
62
63     def get_datetime(self, dtime):
64         return (dtime.strftime('%d.%m.%Y'), dtime.strftime('%H:%M'))
65
66     def get_parameter(self, dtime):
67         date, time = self.get_datetime(dtime)
68
69         post = {'language': 'de',
70             'sessionID': 0,
71             'requestID': 0,
72             'execInst': 'normal',
73             'command': '',
74             'anySigWhenPerfectNoOtherMatches': 1,
75             'itdLPxx_locationServerActive': '',
76             'locationServerActive': 0,
77             'typeInfo_origin': 'invalid',
78             'placeState_origin': 'empty',
79             'placeInfo_origin': 'invalid',
80             'place_origin': 'Wien',
81             'type_origin': self.origin_type, # stop/address/poi
82             'nameState_origin': 'empty',
83             'nameInfo_origin': 'invalid',
84             'anyType_origin': '',
85             'name_origin': self.origin,
86             'typeInfo_destination': 'invalid',
87             'placeState_destination': 'empty',
88             'placeInfo_destination': 'invalid',
89             'place_destination': 'Wien',
90             'type_destination': self.destination_type, # stop/address/poi
91             'nameState_destination': 'empty',
92             'nameInfo_destination': 'invalid',
93             'anyType_destination': '',
94             'name_destination': self.destination,
95             'itdTripDateTimeDepArr': 'dep',
96             'itdDateDayMonthYear': date, # DD.MM.YYYY
97             'itdTime': time, # HH:MM
98             'submitbutton': 'SUCHEN'
99         }
100
101         params = urllib.urlencode(post)
102         return params