recordhepconf.py 5.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
""" 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.

14 15 16 17 18
    Attributes:

        conference (dict):
            metadata of the conference
            schema is https://inspirehep.net/s…records/conferences.json
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    """

    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")

94
        val = de.strftime("%d %b %Y")
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        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])