jhinden Posted October 20, 2015 Posted October 20, 2015 Anyone know how one might go about making a script to use this site to compare cost of living in different cities? Handy for freelancers working on the go.www.numbeo.com/cost-of-living/comparison.jsp
deanishe Posted October 22, 2015 Posted October 22, 2015 The API is simple enough. You can get the names and IDs of cities via the autocomplete API: http://www.numbeo.com/common/CitySearchJson?term=london This returns JSON: [ { "label": "East London, South Africa", "value": "1683" }, { "label": "London, Canada", "value": "2352" }, { "label": "London, United Kingdom", "value": "6512" }, { "label": "London, KY, United States", "value": "9196" }, { "label": "Londonderry, United Kingdom", "value": "6513" } ] Then pass two of those ID numbers to this URL: http://www.numbeo.com/common/dispatcher.jsp?city_id1=1683&city_id2=6512 At this point, you can either take the easy route of opening that URL in the browser, or the harder route of extracting data from the HTML to show in Alfred.
jhinden Posted October 22, 2015 Author Posted October 22, 2015 Thanks for the response. I'm afraid I'm not skilled enough to do much with that though. I tried that link for example in the browser and it doesn't work. Please forgive my ignorance, but perhaps I can ask question that is a bit more general that will help me with Alfred: How can I create a search workflow that has two or more input queries in Alfred? Does this require scripting? http://www.numbeo.com/cost-of-living/compare_cities.jsp?country1={INPUT COUNTRY 1}&country2={INPUT COUNTRY 2}&city1={INPUT CITY 1}&city2={INPUT CITY 1} Typinator has a nice method for doing this (without scripting) and I guess I can just use that, but was hoping to be able to do something like this in Alfred where I simply type: Compare COL Berlin Germany San Francisco California Thanks in advance.
deanishe Posted October 22, 2015 Posted October 22, 2015 (edited) Alfred only works with one input, so you're probably going to have to do some scripting to get around that. In the simplest case, you could say "the input format is |country 1|city 1|country 2|city 2" and then split the single input on | If you follow my tutorial for a Pinboard workflow (creating the workflow, installing the library etc.) and use this Python script instead of pinboard.py, it should do, basically, what you want. #!/usr/bin/python # encoding: utf-8 """ Script Filter for Alfred 2 and Alfred-Workflow library. Compare cost of living in two cities. Save as `numbeo.py` in your workflow directory and create a Script Filter with Argument = required Language = /bin/bash Script = /usr/bin/python numbeo.py "{query}" """ import sys from workflow import Workflow, web log = None API_URL = u'http://www.numbeo.com/common/CitySearchJson' WEB_URL = u'http://www.numbeo.com/common/dispatcher.jsp?city_id1={}&city_id2={}&where=http://www.numbeo.com/cost-of-living/compare_cities.jsp' DELIMITER = u'⬌' def search_city(query): """Search for cities.""" r = web.get(API_URL, {'term': query}) r.raise_for_status() return r.json() def comparison_url(first_id, second_id): """Return URL for webpage comparing the two cities.""" return WEB_URL.format(first_id, second_id) def main(wf): """Run workflow.""" first_id = None query = wf.args[0] if DELIMITER in query: first_id, query = [s.strip() for s in query.split(DELIMITER)] for city in search_city(query): if city['value'] == first_id: continue if first_id: url = comparison_url(first_id, city['value']) wf.add_item(u'Compared to {}'.format(city['label']), url, valid=True, arg=url) else: wf.add_item(city['label'], autocomplete=u'{} {} '.format(city['value'], DELIMITER)) wf.send_feedback() if __name__ == '__main__': wf = Workflow() log = wf.logger sys.exit(wf.run(main)) Connect that Script Filter to an Open URL Output. Edited October 22, 2015 by deanishe
jhinden Posted October 22, 2015 Author Posted October 22, 2015 Cool, thanks so much! Looks like good info on that site. Thank you!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now