return requests.get(url) def __init__(self, names): if len(names) >= 2: firstname = names[0][0].capitalize() + names[0][1:] lastname = names[-1][0].capitalize() + names[-1][1:] resp = self.request_data(firstname,lastname) self.data = {'name': (firstname + " " + lastname).lstrip(), 'results': resp.json()} else: name = names[0][0].capitalize() + names[0][1:] resp1 = self.request_data(None,name) resp2 = self.request_data(name,None) if len(resp1.json()) > 0 and len(resp2.json()) > 0: json = resp1.json().extend(resp2.json()) elif len(resp1.json()) > 0: json = resp1.json() elif len(resp2.json()) > 0: json = resp2.json() else: json = [] self.data = {'name': (name).lstrip(), 'results': json} REGISTRY.add_parser('simple_white_house_log_search', SimpleWhiteHouseLogSearchParser) REGISTRY.add_parser('visitor_white_house_log_search', VisitorWhiteHouseLogSearchParser)
class HispanicOriginMatch(Match): template = "" """docstring for SimpleCensusMatch""" def __init__(self, field, places): self.table = 'B03001' self.field = field self.place = places[0] self.other_places = places[1:] self.geoid = places[0]['full_geoid'] # we would need to get some data url = 'http://api.censusreporter.org/1.0/acs2011_5yr/B03001?geoids=%s' % self.geoid resp = requests.get(url) self.data = resp.json() def _context(self): return { 'place': self.place, 'population': self.data[self.geoid][self.field], 'full_data': self.data[self.geoid], 'other_places': self.other_places } def as_json(self): return json.dumps(self._context()) def as_html(self): template = env.get_template('census/b03001.html') return template.render(**self._context()) REGISTRY.add_parser('simple_census_parser', SimpleCensusParser)
class SimpleBillSearchParser(Parser): def search(self, s): print "TEST!" if SIMPLE_PATTERN.match(s): print "MATCH!" d = SIMPLE_PATTERN.match(s).groupdict() # figure out which table for noun noun = d['noun'].strip() return SimpleBillSearchMatch(noun) return None class SimpleBillSearchMatch(Match): """docstring for SimpleBillSearchMatch""" def __init__(self, noun): # we would need to get some data url = 'http://congress.api.sunlightfoundation.com/bills?apikey=0b32a0061bdd4868b2b7cec4ec765add&query=%s' % noun resp = requests.get(url) self.data = resp.json() def as_json(self): return json.dumps(self.data) def as_html(self): template = env.get_template('bill_search/simple_search.html') return template.render(**self.data) REGISTRY.add_parser('simple_bill_search', SimpleBillSearchParser)
class ContributorsListMatch(Match): def __init__(self, *args, **kwargs): self.contributors = False super(ContributorsListMatch, self).__init__(*args, **kwargs) def extract(self): if self.data['filer'] in AWESOME_DATABASE: contributors = AWESOME_DATABASE[self.data['filer']]['contributors'] self.contributors = map(lambda x: x[0], contributors) def as_html(self): if not self.contributors: return None r = "<ul>" for contributor in self.contributors: r += "<li>%s</li>" % contributor r += "</ul>" return r def as_json(self): return json.dumps(self.contributors) if self.contributors else None REGISTRY.add_parser('money_raised', MoneyRaisedParser) REGISTRY.add_parser('contributors', ContributorParser)
class WerewolfParser(Parser): def search(self, s): if SIMPLE_PATTERN.match(s): d = SIMPLE_PATTERN.match(s).groupdict() noun = d['name'].strip() if(noun[-1] == '?'): noun = noun[0:-1] return WerewolfMatch(noun) return None class WerewolfMatch(Match): def __init__(self, noun): self.name = noun r = Random() r.seed(request.remote_addr) self.is_werewolf = r.choice([True, False]) self.data = { 'is_werewolf': self.is_werewolf, 'name': self.name } def as_json(self): return json.dumps(self.data) def as_html(self): template = env.get_template('werewolf/werewolf_search.html') return template.render(**self.data) REGISTRY.add_parser('werewolf_search', WerewolfParser)
url = 'http://congress.api.sunlightfoundation.com/legislators?apikey=%s&first_name=%s&last_name=%s' % (os.environ['SUNLIGHT_API_KEY'], names[0][0].capitalize() + names[0][1:], names[-1][0].capitalize() + names[-1][1:]) resp = requests.get(url) data = resp.json() print names[-1].capitalize() if(len(data['results']) == 0): url = 'http://congress.api.sunlightfoundation.com/legislators?apikey=%s&nickname=%s&last_name=%s' % (os.environ['SUNLIGHT_API_KEY'], names[0][0].capitalize() + names[0][1:], names[-1][0].capitalize() + names[-1][1:]) resp = requests.get(url) data = resp.json() if(len(data['results']) == 0): url = 'http://congress.api.sunlightfoundation.com/legislators?apikey=%s&last_name=%s' % (os.environ['SUNLIGHT_API_KEY'], names[-1][0].capitalize() + names[-1][1:]) resp = requests.get(url) data = resp.json() if(len(data['results']) == 0): self.data = {'results': []} else: bio_id = data['results'][0]['bioguide_id'] url = 'http://congress.api.sunlightfoundation.com/bills?apikey=%s&sponsor_id=%s' % (os.environ['SUNLIGHT_API_KEY'], bio_id) resp = requests.get(url) self.data = resp.json() def as_json(self): return json.dumps(self.data) def as_html(self): template = env.get_template('bill_search/simple_search.html') return template.render(**self.data) REGISTRY.add_parser('simple_bill_search', SimpleBillSearchParser) REGISTRY.add_parser('supported_bill_search', SupportBillSearchParser)
def _add_bucket(self, sex, age): i = bisect.bisect_left(SEX_AGE['key_ages'], age) possible_min = SEX_AGE['key_ages'][i] possible_max = SEX_AGE['key_ages'][i+1] - 1 if possible_min < self.returned_min_age: self.returned_min_age = possible_min if possible_max > self.returned_max_age: self.returned_max_age = possible_max x = SEX_AGE['key_ages'][i] if x > 85: x = 85 field = SEX_AGE[sex][x] if field not in self.added_fields: self.added_fields.append(field) self.total_population += self.data[self.geoid][field] class RaceMatch(FieldInTableMatch): template = 'census/b02001.html' table = 'B02001' def __init__(self, field, place, label): super(RaceMatch,self).__init__(field, place) self.label = label REGISTRY.add_parser('simple_census_parser', SimpleCensusParser) REGISTRY.add_parser('sex_age_census_parser', SexAgeCensusParser)
if(noun[-1] == '?'): noun = noun[0:-1] return SimpleCapitolWordsMatch(noun) return None class SimpleCapitolWordsMatch(Match): def __init__(self, noun): # we would need to get some data url = 'http://capitolwords.org/api/1/phrases/legislator.json?phrase=%s&sort=count&apikey=%s' % (noun, os.environ['SUNLIGHT_API_KEY']) resp = requests.get(url) self.data = resp.json() for r in self.data['results']: url = 'http://congress.api.sunlightfoundation.com/legislators?bioguide_id=%s&apikey=%s' % (r['legislator'], os.environ['SUNLIGHT_API_KEY']) resp = requests.get(url) subdata = resp.json() legislator_data = subdata['results'] if legislator_data: r['legislator'] = legislator_data[0]['first_name'] + " " + legislator_data[0]['last_name'] else: r['legislator'] = "<span class='notfound'>Name not found</span>" def as_json(self): return json.dumps(self.data) def as_html(self): template = env.get_template('capitol_words/simple_search.html') return template.render(**self.data) REGISTRY.add_parser('capitol_words_search', SimpleCapitolWordsParser)