shopPointSearch.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 baiduMapApiGeocoding(json_params_in):
  48. address = json_params_in['address']
  49. trueList = ["乡镇","村庄","道路","地产小区","商务大厦","政府机构","交叉路口","商圈","生活服务","休闲娱乐","餐饮","宾馆","购物","金融","教育","医疗 ","工业园区 ","旅游景点 ","汽车服务","火车站","长途汽车站","桥 ","停车场/停车区","港口/码头","收费区/收费站","飞机场 ","机场 ","收费处/收费站 ","加油站","绿地"]
  50. falseList = ["UNKNOWN","国家","省","城市","区县"]
  51. try:
  52. ak = 'QdpAXorciOerk5o6UMhzqULSb9IcAv0E'
  53. url = f'https://api.map.baidu.com/geocoding/v3/?address={address}&output=json&ak={ak}'
  54. resp = requests.get (url).json ()['result']
  55. latitude = resp['location']['lat']
  56. longitude = resp['location']['lng']
  57. level = resp['level']
  58. if level in trueList:
  59. memo = 'accurateMid'
  60. elif level in falseList:
  61. memo = 'accurateLow'
  62. elif level == '门址':
  63. memo = 'accurateHigh'
  64. msg = 1
  65. except Exception as e:
  66. latitude = 0
  67. longitude = 0
  68. memo = 'Error'
  69. level = 'Error'
  70. msg = 0
  71. print (e)
  72. out_json = {'lat': latitude,
  73. 'lng': longitude,
  74. 'memo': memo,
  75. 'level':level,
  76. 'msg':msg}
  77. return out_json
  78. def searchAddressByMeituanId(shop_id,token,acctId):
  79. url = f'https://e.waimai.meituan.com/gw/api/poi/getPoiSettingInfo?ignoreSetRouterProxy=true&wmPoiId={shop_id}'
  80. url_address = 'https://e.waimai.meituan.com/gw/api/logistics/other_infos?ignoreSetRouterProxy=true'
  81. 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; " \
  82. "token=%s; " % str (token) + \
  83. "acctId=%s; " % str (acctId) + \
  84. "wmPoiId=%s; " % str (shop_id) + \
  85. "bsid=dRamAy7JP4J-I9cuFLYEouiUxXbO8wbQX1PBXj_k53yFZhqsoZYl9Eqb7GPMTM1KMZuMwMQ9-hHdKkmRT3mjbQ; _lxsdk_s=17f2ede2415-aa8-23e-8f1%7C%7C36"
  86. headers = {
  87. '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',
  88. 'Accept': 'application/json, text/plain, */*',
  89. 'Cookie': Cookie}
  90. address = ''
  91. try:
  92. resp = requests.get (url, headers=headers).json ()['data']
  93. longitude, latitude = baiduMapApi (resp['longitude'], resp['latitude'])
  94. print (longitude, latitude)
  95. try:
  96. resp_adresses = requests.post (url_address, headers=headers).json ()['data']['otherItemList']
  97. for adress_i in resp_adresses:
  98. moduleName = adress_i['moduleName']
  99. if moduleName == "取餐地址":
  100. address = adress_i['content']
  101. print (address)
  102. except:
  103. address = 'coordinateGeneratedAddressError'
  104. print ('Error!')
  105. msg = 1
  106. except:
  107. longitude = 0
  108. latitude = 0
  109. address = 'coordinateNotGenerated'
  110. msg = 0
  111. out_json = {'lat': latitude,
  112. 'lng': longitude,
  113. 'address': address,
  114. 'msg':msg}
  115. return out_json
  116. def searchAddressByElemeId(shop_id,ksid):
  117. url = 'https://app-api.shop.ele.me/shop/invoke/?method=queryShop.getShopView'
  118. headers = {
  119. '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',
  120. 'Accept': 'application/json, text/plain, */*'}
  121. json_params = {"service": "queryShop",
  122. "method": "getShopView",
  123. "params": {"shopId": shop_id},
  124. "id": "C42391FEED4D4055BD2273DC50357372|1676877589661",
  125. "metas": {"appVersion": "1.0.0",
  126. "appName": "melody",
  127. "ksid": ksid,
  128. "shopId": shop_id},
  129. "ncp": "2.0.0"}
  130. try:
  131. resp = requests.post (url, headers=headers, json=json_params).json ()['result']['basis']
  132. longitude, latitude = baiduMapApi (resp['location']['longitude'] * 1000000,
  133. resp['location']['latitude'] * 1000000)
  134. address = resp['address']
  135. msg = 1
  136. print (longitude, latitude, address)
  137. except:
  138. longitude = 0
  139. latitude = 0
  140. address = 'coordinateNotGenerated'
  141. msg = 0
  142. print ('Error!')
  143. out_json = {'lat': latitude,
  144. 'lng': longitude,
  145. 'address': address,
  146. 'msg':msg}
  147. return out_json
  148. def searchAddressByShopId(json_params_in):
  149. host = '124.222.188.59'
  150. passwd = '111...Clown'
  151. db_name = 'zuzu_data'
  152. port = 63306
  153. db = linkTomySql (host, passwd, db_name, port)
  154. brandName = json_params_in['brandName']
  155. wmPlate = json_params_in['wmPlate']
  156. shopId = json_params_in['shopId']
  157. pair = read_key_value_pair (db, brandName, wmPlate, 'all')
  158. if json_params_in['wmPlate'] == 'MEITUAN':
  159. try:
  160. token = pair['data']['session']['token']
  161. acctId = pair['data']['session']['acctId']
  162. msg = 1
  163. except:
  164. update_key_value_pair (db, brandName, wmPlate, 'all', '')
  165. pair = read_key_value_pair (db, brandName, wmPlate, 'all')
  166. try:
  167. token = pair['data']['session']['token']
  168. acctId = pair['data']['session']['acctId']
  169. msg = 1
  170. except:
  171. token = '0'
  172. acctId = '0'
  173. msg = 0
  174. if msg == 1:
  175. out_json = searchAddressByMeituanId (shopId, token, acctId)
  176. else:
  177. out_json = {'lat': 0,
  178. 'lng': 0,
  179. 'address': 'globalParsingError',
  180. 'msg':0}
  181. elif json_params_in['wmPlate'] == 'ELEME':
  182. try:
  183. ksid = pair['data']['session']['ksid']
  184. msg = 1
  185. except:
  186. update_key_value_pair (db, brandName, wmPlate, 'all', '')
  187. pair = read_key_value_pair (db, brandName, wmPlate, 'all')
  188. try:
  189. ksid = pair['data']['session']['ksid']
  190. msg = 1
  191. except:
  192. ksid = '0'
  193. msg = 0
  194. if msg == 1:
  195. out_json = searchAddressByElemeId(shopId,ksid)
  196. else:
  197. out_json = {'lat': 0,
  198. 'lng': 0,
  199. 'address': 'globalParsingError',
  200. 'msg':0}
  201. return out_json
  202. if __name__ == '__main__':
  203. if 1 == 1:
  204. host = '124.222.188.59'
  205. passwd = '111...Clown'
  206. db_name = 'zuzu_data'
  207. port = 63306
  208. db = linkTomySql (host, passwd, db_name, port)
  209. json_params_in = {'brandName':'浆小白',
  210. 'wmPlate':'MEITUAN',
  211. 'shopId':20716671}
  212. result= searchAddressByShopId (json_params_in)
  213. print(result)
  214. if 1==0:
  215. address = {'address' :'上海市溧阳路111号'}
  216. print(baiduMapApiGeocoding (address))
  217. # pair = read_key_value_pair (db, json_params_in['brandName'], json_params_in['wmPlate'], 'all')
  218. #
  219. # if json_params_in['wmPlate'] == 'MEITUAN':
  220. # shop_id = json_params_in['shopId']
  221. # token = pair['data']['session']['token']
  222. # acctId = pair['data']['session']['acctId']
  223. # url = f'https://e.waimai.meituan.com/gw/api/poi/getPoiSettingInfo?ignoreSetRouterProxy=true&wmPoiId={shop_id}'
  224. # url_address = 'https://e.waimai.meituan.com/gw/api/logistics/other_infos?ignoreSetRouterProxy=true'
  225. # 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; " \
  226. # "token=%s; " % str (token) + \
  227. # "acctId=%s; " % str (acctId) + \
  228. # "wmPoiId=%s; " % str (shop_id) + \
  229. # "bsid=dRamAy7JP4J-I9cuFLYEouiUxXbO8wbQX1PBXj_k53yFZhqsoZYl9Eqb7GPMTM1KMZuMwMQ9-hHdKkmRT3mjbQ; _lxsdk_s=17f2ede2415-aa8-23e-8f1%7C%7C36"
  230. # headers = {
  231. # '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',
  232. # 'Accept': 'application/json, text/plain, */*',
  233. # 'Cookie': Cookie}
  234. # try:
  235. # resp = requests.get (url, headers=headers).json ()['data']
  236. # longitude, latitude = baiduMapApi (resp['longitude'], resp['latitude'])
  237. # print (longitude, latitude)
  238. # resp_adresses = requests.post(url_address,headers=headers).json()['data']['otherItemList']
  239. # adress = '无法解析'
  240. # for adress_i in resp_adresses:
  241. # moduleName = adress_i['moduleName']
  242. # if moduleName == "取餐地址":
  243. # adress = adress_i['content']
  244. # print(adress)
  245. # except:
  246. # print('Error!')
  247. #
  248. #
  249. # elif json_params_in['wmPlate'] == 'ELEME':
  250. # shop_id = json_params_in['shopId']
  251. # ksid = pair['data']['session']['ksid']
  252. # url = 'https://app-api.shop.ele.me/shop/invoke/?method=queryShop.getShopView'
  253. # 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',
  254. # 'Accept': 'application/json, text/plain, */*'}
  255. # json_params = {"service":"queryShop",
  256. # "method":"getShopView",
  257. # "params":{"shopId":shop_id},
  258. # "id":"C42391FEED4D4055BD2273DC50357372|1676877589661",
  259. # "metas":{"appVersion":"1.0.0",
  260. # "appName":"melody",
  261. # "ksid":ksid,
  262. # "shopId":shop_id},
  263. # "ncp":"2.0.0"}
  264. # try:
  265. # resp = requests.post(url,headers=headers,json=json_params).json()['result']['basis']
  266. # longitude, latitude = baiduMapApi (resp['location']['longitude']*1000000, resp['location']['latitude']*1000000)
  267. # address = resp['address']
  268. # print (longitude, latitude, address)
  269. # except:
  270. # print ('Error!')