The code is for learning and communication only , Do not use for illegal purposes
# -*- coding:utf-8 -*-
import requests
import datetime
import time
import json
import os
import xlrd
import xlwt
from xlutils.copy import copy
'''
The function point :
1、MeiTuan It is preferred that the goods on the merchant side are automatically put on the shelves
2、MeiTuan It is preferred that the goods on the merchant side are automatically scheduled
3、MeiTuan It is preferred to automatically collect commodity orders on the merchant side
wx:walei5201314
'''
retry = 3
timeout = 20
excelTitle = ["productId", "skuId", " Name of commodity ", " Sales area ", " Supply price ", " stock ", " Sales unit ", " Sales date ", " Date of manufacture ", " The market price ", " Pit position "]
excelPwd = os.getcwd() + "/paiqi/"
if not os.path.exists(excelPwd):
os.mkdir(excelPwd)
cookie = ""
mallPassportToken = ""
def initExcel(path):
try:
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'double', cell_overwrite_ok=True)
for i in range(0, len(excelTitle)):
sheet1.write(0, i, excelTitle[i])
f.save(path)
return True
except Exception as e:
return False
def writeExcel(data, path):
print("===========================================================")
print(data)
print("===========================================================")
try:
workbook = xlrd.open_workbook(path)
sheets = workbook.sheet_names()
worksheet = workbook.sheet_by_name(sheets[0])
rows_old = worksheet.nrows
new_workbook = copy(workbook)
new_worksheet = new_workbook.get_sheet(0)
for j in range(0, len(data)):
try:
new_worksheet.write(rows_old, j, str(data[j]))
except Exception as e:
continue
new_workbook.save(path)
return True
except Exception as e:
pass
return False
def dateTots(s):
try:
return int(time.mktime(time.strptime(s, "%Y/%m/%d"))) * 1000
except Exception as e:
return None
def getHtml(url, headers):
for i in range(retry):
try:
resp = requests.get(url, headers=headers, timeout=timeout)
return json.loads(resp.content.decode("utf-8"))
except Exception as e:
pass
def postHtml(url, data, headers):
for i in range(retry):
try:
resp = requests.post(url, headers=headers, data=data, timeout=timeout)
return json.loads(resp.content.decode("utf-8"))
except Exception as e:
pass
def updateCookie():
global cookie
global mallPassportToken
try:
with open("cookie.txt", "r", encoding="utf-8") as f:
fs = f.readlines()
cookie = fs[0]
except Exception as e:
print(" Startup error : The directory where the program is located is not read cookie.txt file !")
exit(0)
def mkdir(path):
try:
if os.path.exists(path):
return True
os.mkdir(path)
return True
except Exception as e:
pass
return False
def getCurrentTime():
return str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
def getCurrentDate():
return str(time.strftime('%Y-%m-%d', time.localtime(time.time())))
def getSechduleRegionList():
url = "https://vss-grocery.meituan.com/api/vss/shepherd/schedule/VendorSkuScheduleQueryApiService/searchVendorHistoryScheduleRegion?version="
headers = {
"mall-passport-token": mallPassportToken,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.2(0x18000231) NetType/4G Language/zh_CN",
"mall-login-type": "passport-temp",
"Referer": "https://servicewechat.com/sssssss5521212/148/page-frame.html",
"Cookie": cookie
}
res = postHtml(url, {
}, headers)
try:
return res['data']
except Exception as e:
pass
def tsToDate(ts):
if ts:
timeArray = time.localtime(int(ts))
return str(time.strftime("%Y-%m-%d", timeArray))
return ""
def getSellPosition(sellPosition):
if sellPosition is None or sellPosition == "null":
return ""
elif str(sellPosition) == "4":
return " Ten thousand people group "
elif str(sellPosition) == "3":
return " seckill "
elif str(sellPosition) == "2":
return " Explosive "
elif str(sellPosition) == "1":
return " routine "
return ""
def schduleParser(schduleStartTs, schduleEndTs, regionId, regionName, excelPath):
url = "https://vss-grocery.meituan.com/api/vss/shepherd/schedule/VendorSkuScheduleQueryApiService/searchScheduleList?version="
page = 1
while True:
data = {
"paging": {
"offset": (page - 1) * 10,
"limit": 10
},
"skuName": "",
"scheduleStatus": 0,
"saleStartTime": schduleStartTs,
"saleEndTime": schduleEndTs
}
headers = {
"mall-passport-token": mallPassportToken,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.2(0x18000231) NetType/4G Language/zh_CN",
"mall-login-type": "passport-temp",
"Referer": "https://servicewechat.com/wx40f6ce973eb82d6b/148/page-frame.html",
"Cookie": cookie
}
res = postHtml(url, json.dumps(data), headers)
try:
scheduleList = res['data']['scheduleList']
if scheduleList and len(scheduleList) > 0:
for schedule in scheduleList:
try:
datas = []
datas.append(schedule['productId'])
datas.append(schedule['skuId'])
datas.append(schedule['name'])
datas.append(schedule['stock'])
datas.append(schedule['unitName'])
datas.append(tsToDate(str(schedule['saleDate'])[: 10]))
try:
datas.append(tsToDate(str(schedule['productProductionDate'])[: 10]) if schedule['productProductionDate'] else "")
except Exception as e:
datas.append("")
try:
datas.append(str(schedule['cutlinePrice']) if schedule['cutlinePrice'] else "")
except Exception as e:
datas.append("")
try:
datas.append(getSellPosition(str(schedule['sellPosition'])))
except Exception as e:
datas.append("")
writeExcel(datas, excelPath)
except Exception as e:
pass
total = int(res['data']['total'])
if total < 10:
break
page += 1
else:
break
except Exception as e:
pass
return False
def getBeforeDaysDate(days):
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days=days))
return dateTots(str(threeDayAgo.strftime('%Y/%m/%d')))
def main():
updateCookie()
excelPath = excelPwd + getCurrentDate() + ".xls"
if status:
startTs = getBeforeDaysDate(0)
sechduleRegionList = getSechduleRegionList()
if sechduleRegionList and len(sechduleRegionList) > 0:
for sechduleRegion in sechduleRegionList:
try:
schduleParser(startTs, endTs, str(sechduleRegion['regionId']), sechduleRegion['regionName'], excelPath)
except Exception as e:
pass
else:
print(" Failed to get sales region , Please check if your login has expired !")
else:
print("excel:%s Create failure ! Please check excel Open or not , If yes, please close !" % excelPath)
if __name__ == '__main__':
main()