Skip to content

Commit

Permalink
download file bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benben17 committed Apr 4, 2024
1 parent 26f976c commit 2983769
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
15 changes: 15 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DEBUG=False
PORT=6000
SECRET_KEY="ae6xxxxxaf4f90de0bb"
APPID="xxx" # 微信小程序id
APPSECRET= # 微信小程序密钥
MAIL_USERNAME=[email protected] # 发件邮箱用户名
MAIL_PASSWORD=xxxx # 发件邮箱密码 注意申请客户端授权码
MAIL_DEFAULT_SENDER='"rss2ebook"<[email protected]>'
MAIL_ATT_MAX_SIZE=20 # 设置附件大小 单位M

RSS2EBOOK_KEY= # key

GOOGLE_ID=774931221812-.apps.googleusercontent.com # google id
GOOGLE_SEARCH_KEY=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 # google search key
UPGRADE_USER_URL= # 升级用户url
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ target/
*.pyc
*.db
ebooks/*
*.env

# Folder to direct output to when testing command-line (will be ignored by git)
/venv/
Expand Down
45 changes: 25 additions & 20 deletions book/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,24 @@ def search_net_book(title=None, author=None, isbn=None, openid="", ):

def download_net_book(ipfs_cid, filename):
file_path = config.DOWNLOAD_DIR + filename

# 当文件存在,不下载直接返回文件路径
if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
return file_path

url_list = [
'https://dweb.link',
'https://cloudflare-ipfs.com',
'https://gateway.pinata.cloud',
'https://gateway.ipfs.io',
'https://ipfs.joaoleitao.org',
'https://cf-ipfs.com',
'https://hardbin.com'
]
for url in url_list:
full_url = f"{url}/ipfs/{ipfs_cid}?filename={filename}"
logging.info("start download:" + filename + ipfs_cid)
# url_list = [
# 'https://dweb.link',
# 'https://cloudflare-ipfs.com',
# 'https://gateway.pinata.cloud',
# 'https://gateway.ipfs.io',
# 'https://ipfs.joaoleitao.org',
# 'https://cf-ipfs.com',
# 'https://hardbin.com'
# ]
file_download_url = 'https://netfile.rss2ebook.com'
# for url in url_list:
full_url = f"{file_download_url}/ipfs/{ipfs_cid}?filename={filename}"
for attempt in range(3):
try:
response = requests.get(full_url, stream=True, timeout=30)
response.raise_for_status() # Raise exception if response status code is not 200
Expand All @@ -183,9 +185,12 @@ def download_net_book(ipfs_cid, filename):
logging.info(f"{filename}:File downloaded successfully")
return file_path
except RequestException as 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
logging.error(f"Error downloading from {full_url} on attempt {attempt + 1}: {e}")
if attempt == 2: # If this was the last attempt
return None
time.sleep(5)
# logging.error(f"{filename} Could not download file from any of the URLs provided")
# return None


def is_file_24_hours(file_path):
Expand Down Expand Up @@ -263,12 +268,12 @@ def utc_now():


if __name__ == '__main__':
print(is_isbn('1-63995-000-1'))
print(get_now())
# print(is_isbn('1-63995-000-1'))
# print(get_now())
# author = "[]未知12213COMchenjin5.comePUBw.COM 12344"
# author = str(author).translate(str.maketrans('', '', '[]未知COAY.COMchenjin5.comePUBw.COM'))
# print(author)
# print(filesize_format(100022000000000000000000000000))
print(search_net_book("javascrip"))
# ipfs_id = 'bafykbzacedg535kz7z6imhntm5cuuknmutqmdktwt7di3l64cdi5vdepiohjk'
# download_net_book(ipfs_id,"平凡的世界.epub")
# print(search_net_book("javascrip"))
ipfs_id = 'bafykbzacedg535kz7z6imhntm5cuuknmutqmdktwt7di3l64cdi5vdepiohjk'
download_net_book(ipfs_id,"平凡的世界.epub")
4 changes: 2 additions & 2 deletions book/views/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def wechat():
# 每个用户每天最多下载5本书
if not user or not user.kindle_email: # 必须绑定邮箱
return wx_reply_xml(from_user, to_user, no_bind_email_msg)
usersend = Userlog.query.filter(Userlog.wx_openid == from_user, Userlog.status == 1,
user_send_count = Userlog.query.filter(Userlog.wx_openid == from_user, Userlog.status == 1,
Userlog.create_time > get_now()).all()
if len(usersend) > 6:
if len(user_send_count) > 6:
wx_reply_xml(from_user, to_user, "今天已经下载5本书,请明天在进行发送!")
book_info = cache.get(f'{from_user}_{content}')
if book_info is not None:
Expand Down
2 changes: 1 addition & 1 deletion runtime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-3.11
python-3.9

0 comments on commit 2983769

Please sign in to comment.