123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- # -*- codeing = utf-8 -*-
- # @Time : 2023/2/20 14:44
- # @Author : Clown
- # @File : demo_坐标查询.py
- # @Software : PyCharm
- import json
- import requests
- import pymysql
- from all_key_table import update_key_value_pair
- def linkTomySql(host, passwd, db_name, port):
- '''连接至数据库返回【db】,v2新增local_infile=1 打开文件导入权限'''
- try:
- # 本地连接为:localhost 服务器连接为:124.222.188.59
- db = pymysql.connect (
- host=host, user="root",
- passwd=passwd,
- db=db_name,
- charset='utf8mb4',
- local_infile=1,
- port=port)
- # print ('\nconnect to mysql server 成功')
- # print ('---------------------------------------')
- except:
- print ("\ncould not connect to mysql server")
- db = "连接失败"
- return db
- def read_key_value_pair(db, brand_name, wm_plate, owner):
- '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,以键值对的形式输出
- db:数据库,
- brand_name:品牌名,
- wm_plate:外卖平台MEITUAN或ELEME,
- owner:账号权限all或one
- '''
- cursor = db.cursor ()
- sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
- cursor.execute (sql)
- pair = json.loads (cursor.fetchall ()[0][0])
- return pair
- def baiduMapApi(longitude,latitude):
- try:
- ak = 'QdpAXorciOerk5o6UMhzqULSb9IcAv0E'
- url = f'https://api.map.baidu.com/geoconv/v1/?coords={longitude/1000000},{latitude/1000000}&from=3&to=5&ak={ak}'
- resp = requests.get(url).json()['result'][0]
- return resp['x'],resp['y']
- except Exception as e:
- print(e)
- def baiduMapApiGeocoding(json_params_in):
- address = json_params_in['address']
- trueList = ["乡镇","村庄","道路","地产小区","商务大厦","政府机构","交叉路口","商圈","生活服务","休闲娱乐","餐饮","宾馆","购物","金融","教育","医疗 ","工业园区 ","旅游景点 ","汽车服务","火车站","长途汽车站","桥 ","停车场/停车区","港口/码头","收费区/收费站","飞机场 ","机场 ","收费处/收费站 ","加油站","绿地"]
- falseList = ["UNKNOWN","国家","省","城市","区县"]
- try:
- ak = 'QdpAXorciOerk5o6UMhzqULSb9IcAv0E'
- url = f'https://api.map.baidu.com/geocoding/v3/?address={address}&output=json&ak={ak}'
- resp = requests.get (url).json ()['result']
- latitude = resp['location']['lat']
- longitude = resp['location']['lng']
- level = resp['level']
- if level in trueList:
- memo = 'accurateMid'
- elif level in falseList:
- memo = 'accurateLow'
- elif level == '门址':
- memo = 'accurateHigh'
- msg = 1
- except Exception as e:
- latitude = 0
- longitude = 0
- memo = 'Error'
- level = 'Error'
- msg = 0
- print (e)
- out_json = {'lat': latitude,
- 'lng': longitude,
- 'memo': memo,
- 'level':level,
- 'msg':msg}
- return out_json
- def searchAddressByMeituanId(shop_id,token,acctId):
- url = f'https://e.waimai.meituan.com/gw/api/poi/getPoiSettingInfo?ignoreSetRouterProxy=true&wmPoiId={shop_id}'
- url_address = 'https://e.waimai.meituan.com/gw/api/logistics/other_infos?ignoreSetRouterProxy=true'
- 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; " \
- "token=%s; " % str (token) + \
- "acctId=%s; " % str (acctId) + \
- "wmPoiId=%s; " % str (shop_id) + \
- "bsid=dRamAy7JP4J-I9cuFLYEouiUxXbO8wbQX1PBXj_k53yFZhqsoZYl9Eqb7GPMTM1KMZuMwMQ9-hHdKkmRT3mjbQ; _lxsdk_s=17f2ede2415-aa8-23e-8f1%7C%7C36"
- 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',
- 'Accept': 'application/json, text/plain, */*',
- 'Cookie': Cookie}
- address = ''
- try:
- resp = requests.get (url, headers=headers).json ()['data']
- longitude, latitude = baiduMapApi (resp['longitude'], resp['latitude'])
- print (longitude, latitude)
- try:
- resp_adresses = requests.post (url_address, headers=headers).json ()['data']['otherItemList']
- for adress_i in resp_adresses:
- moduleName = adress_i['moduleName']
- if moduleName == "取餐地址":
- address = adress_i['content']
- print (address)
- except:
- address = 'coordinateGeneratedAddressError'
- print ('Error!')
- msg = 1
- except:
- longitude = 0
- latitude = 0
- address = 'coordinateNotGenerated'
- msg = 0
- out_json = {'lat': latitude,
- 'lng': longitude,
- 'address': address,
- 'msg':msg}
- return out_json
- def searchAddressByElemeId(shop_id,ksid):
- url = 'https://app-api.shop.ele.me/shop/invoke/?method=queryShop.getShopView'
- 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',
- 'Accept': 'application/json, text/plain, */*'}
- json_params = {"service": "queryShop",
- "method": "getShopView",
- "params": {"shopId": shop_id},
- "id": "C42391FEED4D4055BD2273DC50357372|1676877589661",
- "metas": {"appVersion": "1.0.0",
- "appName": "melody",
- "ksid": ksid,
- "shopId": shop_id},
- "ncp": "2.0.0"}
- try:
- resp = requests.post (url, headers=headers, json=json_params).json ()['result']['basis']
- longitude, latitude = baiduMapApi (resp['location']['longitude'] * 1000000,
- resp['location']['latitude'] * 1000000)
- address = resp['address']
- msg = 1
- print (longitude, latitude, address)
- except:
- longitude = 0
- latitude = 0
- address = 'coordinateNotGenerated'
- msg = 0
- print ('Error!')
- out_json = {'lat': latitude,
- 'lng': longitude,
- 'address': address,
- 'msg':msg}
- return out_json
- def searchAddressByShopId(json_params_in):
- host = '124.222.188.59'
- passwd = '111...Clown'
- db_name = 'zuzu_data'
- port = 63306
- db = linkTomySql (host, passwd, db_name, port)
- brandName = json_params_in['brandName']
- wmPlate = json_params_in['wmPlate']
- shopId = json_params_in['shopId']
- pair = read_key_value_pair (db, brandName, wmPlate, 'all')
- if json_params_in['wmPlate'] == 'MEITUAN':
- try:
- token = pair['data']['session']['token']
- acctId = pair['data']['session']['acctId']
- msg = 1
- except:
- update_key_value_pair (db, brandName, wmPlate, 'all', '')
- pair = read_key_value_pair (db, brandName, wmPlate, 'all')
- try:
- token = pair['data']['session']['token']
- acctId = pair['data']['session']['acctId']
- msg = 1
- except:
- token = '0'
- acctId = '0'
- msg = 0
- if msg == 1:
- out_json = searchAddressByMeituanId (shopId, token, acctId)
- else:
- out_json = {'lat': 0,
- 'lng': 0,
- 'address': 'globalParsingError',
- 'msg':0}
- elif json_params_in['wmPlate'] == 'ELEME':
- try:
- ksid = pair['data']['session']['ksid']
- msg = 1
- except:
- update_key_value_pair (db, brandName, wmPlate, 'all', '')
- pair = read_key_value_pair (db, brandName, wmPlate, 'all')
- try:
- ksid = pair['data']['session']['ksid']
- msg = 1
- except:
- ksid = '0'
- msg = 0
- if msg == 1:
- out_json = searchAddressByElemeId(shopId,ksid)
- else:
- out_json = {'lat': 0,
- 'lng': 0,
- 'address': 'globalParsingError',
- 'msg':0}
- return out_json
- if __name__ == '__main__':
- if 1 == 1:
- host = '124.222.188.59'
- passwd = '111...Clown'
- db_name = 'zuzu_data'
- port = 63306
- db = linkTomySql (host, passwd, db_name, port)
- json_params_in = {'brandName':'浆小白',
- 'wmPlate':'MEITUAN',
- 'shopId':20716671}
- result= searchAddressByShopId (json_params_in)
- print(result)
- if 1==0:
- address = {'address' :'上海市溧阳路111号'}
- print(baiduMapApiGeocoding (address))
- # pair = read_key_value_pair (db, json_params_in['brandName'], json_params_in['wmPlate'], 'all')
- #
- # if json_params_in['wmPlate'] == 'MEITUAN':
- # shop_id = json_params_in['shopId']
- # token = pair['data']['session']['token']
- # acctId = pair['data']['session']['acctId']
- # url = f'https://e.waimai.meituan.com/gw/api/poi/getPoiSettingInfo?ignoreSetRouterProxy=true&wmPoiId={shop_id}'
- # url_address = 'https://e.waimai.meituan.com/gw/api/logistics/other_infos?ignoreSetRouterProxy=true'
- # 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; " \
- # "token=%s; " % str (token) + \
- # "acctId=%s; " % str (acctId) + \
- # "wmPoiId=%s; " % str (shop_id) + \
- # "bsid=dRamAy7JP4J-I9cuFLYEouiUxXbO8wbQX1PBXj_k53yFZhqsoZYl9Eqb7GPMTM1KMZuMwMQ9-hHdKkmRT3mjbQ; _lxsdk_s=17f2ede2415-aa8-23e-8f1%7C%7C36"
- # 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',
- # 'Accept': 'application/json, text/plain, */*',
- # 'Cookie': Cookie}
- # try:
- # resp = requests.get (url, headers=headers).json ()['data']
- # longitude, latitude = baiduMapApi (resp['longitude'], resp['latitude'])
- # print (longitude, latitude)
- # resp_adresses = requests.post(url_address,headers=headers).json()['data']['otherItemList']
- # adress = '无法解析'
- # for adress_i in resp_adresses:
- # moduleName = adress_i['moduleName']
- # if moduleName == "取餐地址":
- # adress = adress_i['content']
- # print(adress)
- # except:
- # print('Error!')
- #
- #
- # elif json_params_in['wmPlate'] == 'ELEME':
- # shop_id = json_params_in['shopId']
- # ksid = pair['data']['session']['ksid']
- # url = 'https://app-api.shop.ele.me/shop/invoke/?method=queryShop.getShopView'
- # 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',
- # 'Accept': 'application/json, text/plain, */*'}
- # json_params = {"service":"queryShop",
- # "method":"getShopView",
- # "params":{"shopId":shop_id},
- # "id":"C42391FEED4D4055BD2273DC50357372|1676877589661",
- # "metas":{"appVersion":"1.0.0",
- # "appName":"melody",
- # "ksid":ksid,
- # "shopId":shop_id},
- # "ncp":"2.0.0"}
- # try:
- # resp = requests.post(url,headers=headers,json=json_params).json()['result']['basis']
- # longitude, latitude = baiduMapApi (resp['location']['longitude']*1000000, resp['location']['latitude']*1000000)
- # address = resp['address']
- # print (longitude, latitude, address)
- # except:
- # print ('Error!')
|