2 flightCal -- make hCalendar from text flight info
4 :Author: `Dan Connolly`_
5 :Copyright: `W3C Open Source License`_ Share and enjoy.
7 .. _Dan Connolly: http://www.w3.org/People/Connolly/
8 .. _W3C Open Source License: http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
16 python title <flightinfo.txt >flightinfo.html
19 where flightinfo.txt is a text flight itinerary
20 in a format produced, I think, by Sabre_.
22 .. _Sabre: http://en.wikipedia.org/wiki/Sabre_%28computer_system%29
28 The examples in the docstrings below are executable doctest_ unit
29 tests. Check them a la::
31 python flightCal.py --test
33 @@with files flttbl.xml and Airports in the current directory.
35 .. _doctest: http://www.python.org/doc/lib/module-doctest.html
37 This module is documented in rst_ format for use with epydoc_.
39 .. _epydoc: http://epydoc.sourceforge.net/
40 .. _rst: http://docutils.sourceforge.net/docs/user/rst/quickstart.html
45 from genshi.template import MarkupTemplate # http://genshi.edgewall.org/
47 import fltstxt, zonetab, aptdata
49 __docformat__ = 'restructuredtext en'
52 def main(argv, template="flttbl.xml"):
55 zones=list(zonetab.timezones(exclude=["Indiana"]))
56 sys.stdout.write(convert(sys.stdin, template, web, zones, argv[1]))
59 # Sabre seems to have its own way of naming airports.
60 # I can't find a list of them online.
61 # So in a file called Airports@@, we list them, a la:
65 Airports = [s.strip().split(" ", 1) for s in file("Airports")]
73 sys.stderr.write('%s ' % a)
74 sys.stderr.write("\n")
78 def offsettz(zones, when, where):
79 """Use latitude/longitude find utc offset for a a local time.
81 >>> offsettz(zonetab.timezones(),
82 ... {'float': '2006-10-06T15:50:00'},
83 ... {'geo': {'longitude': -94, 'latitude': 39}})
84 '2006-10-06T15:50:00-05:00'
88 lat = where['geo']['latitude']
89 lon = where['geo']['longitude']
93 tz = zonetab.nearest_tz(lat, lon, zones)[2]
94 out = zonetab.stdtime(tz, when['float'])
95 when['date_time'] = out
100 """Set nickname to the iata code
101 if the org field of a card is a known airport name
103 org = card['org']['organization_name']
104 for iata, n in Airports:
106 card['nickname'] = {'text': iata}
110 def fltlink(carrier, num):
113 >>> fltlink('AMERICAN AIRLINES', 1557)
118 if carrier=='AMERICAN AIRLINES':
119 return "http://aa2go.com/index.php?page=status&sub=results&Fn=%s" % num
121 # @@todo: look these up from wikipedia. hmm... how to correlate names?
122 airlines = {'AMERICAN AIRLINES': 'AA',
124 # http://en.wikipedia.org/wiki/Midwest_Airlines
125 'MIDWEST AIRLINES': 'YX',
126 'UNITED AIRLINES': 'UA',
127 # http://en.wikipedia.org/wiki/Delta_Air_Lines
128 'DELTA AIR LINES INC': 'DL',
129 # http://en.wikipedia.org/wiki/US_Airways
131 # http://en.wikipedia.org/wiki/KLM
132 'KLM ROYAL DUTCH': 'KL'
134 airlineCode = airlines[carrier]
135 action = 'https://bwi.flightview.com/fvSabreVT/fvCPL.exe'
136 params = {'AL': airlineCode,
139 'Find1': 'Track+Flight',
142 q = '&'.join(['%s=%s' % (n, v) for n, v in params.iteritems()])
143 return "%s?%s" % (action, q)
150 def convert(infp, template, web, zones, title):
151 events = list(fltstxt.flights(infp))
153 # look up airport lat/long; fill in time offsets
155 flt['url'] = fltlink(flt['carrier'], flt['FLT']) #@@
156 for apt in [flt['location'][x] for x in ['lv', 'ar']]:
160 progress("unrecognized airport", apt['fn']['text'])
163 aptdata.airportCard(web,
164 apt['nickname']['text'], apt)
165 offsettz(zones, flt['dtstart'], flt['location']['lv'])
166 offsettz(zones, flt['dtend'], flt['location']['ar'])
169 tmpl = MarkupTemplate(file(template).read())
170 stream = tmpl.generate(title=title,
172 return stream.render()
177 if '--online' in sys.argv:
178 web = aptdata.WebCache("wikipedia-cache").get
179 del argv[argv.index('--online')]
183 if __name__ == '__main__':
185 if '--test' in sys.argv: