litloop 1.18.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. litloop/__init__.py +89 -0
  2. litloop/__main__.py +6 -0
  3. litloop/_source_hygiene.py +158 -0
  4. litloop/agents/__init__.py +13 -0
  5. litloop/agents/client.py +164 -0
  6. litloop/agents/runner.py +123 -0
  7. litloop/agents/system.py +54 -0
  8. litloop/agents/tools.py +142 -0
  9. litloop/app_server.py +8 -0
  10. litloop/browser/__init__.py +1 -0
  11. litloop/browser/backend.py +1009 -0
  12. litloop/browser/camofox_api.py +368 -0
  13. litloop/browser/chrome_copy.py +136 -0
  14. litloop/browser/cloakbrowser_compat.py +72 -0
  15. litloop/browser/cookies.py +347 -0
  16. litloop/browser/engine.py +965 -0
  17. litloop/browser/login.py +636 -0
  18. litloop/browser/pdf.py +263 -0
  19. litloop/browser/profile_health.py +167 -0
  20. litloop/browser/runtime.py +210 -0
  21. litloop/cache.py +104 -0
  22. litloop/citations/__init__.py +1 -0
  23. litloop/citations/bib_import.py +102 -0
  24. litloop/citations/bibtex_crossref.py +143 -0
  25. litloop/citations/formats.py +225 -0
  26. litloop/citations/paper_list.py +200 -0
  27. litloop/citations/session_export.py +94 -0
  28. litloop/cli/__init__.py +12 -0
  29. litloop/cli/_utils.py +267 -0
  30. litloop/cli/commands.py +1098 -0
  31. litloop/cli/doctor.py +141 -0
  32. litloop/cli/main.py +1173 -0
  33. litloop/cli/web.py +190 -0
  34. litloop/config.py +413 -0
  35. litloop/data/VARCON-LICENSE.txt +62 -0
  36. litloop/data/elements.tsv +119 -0
  37. litloop/data/institutional_identity_policy.json +165 -0
  38. litloop/data/publisher_access_catalog.json +969 -0
  39. litloop/data/publisher_carsi.json +105 -0
  40. litloop/data/spelling_search.tsv.gz +0 -0
  41. litloop/data/spelling_us.tsv.gz +0 -0
  42. litloop/data/webvpn.dat +0 -0
  43. litloop/data/webvpn.json +603 -0
  44. litloop/db.py +1922 -0
  45. litloop/deps.py +128 -0
  46. litloop/desktop/__init__.py +1 -0
  47. litloop/desktop/agent_runner.py +537 -0
  48. litloop/desktop/download_jobs.py +571 -0
  49. litloop/desktop/error_text.py +192 -0
  50. litloop/desktop/server.py +999 -0
  51. litloop/desktop/settings.py +552 -0
  52. litloop/desktop/translate.py +103 -0
  53. litloop/diagnostics.py +126 -0
  54. litloop/download/__init__.py +1 -0
  55. litloop/download/doi_check.py +44 -0
  56. litloop/download/fetcher.py +975 -0
  57. litloop/download/markdown.py +130 -0
  58. litloop/download/pdf.py +454 -0
  59. litloop/download/pipeline.py +1166 -0
  60. litloop/download/progress.py +312 -0
  61. litloop/download/rename.py +211 -0
  62. litloop/extractors/__init__.py +0 -0
  63. litloop/extractors/html_extractor.py +31 -0
  64. litloop/extractors/pdf_extractor.py +151 -0
  65. litloop/extractors/publisher_adapters/__init__.py +0 -0
  66. litloop/extractors/publisher_adapters/acs.py +167 -0
  67. litloop/extractors/publisher_adapters/elsevier.py +178 -0
  68. litloop/extractors/publisher_adapters/generic.py +191 -0
  69. litloop/extractors/publisher_adapters/nature.py +204 -0
  70. litloop/extractors/publisher_adapters/rsc.py +154 -0
  71. litloop/extractors/publisher_adapters/tandfonline.py +154 -0
  72. litloop/extractors/publisher_adapters/wiley.py +160 -0
  73. litloop/http_utils.py +57 -0
  74. litloop/identifiers.py +134 -0
  75. litloop/institutional/__init__.py +1 -0
  76. litloop/institutional/auth.py +576 -0
  77. litloop/institutional/carsi_login.py +569 -0
  78. litloop/institutional/config_adapter.py +63 -0
  79. litloop/institutional/proxy_check.py +98 -0
  80. litloop/institutional/remote_assist.py +274 -0
  81. litloop/institutional/schools.py +102 -0
  82. litloop/institutional/session_heal.py +73 -0
  83. litloop/institutional/session_store.py +105 -0
  84. litloop/log.py +47 -0
  85. litloop/models.py +269 -0
  86. litloop/network.py +417 -0
  87. litloop/optional.py +310 -0
  88. litloop/publishers/__init__.py +1 -0
  89. litloop/publishers/access.py +232 -0
  90. litloop/publishers/pdf_router.py +451 -0
  91. litloop/publishers/profiles.py +899 -0
  92. litloop/publishers/strategies/__init__.py +122 -0
  93. litloop/publishers/strategies/_core.py +2340 -0
  94. litloop/publishers/strategies/_elsevier_api.py +578 -0
  95. litloop/publishers/strategies/acm.py +27 -0
  96. litloop/publishers/strategies/acs.py +42 -0
  97. litloop/publishers/strategies/aip.py +27 -0
  98. litloop/publishers/strategies/aps.py +29 -0
  99. litloop/publishers/strategies/asce.py +28 -0
  100. litloop/publishers/strategies/base.py +108 -0
  101. litloop/publishers/strategies/copernicus.py +24 -0
  102. litloop/publishers/strategies/elsevier.py +66 -0
  103. litloop/publishers/strategies/generic.py +39 -0
  104. litloop/publishers/strategies/ieee.py +28 -0
  105. litloop/publishers/strategies/iop.py +27 -0
  106. litloop/publishers/strategies/nature.py +27 -0
  107. litloop/publishers/strategies/oxford.py +27 -0
  108. litloop/publishers/strategies/registry.py +83 -0
  109. litloop/publishers/strategies/royalsociety.py +28 -0
  110. litloop/publishers/strategies/rsc.py +27 -0
  111. litloop/publishers/strategies/sage.py +30 -0
  112. litloop/publishers/strategies/science.py +27 -0
  113. litloop/publishers/strategies/springer.py +34 -0
  114. litloop/publishers/strategies/tandfonline.py +32 -0
  115. litloop/publishers/strategies/wiley.py +32 -0
  116. litloop/publishers/supplementary.py +219 -0
  117. litloop/quota.py +202 -0
  118. litloop/search/__init__.py +1 -0
  119. litloop/search/abstracts.py +291 -0
  120. litloop/search/deep.py +1216 -0
  121. litloop/search/quick.py +1102 -0
  122. litloop/search/scansci_find.py +410 -0
  123. litloop/search/screening.py +225 -0
  124. litloop/search/title_to_doi.py +163 -0
  125. litloop/search/watches.py +124 -0
  126. litloop/server.py +1731 -0
  127. litloop/sources/__init__.py +1339 -0
  128. litloop/sources/_batch.py +529 -0
  129. litloop/sources/_index.py +221 -0
  130. litloop/sources/_routes.py +290 -0
  131. litloop/sources/altcha.py +90 -0
  132. litloop/sources/arxiv.py +45 -0
  133. litloop/sources/attempt.py +91 -0
  134. litloop/sources/carsi.py +74 -0
  135. litloop/sources/core_api.py +46 -0
  136. litloop/sources/crossref.py +162 -0
  137. litloop/sources/elsevier_api.py +620 -0
  138. litloop/sources/elsevier_check.py +274 -0
  139. litloop/sources/europepmc.py +81 -0
  140. litloop/sources/ezproxy.py +317 -0
  141. litloop/sources/instsci.py +1295 -0
  142. litloop/sources/libgen.py +116 -0
  143. litloop/sources/nature.py +39 -0
  144. litloop/sources/oa_discovery.py +135 -0
  145. litloop/sources/openaire.py +122 -0
  146. litloop/sources/openalex.py +100 -0
  147. litloop/sources/publishers.py +684 -0
  148. litloop/sources/sage_cn.py +139 -0
  149. litloop/sources/scibban.py +10 -0
  150. litloop/sources/scihub.py +1379 -0
  151. litloop/sources/scihub_domains.py +203 -0
  152. litloop/sources/scihub_speed.py +187 -0
  153. litloop/sources/scoring.py +250 -0
  154. litloop/sources/semantic_scholar.py +146 -0
  155. litloop/sources/springer_tdm.py +172 -0
  156. litloop/sources/unpaywall.py +116 -0
  157. litloop/sources/vpnsci.py +38 -0
  158. litloop/store.py +264 -0
  159. litloop/templates/index.html +347 -0
  160. litloop/terms/__init__.py +1 -0
  161. litloop/terms/glossary.py +422 -0
  162. litloop/terms/spelling.py +89 -0
  163. litloop/terms/text.py +106 -0
  164. litloop/tor/__init__.py +1 -0
  165. litloop/tor/embedded.py +371 -0
  166. litloop/tor/proxy.py +110 -0
  167. litloop/updates.py +148 -0
  168. litloop/zotero/__init__.py +1 -0
  169. litloop/zotero/bridge.py +309 -0
  170. litloop/zotero/client.py +317 -0
  171. litloop-1.18.3.dist-info/METADATA +347 -0
  172. litloop-1.18.3.dist-info/RECORD +176 -0
  173. litloop-1.18.3.dist-info/WHEEL +5 -0
  174. litloop-1.18.3.dist-info/entry_points.txt +2 -0
  175. litloop-1.18.3.dist-info/licenses/LICENSE +197 -0
  176. litloop-1.18.3.dist-info/top_level.txt +1 -0
litloop/cache.py ADDED
@@ -0,0 +1,104 @@
1
+ """Cache management for LitLoop."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import json
7
+ import time
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+
12
+ def cache_key(identifier: str) -> str:
13
+ return hashlib.sha256(identifier.lower().encode("utf-8")).hexdigest()[:24]
14
+
15
+
16
+ def cache_path(identifier: str, config: dict[str, Any]) -> Path:
17
+ return Path(config["cache_dir"]) / f"{cache_key(identifier)}.json"
18
+
19
+
20
+ def cache_get(identifier: str, config: dict[str, Any]) -> dict[str, Any] | None:
21
+ path = cache_path(identifier, config)
22
+ if not path.exists():
23
+ return None
24
+ ttl = float(config.get("cache_ttl_hours", 168)) * 3600
25
+ if ttl > 0 and time.time() - path.stat().st_mtime > ttl:
26
+ return None
27
+ try:
28
+ with path.open("r", encoding="utf-8") as fh:
29
+ return json.load(fh)
30
+ except Exception:
31
+ return None
32
+
33
+
34
+ def cache_set(identifier: str, result: dict[str, Any], config: dict[str, Any]) -> None:
35
+ cache_dir = Path(config["cache_dir"])
36
+ cache_dir.mkdir(parents=True, exist_ok=True)
37
+ target = cache_path(identifier, config)
38
+ tmp = target.with_suffix(".tmp")
39
+ try:
40
+ with tmp.open("w", encoding="utf-8") as fh:
41
+ json.dump(result, fh, indent=2, ensure_ascii=False)
42
+ tmp.replace(target)
43
+ except Exception:
44
+ tmp.unlink(missing_ok=True)
45
+ raise
46
+
47
+
48
+ def cache_clear(identifier: str | None, config: dict[str, Any]) -> int:
49
+ cache_dir = Path(config["cache_dir"])
50
+ if not cache_dir.exists():
51
+ return 0
52
+ if identifier:
53
+ path = cache_path(identifier, config)
54
+ if path.exists():
55
+ path.unlink()
56
+ return 1
57
+ return 0
58
+ count = 0
59
+ for f in cache_dir.glob("*.json"):
60
+ f.unlink()
61
+ count += 1
62
+ return count
63
+
64
+
65
+ def search_cache_key(payload: dict[str, Any]) -> str:
66
+ raw = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=str)
67
+ return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:24]
68
+
69
+
70
+ def search_cache_path(payload: dict[str, Any], config: dict[str, Any]) -> Path:
71
+ return Path(config["cache_dir"]) / f"search_{search_cache_key(payload)}.json"
72
+
73
+
74
+ def search_cache_get(payload: dict[str, Any], config: dict[str, Any]) -> dict[str, Any] | None:
75
+ ttl_hours = float(config.get("search_cache_ttl_hours", 168))
76
+ if ttl_hours <= 0:
77
+ return None
78
+ path = search_cache_path(payload, config)
79
+ if not path.exists():
80
+ return None
81
+ if time.time() - path.stat().st_mtime > ttl_hours * 3600:
82
+ return None
83
+ try:
84
+ with path.open("r", encoding="utf-8") as fh:
85
+ data = json.load(fh)
86
+ return data if isinstance(data, dict) else None
87
+ except Exception:
88
+ return None
89
+
90
+
91
+ def search_cache_set(payload: dict[str, Any], data: dict[str, Any], config: dict[str, Any]) -> None:
92
+ if float(config.get("search_cache_ttl_hours", 168)) <= 0:
93
+ return
94
+ cache_dir = Path(config["cache_dir"])
95
+ cache_dir.mkdir(parents=True, exist_ok=True)
96
+ target = search_cache_path(payload, config)
97
+ tmp = target.with_name(f".{target.name}.{hash(target) & 0xFFFF:x}.tmp")
98
+ try:
99
+ with tmp.open("w", encoding="utf-8", newline="\n") as fh:
100
+ json.dump(data, fh, indent=2, ensure_ascii=False)
101
+ tmp.replace(target)
102
+ except Exception:
103
+ tmp.unlink(missing_ok=True)
104
+ raise
@@ -0,0 +1 @@
1
+ """Citation formats, BibTeX parsing, paper lists, and session export."""
@@ -0,0 +1,102 @@
1
+ """BibTeX file parser for bulk import."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+
10
+ def parse_bib_file(file_path: str | Path) -> list[dict[str, str]]:
11
+ """Parse a .bib file and extract entries with DOIs.
12
+
13
+ Returns list of dicts with keys: cite_key, doi, title, authors, year
14
+ """
15
+ path = Path(file_path)
16
+ if not path.exists():
17
+ return []
18
+
19
+ try:
20
+ content = path.read_text(encoding="utf-8")
21
+ except UnicodeDecodeError:
22
+ content = path.read_text(encoding="latin-1")
23
+
24
+ return parse_bib_text(content)
25
+
26
+
27
+ def parse_bib_text(content: str, *, require_doi: bool = True) -> list[dict[str, str]]:
28
+ """Parse BibTeX text and extract entries.
29
+
30
+ Args:
31
+ content: BibTeX text content
32
+ require_doi: If True (default), only return entries with DOI.
33
+ If False, return all entries.
34
+ """
35
+ entries = []
36
+ # Find entry starts: @type{key,
37
+ entry_start = re.compile(r"@(\w+)\s*\{([^,]+),", re.IGNORECASE)
38
+
39
+ pos = 0
40
+ while pos < len(content):
41
+ m = entry_start.search(content, pos)
42
+ if not m:
43
+ break
44
+
45
+ entry_type = m.group(1).lower()
46
+ cite_key = m.group(2).strip()
47
+ # Find matching closing brace
48
+ brace_start = m.end()
49
+ depth = 1
50
+ i = brace_start
51
+ while i < len(content) and depth > 0:
52
+ if content[i] == '{':
53
+ depth += 1
54
+ elif content[i] == '}':
55
+ depth -= 1
56
+ i += 1
57
+ body = content[brace_start:i - 1]
58
+
59
+ entry = {"cite_key": cite_key, "type": entry_type}
60
+
61
+ # Extract fields: key = {value} or key = "value" (handles nested braces)
62
+ field_re = re.compile(r"(\w+)\s*=\s*", re.IGNORECASE)
63
+ fpos = 0
64
+ while fpos < len(body):
65
+ fm = field_re.search(body, fpos)
66
+ if not fm:
67
+ break
68
+ key = fm.group(1).lower()
69
+ val_start = fm.end()
70
+ if val_start < len(body) and body[val_start] == '{':
71
+ depth = 1
72
+ j = val_start + 1
73
+ while j < len(body) and depth > 0:
74
+ if body[j] == '{':
75
+ depth += 1
76
+ elif body[j] == '}':
77
+ depth -= 1
78
+ j += 1
79
+ value = body[val_start + 1:j - 1]
80
+ fpos = j
81
+ elif val_start < len(body) and body[val_start] == '"':
82
+ end_q = body.find('"', val_start + 1)
83
+ if end_q == -1:
84
+ break
85
+ value = body[val_start + 1:end_q]
86
+ fpos = end_q + 1
87
+ else:
88
+ m_num = re.match(r"[^,}\s]+", body[val_start:])
89
+ if m_num:
90
+ value = m_num.group(0)
91
+ fpos = val_start + m_num.end()
92
+ else:
93
+ break
94
+ entry[key] = value.strip()
95
+
96
+ if not require_doi or entry.get("doi"):
97
+ entries.append(entry)
98
+
99
+ pos = i
100
+
101
+ return entries
102
+
@@ -0,0 +1,143 @@
1
+ """BibTeX generation from Crossref metadata."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ import urllib.parse
7
+ from typing import Any
8
+
9
+ from ..network import fetch_json
10
+
11
+
12
+ def fetch_bibtex(doi: str, config: dict[str, Any]) -> str | None:
13
+ """Fetch BibTeX entry for a DOI from Crossref."""
14
+ q = urllib.parse.quote(doi, safe="")
15
+ url = f"https://api.crossref.org/works/{q}"
16
+ try:
17
+ payload = fetch_json(url, config, headers={"Accept": "application/json"})
18
+ if not payload:
19
+ return None
20
+ msg = payload.get("message", {})
21
+ return _crossref_to_bibtex(msg, doi)
22
+ except Exception:
23
+ return None
24
+
25
+
26
+ def _crossref_to_bibtex(msg: dict[str, Any], doi: str) -> str | None:
27
+ entry_type = _detect_entry_type(msg.get("type", ""))
28
+ if not entry_type:
29
+ entry_type = "article"
30
+
31
+ cite_key = _make_cite_key(msg, doi)
32
+ fields: dict[str, str] = {}
33
+
34
+ # Title
35
+ titles = msg.get("title", [])
36
+ if titles:
37
+ fields["title"] = _clean(titles[0])
38
+
39
+ # Authors
40
+ authors = msg.get("author", [])
41
+ if authors:
42
+ names = []
43
+ for a in authors:
44
+ family = a.get("family", "")
45
+ given = a.get("given", "")
46
+ if family and given:
47
+ names.append(f"{family}, {given}")
48
+ elif family:
49
+ names.append(family)
50
+ if names:
51
+ fields["author"] = " and ".join(names)
52
+
53
+ # Journal/Booktitle
54
+ container = msg.get("container-title", [])
55
+ if container:
56
+ fields["journal"] = _clean(container[0])
57
+
58
+ # Year
59
+ parts = msg.get("published-print", msg.get("published-online", msg.get("created", {})))
60
+ date_parts = parts.get("date-parts", [[]]) if isinstance(parts, dict) else [[]]
61
+ if date_parts and date_parts[0]:
62
+ fields["year"] = str(date_parts[0][0])
63
+
64
+ # Volume, Issue, Pages
65
+ if msg.get("volume"):
66
+ fields["volume"] = str(msg["volume"])
67
+ if msg.get("issue"):
68
+ fields["number"] = str(msg["issue"])
69
+ if msg.get("page"):
70
+ fields["pages"] = msg["page"].replace("-", "--")
71
+
72
+ # DOI
73
+ fields["doi"] = doi
74
+
75
+ # URL
76
+ fields["url"] = f"https://doi.org/{doi}"
77
+
78
+ # Publisher
79
+ if msg.get("publisher"):
80
+ fields["publisher"] = _clean(msg["publisher"])
81
+
82
+ # Abstract
83
+ if msg.get("abstract"):
84
+ fields["abstract"] = _clean(msg["abstract"])
85
+
86
+ return _format_bibtex(entry_type, cite_key, fields)
87
+
88
+
89
+ def _detect_entry_type(crossref_type: str) -> str:
90
+ mapping = {
91
+ "journal-article": "article",
92
+ "proceedings-article": "inproceedings",
93
+ "book-chapter": "incollection",
94
+ "book": "book",
95
+ "monograph": "book",
96
+ "edited-book": "book",
97
+ "reference-book": "book",
98
+ "dissertation": "phdthesis",
99
+ "report": "techreport",
100
+ "posted-content": "misc",
101
+ "proceedings": "proceedings",
102
+ }
103
+ return mapping.get(crossref_type, "article")
104
+
105
+
106
+ def _make_cite_key(msg: dict[str, Any], doi: str) -> str:
107
+ authors = msg.get("author", [])
108
+ first_author = ""
109
+ if authors:
110
+ first_author = authors[0].get("family", authors[0].get("given", ""))
111
+ first_author = re.sub(r"[^A-Za-z]", "", first_author)[:6]
112
+
113
+ parts = msg.get("published-print", msg.get("published-online", msg.get("created", {})))
114
+ date_parts = parts.get("date-parts", [[]]) if isinstance(parts, dict) else [[]]
115
+ year = str(date_parts[0][0]) if date_parts and date_parts[0] else "XXXX"
116
+
117
+ titles = msg.get("title", [])
118
+ first_word = ""
119
+ if titles:
120
+ words = re.findall(r"[A-Za-z]+", titles[0])
121
+ if words:
122
+ first_word = words[0][:6]
123
+
124
+ if not (first_author or first_word):
125
+ # Crossref gave us no author and no title: a bare "xxxx" key would
126
+ # collide with every other such entry, so lean on the DOI instead.
127
+ return re.sub(r"[^A-Za-z0-9]+", "_", doi).strip("_").lower() or "ref"
128
+ return f"{first_author}{year}{first_word}".lower()
129
+
130
+
131
+ def _clean(text: str) -> str:
132
+ text = re.sub(r"<[^>]+>", "", text)
133
+ text = text.replace("&", r"\&").replace("%", r"\%").replace("_", r"\_")
134
+ text = text.replace("{", r"\{").replace("}", r"\}")
135
+ return text.strip()
136
+
137
+
138
+ def _format_bibtex(entry_type: str, cite_key: str, fields: dict[str, str]) -> str:
139
+ lines = [f"@{entry_type}{{{cite_key},"]
140
+ for key, value in fields.items():
141
+ lines.append(f" {key} = {{{value}}},")
142
+ lines.append("}")
143
+ return "\n".join(lines)
@@ -0,0 +1,225 @@
1
+ """Citation export in multiple formats (BibTeX, RIS, EndNote)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from typing import Any
7
+
8
+ from ..network import fetch_json
9
+
10
+
11
+ def fetch_metadata(doi: str, config: dict[str, Any]) -> dict[str, Any] | None:
12
+ """Fetch paper metadata from Crossref API."""
13
+ import urllib.parse
14
+ q = urllib.parse.quote(doi, safe="")
15
+ url = f"https://api.crossref.org/works/{q}"
16
+ try:
17
+ payload = fetch_json(url, config, headers={"Accept": "application/json"})
18
+ if not payload:
19
+ return None
20
+ return payload.get("message", {})
21
+ except Exception:
22
+ return None
23
+
24
+
25
+ def _extract_fields(msg: dict[str, Any], doi: str) -> dict[str, str]:
26
+ """Extract common fields from Crossref metadata."""
27
+ fields: dict[str, str] = {}
28
+
29
+ # Title
30
+ titles = msg.get("title", [])
31
+ if titles:
32
+ fields["title"] = _clean_html(titles[0])
33
+
34
+ # Authors
35
+ authors = msg.get("author", [])
36
+ if authors:
37
+ fields["authors"] = authors
38
+
39
+ # Journal
40
+ container = msg.get("container-title", [])
41
+ if container:
42
+ fields["journal"] = _clean_html(container[0])
43
+
44
+ # Year
45
+ parts = msg.get("published-print", msg.get("published-online", msg.get("created", {})))
46
+ date_parts = parts.get("date-parts", [[]]) if isinstance(parts, dict) else [[]]
47
+ if date_parts and date_parts[0]:
48
+ fields["year"] = str(date_parts[0][0])
49
+
50
+ # Volume, Issue, Pages
51
+ if msg.get("volume"):
52
+ fields["volume"] = str(msg["volume"])
53
+ if msg.get("issue"):
54
+ fields["issue"] = str(msg["issue"])
55
+ if msg.get("page"):
56
+ fields["pages"] = msg["page"]
57
+
58
+ fields["doi"] = doi
59
+ fields["url"] = f"https://doi.org/{doi}"
60
+
61
+ if msg.get("publisher"):
62
+ fields["publisher"] = _clean_html(msg["publisher"])
63
+
64
+ if msg.get("abstract"):
65
+ fields["abstract"] = _clean_html(msg["abstract"])
66
+
67
+ # Entry type
68
+ type_mapping = {
69
+ "journal-article": "JOUR",
70
+ "proceedings-article": "CONF",
71
+ "book-chapter": "CHAP",
72
+ "book": "BOOK",
73
+ "monograph": "BOOK",
74
+ "dissertation": "THES",
75
+ "report": "RPRT",
76
+ "posted-content": "UNPB",
77
+ }
78
+ fields["entry_type"] = type_mapping.get(msg.get("type", ""), "JOUR")
79
+
80
+ return fields
81
+
82
+
83
+ def _clean_html(text: str) -> str:
84
+ text = re.sub(r"<[^>]+>", "", text)
85
+ return text.strip()
86
+
87
+
88
+ def _format_authors_ris(authors: list[dict]) -> list[str]:
89
+ """Format authors for RIS format."""
90
+ result = []
91
+ for a in authors:
92
+ family = a.get("family", "")
93
+ given = a.get("given", "")
94
+ if family:
95
+ result.append(f"{family}, {given}".strip(", "))
96
+ return result
97
+
98
+
99
+ def to_ris(doi: str, config: dict[str, Any]) -> str | None:
100
+ """Generate RIS format citation."""
101
+ msg = fetch_metadata(doi, config)
102
+ if not msg:
103
+ return None
104
+ return _metadata_to_ris(msg, doi)
105
+
106
+
107
+ def _metadata_to_ris(msg: dict[str, Any], doi: str) -> str:
108
+ fields = _extract_fields(msg, doi)
109
+ lines = []
110
+
111
+ # Type
112
+ lines.append(f"TY - {fields.get('entry_type', 'JOUR')}")
113
+
114
+ # Authors
115
+ authors = fields.get("authors", [])
116
+ for a in _format_authors_ris(authors):
117
+ lines.append(f"AU - {a}")
118
+
119
+ # Title
120
+ if "title" in fields:
121
+ lines.append(f"TI - {fields['title']}")
122
+
123
+ # Journal
124
+ if "journal" in fields:
125
+ lines.append(f"JO - {fields['journal']}")
126
+
127
+ # Year
128
+ if "year" in fields:
129
+ lines.append(f"PY - {fields['year']}")
130
+
131
+ # Volume
132
+ if "volume" in fields:
133
+ lines.append(f"VL - {fields['volume']}")
134
+
135
+ # Issue
136
+ if "issue" in fields:
137
+ lines.append(f"IS - {fields['issue']}")
138
+
139
+ # Pages
140
+ if "pages" in fields:
141
+ pages = fields["pages"]
142
+ if "-" in pages:
143
+ parts = pages.split("-", 1)
144
+ lines.append(f"SP - {parts[0].strip()}")
145
+ lines.append(f"EP - {parts[1].strip()}")
146
+ else:
147
+ lines.append(f"SP - {pages}")
148
+
149
+ # DOI
150
+ lines.append(f"DO - {doi}")
151
+
152
+ # URL
153
+ lines.append(f"UR - https://doi.org/{doi}")
154
+
155
+ # Publisher
156
+ if "publisher" in fields:
157
+ lines.append(f"PB - {fields['publisher']}")
158
+
159
+ # Abstract
160
+ if "abstract" in fields:
161
+ lines.append(f"AB - {fields['abstract']}")
162
+
163
+ lines.append("ER - ")
164
+ return "\n".join(lines)
165
+
166
+
167
+ def to_endnote(doi: str, config: dict[str, Any]) -> str | None:
168
+ """Generate EndNote format citation."""
169
+ msg = fetch_metadata(doi, config)
170
+ if not msg:
171
+ return None
172
+ return _metadata_to_endnote(msg, doi)
173
+
174
+
175
+ def _metadata_to_endnote(msg: dict[str, Any], doi: str) -> str:
176
+ fields = _extract_fields(msg, doi)
177
+ lines = []
178
+
179
+ # Type
180
+ lines.append(f"%0 {fields.get('entry_type', 'JOUR')}")
181
+
182
+ # Authors
183
+ authors = fields.get("authors", [])
184
+ for a in _format_authors_ris(authors):
185
+ lines.append(f"%A {a}")
186
+
187
+ # Title
188
+ if "title" in fields:
189
+ lines.append(f"%T {fields['title']}")
190
+
191
+ # Journal
192
+ if "journal" in fields:
193
+ lines.append(f"%J {fields['journal']}")
194
+
195
+ # Year
196
+ if "year" in fields:
197
+ lines.append(f"%D {fields['year']}")
198
+
199
+ # Volume
200
+ if "volume" in fields:
201
+ lines.append(f"%V {fields['volume']}")
202
+
203
+ # Issue
204
+ if "issue" in fields:
205
+ lines.append(f"%N {fields['issue']}")
206
+
207
+ # Pages
208
+ if "pages" in fields:
209
+ lines.append(f"%P {fields['pages']}")
210
+
211
+ # DOI
212
+ lines.append(f"%R {doi}")
213
+
214
+ # URL
215
+ lines.append(f"%U https://doi.org/{doi}")
216
+
217
+ # Publisher
218
+ if "publisher" in fields:
219
+ lines.append(f"%I {fields['publisher']}")
220
+
221
+ # Abstract
222
+ if "abstract" in fields:
223
+ lines.append(f"%X {fields['abstract']}")
224
+
225
+ return "\n".join(lines)