|
@@ -8,6 +8,9 @@ from concurrent.futures import ThreadPoolExecutor
|
|
|
from model.DataBaseUtils import MysqlUtils
|
|
|
from model.ComUtils import *
|
|
|
from model.DateUtils import DateUtils
|
|
|
+from PIL import Image
|
|
|
+from io import BytesIO
|
|
|
+import cv2
|
|
|
|
|
|
du = DateUtils()
|
|
|
db = MysqlUtils()
|
|
@@ -193,6 +196,22 @@ def get_adcreatives(account_id, access_token, flag, adc_ids, dt): # 获取创
|
|
|
|
|
|
def images_info_get(account_id, access_token, image_ids): # 获取图片信息
|
|
|
# 接口 https://developers.e.qq.com/docs/api/business_assets/image/images_get?version=1.3
|
|
|
+
|
|
|
+ def get_image_info(preview_url, err_num=5):
|
|
|
+ try:
|
|
|
+ if not preview_url:
|
|
|
+ return None
|
|
|
+ rsp = requests.get(preview_url, timeout=5)
|
|
|
+ # 1.图片写入内存
|
|
|
+ im = Image.open(BytesIO(rsp.content))
|
|
|
+ # 2.获取图片属性
|
|
|
+ image_format = im.format
|
|
|
+ # image_size = len(rsp.content)
|
|
|
+ return image_format
|
|
|
+ except:
|
|
|
+ if err_num < 5:
|
|
|
+ return get_image_info(preview_url, err_num=err_num + 1)
|
|
|
+
|
|
|
# 1.更新数据
|
|
|
id_content = ','.join([''' '{}' '''.format(i) for i in image_ids.split(',')])
|
|
|
id_content = id_content[:-1]
|
|
@@ -259,11 +278,12 @@ def images_info_get(account_id, access_token, image_ids): # 获取图片信息
|
|
|
break
|
|
|
data = []
|
|
|
for i in li:
|
|
|
+ image_format = get_image_info(i['preview_url'])
|
|
|
data.append(
|
|
|
- (i['image_id'], i['width'], i['height'], i['signature'], i['preview_url'], i['file_size']))
|
|
|
+ (i['image_id'], i['width'], i['height'], i['signature'], i['preview_url'], i['file_size'], image_format))
|
|
|
logging.info(f"{account_id} 有新图片:" + str(li.__len__()))
|
|
|
if li.__len__() > 0:
|
|
|
- sql = "insert IGNORE into image_info (image_id,width,height,signature,preview_url,size) value (%s,%s,%s,%s,%s,%s)"
|
|
|
+ sql = "insert IGNORE into image_info (image_id,width,height,signature,preview_url,size,type) value (%s,%s,%s,%s,%s,%s,%s)"
|
|
|
db.quchen_text.executeMany(sql, data)
|
|
|
db.close()
|
|
|
|
|
@@ -271,6 +291,26 @@ def images_info_get(account_id, access_token, image_ids): # 获取图片信息
|
|
|
def video_info_get(account_id, access_token, image_ids): # 获取视频信息
|
|
|
# 接口 https://developers.e.qq.com/docs/api/business_assets/video/videos_get?version=1.3
|
|
|
|
|
|
+ def get_video_info(video_url, err_num=0):
|
|
|
+ try:
|
|
|
+ if video_url:
|
|
|
+ return None, None
|
|
|
+ rsp = requests.get(video_url)
|
|
|
+ with open('/tmp/aa.mp4', 'wb') as f:
|
|
|
+ f.write(rsp.content)
|
|
|
+ video_size = len(rsp.content)
|
|
|
+ cap = cv2.VideoCapture('/tmp/aa.mp4') # 视频流
|
|
|
+ if cap.isOpened():
|
|
|
+ rate = cap.get(5)
|
|
|
+ frame_num = cap.get(7)
|
|
|
+ duration = frame_num / rate
|
|
|
+ byte_rate = (video_size / (duration / 8))
|
|
|
+ return duration, byte_rate
|
|
|
+ except:
|
|
|
+ if err_num < 5:
|
|
|
+ return get_video_info(video_url, err_num=err_num + 1)
|
|
|
+ return None, None
|
|
|
+
|
|
|
# 1.数据库获取,查看是否需要获取对应数据
|
|
|
id_content = ','.join([''' '{}' '''.format(i) for i in image_ids.split(',')])
|
|
|
id_content = id_content[:-1]
|
|
@@ -336,11 +376,16 @@ def video_info_get(account_id, access_token, image_ids): # 获取视频信息
|
|
|
break
|
|
|
data = []
|
|
|
for i in li:
|
|
|
- data.append((i['video_id'], i['width'], i['height'], i['signature'],
|
|
|
- i['preview_url'], i['file_size']))
|
|
|
+ duration, byte_rate = get_video_info(i['preview_url'])
|
|
|
+ data.append((i['video_id'], i['width'], i['height'],
|
|
|
+ i['signature'], i['preview_url'], i['file_size'],
|
|
|
+ 'mp4', byte_rate, duration))
|
|
|
logging.info(f"{account_id} 获取到新视频:" + str(li.__len__()))
|
|
|
if li.__len__() > 0:
|
|
|
- sql = "insert IGNORE into video_info (video_id,width,height,signature,preview_url,size) value (%s,%s,%s,%s,%s,%s)"
|
|
|
+ sql = '''insert IGNORE into video_info (video_id,width,height,
|
|
|
+ signature,preview_url,size,type,byte_rate,video_length)
|
|
|
+ value (%s,%s,%s,
|
|
|
+ %s,%s,%s,%s,%s,%s)'''
|
|
|
db.quchen_text.executeMany(sql, data)
|
|
|
db.close()
|
|
|
|