This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamap.py
64 lines (50 loc) · 2.1 KB
/
amap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
#coding=utf-8
#author:[email protected]
#last modified: 2018-02-12
from feedback import Feedback
import urllib2
import urllib
import json
import sys, os.path
AK = '10e08aaa9afb3c590d43f5fd86eef081'
CITY = '北京'
API_URL_BASE = 'http://restapi.amap.com/v3/place/text'
MAP_URL_BASE = 'https://ditu.amap.com/search?query=%s&city=%s'
def init_env():
global CITY, AK
if os.path.exists('city.txt'):
CITY = file('city.txt', 'r').read().strip('\r\n \t')
if os.path.exists('akey.txt'):
AK = file('akey.txt', 'r').read().strip('\r\n \t')
def main(args):
global CITY, AK, API_URL_BASE, MAP_URL_BASE
init_env()
region = urllib.quote(CITY)
if len(args) == 2:
query = urllib.quote(args[1])
# query = urllib.quote('天安门')
result = json.load(urllib2.urlopen(
'%s?keywords=%s&city=%s&output=json&key=%s&extensions=all' % (API_URL_BASE, query, region, AK)))
feeds = Feedback()
if result['status'] == '1':
if result['count'] == '0':
if urllib.quote('到') in query or urllib.quote('去') in query:
map_url = MAP_URL_BASE % (query, region)
feeds.add_item(title=args[1], subtitle=u"搜索规划路径",
valid='YES', arg=map_url, icon='icon.png')
else:
for i in result['pois']:
name = i.get('name', u'搜索不到结果')
address = i.get('address', '')
if urllib.quote('到') in query or urllib.quote('去') in query:
map_url = MAP_URL_BASE % (query, region)
else:
map_url = MAP_URL_BASE % (name, region)
feeds.add_item(title=name, subtitle=address, valid='YES', arg=map_url, icon='icon.png')
else:
feeds.add_item(title=u'内容未找到', subtitle=u'输入内容有误', valid='no', arg=MAP_URL_BASE, icon='icon.png')
print(feeds)
return
if __name__ == '__main__':
main(sys.argv)