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 e36c305 + 7d565be commit 9ff066c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
56 changes: 36 additions & 20 deletions frn_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,40 @@
# Simple Python wrapper around the Broadband API provided by the FCC.

class FrnApi(BaseAPIRequest):
"""
Provides a pythonic abstraction to the API @ http://reboot.fcc.gov/developer/frn-conversions-api
Two kinds of requests possible:
1. request(stateCode='IL') or request(stateCode=17) or request(stateCode='IL',multi='No')
returns a dictionary of FRNs in this state (keyed to 'Frns');
if multi==Yes, includes FRNs also operating in other states.
2. request(frn=0017855545)
returns a dictionary of information about this FRN, keyed to "Info"
"""

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"
self.url_getList="http://data.fcc.gov/api/frn/getList"
self.url_getInfo="http://data.fcc.gov/api/frn/getInfo"


def format_url(self, **args):
def set_url(self, **args):
if 'stateCode' in args:
self.url=self.url_getList
if not 'multi' in args: args['multi']='Yes'
self.url=self.url_getList % (args['stateCode'],args['multi'])
#self.formatted_url=self.url_getList % (args['stateCode'],args['multi'])
elif 'frn' in args:
self.url = self.url_getInfo % (args['frn'])
self.url=self.url_getInfo
#self.formatted_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)
self.url=self.url_getList
#print self.url
return args

def request(self, **args):
args=self.set_url(**args)
#if not 'multi' in args: args['multi'] = 'Yes'
return BaseAPIRequest.request(self,**args)


# Demonstration of how to use BroadbandApi
Expand All @@ -40,7 +47,16 @@ def request_state(self, stateCode, multi='Yes'):
#print bb.request(stateCode='IL')

#exit()
x=bb.request_state('IL')
#x=bb.request_state('IL')
x=bb.request(stateCode='IL')
print type(x)
print len(x)
print x.keys()

#print x

print
x=bb.request(frn='0017855545')
print type(x)
print len(x)
print x
2 changes: 1 addition & 1 deletion generic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def format_url(self, **args):
# Requests the API and returns the JSON object.
def request(self, **args):
self.format_url(**args)

#print self.formatted_url
t = urllib.urlopen(self.formatted_url).read().strip()
if t.startswith("callback("):
t=t[t.index("(")+1:-1]
Expand Down
23 changes: 23 additions & 0 deletions tests/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
sys.path.append('../')

from broadband_api import *
from frn_api import *
from block_conversion_api import *

class TestBroadbandAPI (unittest.TestCase):
def setUp(self):
self.bb = BroadbandApi()
self.frnapi = FrnApi()


#Sweep across the US and compare to precomputed values.
Expand Down Expand Up @@ -48,6 +50,27 @@ def test_Nowhere(self):

self.assertTrue(result['status'] == 'Fail')

def test_FRN(self):
result = self.frnapi.request(frn='0017855545')
self.assertTrue(result['Info']['frn'] == '0017855545')

def test_companyName(self):
result = self.frnapi.request(frn='0017855545')
self.assertTrue(result['Info']['companyName']=='Cygnus Telecommunications Corporation')

def test_FRNapiIsDict(self):
result1 = self.frnapi.request(stateCode='IL')
result2 = self.frnapi.request(frn='0017855545')
self.assertTrue(type(result1)==type({}) and type(result2)==type({}))
def test_CygnusInIL(self):
result = self.frnapi.request(stateCode='IL')
#print result['Frns']
#Cygnus Telecommunications Corporation
self.assertTrue('Cygnus Telecommunications Corporation' in [x['companyName'] for x in result['Frns']['Frn']])







Expand Down

0 comments on commit 9ff066c

Please sign in to comment.