citeget.acquire_references¶
Tools for acquiring academic references as PDFs.
Given a structured reference (title, authors, year, URL, etc.), this module tries multiple strategies to obtain a PDF:
Direct URL download (if a URL is provided and yields a PDF)
ArXiv download (if URL is an arxiv link, get the PDF directly)
Libgen search with progressively adjusted query specificity
The query strategy for libgen searches:
Start with the full title (no punctuation) as a mid-specificity query
If too many results (>20) and no good match: add first author surname
If no results: try shorter title (first 5 words)
If still no results: try author + key title words
Module Attributes
no budget). |
|
Most bytes one |
|
The note on a reference refused because the job's byte budget was spent. |
Functions
|
Acquire files for a list of references. |
|
Try to acquire a single reference. |
|
Check which references already have downloaded files. |
Generate a sequence of search queries from most to least specific. |
|
|
Parse a reference string into a Reference object. |
|
Parse a references section into a list of Reference objects. |
|
Resolve the working directory for an acquisition session. |
|
Write a markdown file listing references that could not be acquired. |
|
Write a references.md with hyperlinks to local downloaded files. |
Classes
|
Result of trying to acquire a reference. |
|
A parsed academic reference. |
- class citeget.acquire_references.AcquisitionResult(reference, success=False, filepath=None, method='', queries_tried=list(), notes='')[source]¶
Bases:
objectResult of trying to acquire a reference.
- citeget.acquire_references.DEFAULT_MAX_JOB_BYTES = 536870912¶
Most bytes one
acquire_all_references()run may write into its download directory. The per-response ceiling (CITEGET_MAX_RESPONSE_BYTES) bounds one file; this bounds a job of many references, on a disk shared by every user of a hosted deployment (#22). Five full-size responses: a job of many books needs the variable raised, which is the point on a small shared server.
- citeget.acquire_references.JOB_BUDGET_NOTE = 'job byte budget reached'¶
The note on a reference refused because the job’s byte budget was spent.
- citeget.acquire_references.MAX_JOB_BYTES_ENV_VAR = 'CITEGET_MAX_JOB_BYTES'¶
no budget).
- Type:
Environment variable for the per-job byte budget (
0
- class citeget.acquire_references.Reference(number, raw, title='', authors='', year='', venue='', url='', doi='', source_text='')[source]¶
Bases:
objectA parsed academic reference.
- citeget.acquire_references.acquire_all_references(references, download_dir, *, work_dir=None, log_file=None, strategy=None, libgen_topics=('articles', 'books'), delay=2.0, verbose=True, convert_to_pdf=False, fetch_fallback=True, concurrency_key='auto', max_concurrent_jobs=None, max_job_bytes=None)[source]¶
Acquire files for a list of references.
Checks for already-downloaded files first and skips them, reporting which references are being skipped so the user can re-download by renaming or removing the existing file.
Each downloaded file is saved with an extension that matches its actual content (
.pdf,.epub,.mobi,.djvu, …). A non-PDF is never renamed to.pdf.- Parameters:
references (
list) – List of Reference objects.download_dir (
str|Path) – Where to save files (thereferences/subdirectory inside the work_dir, or a standalone directory).work_dir (
str|Path|None) – Optional work directory — if given,log_filedefaults to{work_dir}/{datetime}__acquisition_log.txt.log_file (
str|Path|None) – Explicit path for the acquisition log. If None andwork_diris given, auto-generated with timestamp.strategy – Acquisition strategy — a callable, registered name, or
Nonefor the legacy chain. Pass"default"to use the composable default fromciteget.resolve.libgen_topics (
tuple) – Libgen topics to try (legacy chain only).delay (
float) – Seconds between operations (rate limiting).verbose (
bool) – Print progress.convert_to_pdf (
bool) – WhenTrue, convert non-PDF downloads to PDF viapdfdol(Calibre’sebook-convert) when available. Prints a hint if the flag is on but the converter isn’t available. Defaults toFalse— native formats preserved.fetch_fallback (
bool) – If True (default), fall back to fetchingref.urlas Markdown when all academic strategies fail (catches non-paper references like blog posts, docs, product pages).concurrency_key (
Optional[str]) – Who this run counts against for the concurrent-run cap (seeciteget.joblimit)."auto"(default) uses the parent ofwork_dir, so runs whose work dirs sit side by side (a hosted app’s per-userjobs/directory) share one cap; with nowork_dirthere is no cap. Pass a string (a user id) to choose the key, orNoneto switch the cap off.max_concurrent_jobs (
Optional[int]) – The cap for that key. Defaults to theCITEGET_MAX_CONCURRENT_JOBSenvironment variable, elseciteget.joblimit.DEFAULT_MAX_CONCURRENT_JOBS;0means no cap.max_job_bytes (
Optional[int]) – Most bytes this run may add todownload_dir. Defaults to theCITEGET_MAX_JOB_BYTESenvironment variable, elseDEFAULT_MAX_JOB_BYTES;0means no budget. Once it is spent, the remaining references fail withJOB_BUDGET_NOTEinstead of being fetched. A reference whose download would overshoot it is refused the same way, its file is removed, and the budget counts as spent. Every file that appears indownload_dirwhile a reference is acquired counts, which over-counts (never under-counts) when another run shares the directory; files already there before the run do not count. A browser download’s temporary spool file lives outsidedownload_dirand is bounded by the per-response ceiling instead.
- Raises:
JobLimitExceeded – the key already has
max_concurrent_jobsruns in progress. Raised before any work.- Return type:
- Returns:
(successes, failures, log_entries) where successes and failures are lists of AcquisitionResult.
- citeget.acquire_references.acquire_reference(ref, download_dir, *, log_entries, strategy=None, libgen_topics=('articles', 'books'), timeout=30000, verbose=False, convert_to_pdf=False, fetch_fallback=True)[source]¶
Try to acquire a single reference.
When strategy is given, delegates entirely to the composable strategy system in
citeget.resolve. Otherwise falls back to the legacy hard-coded chain for backward compatibility.The legacy chain now searches all
libgen_topicssimultaneously in a single request (multi-topic search), rather than sequentially.If all academic strategies fail and fetch_fallback is
True(the default), and the reference has a URL, the page is fetched as Markdown viaciteget.fetch.fetch_one(). This catches non-paper references (blog posts, docs, product pages) that won’t appear in libgen/arxiv.- Parameters:
ref (
Reference) – The reference to acquire.log_entries (
list) – List to append log dicts to (mutated in place).strategy – An
AcquisitionStrategycallable, a registered strategy name (str), orNonefor the legacy chain. Pass"default"to use the new composable default.libgen_topics (
tuple) – Topics to search on libgen (searched simultaneously).timeout (
int) – Timeout for browser operations (legacy chain only).verbose (
bool) – Print diagnostic info on download attempts.convert_to_pdf (
bool) – WhenTrue, non-PDF downloads (EPUB, MOBI, DjVu, …) are converted to PDF viapdfdol(Calibre’sebook-convertunder the hood) when available. On conversion failure or when the converter is missing, the native format is kept — a non-PDF is never renamed to.pdf.fetch_fallback (
bool) – If True (default), fall back to fetchingref.urlas Markdown when all academic strategies fail.
- Return type:
- citeget.acquire_references.check_existing_downloads(references, download_dir)[source]¶
Check which references already have downloaded files.
- Return type:
- Returns:
(to_acquire, already_have) — two lists of Reference objects.
already_haveis a list of (Reference, filepath) tuples.
- citeget.acquire_references.generate_search_queries(ref)[source]¶
Generate a sequence of search queries from most to least specific.
Returns list of (query_string, description) tuples.
- Return type:
- citeget.acquire_references.parse_reference(text, number=0)[source]¶
Parse a reference string into a Reference object.
- Handles formats like:
[1] A. Author, “Title,” in Venue, year. URL A. Author, “Title,” Venue, vol. X, pp. Y-Z, year.
- Return type:
- citeget.acquire_references.parse_references_section(text)[source]¶
Parse a references section into a list of Reference objects.
Expects lines starting with [N] as reference entries.
- Return type:
- citeget.acquire_references.resolve_work_dir(reference_file=None, work_dir=None)[source]¶
Resolve the working directory for an acquisition session.
Rules:
If
work_diris given as a full path, use it (create if needed, parent must exist).If
work_diris a bare name (no slashes), use~/Downloads/{name}.If
work_diris None andreference_fileis given, derive from the reference file:{stem} -- acquired_references/in the same directory.If neither is given, raise ValueError.
- Return type:
- Returns:
Resolved Path to the working directory.
- Raises:
ValueError – If inputs are insufficient or invalid.
FileNotFoundError – If a specified parent directory doesn’t exist.