view boxi.py @ 66:53c37a02d471

working, I think/hope
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 14 Dec 2023 20:17:48 +0000
parents fee51ab07d09
children
line wrap: on
line source

#!/bin/python
from sys import stdin
from urllib2 import Request,urlopen, HTTPError
from base64 import b64encode
import re
type='xyzzy'
l=''
year='2014'
tsplit=re.compile('\t\t*')

def cc(names):
  return ' '.join(map(lambda n:n[0]+n[1:].lower(),names.split()))

while l=='':
  l=stdin.readline().rstrip()
if l!="\t\tPG Applications List for ILCC+Henry S. Thompson+ht+Informatics_Amended : 456344":
  print "Not what I was expecting: %s"%l
  exit(1)
for l in stdin:
  l=l.rstrip().decode('latin-1');
  if l=='':
    continue
  if l.find('Count:')==0:
    exit()
  if l.find('Pgm Code')==0:
    continue
  if l.find(type)==0:
    continue
  
  ff=l.split('\t')
  if len(ff)==9:
    (tf,uun,name,cat,stat,email,country,pgm,entry)=ff
  elif len(ff)==8:
    (tf,uun,name,cat,stat,email,pgm,entry)=ff
    country=""
  else:
    print "Bad input: %s"%('|'.join(l.split('\t')))
    continue
  if tf!='':
    type=tf
  #if stat not in ('SD','SP'):
  #  continue
  (sn,fn)=name.split(", ")
  surname=cc(sn)
  forenames=cc(fn)
  req='<app year="%s" uun="%s" type="%s" surname="%s" forenames="%s" cat="%s" stat="%s" pgm="%s" entry="%s" email="%s" country="%s"/>'%(year,uun,type,surname,forenames,cat,stat,pgm,entry,email,country)
  #print req.encode('iso-8859-1')
  #continue
  r=Request("http://localhost:8080/exist/apps/phd/new-app-maybe.xq",
            req.encode('utf-8'),headers={'Content-Type':'application/xml;charset=UTF-8'})
  try:
    res=urlopen(r)
  except HTTPError as err:
    print "Error:",err.read()
    print req
    exit(1)
  print res.read()