From 89712d6b6cbafb7cdb1ed89648b21579d25b0bf3 Mon Sep 17 00:00:00 2001 From: leezhua17 <892100089@qq.com> Date: Sun, 9 Apr 2023 16:28:53 +0800 Subject: [PATCH] bug fix --- app.py | 2 +- book/utils/__init__.py | 13 +++++-------- book/utils/ebookConvert.py | 29 +++++++++++++++++++++++++++++ book/views/feed.py | 9 +++++++-- book/views/myfeed.py | 28 ++++++++++++++++++++++++++++ book/views/user.py | 3 ++- requirements.txt | 2 +- 7 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 book/utils/ebookConvert.py diff --git a/app.py b/app.py index 0a4850f..1a17bd8 100644 --- a/app.py +++ b/app.py @@ -6,5 +6,5 @@ if __name__ == '__main__': - print(app.url_map) + # print(app.url_map) app.run(debug=True,threaded=True,use_reloader=False,port=8000) diff --git a/book/utils/__init__.py b/book/utils/__init__.py index e133db8..487578d 100644 --- a/book/utils/__init__.py +++ b/book/utils/__init__.py @@ -144,8 +144,8 @@ def download_net_book(ipfs_cid, filename): 'https://cloudflare-ipfs.com', 'https://gateway.pinata.cloud', 'https://gateway.ipfs.io', - 'https://ipfs.jpu.jp' - + 'https://ipfs.jpu.jp', + 'https://cf-ipfs.com' ] for url in url_list: full_url = f"{url}/ipfs/{ipfs_cid}?filename={filename}" @@ -160,19 +160,16 @@ def download_net_book(ipfs_cid, filename): logging.info(f"{filename}:File downloaded successfully") return file_path except RequestException as e: - logging.info(f"Error downloading from {full_url}: {e}") - + logging.error(f"Error downloading from {full_url}: {e}") logging.error(f"{filename} Could not download file from any of the URLs provided") return None -# 判断文件是否创建超过24小时 + def is_file_24_hours(file_path): - # 获取文件创建时间戳 + '''判断文件是否创建超过24小时''' ctime = os.path.getctime(file_path) - # 获取当前时间戳 current_time = time.time() - # 判断文件是否在24小时内创建 return current_time - ctime > 24 * 60 * 60 diff --git a/book/utils/ebookConvert.py b/book/utils/ebookConvert.py new file mode 100644 index 0000000..febe687 --- /dev/null +++ b/book/utils/ebookConvert.py @@ -0,0 +1,29 @@ +import os +import subprocess + +import config + + +def mobi_to_epub(mobi_file_path): + """ + 将mobi格式电子书转换为epub格式 + :param mobi_file_path: mobi文件的路径 + :return: epub文件的路径 + """ + # 获取mobi文件名和目录 + mobi_file_name = os.path.basename(mobi_file_path) + # 生成epub文件路径 + epub_file_name = os.path.splitext(mobi_file_name)[0] + '.epub' + epub_file_path = os.path.join(config.DOWNLOAD_DIR, epub_file_name) + + # 调用calibre进行转换 + command = f"ebook-convert {mobi_file_path} {epub_file_path}" + try: + status = subprocess.check_call(command, shell=True, stdout=subprocess.PIPE) + if status == 0: + return config.DOWNLOAD_URL + epub_file_name + return "error" + except subprocess.CalledProcessError as e: + print(f"Conversion failed with return code {e.returncode}.") + return False + diff --git a/book/views/feed.py b/book/views/feed.py index 735c42b..1ca1863 100644 --- a/book/views/feed.py +++ b/book/views/feed.py @@ -1,4 +1,6 @@ import json +import logging + import requests from flask_jwt_extended import jwt_required, get_jwt_identity import config @@ -13,6 +15,8 @@ # 用户同步 @app.route('/api/v2/sync/user/add', methods=['POST']) def user_add(): + + res = sync_post(request.path) if res['status'].lower() == "ok": return APIResponse.success(msg=res['msg']) @@ -112,6 +116,7 @@ def my_rss_add(): def get_pub_rss(): api_path = '/api/v2/rss/pub' res = sync_post(api_path) + # logging.error(res) if res['status'] == "ok": return APIResponse.success(data=res['data']) else: @@ -170,6 +175,6 @@ def sync_post(path): json_data = json.loads(res.text) if json_data['status'] == "ok": return json_data - return {"status": "failed", "msg": res.content} + return {"status": "failed", "msg": json_data['msg']} else: - return {"status": "failed", "msg": res.content} + return {"status": "failed", "msg": res.text} diff --git a/book/views/myfeed.py b/book/views/myfeed.py index b28b04f..c5dce1f 100644 --- a/book/views/myfeed.py +++ b/book/views/myfeed.py @@ -1,3 +1,31 @@ +import logging +from flask import request +from flask_jwt_extended import get_jwt_identity, jwt_required +from book import app, jwt, Userlog, db +from book.utils.ApiResponse import APIResponse +@app.route("/user/send/ebook", methods=["POST"]) +@jwt_required() +def dl_ebook(): + try: + data = request.get_json() + book_name = data.get('book_name') + ipfs_cid = data.get('ipfs_cid') + if not all(x in data for x in ['book_name', 'ipfs_cid']): + return APIResponse.internal_server_error(msg="参数错误!") + user = get_jwt_identity() + if user.wx_openid is None: + return APIResponse.bad_request(msg="请先关注公众号,发送注册邮箱进行绑定") + filesize = data.get('filesize', 0) + user_log = Userlog(wx_openid=user.wx_openid, book_name=book_name, receive_email=user.kindle_email, + user_id=user.id, operation_type='download', + status=0, ipfs_cid=ipfs_cid, filesize=filesize) + db.session.add(user_log) + db.session.commit() + return APIResponse.success(msg="发送成功,邮箱接收!") + + except Exception as e: + logging.error(e) + return APIResponse.bad_request(msg="发送失败") diff --git a/book/views/user.py b/book/views/user.py index 95c6a0d..e07e87b 100644 --- a/book/views/user.py +++ b/book/views/user.py @@ -134,7 +134,8 @@ def sync_user(user): data = { 'key': config.RSS2EBOOK_KEY, 'user_name': user.name, - 'to_email': user.email + 'to_email': user.email, + 'expiration_days': '360' } res = requests.post(config.RSS2EBOOK_URL + path, data=data, headers=config.headers) if res.status_code == 200: diff --git a/requirements.txt b/requirements.txt index c61bc32..e0a1346 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ APScheduler==3.10.1 blinker==1.6 -cachelib +cachelib==0.9.0 certifi==2022.12.7 charset-normalizer==3.1.0 click==8.1.3