staging
zocrates.discovery.staging ¶
Temporary download staging, SSRF validation, byte capping, and cleanup for candidate PDF papers.
DEFAULT_DOWNLOAD_TIMEOUT
module-attribute
¶
CandidatePaper
dataclass
¶
CandidatePaper(candidate_id, title, authors=list(), year=None, venue=None, doi=None, arxiv_id=None, openalex_id='', abstract=None, tldr=None, is_oa=False, oa_pdf_url=None, oa_pdf_urls=list(), citation_count=0, primary_topic=None, keywords=list(), in_library=False)
Represents a discovered scholarly paper candidate.
from_dict
classmethod
¶
Deserialize CandidatePaper from a dictionary.
Source code in zocrates/discovery/models.py
to_dict ¶
Serialize CandidatePaper to a plain dictionary.
Source code in zocrates/discovery/models.py
_stream_to_file
async
¶
Stream remote URL to local destination path with hop-by-hop HTTPS validation, byte capping, and magic byte checks.
Source code in zocrates/discovery/staging.py
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
cleanup_stale_staging_directories ¶
Remove staging subdirectories older than max_age_seconds (default: 2 hours).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age_seconds
|
int
|
Age threshold in seconds. |
7200
|
Returns:
| Type | Description |
|---|---|
int
|
Number of directories removed. |
Source code in zocrates/discovery/staging.py
download_and_stage_pdf
async
¶
Download and stage an Open Access PDF for a candidate paper.
Enforces
- SSRF Protection: strictly requires https:// URL scheme.
- Safe Filename: sanitizes candidate_id against directory traversal.
- Directory Containment: ensures target path is strictly within staging_dir.
- Magic Byte Verification: first 5 bytes must equal b"%PDF-".
- Streaming Byte Cap: aborts if cumulative bytes exceed MAX_PDF_BYTES (100 MB).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate
|
CandidatePaper
|
CandidatePaper instance. |
required |
staging_dir
|
Path
|
Target batch directory Path. |
required |
client
|
AsyncClient | None
|
Optional existing httpx.AsyncClient for connection reuse. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Absolute file path string on success, or None on failure/non-PDF. |
Source code in zocrates/discovery/staging.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
extract_pdf_from_link_header ¶
Extract a direct PDF URL from HTTP Link header (Signposting RFC 8288).
Source code in zocrates/discovery/staging.py
extract_pdf_url_from_html ¶
Extract a direct PDF URL from an HTML landing page using standard scholarly metadata tags (Highwire/Google Scholar, Open Access Signposting, EPrints, and DSpace).
Source code in zocrates/discovery/staging.py
get_staging_root ¶
Return the root directory for temporary discovery batch staging.
Prefers
Source code in zocrates/discovery/staging.py
resolve_dspace7_pdf
async
¶
Resolve a direct bitstream PDF URL from a modern DSpace 7 Angular SPA landing page.
DSpace 7 returns a lightweight JavaScript SPA shell (