demo_坐标查询.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2023/2/20 14:44
  3. # @Author : Clown
  4. # @File : demo_坐标查询.py
  5. # @Software : PyCharm
  6. import json
  7. import requests
  8. import pymysql
  9. from all_key_table import update_key_value_pair
  10. def linkTomySql(host, passwd, db_name, port):
  11. '''连接至数据库返回【db】,v2新增local_infile=1 打开文件导入权限'''
  12. try:
  13. # 本地连接为:localhost 服务器连接为:124.222.188.59
  14. db = pymysql.connect (
  15. host=host, user="root",
  16. passwd=passwd,
  17. db=db_name,
  18. charset='utf8mb4',
  19. local_infile=1,
  20. port=port)
  21. # print ('\nconnect to mysql server 成功')
  22. # print ('---------------------------------------')
  23. except:
  24. print ("\ncould not connect to mysql server")
  25. db = "连接失败"
  26. return db
  27. def read_key_value_pair(db, brand_name, wm_plate, owner):
  28. '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,以键值对的形式输出
  29. db:数据库,
  30. brand_name:品牌名,
  31. wm_plate:外卖平台MEITUAN或ELEME,
  32. owner:账号权限all或one
  33. '''
  34. cursor = db.cursor ()
  35. sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
  36. cursor.execute (sql)
  37. pair = json.loads (cursor.fetchall ()[0][0])
  38. return pair
  39. def baiduMapApi(longitude,latitude):
  40. try:
  41. ak = 'QdpAXorciOerk5o6UMhzqULSb9IcAv0E'
  42. url = f'https://api.map.baidu.com/geoconv/v1/?coords={longitude/1000000},{latitude/1000000}&from=3&to=5&ak={ak}'
  43. resp = requests.get(url).json()['result'][0]
  44. return resp['x'],resp['y']
  45. except Exception as e:
  46. print(e)
  47. def searchAddressByMeituanId(shop_id,token,acctId):
  48. url = f'https://e.waimai.meituan.com/gw/api/poi/getPoiSettingInfo?ignoreSetRouterProxy=true&wmPoiId={shop_id}'
  49. url_address = 'https://e.waimai.meituan.com/gw/api/logistics/other_infos?ignoreSetRouterProxy=true'
  50. Cookie = "_lxsdk_cuid=179f429d147c8-0a9df495c11ad-f7f1939-1344de-179f429d147c8; _lxsdk=179f429d147c8-0a9df495c11ad-f7f1939-1344de-179f429d147c8; uuid=a2eb8a0d50af269ca4b5.1632046159.1.0.0; igateApp=marketingpc; e_u_id_3299326472=eeecd0a11f674170367d0e3bed656be4; " \
  51. "token=%s; " % str (token) + \
  52. "acctId=%s; " % str (acctId) + \
  53. "wmPoiId=%s; " % str (shop_id) + \
  54. "bsid=dRamAy7JP4J-I9cuFLYEouiUxXbO8wbQX1PBXj_k53yFZhqsoZYl9Eqb7GPMTM1KMZuMwMQ9-hHdKkmRT3mjbQ; _lxsdk_s=17f2ede2415-aa8-23e-8f1%7C%7C36"
  55. headers = {
  56. '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',
  57. 'Accept': 'application/json, text/plain, */*',
  58. 'Cookie': Cookie}
  59. address = ''
  60. try:
  61. resp = requests.get (url, headers=headers).json ()['data']
  62. longitude, latitude = baiduMapApi (resp['longitude'], resp['latitude'])
  63. print (longitude, latitude)
  64. try:
  65. resp_adresses = requests.post (url_address, headers=headers).json ()['data']['otherItemList']
  66. for adress_i in resp_adresses:
  67. moduleName = adress_i['moduleName']
  68. if moduleName == "取餐地址":
  69. address = adress_i['content']
  70. print (address)
  71. except:
  72. address = 'coordinateGeneratedAddressError'
  73. print ('Error!')
  74. msg = 1
  75. except:
  76. longitude = 0
  77. latitude = 0
  78. address = 'coordinateNotGenerated'
  79. msg = 0
  80. out_json = {'lat': latitude,
  81. 'lng': longitude,
  82. 'address': address,
  83. 'msg':msg}
  84. return out_json
  85. def searchAddressByElemeId(shop_id,ksid):
  86. url = 'https://app-api.shop.ele.me/shop/invoke/?method=queryShop.getShopView'
  87. headers = {
  88. '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',
  89. 'Accept': 'application/json, text/plain, */*'}
  90. json_params = {"service": "queryShop",
  91. "method": "getShopView",
  92. "params": {"shopId": shop_id},
  93. "id": "C42391FEED4D4055BD2273DC50357372|1676877589661",
  94. "metas": {"appVersion": "1.0.0",
  95. "appName": "melody",
  96. "ksid": ksid,
  97. "shopId": shop_id},
  98. "ncp": "2.0.0"}
  99. try:
  100. resp = requests.post (url, headers=headers, json=json_params).json ()['result']['basis']
  101. longitude, latitude = baiduMapApi (resp['location']['longitude'] * 1000000,
  102. resp['location']['latitude'] * 1000000)
  103. address = resp['address']
  104. msg = 1
  105. print (longitude, latitude, address)
  106. except:
  107. longitude = 0
  108. latitude = 0
  109. address = 'coordinateNotGenerated'
  110. msg = 0
  111. print ('Error!')
  112. out_json = {'lat': latitude,
  113. 'lng': longitude,
  114. 'address': address,
  115. 'msg':msg}
  116. return out_json
  117. def searchAddressByShopId(json_params_in):
  118. host = '124.222.188.59'
  119. passwd = '111...Clown'
  120. db_name = 'zuzu_data'
  121. port = 63306
  122. db = linkTomySql (host, passwd, db_name, port)
  123. brandName = json_params_in['brandName']
  124. wmPlate = json_params_in['wmPlate']
  125. shopId = json_params_in['shopId']
  126. pair = read_key_value_pair (db, brandName, wmPlate, 'all')
  127. if json_params_in['wmPlate'] == 'MEITUAN':
  128. try:
  129. token = pair['data']['session']['token']
  130. acctId = pair['data']['session']['acctId']
  131. msg = 1
  132. except:
  133. update_key_value_pair (db, brandName, wmPlate, 'all', '')
  134. pair = read_key_value_pair (db, brandName, wmPlate, 'all')
  135. try:
  136. token = pair['data']['session']['token']
  137. acctId = pair['data']['session']['acctId']
  138. msg = 1
  139. except:
  140. token = '0'
  141. acctId = '0'
  142. msg = 0
  143. if msg == 1:
  144. out_json = searchAddressByMeituanId (shopId, token, acctId)
  145. else:
  146. out_json = {'lat': 0,
  147. 'lng': 0,
  148. 'address': 'globalParsingError',
  149. 'msg':0}
  150. elif json_params_in['wmPlate'] == 'ELEME':
  151. try:
  152. ksid = pair['data']['session']['ksid']
  153. msg = 1
  154. except:
  155. update_key_value_pair (db, brandName, wmPlate, 'all', '')
  156. pair = read_key_value_pair (db, brandName, wmPlate, 'all')
  157. try:
  158. ksid = pair['data']['session']['ksid']
  159. msg = 1
  160. except:
  161. ksid = '0'
  162. msg = 0
  163. if msg == 1:
  164. out_json = searchAddressByElemeId(shopId,ksid)
  165. else:
  166. out_json = {'lat': 0,
  167. 'lng': 0,
  168. 'address': 'globalParsingError',
  169. 'msg':0}
  170. return out_json
  171. if __name__ == '__main__':
  172. host = '124.222.188.59'
  173. passwd = '111...Clown'
  174. db_name = 'zuzu_data'
  175. port = 63306
  176. db = linkTomySql (host, passwd, db_name, port)
  177. json_params_in = {'brandName':'粥小鲜',
  178. 'wmPlate':'ELEME',
  179. 'shopId':508370837}
  180. result= searchAddressByShopId (json_params_in)
  181. print(result)
  182. # pair = read_key_value_pair (db, json_params_in['brandName'], json_params_in['wmPlate'], 'all')
  183. #
  184. # if json_params_in['wmPlate'] == 'MEITUAN':
  185. # shop_id = json_params_in['shopId']
  186. # token = pair['data']['session']['token']
  187. # acctId = pair['data']['session']['acctId']
  188. # url = f'https://e.waimai.meituan.com/gw/api/poi/getPoiSettingInfo?ignoreSetRouterProxy=true&wmPoiId={shop_id}'
  189. # url_address = 'https://e.waimai.meituan.com/gw/api/logistics/other_infos?ignoreSetRouterProxy=true'
  190. # Cookie = "_lxsdk_cuid=179f429d147c8-0a9df495c11ad-f7f1939-1344de-179f429d147c8; _lxsdk=179f429d147c8-0a9df495c11ad-f7f1939-1344de-179f429d147c8; uuid=a2eb8a0d50af269ca4b5.1632046159.1.0.0; igateApp=marketingpc; e_u_id_3299326472=eeecd0a11f674170367d0e3bed656be4; " \
  191. # "token=%s; " % str (token) + \
  192. # "acctId=%s; " % str (acctId) + \
  193. # "wmPoiId=%s; " % str (shop_id) + \
  194. # "bsid=dRamAy7JP4J-I9cuFLYEouiUxXbO8wbQX1PBXj_k53yFZhqsoZYl9Eqb7GPMTM1KMZuMwMQ9-hHdKkmRT3mjbQ; _lxsdk_s=17f2ede2415-aa8-23e-8f1%7C%7C36"
  195. # headers = {
  196. # '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',
  197. # 'Accept': 'application/json, text/plain, */*',
  198. # 'Cookie': Cookie}
  199. # try:
  200. # resp = requests.get (url, headers=headers).json ()['data']
  201. # longitude, latitude = baiduMapApi (resp['longitude'], resp['latitude'])
  202. # print (longitude, latitude)
  203. # resp_adresses = requests.post(url_address,headers=headers).json()['data']['otherItemList']
  204. # adress = '无法解析'
  205. # for adress_i in resp_adresses:
  206. # moduleName = adress_i['moduleName']
  207. # if moduleName == "取餐地址":
  208. # adress = adress_i['content']
  209. # print(adress)
  210. # except:
  211. # print('Error!')
  212. #
  213. #
  214. # elif json_params_in['wmPlate'] == 'ELEME':
  215. # shop_id = json_params_in['shopId']
  216. # ksid = pair['data']['session']['ksid']
  217. # url = 'https://app-api.shop.ele.me/shop/invoke/?method=queryShop.getShopView'
  218. # headers = { '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',
  219. # 'Accept': 'application/json, text/plain, */*'}
  220. # json_params = {"service":"queryShop",
  221. # "method":"getShopView",
  222. # "params":{"shopId":shop_id},
  223. # "id":"C42391FEED4D4055BD2273DC50357372|1676877589661",
  224. # "metas":{"appVersion":"1.0.0",
  225. # "appName":"melody",
  226. # "ksid":ksid,
  227. # "shopId":shop_id},
  228. # "ncp":"2.0.0"}
  229. # try:
  230. # resp = requests.post(url,headers=headers,json=json_params).json()['result']['basis']
  231. # longitude, latitude = baiduMapApi (resp['location']['longitude']*1000000, resp['location']['latitude']*1000000)
  232. # address = resp['address']
  233. # print (longitude, latitude, address)
  234. # except:
  235. # print ('Error!')