-
Notifications
You must be signed in to change notification settings - Fork 23
/
tasks.py
329 lines (275 loc) · 10.1 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import codecs
import json
import os
import re
import shutil
from datetime import datetime
from io import BytesIO
from subprocess import PIPE, run
import fitz
import requests
from bs4 import BeautifulSoup
from celery.utils.log import get_task_logger
from django.apps import apps
from django.core.cache import cache
from django.core.files import File
from django.core.files.base import ContentFile
from PIL import Image
from pytz import timezone as pytz_tz
from paper.utils import (
check_crossref_title,
check_pdf_title,
get_cache_key,
get_crossref_results,
get_csl_item,
get_pdf_from_url,
get_pdf_location_for_csl_item,
)
from researchhub.celery import QUEUE_CERMINE, QUEUE_PAPER_MISC, app
from researchhub.settings import APP_ENV
from utils import sentry
from utils.http import check_url_contains_pdf
logger = get_task_logger(__name__)
@app.task(queue=QUEUE_PAPER_MISC)
def censored_paper_cleanup(paper_id):
Paper = apps.get_model("paper.Paper")
paper = Paper.objects.filter(id=paper_id).first()
if not paper.is_removed:
paper.is_removed = True
paper.save()
if paper:
paper.votes.update(is_removed=True)
for vote in paper.votes.all():
if vote.vote_type == 1:
user = vote.created_by
uploaded_by = paper.uploaded_by
uploaded_by.set_probable_spammer()
@app.task(queue=QUEUE_PAPER_MISC)
def download_pdf(paper_id, retry=0):
if retry > 3:
return
Paper = apps.get_model("paper.Paper")
paper = Paper.objects.get(id=paper_id)
paper_pdf_url = paper.pdf_url
paper_url = paper.url
paper_url_contains_pdf = check_url_contains_pdf(paper_url)
pdf_url_contains_pdf = check_url_contains_pdf(paper_pdf_url)
if pdf_url_contains_pdf or paper_url_contains_pdf:
pdf_url = paper_pdf_url or paper_url
try:
pdf = get_pdf_from_url(pdf_url)
filename = pdf_url.split("/").pop()
if not filename.endswith(".pdf"):
filename += ".pdf"
paper.file.save(filename, pdf)
paper.save(update_fields=["file"])
paper.extract_pdf_preview(use_celery=True)
paper.set_paper_completeness()
paper.compress_and_linearize_file()
celery_extract_pdf_sections.apply_async(
(paper_id,), priority=5, countdown=15
)
return True
except Exception as e:
sentry.log_info(e)
download_pdf.apply_async(
(paper.id, retry + 1), priority=7, countdown=15 * (retry + 1)
)
return False
csl_item = None
pdf_url = None
if paper_url:
csl_item = get_csl_item(paper_url)
elif paper_pdf_url:
csl_item = get_csl_item(paper_pdf_url)
if csl_item:
oa_result = get_pdf_location_for_csl_item(csl_item)
if oa_result:
oa_url_1 = oa_result.get("url", None)
oa_url_2 = oa_result.get("url_for_pdf", None)
oa_pdf_url = oa_url_1 or oa_url_2
pdf_url = oa_pdf_url
pdf_url_contains_pdf = check_url_contains_pdf(pdf_url)
if pdf_url_contains_pdf:
paper.pdf_url = pdf_url
paper.save()
download_pdf.apply_async(
(paper.id, retry + 1), priority=7, countdown=15 * (retry + 1)
)
return
return False
@app.task(queue=QUEUE_PAPER_MISC)
def celery_extract_pdf_preview(paper_id, retry=0):
if paper_id is None or retry > 2:
print("No paper id for pdf preview")
return False
print(f"Extracting pdf figures for paper: {paper_id}")
Paper = apps.get_model("paper.Paper")
Figure = apps.get_model("paper.Figure")
paper = Paper.objects.get(id=paper_id)
file = paper.file
if not file:
print(f"No file exists for paper: {paper_id}")
celery_extract_pdf_preview.apply_async(
(paper.id, retry + 1),
priority=6,
countdown=10,
)
return False
file_url = file.url
try:
res = requests.get(file_url)
doc = fitz.open(stream=res.content, filetype="pdf")
extracted_figures = Figure.objects.filter(paper=paper)
for page in doc:
pix = page.get_pixmap(alpha=False)
output_filename = f"{paper_id}-{page.number}.png"
if not extracted_figures.filter(
file__contains=output_filename, figure_type=Figure.PREVIEW
):
img_buffer = BytesIO()
img_buffer.write(pix.pil_tobytes(format="PNG"))
image = Image.open(img_buffer)
image.save(img_buffer, "png", quality=0)
file = ContentFile(img_buffer.getvalue(), name=output_filename)
Figure.objects.create(
file=file, paper=paper, figure_type=Figure.PREVIEW
)
except Exception as e:
sentry.log_error(e)
finally:
cache_key = get_cache_key("figure", paper_id)
cache.delete(cache_key)
return True
@app.task(queue=QUEUE_PAPER_MISC)
def celery_extract_meta_data(paper_id, title, check_title):
if paper_id is None:
return
Paper = apps.get_model("paper.Paper")
date_format = "%Y-%m-%dT%H:%M:%SZ"
paper = Paper.objects.get(id=paper_id)
if check_title:
has_title = check_pdf_title(title, paper.file)
if not has_title:
return
best_matching_result = get_crossref_results(title, index=1)[0]
try:
if "title" in best_matching_result:
crossref_title = best_matching_result.get("title", [""])[0]
else:
crossref_title = best_matching_result.get("container-title", [""])
crossref_title = crossref_title[0]
similar_title = check_crossref_title(title, crossref_title)
if not similar_title:
return
if not paper.doi:
doi = best_matching_result.get("DOI", paper.doi)
paper.doi = doi
url = best_matching_result.get("URL", None)
publish_date = best_matching_result["created"]["date-time"]
publish_date = datetime.strptime(publish_date, date_format).date()
tagline = best_matching_result.get("abstract", "")
tagline = re.sub(r"<[^<]+>", "", tagline) # Removing any jat xml tags
paper.url = url
paper.paper_publish_date = publish_date
if not paper.tagline:
paper.tagline = tagline
paper_cache_key = get_cache_key("paper", paper_id)
cache.delete(paper_cache_key)
paper.check_doi()
paper.save()
except Exception as e:
sentry.log_info(e)
@app.task(queue=QUEUE_CERMINE)
def celery_extract_pdf_sections(paper_id):
if paper_id is None:
return False, "No Paper Id"
Paper = apps.get_model("paper.Paper")
Figure = apps.get_model("paper.Figure")
paper = Paper.objects.get(id=paper_id)
file = paper.file
if not file:
return False, "No Paper File"
path = f"/tmp/pdf_cermine/{paper_id}/"
filename = f"{paper_id}.pdf"
extract_filename = f"{paper_id}.html"
file_path = f"{path}{filename}"
extract_file_path = f"{path}{paper_id}.cermxml"
images_path = f"{path}{paper_id}.images"
file_url = file.url
return_code = -1
if not os.path.isdir(path):
os.mkdir(path)
try:
res = requests.get(file_url)
with open(file_path, "wb+") as f:
f.write(res.content)
args = [
"java",
"-cp",
"cermine-impl-1.13-jar-with-dependencies.jar",
"pl.edu.icm.cermine.ContentExtractor",
"-path",
path,
]
call_res = run(args, stdout=PIPE, stderr=PIPE)
return_code = call_res.returncode
with codecs.open(extract_file_path, "rb") as f:
soup = BeautifulSoup(f, "lxml")
paper.pdf_file_extract.save(extract_filename, ContentFile(soup.encode()))
paper.save()
figures = os.listdir(images_path)
for extracted_figure in figures:
extracted_figure_path = f"{images_path}/{extracted_figure}"
with open(extracted_figure_path, "rb") as f:
extracted_figures = Figure.objects.filter(paper=paper)
split_file_name = f.name.split("/")
file_name = split_file_name[-1]
if not extracted_figures.filter(
file__contains=file_name, figure_type=Figure.FIGURE
):
Figure.objects.create(
file=File(f, name=file_name),
paper=paper,
figure_type=Figure.FIGURE,
)
except Exception as e:
stdout = call_res.stdout.decode("utf8")
message = f"{return_code}; {stdout}; "
try:
message += str(os.listdir(path))
except Exception as e:
message += str(os.listdir("/tmp/pdf_cermine")) + str(e)
sentry.log_error(e, message=message)
return False, return_code
finally:
shutil.rmtree(path)
return True, return_code
@app.task
def log_daily_uploads():
from analytics.amplitude import Amplitude
Paper = apps.get_model("paper.Paper")
amp = Amplitude()
url = amp.api_url
key = amp.api_key
today = datetime.now(tz=pytz_tz("US/Pacific"))
start_date = today.replace(hour=0, minute=0, second=0)
end_date = today.replace(hour=23, minute=59, second=59)
papers = Paper.objects.filter(
created_date__gte=start_date,
created_date__lte=end_date,
uploaded_by__isnull=True,
)
paper_count = papers.count()
data = {
"device_id": f"rh_{APP_ENV}",
"event_type": "daily_autopull_count",
"time": int(today.timestamp()),
"insert_id": f"daily_autopull_{today.strftime('%Y-%m-%d')}",
"event_properties": {"amount": paper_count},
}
hit = {"events": [data], "api_key": key}
hit = json.dumps(hit)
headers = {"Content-Type": "application/json", "Accept": "*/*"}
request = requests.post(url, data=hit, headers=headers)
return request.status_code, paper_count