-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
255 lines (208 loc) · 7.88 KB
/
main.py
File metadata and controls
255 lines (208 loc) · 7.88 KB
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
#!/usr/bin/env python3
"""
Journal Club Publication Watcher
A tool for searching PubMed and CrossRef for relevant academic publications.
Generates PowerPoint slides, HTML dashboards, and text summaries for quick review.
See CHANGELOG.md for version history.
"""
import argparse
import json
import os
import webbrowser
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Tuple
from config.config_loader import load_config
from core.date_utils import ask_user_date
from core.paper_processor import process_papers, combine_components
from fetch_modules.crossref_client import lookup_crossref
from fetch_modules.pubmed_client import lookup_pubmed
from output_modules.file_writers import write_txt_file, write_json_file
from output_modules.html_builder import write_html_dashboard
from make_modules.pptx_maker import create_presentation
from utils.browser_utils import open_links_in_safari
from utils.display import print_opener, print_results_summary
VERSION = "3.7.0"
UPDATE_DATE = "20260126"
def parse_arguments() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description="Journal lookup tool for academic research",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s --auto --mode keywords # Auto mode, keywords only
%(prog)s --mode authors # Interactive mode, authors only
%(prog)s # Interactive mode, both keywords and authors
"""
)
parser.add_argument(
'--auto',
action='store_true',
help="Run in automatic (non-interactive) mode"
)
parser.add_argument(
"--mode",
choices=["both", "keywords", "authors"],
default="both",
help="Select search mode (default: both)"
)
parser.add_argument(
"--config-dir",
default="config",
help="Configuration directory path (default: config)"
)
parser.add_argument(
"--output-dir",
default=".",
help="Output directory for generated files (default: current directory)"
)
return parser.parse_args()
def search_publications(
config: Dict,
date_range: Tuple[str, str],
mode: str
) -> Tuple[List, List, List, Dict]:
"""
Search for publications based on configuration and mode.
Returns:
Tuple of (keyword_papers, pubmed_author_papers, crossref_papers, keyword_frequencies)
"""
keyword_papers = []
pubmed_author_papers = []
crossref_papers = []
keyword_frequencies = {kw: 0 for kw in config.get('topics', [])}
# Keyword-based search
if mode in ["keywords", "both"] and config.get('topics'):
print('Searching PubMed by keywords...')
keyword_papers, _, keyword_frequencies = lookup_pubmed(
config_file_dict=config,
start_end_date=date_range,
mode="keywords"
)
print(f' Found {len(keyword_papers)} keyword-based papers')
# Author-based searches (via CrossRef ORCID)
if mode in ["authors", "both"] and (config.get('orcids') or config.get('named_authors')):
print('Searching by authors...')
# CrossRef ORCID search
if config.get('orcids'):
print(' Searching CrossRef by ORCIDs...')
crossref_papers = lookup_crossref(
orcids=config['orcids'],
start_end_date=date_range
)
print(f' Found {len(crossref_papers)} CrossRef papers')
return keyword_papers, pubmed_author_papers, crossref_papers, keyword_frequencies
def generate_outputs(
config: Dict,
date_range: Tuple[str, str],
components_keyword: List,
components_orcid: List,
keyword_frequencies: Dict,
output_dir: str,
auto_mode: bool
) -> None:
"""Generate all output files."""
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
components_all = components_keyword + components_orcid
# Text file
print('Writing text file...')
write_txt_file(
start_end_date=date_range,
config_file_dict=config,
components_keyword=components_keyword,
components_orcid=components_orcid,
keyword_frequency_dict=keyword_frequencies,
txt_name=str(output_path / 'publications.txt'),
auto_mode=auto_mode
)
# PowerPoint presentation
print('Creating PowerPoint presentation...')
create_presentation(
start_end_date=date_range,
config_file_dict=config,
components_keyword=components_keyword,
components_orcid=components_orcid,
keyword_frequency_dict=keyword_frequencies,
auto_mode=auto_mode,
version=VERSION,
output_path=str(output_path / 'publications.pptx')
)
# HTML dashboard
print('Creating HTML dashboard...')
html_path = output_path / 'publications.html'
write_html_dashboard(
start_end_date=date_range,
config_file_dict=config,
components=components_all,
keyword_frequency_dict=keyword_frequencies,
html_name=str(html_path),
auto_mode=auto_mode
)
# JSON results
print('Saving JSON results...')
write_json_file(
data={
"start_end_date": date_range,
"config_file_dict": config,
"components": components_all,
"keyword_frequency_dict": keyword_frequencies,
"generated_at": datetime.now().isoformat(),
"version": VERSION
},
filename=str(output_path / 'results.json')
)
# Open browser if not in auto mode
if not auto_mode:
print(' Opening dashboard in browser...')
webbrowser.open(f'file://{html_path.resolve()}')
def main() -> None:
"""Main entry point."""
args = parse_arguments()
# Display header
print_opener(VERSION, UPDATE_DATE)
if args.auto:
print(" Running in AUTO mode (non-interactive)")
try:
# Load configuration
# print(' Loading configuration...')
config, keyword_frequencies = load_config(args.config_dir)
# Get date range
date_range = ask_user_date(
config['lookup_frequency'],
auto_mode=args.auto
)
# Search for publications
print('Searching for publications...')
keyword_papers, pubmed_author_papers, crossref_papers, keyword_frequencies = search_publications(
config, date_range, args.mode
)
# Process papers into components
print('Processing paper data...')
components_keyword, components_orcid = process_papers(
keyword_papers, pubmed_author_papers, crossref_papers
)
# Display results summary
print_results_summary(components_keyword, components_orcid)
if not (components_keyword or components_orcid):
print("No papers found matching your criteria.")
return
# Generate outputs
generate_outputs(
config, date_range, components_keyword, components_orcid,
keyword_frequencies, args.output_dir, args.auto
)
# Open links in browser
if not args.auto:
components_all = components_keyword + components_orcid
open_links_in_safari(components_all, auto_mode=args.auto)
print("Journal lookup completed successfully!")
except KeyboardInterrupt:
print("\nOperation cancelled by user.")
except Exception as e:
print(f" Error: {e}")
if not args.auto:
raise
if __name__ == "__main__":
main()