Mercurial > hg > python
comparison trip2xml.py @ 2:e07789816ca5
adding more python files from lib/python on origen
author | Henry Thompson <ht@markup.co.uk> |
---|---|
date | Mon, 09 Mar 2020 16:48:09 +0000 |
parents | |
children | 2d7c91f89f6b |
comparison
equal
deleted
inserted
replaced
1:0a3abe59e364 | 2:e07789816ca5 |
---|---|
1 #!/usr/bin/python | |
2 '''Convert a screen-scrape from Check My Trip to diary-style XML''' | |
3 import re, sys, datetime | |
4 year="2016" | |
5 leg=re.compile(year+"TO") | |
6 duration=re.compile("([0-9][0-9]) ([A-Za-z]*) %s .*duration"%year) | |
7 plusOne=re.compile(" [+]1 day$") | |
8 flight=re.compile("\\\\| *([^ ]*) *confirmed") | |
9 dep=re.compile("^Dep: (.*)") | |
10 arr=re.compile("^Arr: (.*)") | |
11 CS=", " | |
12 | |
13 cleg=None | |
14 class Leg: | |
15 def __init__(self): | |
16 self.flights=[] | |
17 self.p1=False | |
18 self.dd=None | |
19 | |
20 def addFlight(self,flight): | |
21 self.flights.append(flight) | |
22 | |
23 def setDD(self,m): | |
24 print 'sdd' | |
25 td="%s-%s-%s"%(year,m.group(2),m.group(1)) | |
26 self.ddate=datetime.datetime.strptime(td,"%Y-%B-%d").date() | |
27 self.dd=self.ddate.isoformat() | |
28 if self.p1: | |
29 self.ads=" -- %s"%(self.ddate+datetime.timedelta(1)).isoformat() | |
30 else: | |
31 self.ads="" | |
32 | |
33 def setPlusOne(self): | |
34 print 'p1' | |
35 self.p1=True | |
36 | |
37 def __str__(self): | |
38 fa=self.flights[0].fa | |
39 ta=self.flights[-1].ta | |
40 if len(self.flights)>1: | |
41 va=" via "+CS.join([f.fa for f in self.flights[1:]]) | |
42 else: | |
43 va="" | |
44 ff=CS.join([f.fn for f in self.flights]) | |
45 tt=CS.join(["%s--%s"%(f.dt,f.at) for f in self.flights]) | |
46 return "<item term='%s%s'>%s->%s%s; %s; %s</item>"%(self.dd,self.ads,fa,ta,va,ff,tt) | |
47 | |
48 class Flight(): | |
49 def __init__(self,fn): | |
50 self.fn=fn # flight number | |
51 | |
52 def setDep(self,ds): | |
53 dss=ds.split('|') | |
54 self.dt=dss[0][:2]+dss[0][3:5] | |
55 self.fa=dss[1].split()[-1] | |
56 | |
57 def setArr(self,ax): | |
58 ass=ax.split('|') | |
59 self.at=ass[0][:2]+ass[0][3:5] | |
60 if (ass[0].find('(+1 day)')==6): | |
61 self.at+="+1" | |
62 self.ta=ass[1].split()[-1] | |
63 | |
64 print "<list type='defn'>" | |
65 for l in sys.stdin: | |
66 if leg.search(l): | |
67 if cleg is not None: | |
68 print cleg | |
69 cleg=Leg() | |
70 m = flight.search(l) | |
71 if m: | |
72 fl=Flight(m.group(1)) | |
73 cleg.addFlight(fl) | |
74 continue | |
75 if ((cleg is not None) and | |
76 (cleg.dd is None)): | |
77 m=duration.search(l) | |
78 if m: | |
79 cleg.setDD(m) | |
80 continue | |
81 m=plusOne.search(l) | |
82 if m: | |
83 cleg.setPlusOne() | |
84 continue | |
85 m=dep.search(l) | |
86 if m: | |
87 fl.setDep(m.group(1)) | |
88 continue | |
89 m=arr.search(l) | |
90 if m: | |
91 fl.setArr(m.group(1)) | |
92 print cleg | |
93 print "</list>" |