Skip to content

Commit

Permalink
Merge branch 'master' of github.com:codeforamerica/FCC-Python-Egg
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfn committed Apr 17, 2011
2 parents f2d53bb + 7c9f2f9 commit 7ce79dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
46 changes: 46 additions & 0 deletions 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
7 changes: 6 additions & 1 deletion generic_api.py
Expand Up @@ -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


Expand Down

0 comments on commit 7ce79dc

Please sign in to comment.