Skip to content

Commit e34c49d

Browse files
committed
compat: deal with arxiv url change
1 parent 3890467 commit e34c49d

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

crazy_functions/Latex_Function.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,43 @@ def is_float(s):
138138
cached_translation_pdf = check_cached_translation_pdf(arxiv_id)
139139
if cached_translation_pdf and allow_cache: return cached_translation_pdf, arxiv_id
140140

141-
url_tar = url_.replace('/abs/', '/e-print/')
142-
translation_dir = pj(ARXIV_CACHE_DIR, arxiv_id, 'e-print')
143141
extract_dst = pj(ARXIV_CACHE_DIR, arxiv_id, 'extract')
142+
translation_dir = pj(ARXIV_CACHE_DIR, arxiv_id, 'e-print')
143+
dst = pj(translation_dir, arxiv_id + '.tar')
144144
os.makedirs(translation_dir, exist_ok=True)
145-
146145
# <-------------- download arxiv source file ------------->
147-
dst = pj(translation_dir, arxiv_id + '.tar')
148-
if os.path.exists(dst):
149-
yield from update_ui_lastest_msg("调用缓存", chatbot=chatbot, history=history) # 刷新界面
146+
147+
def fix_url_and_download():
148+
for url_tar in [url_.replace('/abs/', '/e-print/'), url_.replace('/abs/', '/src/')]:
149+
# for url_tar in [url_.replace('/abs/', '/src/'), url_.replace('/abs/', '/e-print/')]:
150+
proxies = get_conf('proxies')
151+
r = requests.get(url_tar, proxies=proxies)
152+
if r.status_code == 200:
153+
with open(dst, 'wb+') as f:
154+
f.write(r.content)
155+
return True
156+
return False
157+
158+
if os.path.exists(dst) and allow_cache:
159+
yield from update_ui_lastest_msg(f"调用缓存 {arxiv_id}", chatbot=chatbot, history=history) # 刷新界面
160+
success = True
150161
else:
151-
yield from update_ui_lastest_msg("开始下载", chatbot=chatbot, history=history) # 刷新界面
152-
proxies = get_conf('proxies')
153-
r = requests.get(url_tar, proxies=proxies)
154-
with open(dst, 'wb+') as f:
155-
f.write(r.content)
162+
yield from update_ui_lastest_msg(f"开始下载 {arxiv_id}", chatbot=chatbot, history=history) # 刷新界面
163+
success = fix_url_and_download()
164+
yield from update_ui_lastest_msg(f"下载完成 {arxiv_id}", chatbot=chatbot, history=history) # 刷新界面
165+
166+
167+
if not success:
168+
yield from update_ui_lastest_msg(f"下载失败 {arxiv_id}", chatbot=chatbot, history=history)
169+
raise tarfile.ReadError(f"论文下载失败 {arxiv_id}")
170+
156171
# <-------------- extract file ------------->
157-
yield from update_ui_lastest_msg("下载完成", chatbot=chatbot, history=history) # 刷新界面
158172
from toolbox import extract_archive
159-
extract_archive(file_path=dst, dest_dir=extract_dst)
173+
try:
174+
extract_archive(file_path=dst, dest_dir=extract_dst)
175+
except tarfile.ReadError:
176+
os.remove(dst)
177+
raise tarfile.ReadError(f"论文下载失败")
160178
return extract_dst, arxiv_id
161179

162180

0 commit comments

Comments
 (0)