SciPost Code Repository

Skip to content
Snippets Groups Projects
services.py 2.43 KiB
Newer Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
Jorran de Wit's avatar
Jorran de Wit committed
__license__ = "AGPL v3"


# Module for making external api calls as needed in the submissions cycle
import feedparser
import logging

Jorran de Wit's avatar
Jorran de Wit committed
from profiles.models import Profile
Jorran de Wit's avatar
Jorran de Wit committed

Jorran de Wit's avatar
Jorran de Wit committed
from .models import ConflictOfInterest
Jorran de Wit's avatar
Jorran de Wit committed

logger = logging.getLogger('scipost.conflicts.arxiv')


class ArxivCaller:
Jorran de Wit's avatar
Jorran de Wit committed
    """
    ArXiv Caller will help retrieve author data from arXiv API.
    """
Jorran de Wit's avatar
Jorran de Wit committed

    query_base_url = 'https://export.arxiv.org/api/query?search_query={query}'

Jorran de Wit's avatar
Jorran de Wit committed
    def __init__(self):
        """
        Init ArXivCaller.
        """
        logger.info('Update COI from ArXiv')
Jorran de Wit's avatar
Jorran de Wit committed

    def _search_result_present(self, data):
        if len(data.get('entries', [])) > 0:
            return 'title' in data['entries'][0]
        return False

Jorran de Wit's avatar
Jorran de Wit committed
    def compare(self, author_profiles, relating_profiles, submission=None):
Jorran de Wit's avatar
Jorran de Wit committed
        """
        Compare two list of Profiles using the ArXiv API.
        """
Jorran de Wit's avatar
Jorran de Wit committed

        count = 0
Jorran de Wit's avatar
Jorran de Wit committed
        for profile in author_profiles:
            for rel_profile in relating_profiles:
                search_query = 'au:({profile}+AND+({rel_profile}))'.format(
                    profile=profile.last_name,
                    rel_profile=rel_profile.last_name,
                )
                queryurl = self.query_base_url.format(query=search_query)
                queryurl += '&sortBy=submittedDate&sortOrder=descending&max_results=5'
                queryurl = queryurl.replace(' ', '+')

                # Call the API
                response_content = feedparser.parse(queryurl)
                logger.info('GET [{profile}] [request] | {url}'.format(
                    profile=profile.last_name, url=queryurl))

                if self._search_result_present(response_content):

                    for conflict in response_content['entries']:
                        coi, new = ConflictOfInterest.objects.get_or_create(
                            header=conflict['title'],
                            url=conflict['link'].replace('http:', 'https:'),
                            profile=profile,
                            related_profile=rel_profile,
                            type='coauthor',
                        )
Jorran de Wit's avatar
Jorran de Wit committed
                        if submission:
                            coi.related_submissions.add(submission)
Jorran de Wit's avatar
Jorran de Wit committed
                        count += 1 if new else 0
                    logger.info('Found results | {response}.'.format(response=response_content))
        return count