env
This commit is contained in:
13
main.py
13
main.py
@@ -4,6 +4,10 @@ import requests
|
|||||||
|
|
||||||
from PIL import Image, ImageFile
|
from PIL import Image, ImageFile
|
||||||
|
|
||||||
|
# 读取 .env
|
||||||
|
from dotenv import dotenv_values
|
||||||
|
config = dotenv_values(".env")
|
||||||
|
|
||||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||||
|
|
||||||
# 下载图片(使用OSS下载)
|
# 下载图片(使用OSS下载)
|
||||||
@@ -12,9 +16,8 @@ def download_image(url:str) -> Image:
|
|||||||
try:
|
try:
|
||||||
url = url.replace('http://image.gameuiux.cn/', '').replace('https://image.gameuiux.cn/', '')
|
url = url.replace('http://image.gameuiux.cn/', '').replace('https://image.gameuiux.cn/', '')
|
||||||
oss2.defaults.connection_pool_size = 100
|
oss2.defaults.connection_pool_size = 100
|
||||||
oss_host = 'oss-cn-shanghai-internal.aliyuncs.com'
|
oss_auth = oss2.Auth(config['OSS_ACCESS_KEY_ID'], config['OSS_ACCESS_KEY_SECRET'])
|
||||||
oss_auth = oss2.Auth('LTAI4GH3qP6VA3QpmTYCgXEW', 'r2wz4bJty8iYfGIcFmEqlY1yon2Ruy')
|
return Image.open(io.BytesIO(oss2.Bucket(oss_auth, f'http://{config["OSS_HOST"]}', config['OSS_BUCKET_NAME']).get_object(url).read()))
|
||||||
return Image.open(io.BytesIO(oss2.Bucket(oss_auth, f'http://{oss_host}', 'gameui-image2').get_object(url).read()))
|
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
@@ -29,7 +32,7 @@ def download_image(url:str) -> Image:
|
|||||||
import pymysql
|
import pymysql
|
||||||
import pymysql.cursors
|
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 = conn.cursor()
|
||||||
cursor.execute("SELECT id, content FROM web_images LIMIT 10")
|
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()
|
rows = cursor.fetchall()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
print(row)
|
print(row)
|
||||||
image = download_image(row.content)
|
image = download_image(row['content'])
|
||||||
|
|
||||||
# 关闭游标和连接
|
# 关闭游标和连接
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
Reference in New Issue
Block a user