view trip2xml.py @ 31:1252f8100c6a

printouts to aid further testing
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 29 Jul 2021 12:44:00 +0100
parents 2d7c91f89f6b
children
line wrap: on
line source

#!/usr/bin/python
'''Convert a screen-scrape from Check My Trip to diary-style XML'''
import re, sys, datetime
year="2016"
leg=re.compile(year+"TO")
duration=re.compile("([0-9][0-9]) ([A-Za-z]*) %s .*duration"%year)
plusOne=re.compile(" [+]1 day$")
flight=re.compile("\\\\| *([^ ]*) *confirmed")
dep=re.compile("^Dep: (.*)")
arr=re.compile("^Arr: (.*)")
CS=", "

cleg=None
class Leg:
  def __init__(self):
    self.flights=[]
    self.p1=False

  def addFlight(self,flight):
    self.flights.append(flight)

  def setDD(self,m):
    td="%s-%s-%s"%(year,m.group(2),m.group(1))
    self.ddate=datetime.datetime.strptime(td,"%Y-%B-%d").date()
    self.dd=self.ddate.isoformat()
    if self.p1:
      self.ads=" -- %s"%(self.ddate+datetime.timedelta(1)).isoformat()
    else:
      self.ads=""

  def setPlusOne(self):
    self.p1=True

  def __str__(self):
    fa=self.flights[0].fa
    ta=self.flights[-1].ta
    if len(self.flights)>1:
      va=" via "+CS.join([f.fa for f in self.flights[1:]])
    else:
      va=""
    ff=CS.join([f.fn for f in self.flights])
    tt=CS.join(["%s--%s"%(f.dt,f.at) for f in self.flights])
    return "<item term='%s%s'>%s->%s%s; %s; %s</item>"%(self.dd,self.ads,fa,ta,va,ff,tt)

class Flight():
  def __init__(self,fn):
    self.fn=fn # flight number

  def setDep(self,ds):
    dss=ds.split('|')
    self.dt=dss[0][:2]+dss[0][3:5]
    self.fa=dss[1].split()[-1]

  def setArr(self,ax):
    ass=ax.split('|')
    self.at=ass[0][:2]+ass[0][3:5]
    self.ta=ass[1].split()[-1]
    
print "<list type='defn'>"
for l in sys.stdin:
  if leg.search(l):
    if cleg is not None:
      print cleg
    cleg=Leg()
  m = flight.search(l)
  if m:
    fl=Flight(m.group(1))
    cleg.addFlight(fl)
    continue
  m=duration.search(l)
  if m:
    cleg.setDD(m)
    continue
  m=plusOne.search(l)
  if m:
    cleg.setPlusOne()
    continue
  m=dep.search(l)
  if m:
    fl.setDep(m.group(1))
    continue
  m=arr.search(l)
  if m:
    fl.setArr(m.group(1))
print cleg
print "</list>"