Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
add all files
Browse files Browse the repository at this point in the history
  • Loading branch information
wofeiwo committed Feb 12, 2018
1 parent 9b9696c commit c2c41c8
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
Binary file added 29388DAE-861D-424E-9B53-9D07B2D789B6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions amap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/python
#coding=utf-8
#author:wofeiwo@gmail.com
#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)
48 changes: 48 additions & 0 deletions feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#author: Peter Okma
#coding=utf-8
import xml.etree.ElementTree as et


class Feedback():
"""Feeback used by Alfred Script Filter
Usage:
fb = Feedback()
fb.add_item('Hello', 'World')
fb.add_item('Foo', 'Bar')
print fb
"""

def __init__(self):
self.feedback = et.Element('items')

def __repr__(self):
"""XML representation used by Alfred
Returns:
XML string
"""
return et.tostring(self.feedback)

def add_item(self, title, subtitle="", arg="", valid="yes", autocomplete="", icon="icon.png"):
"""
Add item to alfred Feedback
Args:
title(str): the title displayed by Alfred
Keyword Args:
subtitle(str): the subtitle displayed by Alfred
arg(str): the value returned by alfred when item is selected
valid(str): whether or not the entry can be selected in Alfred to trigger an action
autcomplete(str): the text to be inserted if an invalid item is selected. This is only used if 'valid' is 'no'
icon(str): filename of icon that Alfred will display
"""
item = et.SubElement(self.feedback, 'item', uid=str(len(self.feedback)),
arg=arg, valid=valid, autocomplete=autocomplete)
_title = et.SubElement(item, 'title')
_title.text = title
_sub = et.SubElement(item, 'subtitle')
_sub.text = subtitle
_icon = et.SubElement(item, 'icon')
_icon.text = icon
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 高德地图.alfredworkflow
Binary file not shown.

0 comments on commit c2c41c8

Please sign in to comment.