Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
leezhua17 committed Apr 9, 2023
1 parent 96ac996 commit 89712d6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 5 additions & 8 deletions book/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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


Expand Down
29 changes: 29 additions & 0 deletions book/utils/ebookConvert.py
Original file line number Diff line number Diff line change
@@ -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

9 changes: 7 additions & 2 deletions book/views/feed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import logging

import requests
from flask_jwt_extended import jwt_required, get_jwt_identity
import config
Expand All @@ -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'])
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}
28 changes: 28 additions & 0 deletions book/views/myfeed.py
Original file line number Diff line number Diff line change
@@ -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="发送失败")
3 changes: 2 additions & 1 deletion book/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 89712d6

Please sign in to comment.