上传动态图.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2024/3/7 19:54
  3. # @Author : Clown
  4. # @File : 上传动态图.py
  5. # @Software : PyCharm
  6. from time import sleep
  7. import requests
  8. import csv
  9. import random
  10. import xlrd
  11. import os
  12. import sys
  13. from datetime import datetime,timedelta
  14. import json
  15. import pymysql
  16. from PIL import Image
  17. import base64
  18. def linkTomySql(host, passwd, db_name, port):
  19. '''连接至数据库返回【db】,v2新增local_infile=1 打开文件导入权限'''
  20. try:
  21. # 本地连接为:localhost 服务器连接为:124.222.188.59
  22. db = pymysql.connect (
  23. host=host, user="root",
  24. passwd=passwd,
  25. db=db_name,
  26. charset='utf8mb4',
  27. local_infile=1,
  28. port=port)
  29. # print ('\nconnect to mysql server 成功')
  30. # print ('---------------------------------------')
  31. except:
  32. print ("\ncould not connect to mysql server")
  33. db = "连接失败"
  34. return db
  35. def read_key_value_pair(db, brand_name, wm_plate, owner):
  36. '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,以键值对的形式输出
  37. db:数据库,
  38. brand_name:品牌名,
  39. wm_plate:外卖平台MEITUAN或ELEME,
  40. owner:账号权限all或one
  41. '''
  42. cursor = db.cursor ()
  43. sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
  44. cursor.execute (sql)
  45. pair = json.loads (cursor.fetchall ()[0][0])
  46. return pair
  47. def get_shops_info_to_list(db, brand_name, wm_plate, key_name):
  48. '''获取门店信息表【shops_info_to_list】中的信息,
  49. 并返回表单shops_info_df【shop_id,shop_name,update_datetime,info_for_script】
  50. db:数据库信息
  51. brand_name:品牌
  52. wm_plate:外卖平台
  53. key_name:关键信息字段名,如无填‘’,如有填对应键值对的key
  54. '''
  55. cursor = db.cursor ()
  56. if key_name == '':
  57. sql = f'SELECT shop_id,shop_name,update_datetime FROM shops_info_for_script WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}";'
  58. cursor.execute (sql)
  59. shops_info = cursor.fetchall ()
  60. shops_info_df = []
  61. for shop_info in shops_info:
  62. shop_info_dict = {'shop_id': shop_info[0],
  63. 'shop_name': shop_info[1]}
  64. shops_info_df.append (shop_info_dict)
  65. return shops_info_df
  66. else:
  67. 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}";'
  68. cursor.execute (sql)
  69. shops_info = cursor.fetchall ()
  70. shops_info_df = []
  71. for shop_info in shops_info:
  72. shop_info_dict = {'shop_id': shop_info[0],
  73. 'shop_name': shop_info[1],
  74. 'update_datetime': shop_info[2],
  75. f'{key_name}': shop_info[3]}
  76. shops_info_df.append (shop_info_dict)
  77. return shops_info_df
  78. def image_to_base64(image_path):
  79. with open(image_path, "rb") as image_file:
  80. encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
  81. return encoded_string
  82. if __name__ == '__main__':
  83. host = '124.222.188.59'
  84. passwd = '111...Clown'
  85. db_name = 'zuzu_data'
  86. port = 63306
  87. db = linkTomySql(host, passwd, db_name, port)
  88. brand_name = '粥小鲜'
  89. owner = 'all'
  90. keys_dict = {'elm': '',
  91. 'elm_shops': '',
  92. 'mt': '',
  93. 'mt_shops': ''}
  94. keys_dict['elm'] = read_key_value_pair(db, brand_name, 'ELEME', owner)
  95. keys_dict['elm_shops'] = get_shops_info_to_list(db, brand_name, 'ELEME', '')
  96. keys_dict['mt'] = read_key_value_pair(db, brand_name, 'MEITUAN', owner)
  97. keys_dict['mt_shops'] = get_shops_info_to_list(db, brand_name, 'MEITUAN', '')
  98. token = keys_dict['mt']['data']['session']['token']
  99. acctId = keys_dict['mt']['data']['session']['acctId']
  100. shop_id = 19666068
  101. file_path = r'C:\Users\ClownHe\Desktop\1业务支持需求单\美-暖冬.gif'
  102. # file_path = r'C:\Users\ClownHe\Desktop\1业务支持需求单\ceshi.jpg'
  103. base64_str = image_to_base64(file_path)
  104. # print(base64_str)
  105. data = {'multipart':base64_str,'needPicPropaganda':1}
  106. url = 'https://e.waimai.meituan.com/reuse/product/uploadTool/w/uploadImg'
  107. Cookie = cookie = f"token={token}; acctId={acctId}; wmPoiId={shop_id};"
  108. header = {
  109. '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',
  110. 'Accept': 'application/json, text/plain, */*',
  111. 'Cookie': Cookie}
  112. # resp = requests.post(url,data=data,headers=header).text
  113. # print(resp)
  114. url_save = 'https://e.waimai.meituan.com/reuse/product/food/w/saveMultiImage'
  115. json_save = {'wmPoiId':19666068,
  116. 'newUrl':'http://p0.meituan.net/business/354250f999e92885a4390e07ad78005f1363077.gif?t=1705908020128',
  117. 'spuId':12626543674,
  118. 'sequence':2,
  119. 'qualityScore':1,
  120. 'picExtend':{"source":5}}
  121. resp = requests.post(url_save,data=json_save,headers=header).text
  122. print(resp)