-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from arXiv/get_record
Get record
- Loading branch information
Showing
22 changed files
with
766 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Optional, List | ||
|
||
from arxiv.db import Session | ||
from arxiv.db.models import Metadata | ||
from arxiv.identifier import Identifier | ||
|
||
|
||
def get_record_data_current(arxiv_id: Identifier )-> Optional[Metadata]: | ||
"""fetch latest metadata for a specific paper""" | ||
data=(Session.query(Metadata) | ||
.filter(Metadata.paper_id == arxiv_id.id) | ||
.filter(Metadata.is_current==1) | ||
.first() | ||
) | ||
return data | ||
|
||
def get_record_data_all(arxiv_id: Identifier)-> Optional[List[Metadata]]: | ||
"""fetch all metadata for a specific paper""" | ||
data=(Session.query(Metadata) | ||
.filter(Metadata.paper_id == arxiv_id.id) | ||
.all() | ||
) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Dict | ||
from datetime import datetime, timezone | ||
|
||
from flask import render_template | ||
|
||
from arxiv.identifier import Identifier | ||
|
||
from oaipmh.processors.db import get_record_data_all, get_record_data_current | ||
from oaipmh.data.oai_errors import OAINonexistentID | ||
from oaipmh.data.oai_properties import OAIParams, MetadataFormat | ||
from oaipmh.serializers.create_records import arXivOldRecord, arXivRawRecord, arXivRecord, dcRecord | ||
from oaipmh.serializers.output_formats import Response | ||
|
||
def do_get_record(arxiv_id: Identifier, format: MetadataFormat, query_data: Dict[OAIParams, str])-> Response: | ||
"""fetches the required data for a record for a specific format | ||
converts data into specif format and renders record template | ||
""" | ||
if format.all_versions: | ||
data=get_record_data_all(arxiv_id) | ||
if not data: | ||
raise OAINonexistentID("Nothing found for this ID",query_params=query_data) | ||
|
||
if format.prefix=="oai_dc": | ||
record=dcRecord(data) | ||
else: #arXivRaw | ||
record=arXivRawRecord(data) | ||
else: | ||
data=get_record_data_current(arxiv_id) | ||
if data is None: | ||
raise OAINonexistentID("Nothing found for this ID",query_params=query_data) | ||
if format.prefix=="arXivOld": | ||
record= arXivOldRecord(data) | ||
else: #arXiv | ||
record= arXivRecord(data) | ||
|
||
response=render_template("get_record.xml", | ||
response_date=datetime.now(timezone.utc), | ||
query_params=query_data, | ||
record=record, | ||
format=format.prefix | ||
) | ||
headers={"Content-Type":"application/xml"} | ||
return response, 200, headers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from typing import List, Optional | ||
from datetime import datetime | ||
|
||
from arxiv.authors import parse_author_affil | ||
from arxiv.db.models import Metadata | ||
from arxiv.document.version import VersionEntry | ||
from arxiv.taxonomy.category import Category | ||
from arxiv.taxonomy.definitions import CATEGORIES | ||
|
||
from oaipmh.processors.create_set_list import make_set_str | ||
|
||
class Header: | ||
def __init__(self, id:str, date:datetime, cats:List[Category]) -> None: | ||
self.id=f"oai:arXiv.org:{id}" | ||
self.date=date | ||
self.sets=[] | ||
for cat in cats: | ||
self.sets.append(make_set_str(cat)) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if not isinstance(other, Header): | ||
return False | ||
return ( | ||
self.id == other.id and | ||
self.date == other.date and | ||
self.sets == other.sets | ||
) | ||
|
||
class Record: #base record class | ||
def __init__(self, current_meta: Metadata): | ||
self.categories: List[Category]=[] | ||
if current_meta.abs_categories: | ||
for cat in current_meta.abs_categories.split(): | ||
self.categories.append(CATEGORIES[cat]) | ||
|
||
date= current_meta.updated if current_meta.updated else current_meta.created | ||
self.header = Header(current_meta.paper_id, date, self.categories) | ||
self.current_meta = current_meta | ||
|
||
#specialized record classes for the different supported metadata types | ||
class arXivRecord(Record): | ||
def __init__(self, current_meta: Metadata): | ||
super().__init__(current_meta) | ||
self.authors= parse_author_affil(current_meta.authors) | ||
|
||
class arXivRawRecord(Record): | ||
def __init__(self, metadata: List[Metadata]): | ||
self.versions: List[VersionEntry]=[] | ||
for version in metadata: | ||
entry= VersionEntry( | ||
version=version.version, | ||
raw='', | ||
submitted_date=version.created, | ||
size_kilobytes = version.source_size // 1024 if version.source_size else 0, | ||
source_flag=self._process_source_format(version.source_format, version.source_flags), | ||
is_current=version.is_current, | ||
source_format=version.source_format | ||
) | ||
self.versions.append(entry) | ||
if version.is_current: | ||
super().__init__(version) | ||
|
||
@staticmethod | ||
def _process_source_format(format: Optional[str], source_flags: Optional[str]) -> Optional[str]: | ||
"""oai excepts the source information to be in the form of flags for both our flag data and source type data""" | ||
format_map={ | ||
'pdftex' :'D', | ||
'tex':'', | ||
'pdf':'', | ||
'withdrawn': 'I', | ||
'html': 'H', | ||
'ps': 'P', | ||
'docx': 'X' | ||
} | ||
shown_flags=['A', 'S'] #not shown: 1, D (duplicates pdftex format sometimes) | ||
|
||
result="" | ||
if source_flags: | ||
for flag in shown_flags: | ||
if flag in source_flags: | ||
result+=flag | ||
result+=format_map.get(format,"") | ||
|
||
return result or None | ||
|
||
class dcRecord(Record): | ||
def __init__(self, metadata: List[Metadata]): | ||
for version in metadata: | ||
if version.is_current: | ||
super().__init__(version) | ||
self.current_version_date=version.created | ||
self.authors= parse_author_affil(version.authors) | ||
|
||
if version.version==1: | ||
self.initial_date=version.created | ||
|
||
def deduplicate_cat_names(self)-> List[str]: | ||
result=[] | ||
for cat in self.categories: | ||
if cat.full_name not in result: | ||
result.append(cat.full_name) | ||
return result | ||
|
||
class arXivOldRecord(Record): | ||
#no extra data | ||
def __init__(self, current_meta: Metadata): | ||
super().__init__(current_meta) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends "base.xml" %} | ||
{% import 'record_formats.xml' as formats %} | ||
|
||
{% block request_element %} | ||
{{ macros.request_element(query_params) }} | ||
{% endblock %} | ||
|
||
{% block interior_xml %} | ||
<GetRecord> | ||
{{formats.create_record(record, format)}} | ||
</GetRecord> | ||
{% endblock %} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
{% macro request_element( attributes={}) %} | ||
<request {% for key, value in attributes.items() %}{{ key }}="{{ value }}" {% endfor %}>{{ url_for("general.oai", _external=True) }}</request> | ||
<request{% for key, value in attributes.items() %} {{ key }}="{{ value }}"{% endfor %}>{{ url_for("general.oai", _external=True) }}</request> | ||
{% endmacro %} | ||
|
||
{% macro header(header) %} | ||
<header> | ||
<identifier>{{header.id}}</identifier> | ||
<datestamp>{{ header.date.strftime('%Y-%m-%d') }}</datestamp> | ||
{% for set in header.sets %} | ||
<setSpec>{{set}}</setSpec> | ||
{% endfor %} | ||
</header> | ||
{% endmacro %} |
Oops, something went wrong.