From fc9d5c8f181e9ecc0aa25e5b4803767094b291e5 Mon Sep 17 00:00:00 2001 From: quadrismegistus Date: Sat, 16 Apr 2011 16:50:57 -0700 Subject: [PATCH 1/2] FrnApi draft 1 created --- frn_api.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 frn_api.py diff --git a/frn_api.py b/frn_api.py new file mode 100644 index 0000000..01ff68e --- /dev/null +++ b/frn_api.py @@ -0,0 +1,46 @@ +from generic_api import * +import urllib,json + + +# Simple Python wrapper around the Broadband API provided by the FCC. + +class FrnApi(BaseAPIRequest): + def __init__(self): + self.url_getList="http://data.fcc.gov/api/frn/getList?stateCode=%s&multi=%s" + self.url_getInfo="http://data.fcc.gov/api/frn/getInfo?frn=%s" + + + def format_url(self, **args): + if 'stateCode' in args: + if not 'multi' in args: args['multi']='Yes' + self.url=self.url_getList % (args['stateCode'],args['multi']) + elif 'frn' in args: + self.url = self.url_getInfo % (args['frn']) + else: + self.url = None + #print self.url + + def request_state(self, stateCode, multi='Yes'): + #return BaseAPIRequest.request(self, **args) + url = self.url_getList % (stateCode, multi) + f = urllib.urlopen(url) + #return [l for l in f] + t=f.read().strip() + + # jsonp to json + t=t[t.index('(')+1:-1] + + return json.loads(t) + + + +# Demonstration of how to use BroadbandApi +if __name__ == "__main__": + bb = FrnApi() + #print bb.request(stateCode='IL') + + #exit() + x=bb.request_state('IL') + print type(x) + print len(x) + print x From 7c9f2f9554b9bcae465578791aea1d813aa08f2c Mon Sep 17 00:00:00 2001 From: quadrismegistus Date: Sat, 16 Apr 2011 17:15:17 -0700 Subject: [PATCH 2/2] Generalized request --- generic_api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/generic_api.py b/generic_api.py index bc79612..d695923 100644 --- a/generic_api.py +++ b/generic_api.py @@ -14,7 +14,12 @@ def format_url(self): # Requests the API and returns the JSON object. def request(self, **args): self.format_url(**args) - object = json.loads("".join([l for l in urllib.urlopen(self.formatted_url)])) + + t = urllib.urlopen(self.formatted_url).read().strip() + if t.startswith("callback("): + t=t[t.index("(")+1:-1] + return json.loads(t) + #object = json.loads("".join([l for l in urllib.urlopen(self.formatted_url)])) return object