123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- # -*- codeing = utf-8 -*-
- # @Time : 2024/3/7 19:54
- # @Author : Clown
- # @File : 上传动态图.py
- # @Software : PyCharm
- from time import sleep
- import requests
- import csv
- import random
- import xlrd
- import os
- import sys
- from datetime import datetime,timedelta
- import json
- import pymysql
- from PIL import Image
- import base64
- def linkTomySql(host, passwd, db_name, port):
- '''连接至数据库返回【db】,v2新增local_infile=1 打开文件导入权限'''
- try:
- # 本地连接为:localhost 服务器连接为:124.222.188.59
- db = pymysql.connect (
- host=host, user="root",
- passwd=passwd,
- db=db_name,
- charset='utf8mb4',
- local_infile=1,
- port=port)
- # print ('\nconnect to mysql server 成功')
- # print ('---------------------------------------')
- except:
- print ("\ncould not connect to mysql server")
- db = "连接失败"
- return db
- def read_key_value_pair(db, brand_name, wm_plate, owner):
- '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,以键值对的形式输出
- db:数据库,
- brand_name:品牌名,
- wm_plate:外卖平台MEITUAN或ELEME,
- owner:账号权限all或one
- '''
- cursor = db.cursor ()
- sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
- cursor.execute (sql)
- pair = json.loads (cursor.fetchall ()[0][0])
- return pair
- def get_shops_info_to_list(db, brand_name, wm_plate, key_name):
- '''获取门店信息表【shops_info_to_list】中的信息,
- 并返回表单shops_info_df【shop_id,shop_name,update_datetime,info_for_script】
- db:数据库信息
- brand_name:品牌
- wm_plate:外卖平台
- key_name:关键信息字段名,如无填‘’,如有填对应键值对的key
- '''
- cursor = db.cursor ()
- if key_name == '':
- sql = f'SELECT shop_id,shop_name,update_datetime FROM shops_info_for_script WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}";'
- cursor.execute (sql)
- shops_info = cursor.fetchall ()
- shops_info_df = []
- for shop_info in shops_info:
- shop_info_dict = {'shop_id': shop_info[0],
- 'shop_name': shop_info[1]}
- shops_info_df.append (shop_info_dict)
- return shops_info_df
- else:
- sql = f'SELECT shop_id,shop_name,update_datetime,info_for_script -> "$.{key_name}" FROM shops_info_for_script WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}";'
- cursor.execute (sql)
- shops_info = cursor.fetchall ()
- shops_info_df = []
- for shop_info in shops_info:
- shop_info_dict = {'shop_id': shop_info[0],
- 'shop_name': shop_info[1],
- 'update_datetime': shop_info[2],
- f'{key_name}': shop_info[3]}
- shops_info_df.append (shop_info_dict)
- return shops_info_df
- def image_to_base64(image_path):
- with open(image_path, "rb") as image_file:
- encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
- return encoded_string
- if __name__ == '__main__':
- host = '124.222.188.59'
- passwd = '111...Clown'
- db_name = 'zuzu_data'
- port = 63306
- db = linkTomySql(host, passwd, db_name, port)
- brand_name = '粥小鲜'
- owner = 'all'
- keys_dict = {'elm': '',
- 'elm_shops': '',
- 'mt': '',
- 'mt_shops': ''}
- keys_dict['elm'] = read_key_value_pair(db, brand_name, 'ELEME', owner)
- keys_dict['elm_shops'] = get_shops_info_to_list(db, brand_name, 'ELEME', '')
- keys_dict['mt'] = read_key_value_pair(db, brand_name, 'MEITUAN', owner)
- keys_dict['mt_shops'] = get_shops_info_to_list(db, brand_name, 'MEITUAN', '')
- token = keys_dict['mt']['data']['session']['token']
- acctId = keys_dict['mt']['data']['session']['acctId']
- shop_id = 19666068
- file_path = r'C:\Users\ClownHe\Desktop\1业务支持需求单\美-暖冬.gif'
- # file_path = r'C:\Users\ClownHe\Desktop\1业务支持需求单\ceshi.jpg'
- base64_str = image_to_base64(file_path)
- # print(base64_str)
- data = {'multipart':base64_str,'needPicPropaganda':1}
- url = 'https://e.waimai.meituan.com/reuse/product/uploadTool/w/uploadImg'
- Cookie = cookie = f"token={token}; acctId={acctId}; wmPoiId={shop_id};"
- header = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
- 'Accept': 'application/json, text/plain, */*',
- 'Cookie': Cookie}
- # resp = requests.post(url,data=data,headers=header).text
- # print(resp)
- url_save = 'https://e.waimai.meituan.com/reuse/product/food/w/saveMultiImage'
- json_save = {'wmPoiId':19666068,
- 'newUrl':'http://p0.meituan.net/business/354250f999e92885a4390e07ad78005f1363077.gif?t=1705908020128',
- 'spuId':12626543674,
- 'sequence':2,
- 'qualityScore':1,
- 'picExtend':{"source":5}}
- resp = requests.post(url_save,data=json_save,headers=header).text
- print(resp)
|