1111from bibtexparser .bwriter import BibTexWriter
1212
1313
14- if __name__ == '__main__' :
14+ try :
1515
1616 parser = argparse .ArgumentParser ()
1717 parser .add_argument ("-b" , "--body" , help = "input issue body here" , type = str , default = "" )
102102 if not req .status_code == 200 :
103103 print (f'Request of { url } could not be processed, got status code { req .status_code } .' )
104104 break
105-
106- data = req .json ()
107105
108- if len (data ['author' ]) > 1 :
109- id = data ['author' ][0 ]['family' ] + 'EtAl' + str (data ['issued' ]['date-parts' ][0 ][0 ])
110- else :
111- id = data ['author' ][0 ]['family' ] + str (data ['issued' ]['date-parts' ][0 ][0 ])
106+ with open ("request.txt" , "w" ) as f :
107+ f .write (req .text )
108+ try :
109+ data = req .json ()
110+ except :
111+ # in case request did not provide the json data, retrieve it from the bib entry
112+ data = {
113+ e [0 ].strip (): e [1 ][1 :].split ("}" )[0 ]
114+ for e in [e for e in [l .split (" = " ) for l in bib .splitlines ()] if len (e ) == 2 ]
115+ }
116+ data ["author" ] = [
117+ {"family" : a .split (", " )[0 ], "given" : a .split (", " )[1 ]}
118+ for a in data ["author" ].split (" and " )
119+ ]
120+ data ["issued" ] = {"date-parts" : [[data ["year" ]]]}
121+
122+ id = data ['author' ][0 ]['family' ] + 'EtAl' * (len (data ['author' ]) > 1 ) + str (data ['issued' ]['date-parts' ][0 ][0 ])
112123 id = id .replace (" " , "_" )
113124
114125 d = db .get_entry_dict ()
162173 line = re .sub (r'%}+' , '%' , line )
163174 line = line .rstrip ('\r \n ' )
164175 print (line )
176+
177+ except Exception as e :
178+ print (f"ERROR : { e } \n " )
0 commit comments