recordhepconfpaper.py 3.33 KB
Newer Older
1
""" store_tools.recordhepconfpaper
2 3 4 5

"""
import requests

6 7 8 9
from .base import (MSG_WELL_FORMED_DATE,
                   REG_DATE_YYYYMM,
                   T4,
                   T6)
10
from .confmixin import ConfMixin
11
from .exception import CheckException
12 13 14
from .recordheppubli import RecordHepPubli


15
class RecordHepConfPaper(RecordHepPubli, ConfMixin):
16 17
    """Conference proceeding from inspirehep.net version 2.

18 19 20
    Schema for conference paper is documented here:
    https://inspire-schemas.readthedocs.io/en/latest/schemas/

21 22 23
    Attributes:

        conference (dict):
24 25
            Schema for conference metadata is document here:
            https://inspire-schemas.readthedocs.io/en/latest/schemas/
26

27
            Main information are:
28 29 30 31 32 33 34 35 36 37 38
            +----------------+----------------------------------------+
            | key            | value                                  |
            +----------------+----------------------------------------+
            | addresses      | [{cities: [], country: str, ...}, ...] |
            | cnum           | str                                    |
            | control_number | int                                    |
            | closing_date   | str                                    |
            | opening_date   | str                                    |
            | titles         | [{title: str}, ...]                    |
            | urls           | [{value: str}, ...]                    |
            +----------------+----------------------------------------+
39

40 41 42 43 44 45 46
    """

    def __init__(self, recjson):

        super().__init__(recjson)

        self.conference = None
47
        self._process_conference_data()
48

49
    def _process_conference_data(self):
50
        """Append the conference data to the record.
51 52

        """
53 54 55
        logger = self.logger
        logger.debug(f"{T4}process conference data")

56 57 58
        # get the URL of the conference record in inspirehep.net
        publication_info = self.get("publication_info", None)
        if publication_info is None:
59
            logger.debug(f"{T6}no 'publication_info' field")
60 61 62 63 64 65 66 67
            return

        urlins = None
        for elt in publication_info:
            if "cnum" in elt:
                urlins = elt.get("conference_record", {}).get("$ref", None)

        if urlins is None:
68
            logger.debug(f"{T6}no 'conference_record.$ref' information")
69 70 71 72 73 74 75 76 77
            return

        # get conference metadata
        try:
            r = requests.get(urlins, timeout=30)
            r.raise_for_status()
            obj = r.json()

        except(requests.RequestException, ValueError):
78
            logger.debug(f"{T6}failed to retrieve conference data")
79 80
            return

81
        # append conference data
82
        self.conference = obj.get("metadata", None)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    def check_submitted_date(self):
        """Check that submitted date is either ``YYYY-MM`` or ``YYYY-MM-DD``.

        Raises:
            CheckException::
                * the date is not well formed

        """
        self.logger.debug(f"{T6}check submitted date")

        xdate = self.submitted()
        if REG_DATE_YYYYMM.match(xdate):
            return

        # recover by using the opening date of the conference
        val = self.conference.get("opening_date", None)
        if val is not None:
            self["preprint_date"] = val

        else:
            raise CheckException(MSG_WELL_FORMED_DATE)