-
-
Save mcskrzypczak/adf559282dda1f7141eb63661a4924ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import urllib2, json, sys, os.path, os | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
query = sys.argv[1] | |
wdata = os.environ['alfred_workflow_cache'] | |
if not os.path.exists(wdata): | |
os.makedirs(wdata) | |
weather = "60" | |
def get_sug(query, weather=weather): | |
url = 'http://m.meteo.pl/ajax/0?weather_type=' + weather + '&query=' + query | |
hdr = {'X-Requested-With': 'XMLHttpRequest'} | |
results = {} | |
req = urllib2.Request(url, headers=hdr) | |
response = urllib2.urlopen(req) | |
the_page = response.read() | |
results = the_page.decode('unicode_escape') | |
return results | |
def recent(): | |
if os.path.exists(wdata + "/city"): | |
f = open(wdata + "/city") | |
lines = f.readlines() | |
city = lines[0][:-1] | |
url = lines[1] | |
ostatnie = '{"title":"Ostatnia miejscowość: ' + city + '", "subtitle":"' + action + '", "arg":"' + url.replace("84", weather) + ';' + city + '", "mods": {"cmd": {"subtitle":"' + cmdaction + '", "arg":"' + url.replace(weather, "84") + ';' + city + '"}}},' | |
return ostatnie | |
def loop(item, num): | |
if number == 0: | |
dane = '{"title":"Nie znaleziono takiej miejscowości"}' | |
else: | |
dane = '' | |
for i in range(0,num): | |
if ":pow:" in item[i]['data']: | |
data = item[i]['data'].replace(":pow:", "/" + weather + "/") | |
else: | |
data = item[i]['data'] + "/" + weather | |
dane = dane + '{"autocomplete":"' + item[i]['value'].encode() + '", "title":"' + item[i]['value'].encode() + '", "subtitle":"' + action + '", "arg":"http://m.meteo.pl/' + data + ';' + item[i]['value'].encode() + '", "mods": {"cmd": {"subtitle":"' + cmdaction + '", "arg":"http://m.meteo.pl/' + data.replace(weather, "84") + ';' + item[i]['value'].encode() + '"}}},' | |
dane = recent() + dane | |
return dane | |
action = "Prognoza 60h. Wciśnij Cmd dla prognozy 84h." | |
cmdaction = "Prognoza 84h." | |
lista = json.loads(get_sug(query)) | |
number = len(lista['suggestions']) | |
if number == 0: | |
item = "" | |
else: | |
item = lista['suggestions'] | |
print '{"items": [' + loop(item, number) + ']}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment