feat: get_abstract_from_elsevier 可自动触发自己
This commit is contained in:
parent
a11dce1358
commit
5529f97ab8
|
|
@ -11,6 +11,7 @@ import requests
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
import os
|
import os
|
||||||
|
from celery import current_app
|
||||||
|
|
||||||
config.email = "caoqianming@foxmail.com"
|
config.email = "caoqianming@foxmail.com"
|
||||||
config.max_retries = 0
|
config.max_retries = 0
|
||||||
|
|
@ -61,11 +62,11 @@ def get_paper_meta_from_openalex(publication_year:int, search_key:str):
|
||||||
|
|
||||||
ELSEVIER_APIKEY = 'aa8868cac9e27d6153ab0a0acd7b50bf'
|
ELSEVIER_APIKEY = 'aa8868cac9e27d6153ab0a0acd7b50bf'
|
||||||
@shared_task(base=CustomTask)
|
@shared_task(base=CustomTask)
|
||||||
def get_abstract_from_elsevier(publication_year: int, number_of_task:int = 100):
|
def get_abstract_from_elsevier(publication_year: int = None, number_of_task:int = 100):
|
||||||
qs = Paper.objects.filter(
|
qs = Paper.objects.filter(has_abstract=True)
|
||||||
publication_year=publication_year,
|
if publication_year is not None:
|
||||||
has_abstract=False
|
qs = qs.filter(publication_year=publication_year)
|
||||||
).exclude(
|
qs = qs.exclude(
|
||||||
fail_reason="elsevier_doi_not_found"
|
fail_reason="elsevier_doi_not_found"
|
||||||
).order_by("publication_date")
|
).order_by("publication_date")
|
||||||
|
|
||||||
|
|
@ -77,8 +78,12 @@ def get_abstract_from_elsevier(publication_year: int, number_of_task:int = 100):
|
||||||
"httpAccept": "text/xml"
|
"httpAccept": "text/xml"
|
||||||
}
|
}
|
||||||
err_msg = ""
|
err_msg = ""
|
||||||
req = requests.Session()
|
if number_of_task is None:
|
||||||
for paper in qs[:number_of_task]:
|
papers = qs.all()
|
||||||
|
else:
|
||||||
|
papers = qs[:number_of_task]
|
||||||
|
with requests.Session() as req:
|
||||||
|
for paper in papers:
|
||||||
try:
|
try:
|
||||||
res = req.get(
|
res = req.get(
|
||||||
f"https://api.elsevier.com/content/article/doi/{paper.doi}",
|
f"https://api.elsevier.com/content/article/doi/{paper.doi}",
|
||||||
|
|
@ -111,12 +116,14 @@ def get_abstract_from_elsevier(publication_year: int, number_of_task:int = 100):
|
||||||
)
|
)
|
||||||
paper.has_abstract = True
|
paper.has_abstract = True
|
||||||
paper.has_abstract_xml = True
|
paper.has_abstract_xml = True
|
||||||
|
paper.fetch_status = "abstract_ready"
|
||||||
|
|
||||||
paras = root.xpath("//ce:para", namespaces=ns)
|
paras = root.xpath("//ce:para", namespaces=ns)
|
||||||
has_fulltext = len(paras) > 0
|
has_fulltext = len(paras) > 0
|
||||||
if has_fulltext:
|
if has_fulltext:
|
||||||
paper.has_fulltext = True
|
paper.has_fulltext = True
|
||||||
paper.has_fulltext_xml = True
|
paper.has_fulltext_xml = True
|
||||||
|
paper.fetch_status = "fulltext_ready"
|
||||||
|
|
||||||
publication_date = paper.publication_date
|
publication_date = paper.publication_date
|
||||||
paper_dir = os.path.join(
|
paper_dir = os.path.join(
|
||||||
|
|
@ -143,4 +150,12 @@ def get_abstract_from_elsevier(publication_year: int, number_of_task:int = 100):
|
||||||
if remaining_count == 0:
|
if remaining_count == 0:
|
||||||
return "done"
|
return "done"
|
||||||
else:
|
else:
|
||||||
|
current_app.send_task(
|
||||||
|
"apps.resm.tasks.get_abstract_from_elsevier",
|
||||||
|
kwargs={
|
||||||
|
"publication_year": publication_year,
|
||||||
|
"number_of_task": number_of_task,
|
||||||
|
},
|
||||||
|
countdown=5,
|
||||||
|
)
|
||||||
return f'{err_msg}, remaining {remaining_count} papers'
|
return f'{err_msg}, remaining {remaining_count} papers'
|
||||||
Loading…
Reference in New Issue