<commit_before>import logging
import requests

from datetime import timedelta

from framework.celery_tasks import app as celery_app
from website import settings
from website import mails
from django.utils import timezone
from django.core.management.base import BaseCommand

import django
django.setup()

from osf.models import Preprint

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def check_crossref_dois(dry_run=True):
    """
    This script is to check for any DOI confirmation messages we may have missed during downtime and alert admins to any
    DOIs that have been pending for X number of days.
    :param dry_run:
    :return:
    """
    # Create one enormous url to check if all our pending crossref DOIs are good, then set all identifers
    preprints_with_pending_dois = Preprint.objects.filter(preprint_doi_created__isnull=True,
                                                      is_published=True)

    if not preprints_with_pending_dois:
        return

    pending_dois = []

    for preprint in preprints_with_pending_dois:
        prefix = preprint.provider.doi_prefix
        pending_dois.append('doi:{}'.format(settings.DOI_FORMAT.format(prefix=prefix, guid=preprint._id)))

    url = '{}works?filter={}'.format(settings.CROSSREF_JSON_API_URL, ','.join(pending_dois))

    try:
        resp = requests.get(url)
        resp.raise_for_status()
    except requests.exceptions.HTTPError as exc:
        logger.error('Could not contact crossref to check for DOIs, response returned with exception {}'.format(exc))
        raise exc

    preprints = resp.json()['message']['items']

    for preprint in preprints:
        guid = preprint['DOI'].split('/')[-1]
        pending_preprint = preprints_with_pending_dois.get(guids___id=guid)
        if not dry_run:
            pending_preprint.set_identifier_values(preprint['DOI'], save=True)
        else:<commit_msg>exclude preprints that have only been "stuck" a short time from unsticking process<commit_after>import logging
import requests

from datetime import timedelta

from framework.celery_tasks import app as celery_app
from website import settings
from website import mails
from django.utils import timezone
from django.core.management.base import BaseCommand

import django
django.setup()

from osf.models import Preprint

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def check_crossref_dois(dry_run=True):
    """
    This script is to check for any DOI confirmation messages we may have missed during downtime and alert admins to any
    DOIs that have been pending for X number of days.
    :param dry_run:
    :return:
    """
    # Create one enormous url to check if all our pending crossref DOIs are good, then set all identifers
    time_since_published = timedelta(days=settings.DAYS_CROSSREF_DOIS_MUST_BE_STUCK_BEFORE_EMAIL)

    preprints_with_pending_dois = Preprint.objects.filter(
        preprint_doi_created__isnull=True,
        is_published=True
    ).exclude(date_published__lt=timezone.now() - time_since_published)

    if not preprints_with_pending_dois:
        return

    pending_dois = []

    for preprint in preprints_with_pending_dois:
        prefix = preprint.provider.doi_prefix
        pending_dois.append('doi:{}'.format(settings.DOI_FORMAT.format(prefix=prefix, guid=preprint._id)))

    url = '{}works?filter={}'.format(settings.CROSSREF_JSON_API_URL, ','.join(pending_dois))

    try:
        resp = requests.get(url)
        resp.raise_for_status()
    except requests.exceptions.HTTPError as exc:
        logger.error('Could not contact crossref to check for DOIs, response returned with exception {}'.format(exc))
        raise exc

    preprints = resp.json()['message']['items']

    for preprint in preprints:
        guid = preprint['DOI'].split('/')[-1]
        pending_preprint = preprints_with_pending_dois.get(guids___id=guid)
        if not dry_run:
            pending_preprint.set_identifier_values(preprint['DOI'], save=True)
        else:
{'fraction_non_alphanumeric': 0.08197127290737989, 'fraction_numerical': 0.0004952947003467063, 'mean_word_length': 3.7129521586931156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3946133', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1164, 'n_words': 320}