Mercurial > hg > python
view bobi.py @ 17:6c9e371b0325
doesn't break, what's there is good, but stuck part-way in 10a
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 25 Mar 2020 19:21:38 +0000 |
parents | 0a3abe59e364 |
children |
line wrap: on
line source
#!/bin/python from sys import stdin from urllib2 import Request,urlopen, HTTPError, URLError l='' year='2016' uuns={} def cc(names): return ' '.join(map(lambda n:n[0]+n[1:].lower(),names.split())) while l=='': l=stdin.readline().rstrip() oldf=l.find("ILCC PGR live applications: ")==0 if ((not oldf) and l.find("EUCLID Admissions core applicant data - applications for next session")!=0 and l.find("HST PGR applicants incl Beihang")!=0): print "Not what I was expecting: %s"%l exit(1) while l.find('UUN\t')<0: l=stdin.readline().rstrip() for l in stdin: l=l.rstrip().decode('latin-1'); if l=='': continue ff=l.split('\t') if oldf: if len(ff)==9: (uun,surname,forenames,cat,stat,dec,entry,email,country)=ff pgrm='PRPHDLNGCC1F' nat='Unknown' else: print "Bad old-style input: %s"%('|'.join(l.split('\t'))) continue else: if len(ff)==11: (nat,stat,pgrm,dec,surname,forenames,entry,cat,uun,email,country)=ff else: print "Bad new-style input: %s"%('|'.join(l.split('\t'))) continue if uun in uuns: print "!!! Careful !!!: %s seen before today with status %s, now %s"%(uun,uuns[uun],stat) else: uuns[uun]=stat surname=cc(surname) forenames=cc(forenames) if pgrm=='PRPHDLNGCC1F': ptype='I' else: ptype='B' req='<app year="%s" uun="%s" type="PHD %s" surname="%s" forenames="%s" cat="%s" stat="%s" decision="%s" pgm="PhD ILCC" entry="%s" email="%s" country="%s" nationality="%s"/>'%(year,uun,ptype,surname,forenames,cat,stat,dec,entry,email,country,nat) #print req.encode('iso-8859-1') #continue r=Request("http://troutbeck.inf.ed.ac.uk:8080/exist/apps/phd/new-app-maybe.xq", req.encode('utf-8'),headers={'Content-Type':'application/xml;charset=UTF-8'}) try: res=urlopen(r) host="troutbeck.inf.ed.ac.uk" except URLError as err1: 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) host="localhost" except URLError as err: print "Failed, no way to database server:",err1.read(),err.read() exit(1) res=res.read() print ptype,res if (not oldf) and res.find("<div>We already")==0: req='<update year="%s" uun="%s" nationality="%s"/>'%(year,uun,nat) r=Request("http://%s:8080/exist/apps/phd/updateApp.xq"%host, 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)