Commit 490e48ac authored by Tobias WEBER's avatar Tobias WEBER
Browse files

continued with instrumental report helper tool

parent bc89b1b7
......@@ -5,28 +5,80 @@
# @license see 'LICENSE' file
#
import sys
import requests_html as req
#
# constants
#
prop_detail_url = "https://userclub.ill.eu/userclub/proposalSearch/details/%d"
prop_search_url = "https://userclub.ill.eu/userclub/proposalSearch"
prop_detail_url = "https://userclub.ill.eu/userclub/proposalSearch/details/%s"
#
# search for all in20 proposals in a given span of years
#
def get_props(session_no, startyear, endyear):
propids = []
proptexts = []
with req.HTMLSession() as session:
cookies = { "JSESSIONID" : session_no }
searchform = { \
"selectedInstrument": "50",
"selectedCollege": "-1",
"accepted": "true",
"year": "-1",
"month": "-1",
"fromYear": startyear,
"toYear": endyear,
"_mainProposer": "on",
"_proposer": "on",
"localContact": "true",
"_localContact": "on",
"instrumentResponsible": "true",
"_instrumentResponsible": "on",
"_subcommitteeMember": "on",
"mainProposerName": "",
"proposerName": "",
"keywords": "",
"proposalNumber": "",
}
reply = session.post(prop_search_url, cookies=cookies, data=searchform)
if reply.status_code != 200:
return None
for elem in reply.html.xpath("body/div[1]/div[2]/div[2]/div[2]/table/tbody/*", clean=False):
row = elem.xpath("tr/td/a/text()")
rowlink = elem.xpath("tr/td/a/@href")
council = row[0].strip()
num = row[1].strip()
proposer = row[3].strip()
proptexts.append(num + " (" + council + ")" + " by " + proposer)
link = rowlink[0].strip()
propids.append(link[link.find("(")+1 : link.find(",")])
return [ propids, proptexts ]
#
# downloads proposal detail information
#
def get_prop_details(prop_no, session_no):
def get_prop_details(session_no, prop_no):
infos = { \
"allproposers": [], "alllabs": [], "allcountries": [], \
"allproposers": [], "alllabs": [], "allcountries": [],
"allinstruments": [], "allreqdays": [], "allallocdays": [], "allgrades": [], "allschedules": [], \
}
#import requests as req
import requests_html as req
#with req.Session() as session:
with req.HTMLSession() as session:
cookies = { "JSESSIONID" : session_no }
......@@ -89,33 +141,96 @@ def get_prop_details(prop_no, session_no):
# get all requested instruments
for elem in reply.html.xpath("div[1]/div[6]/table/tbody/*", clean=False):
row = elem.xpath("tr/td/text()")
row_alt = elem.xpath("tr/td/p/text()")
infos["allinstruments"].append(row[0].strip())
infos["allreqdays"].append(row[1].strip())
infos["allallocdays"].append(row[2].strip())
infos["allgrades"].append(row[3].strip())
infos["allschedules"].append(row[4].strip())
return infos
return None
sched = row[4].strip()
if sched == "" and len(row_alt) > 0:
sched = row_alt[0].strip()
infos["allschedules"].append(sched)
# simplify proposer fields
infos["allproposers_simple"] = ""
for [prop, lab, country] in zip(infos["allproposers"], infos["alllabs"], infos["allcountries"]):
if infos["allproposers_simple"] != "":
infos["allproposers_simple"] += ","
infos["allproposers_simple"] += prop
# remove ";" from title, for csv export
infos["title"] = infos["title"].replace(";", ",")
return infos
# -----------------------------------------------------------------------------
import sys
if len(sys.argv) < 2:
print("Please specify a session id.")
# -----------------------------------------------------------------------------
# command line arguments
if len(sys.argv) < 4:
print("Please specify a session id, a start and an end year.")
exit(-1)
session_no = sys.argv[1]
startyear = sys.argv[2]
endyear = sys.argv[3]
# proposal list
print("Posting proposal search request ...")
[ propids, proptexts ] = get_props(session_no, startyear, endyear)
if propids == None:
print("Error: Cannot get proposal list!")
exit(-1)
print("Found %d proposals." % len(propids))
print()
# proposal details
infos = []
print("Retrieving proposal details ...")
for idx in range(len(propids)):
propid = propids[idx]
proptext = proptexts[idx]
print("Requesting details for proposal %s ..." % proptext)
info = get_prop_details(session_no, propid)
if info == None:
print("Error retrieving proposal details!")
continue
infos.append(info)
print()
# output results
with open("proposals.csv", "w") as file:
print("Writing results to %s ..." % file.name)
file.write("#\n")
file.write("# Column headers: Coucil; Proposal No; Title; Main proposer; All proposers; Allocated days; Schedule; Instrument; Local contact\n")
file.write("#\n")
#prop_no = 79781
prop_no = 79659
for info in infos:
#row = "{council:>10} {id:>12} {mainproposer} {allproposers_simple}".format(**info)
infos = get_prop_details(prop_no, session_no)
print(infos)
for [instr, req, alloc, grade, sched] in zip(info["allinstruments"], info["allreqdays"], info["allallocdays"], info["allgrades"], info["allschedules"]):
row = "{council};{id};{title};{mainproposer};{allproposers_simple};{alloc};{sched};{instr},{localcontact}".format(**info, instr=instr, req=req, alloc=alloc, grade=grade, sched=sched)
file.write(row)
file.write("\n")
# -----------------------------------------------------------------------------
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment