From 50e45944d9bd9e0967eef73037799273f71a02f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Thu, 16 Nov 2023 05:37:16 +0800 Subject: [PATCH] env --- main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index e9cbfd6..59ab0d1 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,10 @@ import requests from PIL import Image, ImageFile +# 读取 .env +from dotenv import dotenv_values +config = dotenv_values(".env") + ImageFile.LOAD_TRUNCATED_IMAGES = True # 下载图片(使用OSS下载) @@ -12,9 +16,8 @@ def download_image(url:str) -> Image: try: url = url.replace('http://image.gameuiux.cn/', '').replace('https://image.gameuiux.cn/', '') oss2.defaults.connection_pool_size = 100 - oss_host = 'oss-cn-shanghai-internal.aliyuncs.com' - oss_auth = oss2.Auth('LTAI4GH3qP6VA3QpmTYCgXEW', 'r2wz4bJty8iYfGIcFmEqlY1yon2Ruy') - return Image.open(io.BytesIO(oss2.Bucket(oss_auth, f'http://{oss_host}', 'gameui-image2').get_object(url).read())) + oss_auth = oss2.Auth(config['OSS_ACCESS_KEY_ID'], config['OSS_ACCESS_KEY_SECRET']) + return Image.open(io.BytesIO(oss2.Bucket(oss_auth, f'http://{config["OSS_HOST"]}', config['OSS_BUCKET_NAME']).get_object(url).read())) except Exception: return None else: @@ -29,7 +32,7 @@ def download_image(url:str) -> Image: import pymysql import pymysql.cursors -conn = pymysql.connect(host='172.21.216.35', user='gameui', password='gameui@2022', database='gameui', cursorclass=pymysql.cursors.DictCursor) +conn = pymysql.connect(host=config['MYSQL_HOST'], user=config['MYSQL_USER'], password=config['MYSQL_PASSWORD'], database=config['MYSQL_NAME'], cursorclass=pymysql.cursors.DictCursor) cursor = conn.cursor() cursor.execute("SELECT id, content FROM web_images LIMIT 10") @@ -37,7 +40,7 @@ cursor.execute("SELECT id, content FROM web_images LIMIT 10") rows = cursor.fetchall() for row in rows: print(row) - image = download_image(row.content) + image = download_image(row['content']) # 关闭游标和连接 cursor.close()