all_key_table.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. print('\nconnect to mysql server 成功')
  39. print('---------------------------------------')
  40. except:
  41. print("\ncould not connect to mysql server")
  42. db = "连接失败"
  43. return db
  44. def wkong_login(username,pw):
  45. '''登录食亨悟空控件,返回值为type:str 【sh_token】'''
  46. try:
  47. cookie = ''
  48. url = 'https://bi.shihengtech.com/api/userlogin/login'
  49. 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',
  50. 'Cookie': cookie}
  51. params = {'username': username,
  52. 'password': pw}
  53. resp = requests.post(url, headers = headers ,json = params)
  54. sh_token = resp.json()['result']
  55. except:
  56. sh_token = ''
  57. return sh_token
  58. def take_token(wm_plate, user, sh_token):
  59. '''
  60. wm_plate:平台名称【MEITUAN】/【ELEME】
  61. user:外卖平台登录账号
  62. sh_token:通信证明
  63. '''
  64. try:
  65. url_wukong = 'https://bi.shihengtech.com/token-owner/acquire_token'
  66. headers_wukong = {
  67. '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',
  68. 'extension': '2.1.0'}
  69. params_wukong = {"platform": wm_plate,
  70. "username": user,
  71. "userToken": sh_token}
  72. resp = requests.post(url_wukong, headers=headers_wukong, json=params_wukong)
  73. data = resp.text
  74. except:
  75. data = {}
  76. '''
  77. 美团参数
  78. data = {"device_uuid":"!ce3baac4-0ef4-4c51-8e06-d50515d383db",
  79. "uuid_update":"true",
  80. "pushToken":"0VlrP_me3r43iR4zJqAPcilheMwouJO32OXkvzjsrXxs*",
  81. "acctId":"113245982",
  82. "token":"0fiJecyopKkIyVG7mb-qItH88bFRjfRkzCeugUySJTco*",
  83. "brandId":"-1",
  84. "city_id":"0",
  85. "isChain":"1",
  86. "existBrandPoi":"true",
  87. "ignore_set_router_proxy":"true",
  88. "region_id":"",
  89. "region_version":"0",
  90. "newCategory":"false",
  91. "bsid":"5-D2l3L0gOXLAhsFiizjlcgSqXQFLctqNKCMyhxp65hgGKgu8FGZskNq5hSJnGTg02QDgHTgXc0bx1Gm_4sqzw",
  92. "city_location_id":"0",
  93. "location_id":"0",
  94. "cityId":"310100",
  95. "provinceId":"310000",
  96. "wmPoiId":"-1",
  97. "_lxsdk_cuid":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  98. "_lxsdk":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  99. "uuid":"d85a7b5354c578c949b1.1654585692.1.0.0",
  100. "set_info":"%7B%22wmPoiId%22%3A14471551%2C%22ignoreSetRouterProxy%22%3Atrue%7D",
  101. "logan_session_token":"3s5se0y0p102jodkb4c3",
  102. "_lxsdk_s":"1815ab1658e-b68-cdb-52e%7C%7CNaN"}
  103. 饿了么参数(仅部分数据,如有需要可打印你出来查看)
  104. data = {'ksid': "NZUWN2MTA1Mjg5MjA0OTM5NjAxT1lGbGRPZTlQ",
  105. 'loginShopId': "99731295",
  106. 'shopId': "99731295"}
  107. '''
  108. return data
  109. def update_key_value_pair(db,brand_name,wm_plate,owner,key_value_pair):
  110. '''
  111. 更新关键表,all_key_table字段,
  112. db:数据库,
  113. brand_name:品牌名,
  114. wm_plate:外卖平台MEITUAN或ELEME,
  115. owner:账号权限all或one
  116. key_value_pair:为空值 ‘’ 时联网自动更新,不为空值时需手动更新
  117. '''
  118. cursor = db.cursor()
  119. if key_value_pair == '':
  120. sql = f'''SELECT * FROM all_key_table WHERE owner = '{owner}' AND brand_name = '{brand_name}' AND wm_plate = '{wm_plate}' ;'''
  121. cursor.execute(sql)
  122. df = cursor.fetchall()
  123. row = df[0]
  124. u_p = json.loads(row[6])
  125. username = u_p['username']
  126. pw = u_p['pw']
  127. wm_plate = row[1]
  128. user = row[4]
  129. try:
  130. cookie = ''
  131. url = 'https://bi.shihengtech.com/api/userlogin/login'
  132. 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',
  133. 'Cookie': cookie}
  134. params = {'username': username,
  135. 'password': pw}
  136. resp = requests.post(url, headers = headers ,json = params)
  137. sh_token = resp.json()['result']
  138. # print(sh_token)
  139. except Exception as e:
  140. print(e)
  141. sh_token = ''
  142. try:
  143. url_wukong = 'https://bi.shihengtech.com/token-owner/acquire_token'
  144. headers_wukong = {
  145. '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',
  146. 'extension': '2.1.0'}
  147. params_wukong = {"platform": wm_plate,
  148. "username": user,
  149. "userToken": sh_token}
  150. resp = requests.post(url_wukong, headers=headers_wukong, json=params_wukong)
  151. data = resp.text
  152. # print(data)
  153. except Exception as e:
  154. print(e)
  155. data = {}
  156. '''
  157. 美团参数
  158. data = {"device_uuid":"!ce3baac4-0ef4-4c51-8e06-d50515d383db",
  159. "uuid_update":"true",
  160. "pushToken":"0VlrP_me3r43iR4zJqAPcilheMwouJO32OXkvzjsrXxs*",
  161. "acctId":"113245982",
  162. "token":"0fiJecyopKkIyVG7mb-qItH88bFRjfRkzCeugUySJTco*",
  163. "brandId":"-1",
  164. "city_id":"0",
  165. "isChain":"1",
  166. "existBrandPoi":"true",
  167. "ignore_set_router_proxy":"true",
  168. "region_id":"",
  169. "region_version":"0",
  170. "newCategory":"false",
  171. "bsid":"5-D2l3L0gOXLAhsFiizjlcgSqXQFLctqNKCMyhxp65hgGKgu8FGZskNq5hSJnGTg02QDgHTgXc0bx1Gm_4sqzw",
  172. "city_location_id":"0",
  173. "location_id":"0",
  174. "cityId":"310100",
  175. "provinceId":"310000",
  176. "wmPoiId":"-1",
  177. "_lxsdk_cuid":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  178. "_lxsdk":"17e235602f4c8-05aa5f2b22c2a3-978153c-1fa400-17e235602f44f",
  179. "uuid":"d85a7b5354c578c949b1.1654585692.1.0.0",
  180. "set_info":"%7B%22wmPoiId%22%3A14471551%2C%22ignoreSetRouterProxy%22%3Atrue%7D",
  181. "logan_session_token":"3s5se0y0p102jodkb4c3",
  182. "_lxsdk_s":"1815ab1658e-b68-cdb-52e%7C%7CNaN"}
  183. 饿了么参数(仅部分数据,如有需要可打印你出来查看)
  184. data = {'ksid': "NZUWN2MTA1Mjg5MjA0OTM5NjAxT1lGbGRPZTlQ",
  185. 'loginShopId': "99731295",
  186. 'shopId': "99731295"}
  187. '''
  188. try:
  189. sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE all_key_table.user = '{user}' ;"
  190. value = (data)
  191. cursor.execute(sql_up, value)
  192. db.commit()
  193. print(user, '已更新')
  194. except Exception as e:
  195. print(e)
  196. else:
  197. try:
  198. 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}' ;"
  199. value = (key_value_pair)
  200. cursor.execute(sql_up, value)
  201. db.commit()
  202. print(f'{brand_name} {wm_plate} 键值对信息已更新')
  203. except Exception as e:
  204. print(e)
  205. db.close()
  206. def read_key_value_pair(db,brand_name,wm_plate,owner):
  207. '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,返回值type:dict【pair】
  208. db:数据库,
  209. brand_name:品牌名,
  210. wm_plate:外卖平台MEITUAN或ELEME,
  211. owner:账号权限all或one
  212. '''
  213. cursor = db.cursor()
  214. sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
  215. cursor.execute(sql)
  216. pair = json.loads(cursor.fetchall()[0][0])
  217. return pair
  218. def update_key_value_pair_7qiaoPlus(db,brand_name,wm_plate,owner,key_value_pair):
  219. '''
  220. 更新关键表,all_key_table字段,
  221. \ndb:数据库,
  222. \nbrand_name:道一云
  223. \nwm_plate:7qiaoPlus
  224. \nacct_kinds:chain
  225. \nowner: all
  226. \nuser:root
  227. \nkey:rootkey
  228. \ndata_pipe:{"corpId":"wp4nJkEAAAXE4BbTWEYSdyrX_-fl8vqA","secret":"fc092ce3365b4da39c682298f6b8684e","account":"wo4nJkEAAARtG8M8Ig6n6r5eyQ1Vj1ow"}
  229. \nkey_value_pair:为空值 ‘’ 时联网自动更新,不为空值时需手动更新
  230. '''
  231. cursor = db.cursor()
  232. if key_value_pair == '':
  233. sql = f'''SELECT * FROM all_key_table WHERE owner = '{owner}' AND brand_name = '{brand_name}' AND wm_plate = '{wm_plate}' ;'''
  234. cursor.execute(sql)
  235. df = cursor.fetchall()
  236. row = df[0]
  237. u_p = json.loads(row[6])
  238. corpId = u_p['corpId']
  239. secret = u_p['secret']
  240. account = u_p['account']
  241. timestamp = str(int(time.time()))
  242. randomint = str(random.randint(1, 10000))
  243. user = 'root'
  244. try:
  245. headers = {
  246. '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'}
  247. url_AccessKey = f'https://qiqiao.do1.com.cn/plus/cgi-bin/securities/access_key?timestamp={timestamp}&random={randomint}&corpId={corpId}&secret={secret}&account={account}'
  248. resp_AccessKey = requests.get(url_AccessKey, headers=headers).json()
  249. accessKey = resp_AccessKey['data']
  250. 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}'
  251. resp = requests.get(url_Token, headers=headers)
  252. data = resp.text
  253. except Exception as e:
  254. print(e)
  255. data = {}
  256. try:
  257. sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE all_key_table.user = '{user}' ;"
  258. value = (data)
  259. cursor.execute(sql_up, value)
  260. db.commit()
  261. print(user, '已更新')
  262. except Exception as e:
  263. print(e)
  264. else:
  265. try:
  266. 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}' ;"
  267. value = (key_value_pair)
  268. cursor.execute(sql_up, value)
  269. db.commit()
  270. print(f'{brand_name} {wm_plate} 键值对信息已更新')
  271. except Exception as e:
  272. print(e)
  273. db.close()
  274. # def test():
  275. # r = requests.get('http://www.baidu.com').text
  276. # print(r)
  277. # print('这是test函数')
  278. #
  279. # def test_demo(a):
  280. # test()
  281. # print(a)
  282. if __name__ == '__main__':
  283. db = linkTomySql('localhost','111???clown','hexingxing')
  284. # db = linkTomySql('localhost','12345678','zuzu_data')
  285. update_key_value_pair(db,'浆小白','ELEME','all','')
  286. # update_key_value_pair_7qiaoPlus(db,'道一云','7qiaoPlus','all','')
  287. #以下为初始化all_key_table表中的所有key_value_pair字段,联网部分
  288. # cursor = db.cursor()
  289. # sql = '''SELECT * FROM all_key_table WHERE owner = 'all';''' #选择所有满足条件的账户
  290. # cursor.execute(sql)
  291. # df = cursor.fetchall()
  292. # for row in df:
  293. # #遍历所有账户,并更新key_value_pair字段
  294. # u_p = json.loads(row[6])
  295. # username = u_p['username']
  296. # pw = u_p['pw']
  297. # sh_token = wkong_login(username,pw)
  298. #
  299. # wm_plate = row[1]
  300. # user = row[4]
  301. # data = take_token(wm_plate,user,sh_token)
  302. # try:
  303. # sql_up = f"UPDATE all_key_table SET key_value_pair = (%s) WHERE all_key_table.user = '{user}' ;"
  304. # value = (data)
  305. # cursor.execute(sql_up,data)
  306. # db.commit()
  307. # print(user,'已更新')
  308. # except Exception as e:
  309. # print(e)
  310. #
  311. # db.close()
  312. #以上为初始化all_key_table表中的所有key_value_pair字段,联网部分