moved iParser to own file
[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.QtCore import Qt
13 from PySide.QtDeclarative import QDeclarativeView
14
15 def QMLModel(overview, details):
16     # Mapping from the "overview" data structure to a "plain" data
17     # structure to be used as model for the qml listview
18     r = []
19     i = 0
20     for item in overview:
21         d = {
22                 'date': item['date'].strftime('%d.%m.%Y') if item['date'] else u'Fußweg',
23                 'duration': item['duration'].strftime('%H:%M'),
24                 'price': item['price'],
25                 'change': item['change'],
26                 'details': details[i],
27         }
28
29         if len(item['time']) == 2 and all(x is not None for x in item['time']):
30             d.update({
31                 'time_from': item['time'][0].strftime('%H:%M'),
32                 'time_to': item['time'][1].strftime('%H:%M'),
33             })
34         else:
35             d.update({'time_from': '-', 'time_to': '-'})
36
37         r.append(d)
38         i += 1
39     return r
40
41
42 class Search:
43
44     def __init__(self, origin, destination, origin_type='stop', destination_type='stop', parent=None):
45         self.origin = origin
46         self.destination = destination
47         self.origin_type = origin_type
48         self.destination_type = destination_type
49         self.parent = parent
50         self.view = None
51         self.qml_model = None
52
53     def get_html(self, dtime=None):
54         if not dtime:
55             dtime = datetime.now()
56         #FIXME replace with logger
57         print "get_html (%s:%s:%s)" % tuple(dtime.timetuple())[3:6]
58         return urllib2.urlopen('%s?%s' % (settings.action, self.get_parameter(dtime)))
59
60     def open_browser(self, dtime=datetime.now()):
61         webbrowser.open('%s?%s' % (settings.action, self.get_parameter(dtime)))
62
63     def open_qml(self, dtime=None):
64         if not dtime:
65             dtime = datetime.now()
66         #FIXME replace with logger
67         print "open_qml (%s:%s:%s)" % tuple(dtime.timetuple())[3:6]
68         p = Parser(self.get_html(dtime))
69         self.qml_model = QMLModel(p.overview, p.details)
70         self.view = QDeclarativeView(self.parent)
71         self.view.setWindowTitle('Search results')
72         self.view.setWindowFlags(Qt.Window)
73         # quick & dirty workaround
74         try:
75             self.view.setAttribute(Qt.WA_Maemo5StackedWindow)
76         except:
77             pass
78         self.view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
79         self.view.setSource('ui/Overview.qml')
80         self.view.rootObject().setProperty('model', self.qml_model)
81         self.view.show()
82
83     def get_datetime(self, dtime):
84         return (dtime.strftime('%d.%m.%Y'), dtime.strftime('%H:%M'))
85
86     def get_parameter(self, dtime):
87         date, time = self.get_datetime(dtime)
88
89         post = {'language': 'de',
90             'sessionID': 0,
91             'requestID': 0,
92             'execInst': 'normal',
93             'command': '',
94             'anySigWhenPerfectNoOtherMatches': 1,
95             'itdLPxx_locationServerActive': '',
96             'locationServerActive': 0,
97             'typeInfo_origin': 'invalid',
98             'placeState_origin': 'empty',
99             'placeInfo_origin': 'invalid',
100             'place_origin': 'Wien',
101             'type_origin': self.origin_type, # stop/address/poi
102             'nameState_origin': 'empty',
103             'nameInfo_origin': 'invalid',
104             'anyType_origin': '',
105             'name_origin': self.origin,
106             'typeInfo_destination': 'invalid',
107             'placeState_destination': 'empty',
108             'placeInfo_destination': 'invalid',
109             'place_destination': 'Wien',
110             'type_destination': self.destination_type, # stop/address/poi
111             'nameState_destination': 'empty',
112             'nameInfo_destination': 'invalid',
113             'anyType_destination': '',
114             'name_destination': self.destination,
115             'itdTripDateTimeDepArr': 'dep',
116             'itdDateDayMonthYear': date, # DD.MM.YYYY
117             'itdTime': time, # HH:MM
118             'submitbutton': 'SUCHEN'
119         }
120
121         params = urllib.urlencode(post)
122         return params