""" store_tools.recordhepconf """ import requests from datetime import datetime from plugin_dbui import CLEAN_SPACES from .recordheppubli import RecordHepPubli class RecordHepConf(RecordHepPubli): """Conference proceeding from inspirehep.net version 2. Attributes: conference (dict): metadata of the conference schema is https://inspirehep.net/s…records/conferences.json """ def __init__(self, recjson): super().__init__(recjson) self.conference = None # get the URL of the conference record in inspirehep.net publication_info = self.get("publication_info", None) if publication_info is None: return urlins = None for elt in publication_info: if "cnum" in elt: urlins = elt.get("conference_record", {}).get("$ref", None) if urlins is None: return # get conference metadata try: r = requests.get(urlins, timeout=30) r.raise_for_status() obj = r.json() except(requests.RequestException, ValueError): return self.conference = obj.get("metadata", None) def conference_country(self): """The country where the conference took place. Returns: str: * the filter *CLEAN_SPACES* is applied. * empty when the country is not defined. """ conference = self.conference if conference is None: return "" val = (conference .get("addresses", [{}])[0] .get("country", "")) return CLEAN_SPACES(val) def conference_dates(self): """The dates of the conference. Returns: str: * usual pattern are ``6-5 March 2012`` or ``30 March - 5 April 2012`` * empty string when not defined """ conference = self.conference if conference is None: return "" opening = conference.get("opening_date", None) closing = conference.get("closing_date", None) if opening is None or closing is None: return "" ds = datetime.strptime(opening, "%Y-%m-%d") de = datetime.strptime(closing, "%Y-%m-%d") val = de.strftime("%d %b %Y") if ds.month == de.month: val = f"{ds.day}-{val}" else: val = f"{ds.strftime('%d %b')} - {val}" return val def conference_id(self): """The conference identifier used in the store. Returns: int or None """ conference = self.conference if conference is None: return "" return conference.get("control_number", None) def conference_key(self): """The conference key used in the store. Returns: str: * empty string when it is not defined """ for elt in self["publication_info"]: if "cnum" in elt: return elt["cnum"] return "" def conference_location(self): """The conference location. Returns: str: - the pattern is ``town, country`` - empty string when town or country is missing - empty string when it is not defined """ conference = self.conference if conference is None: return "" addresses = conference.get("addresses", [{}])[0] city = addresses.get("cities", [None])[0] country = addresses.get("country", None) if city is None or country is None: return "" return f"{city}, {country}" def conference_title(self): """The title of the conference. Returns: str: * empty string when it is not defined * first title is selected is there is more than one """ conference = self.conference if conference is None: return "" val = (conference .get("titles", [{}])[0] .get("title", "")) return val def conference_town(self): """The town where the conference took place. Returns: str: * empty string when it is not defined. """ conference = self.conference if conference is None: return "" val = (conference .get("addresses", [{}])[0] .get("cities", [""])[0]) return val def conference_url(self): """The URL of the conference home page. Returns: str: * select the first URL when there is more than one * empty string when it is not defined """ conference = self.conference if conference is None: return "" val = (conference .get("urls", [{}])[0] .get("value", "")) return val def conference_year(self): """The year of the conference. Returns: str: * empty string when it is not defined. """ conference = self.conference if conference is None: return "" opening = conference.get("opening_date", None) return ("" if opening is None else opening[:4])