Skip to content

Instantly share code, notes, and snippets.

@elpargo
Created May 28, 2013 12:26
Show Gist options
  • Save elpargo/5662399 to your computer and use it in GitHub Desktop.
Save elpargo/5662399 to your computer and use it in GitHub Desktop.
API de provincias v2 + tester + runner
import falcon #webframework
import json #json duh!
import requests #sane http get/post/etc.
from bs4 import BeautifulSoup #sane htmlparser
def get_soup():
#Link u guys provided.... get me the .json !!!!
html_doc = requests.get("http://www.jmarcano.com/mipais/geografia/province/municipios/").text
if not html_doc:
#lame sanity check. TODO: make proper
raise ValueError
soup = BeautifulSoup(html_doc)
return soup
def get_provincias():
soup = get_soup()
#this crap is in a table full of arrgssss luckily for us is the only strong in the whole page :D
res = soup.find_all('strong')
res = [item.text for item in res] #get the actual text not the tag
return json.dumps(res)#json go!
class ProvinciasResource:
def on_get(self, req, resp):
resp.body = get_provincias()
app = api = falcon.API()
provs = ProvinciasResource()
api.add_route('/provincias', provs)
from provinciasapp import app
from pync import Notifier
Notifier.notify('Restarting...',open='http://localhost:8080/provincias',title='weeeee',group='restart')
try:
import nose
result = nose.run()
if result:
Notifier.notify("All dangly and awesome",title='Yay!',group='ok')
else:
Notifier.notify("Test didn't pass :(",activate='com.kodowa.LightTable',title='#FAIL!',group='tests')
from werkzeug.serving import run_simple
run_simple('localhost', 8080, app, use_reloader=True,use_debugger=True, use_evalex=True)
except Exception as e:#catch all
Notifier.notify('Broken pipe :(',activate='com.googlecode.iterm2',title='plop',group='broken')
#generate original exception
print type(e)
raise
from provinciasapp import app as application
from webtest import TestApp
app = TestApp(application)
import json
def validate_package(url):
resp = app.get(url)
assert resp.status == '200 OK', resp.status
assert resp.status_int == 200, resp.status_int
assert resp.content_type == 'application/json'
return json.loads(resp.body)
def test_json():
payload = validate_package('/provincias')
assert len(payload)==32,len(payload)
assert "distrito nacional" in payload, payload
@claudiosanchez
Copy link

Mmm; Interesting. I want to know more....

@rmariuzzo
Copy link

Seems legit ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment