all_key_table.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2022/6/20 13:02
  3. # @Author : Clown
  4. # @File : all_key_table.py
  5. # @Software : PyCharm
  6. '''
  7. 关键表
  8. All data is from this table
  9. 表名:all_key_table
  10. 更新方式:每日自检,如信息失效则更新
  11. 关联脚本:
  12. 字段:
  13. 字段名 值类型 备注
  14. brand_name varchar 品牌名(浆小白/粥小鲜/楼兰说)
  15. wm_plate varchar 外卖平台(MEITUAN/ELEME)
  16. acct_kinds varchar 账号类别(chain连锁/single单店)
  17. owner varchar 平台登录账号所有人(all管理员总账号/one子账号)
  18. user varchar 平台登录用户
  19. key varchar 钥匙
  20. data_pipe json 数据通道接通验证(当前来源食亨)
  21. key_value_pair json 键值对参数(用于启动其他脚本执行)
  22. '''
  23. import requests
  24. import pymysql
  25. import json
  26. import time
  27. import random
  28. def linkTomySql(host,passwd,db_name):
  29. '''连接数据库,返回值为:type:参数 【db】,v2新增local_infile=1 打开文件导入权限'''
  30. try:
  31. #本地连接为:localhost 服务器连接为:124.222.188.59
  32. db = pymysql.connect(
  33. host=host, user="root",
  34. passwd=passwd,
  35. db=db_name,
  36. charset='utf8mb4',
  37. local_infile=1,
  38. port=63306)
  39. print('\nconnect to mysql server 成功')
  40. print('---------------------------------------')
  41. except:
  42. print("\ncould not connect to mysql server")
  43. db = "连接失败"
  44. return db
  45. def wkong_login(username,pw):
  46. '''登录食亨悟空控件,返回值为type:str 【sh_token】'''
  47. try:
  48. cookie = ''
  49. url = 'https://bi.shihengtech.com/api/userlogin/login'
  50. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
  51. 'Cookie': cookie}
  52. params = {'username': username,
  53. 'password': pw}
  54. resp = requests.post(url, headers = headers ,json = params)
  55. sh_token = resp.json()['result']
  56. except:
  57. sh_token = ''
  58. return sh_token
  59. def take_token(wm_plate, user, sh_token):
  60. '''
  61. wm_plate:平台名称【MEITUAN】/【ELEME】
  62. user:外卖平台登录账号
  63. sh_token:通信证明
  64. '''
  65. try:
  66. url_wukong = 'https://bi.shihengtech.com/token-owner/acquire_token'
  67. headers_wukong = {
  68. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
  69. 'extension': '2.1.0'}
  70. params_wukong = {"platform": wm_plate,
  71. "username": user,
  72. "userToken": sh_token}
  73. resp = requests.post(url_wukong, headers=headers_wukong, json=params_wukong)
  74. data = resp.text
  75. except:
  76. data = {}
  77. '''
  78. 美团参数
  79. data = {"device_uuid":"!ce3baac4-0ef4-4c51-8e06-d50515d383db",
  80. "uuid_update":"true",
  81. "pushToken":"0VlrP_me3r43iR4zJqAPcilheMwouJO32OXkvzjsrXxs*",
  82. "acctId":"113245982",
  83. "token":"0fiJecyopKkIyVG7mb-qItH88bFRjfRkzCeugUySJTco*",
  84. "brandId":"-1",
  85. "city_id":"0",
  86. "isChain":"1",
  87. "existBrandPoi":"true",
  88. "ignore_set_router_proxy":"true",
  89. "region_id":"",
  90. "region_version":"0",
  91. "newCategory":"false",
  92. "bsid":"5-D2l3L0gOXLAhsFiizjlcgSqXQFLctqNKCMyhxp65hgGKgu8FGZskNq5hSJnGTg02QDgHTgXc0bx1Gm_4sqzw",
  93. "city_location_id":"0",
  94. "location_id":"0",
  95. "cityId":"310100",
  96. "provinceId":"310000",
  97. "wmPoiId":"-1",
  98. "_lxsdk_cuid":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  99. "_lxsdk":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  100. "uuid":"d85a7b5354c578c949b1.1654585692.1.0.0",
  101. "set_info":"%7B%22wmPoiId%22%3A14471551%2C%22ignoreSetRouterProxy%22%3Atrue%7D",
  102. "logan_session_token":"3s5se0y0p102jodkb4c3",
  103. "_lxsdk_s":"1815ab1658e-b68-cdb-52e%7C%7CNaN"}
  104. 饿了么参数(仅部分数据,如有需要可打印你出来查看)
  105. data = {'ksid': "NZUWN2MTA1Mjg5MjA0OTM5NjAxT1lGbGRPZTlQ",
  106. 'loginShopId': "99731295",
  107. 'shopId': "99731295"}
  108. '''
  109. return data
  110. def update_key_value_pair(db,brand_name,wm_plate,owner,key_value_pair):
  111. '''
  112. 更新关键表,all_key_table字段,
  113. db:数据库,
  114. brand_name:品牌名,
  115. wm_plate:外卖平台MEITUAN或ELEME,
  116. owner:账号权限all或one
  117. key_value_pair:为空值 ‘’ 时联网自动更新,不为空值时需手动更新
  118. '''
  119. cursor = db.cursor()
  120. if key_value_pair == '':
  121. sql = f'''SELECT * FROM all_key_table WHERE owner = '{owner}' AND brand_name = '{brand_name}' AND wm_plate = '{wm_plate}' ;'''
  122. cursor.execute(sql)
  123. df = cursor.fetchall()
  124. row = df[0]
  125. u_p = json.loads(row[6])
  126. username = u_p['username']
  127. pw = u_p['pw']
  128. wm_plate = row[1]
  129. user = row[4]
  130. try:
  131. cookie = ''
  132. url = 'https://bi.shihengtech.com/api/userlogin/login'
  133. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
  134. 'Cookie': cookie}
  135. params = {'username': username,
  136. 'password': pw}
  137. resp = requests.post(url, headers = headers ,json = params)
  138. sh_token = resp.json()['result']
  139. # print(sh_token)
  140. except Exception as e:
  141. print(e)
  142. sh_token = ''
  143. try:
  144. url_wukong = 'https://bi.shihengtech.com/token-owner/acquire_token'
  145. headers_wukong = {
  146. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
  147. 'extension': '2.1.0'}
  148. params_wukong = {"platform": wm_plate,
  149. "username": user,
  150. "userToken": sh_token}
  151. resp = requests.post(url_wukong, headers=headers_wukong, json=params_wukong)
  152. data = resp.text
  153. # print(data)
  154. except Exception as e:
  155. print(e)
  156. data = {}
  157. '''
  158. 美团参数
  159. data = {"device_uuid":"!ce3baac4-0ef4-4c51-8e06-d50515d383db",
  160. "uuid_update":"true",
  161. "pushToken":"0VlrP_me3r43iR4zJqAPcilheMwouJO32OXkvzjsrXxs*",
  162. "acctId":"113245982",
  163. "token":"0fiJecyopKkIyVG7mb-qItH88bFRjfRkzCeugUySJTco*",
  164. "brandId":"-1",
  165. "city_id":"0",
  166. "isChain":"1",
  167. "existBrandPoi":"true",
  168. "ignore_set_router_proxy":"true",
  169. "region_id":"",
  170. "region_version":"0",
  171. "newCategory":"false",
  172. "bsid":"5-D2l3L0gOXLAhsFiizjlcgSqXQFLctqNKCMyhxp65hgGKgu8FGZskNq5hSJnGTg02QDgHTgXc0bx1Gm_4sqzw",
  173. "city_location_id":"0",
  174. "location_id":"0",
  175. "cityId":"310100",
  176. "provinceId":"310000",
  177. "wmPoiId":"-1",
  178. "_lxsdk_cuid":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  179. "_lxsdk":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  180. "uuid":"d85a7b5354c578c949b1.1654585692.1.0.0",
  181. "set_info":"%7B%22wmPoiId%22%3A14471551%2C%22ignoreSetRouterProxy%22%3Atrue%7D",
  182. "logan_session_token":"3s5se0y0p102jodkb4c3",
  183. "_lxsdk_s":"1815ab1658e-b68-cdb-52e%7C%7CNaN"}
  184. 饿了么参数(仅部分数据,如有需要可打印你出来查看)
  185. data = {'ksid': "NZUWN2MTA1Mjg5MjA0OTM5NjAxT1lGbGRPZTlQ",
  186. 'loginShopId': "99731295",
  187. 'shopId': "99731295"}
  188. '''
  189. try:
  190. sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE all_key_table.user = '{user}' ;"
  191. value = (data)
  192. cursor.execute(sql_up, value)
  193. db.commit()
  194. print(user, '已更新')
  195. except Exception as e:
  196. print(e)
  197. else:
  198. try:
  199. sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE owner = '{owner}' AND brand_name = '{brand_name}' AND wm_plate = '{wm_plate}' ;"
  200. value = (key_value_pair)
  201. cursor.execute(sql_up, value)
  202. db.commit()
  203. print(f'{brand_name} {wm_plate} 键值对信息已更新')
  204. except Exception as e:
  205. print(e)
  206. db.close()
  207. def read_key_value_pair(db,brand_name,wm_plate,owner):
  208. '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,返回值type:dict【pair】
  209. db:数据库,
  210. brand_name:品牌名,
  211. wm_plate:外卖平台MEITUAN或ELEME,
  212. owner:账号权限all或one
  213. '''
  214. cursor = db.cursor()
  215. sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
  216. cursor.execute(sql)
  217. pair = json.loads(cursor.fetchall()[0][0])
  218. return pair
  219. def update_key_value_pair_7qiaoPlus(db,brand_name,wm_plate,owner,key_value_pair):
  220. '''
  221. 更新关键表,all_key_table字段,
  222. \ndb:数据库,
  223. \nbrand_name:道一云
  224. \nwm_plate:7qiaoPlus
  225. \nacct_kinds:chain
  226. \nowner: all
  227. \nuser:root
  228. \nkey:rootkey
  229. \ndata_pipe:{"corpId":"wp4nJkEAAAXE4BbTWEYSdyrX_-fl8vqA","secret":"fc092ce3365b4da39c682298f6b8684e","account":"wo4nJkEAAARtG8M8Ig6n6r5eyQ1Vj1ow"}
  230. \nkey_value_pair:为空值 ‘’ 时联网自动更新,不为空值时需手动更新
  231. '''
  232. cursor = db.cursor()
  233. if key_value_pair == '':
  234. sql = f'''SELECT * FROM all_key_table WHERE owner = '{owner}' AND brand_name = '{brand_name}' AND wm_plate = '{wm_plate}' ;'''
  235. cursor.execute(sql)
  236. df = cursor.fetchall()
  237. row = df[0]
  238. u_p = json.loads(row[6])
  239. corpId = u_p['corpId']
  240. secret = u_p['secret']
  241. account = u_p['account']
  242. timestamp = str(int(time.time()))
  243. randomint = str(random.randint(1, 10000))
  244. user = 'root'
  245. try:
  246. headers = {
  247. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}
  248. url_AccessKey = f'https://qiqiao.do1.com.cn/plus/cgi-bin/securities/access_key?timestamp={timestamp}&random={randomint}&corpId={corpId}&secret={secret}&account={account}'
  249. resp_AccessKey = requests.get(url_AccessKey, headers=headers).json()
  250. accessKey = resp_AccessKey['data']
  251. url_Token = f'https://qiqiao.do1.com.cn/plus/cgi-bin/securities/qiqiao_token?timestamp={timestamp}&random={randomint}&corpId={corpId}&secret={secret}&account={account}&accessKey={accessKey}'
  252. resp = requests.get(url_Token, headers=headers)
  253. data = resp.text
  254. except Exception as e:
  255. print(e)
  256. data = {}
  257. try:
  258. sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE all_key_table.user = '{user}' ;"
  259. value = (data)
  260. cursor.execute(sql_up, value)
  261. db.commit()
  262. print(user, '已更新')
  263. except Exception as e:
  264. print(e)
  265. else:
  266. try:
  267. sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE owner = '{owner}' AND brand_name = '{brand_name}' AND wm_plate = '{wm_plate}' ;"
  268. value = (key_value_pair)
  269. cursor.execute(sql_up, value)
  270. db.commit()
  271. print(f'{brand_name} {wm_plate} 键值对信息已更新')
  272. except Exception as e:
  273. print(e)
  274. db.close()
  275. if __name__ == '__main__':
  276. db = linkTomySql('124.222.188.59', '111...Clown', 'zuzu_data')
  277. # db = linkTomySql('localhost','12345678','zuzu_data')
  278. update_key_value_pair(db,'浆小白','ELEME','all','')
  279. # update_key_value_pair_7qiaoPlus(db,'道一云','7qiaoPlus','all','')
  280. #以下为初始化all_key_table表中的所有key_value_pair字段,联网部分
  281. # cursor = db.cursor()
  282. # sql = '''SELECT * FROM all_key_table WHERE owner = 'all';''' #选择所有满足条件的账户
  283. # cursor.execute(sql)
  284. # df = cursor.fetchall()
  285. # for row in df:
  286. # #遍历所有账户,并更新key_value_pair字段
  287. # u_p = json.loads(row[6])
  288. # username = u_p['username']
  289. # pw = u_p['pw']
  290. # sh_token = wkong_login(username,pw)
  291. #
  292. # wm_plate = row[1]
  293. # user = row[4]
  294. # data = take_token(wm_plate,user,sh_token)
  295. # try:
  296. # sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE all_key_table.user = '{user}' ;"
  297. # value = (data)
  298. # cursor.execute(sql_up,data)
  299. # db.commit()
  300. # print(user,'已更新')
  301. # except Exception as e:
  302. # print(e)
  303. #
  304. # db.close()
  305. #以上为初始化all_key_table表中的所有key_value_pair字段,联网部分