Esempio n. 1
0
File: oob.py Progetto: paultag/glare
    def handle(self, email, *args, **kwargs):
        u = User.objects.get(email=email)
        api = SocialGlassAPI(u)

        timeline = api.get_timeline()
        location = api.get_location()
        latest = location.get_current_location()

        lobj = UserLocation.from_glass_location(u, latest)
        lobj.save()

        lon, lat = lobj.point.coords
        legislators = openstates.legislator_geo_search(lat, lon)

        for item in timeline:
            timeline.delete_item(item.id)

        state = legislators[0]['state'] if legislators else 'unknown'

        cover = LegislatorCoverItem(
            legs=legislators,
            state=state,
        )

        id = timeline.add_item(cover)
        for legislator in legislators:
            l = LegislatorTimelineItem(leg=legislator)
            id = timeline.add_item(l)
Esempio n. 2
0
    def locate_targets(self, latlon, chambers=TARGET_CHAMBER_BOTH, order=ORDER_IN_ORDER, state=None):
        """ Find all state legistlators for a location, as comma delimited (lat,lon)
            Returns a list of cached openstate keys in specified order.
        """
        if type(latlon) == tuple:
            lat = latlon[0]
            lon = latlon[1]
        else:
            try:
                (lat, lon) = latlon.split(',')
            except ValueError:
                raise ValueError('USStateData requires location as lat,lon')

        legislators = openstates.legislator_geo_search(lat, lon)
        targets = []
        senators = []
        house_reps = []

        # save full legislator data to cache
        # just uids to result list
        for l in legislators:
            if not l['active']:
                # don't include inactive legislators
                continue

            if state and (state.upper() != l['state'].upper()):
                # limit to one state
                continue

            cache_key = self.KEY_OPENSTATES.format(**l)
            self.cache_set(cache_key, l)

            if l['chamber'] == 'upper':
                senators.append(cache_key)
            if l['chamber'] == 'lower':
                house_reps.append(cache_key)

        if chambers == TARGET_CHAMBER_UPPER:
            targets = senators
        elif chambers == TARGET_CHAMBER_LOWER:
            targets = house_reps
        else:
            # default to TARGET_CHAMBER_BOTH
            if order == ORDER_UPPER_FIRST:
                targets.extend(senators)
                targets.extend(house_reps)
            elif order == ORDER_LOWER_FIRST:
                targets.extend(house_reps)
                targets.extend(senators)
            else:
                # default to name
                targets.extend(senators)
                targets.extend(house_reps)
                targets.sort()

        if order == ORDER_SHUFFLE:
            random.shuffle(targets)

        return targets
Esempio n. 3
0
    def locate_targets(self,
                       latlon,
                       chambers=TARGET_CHAMBER_BOTH,
                       order=ORDER_IN_ORDER,
                       state=None):
        """ Find all state legistlators for a location, as comma delimited (lat,lon)
            Returns a list of cached openstate keys in specified order.
        """
        if type(latlon) == tuple:
            lat = latlon[0]
            lon = latlon[1]
        else:
            try:
                (lat, lon) = latlon.split(',')
            except ValueError:
                raise ValueError('USStateData requires location as lat,lon')

        legislators = openstates.legislator_geo_search(lat, lon)
        targets = []
        senators = []
        house_reps = []

        # save full legislator data to cache
        # just uids to result list
        for l in legislators:
            if not l['active']:
                # don't include inactive legislators
                continue

            if state and (state.upper() != l['state'].upper()):
                # limit to one state
                continue

            cache_key = self.KEY_OPENSTATES.format(**l)
            self.cache_set(cache_key, l)

            if l['chamber'] == 'upper':
                senators.append(cache_key)
            if l['chamber'] == 'lower':
                house_reps.append(cache_key)

        if chambers == TARGET_CHAMBER_UPPER:
            targets = senators
        elif chambers == TARGET_CHAMBER_LOWER:
            targets = house_reps
        else:
            # default to TARGET_CHAMBER_BOTH
            if order == ORDER_UPPER_FIRST:
                targets.extend(senators)
                targets.extend(house_reps)
            elif order == ORDER_LOWER_FIRST:
                targets.extend(house_reps)
                targets.extend(senators)
            else:
                # default to name
                targets.extend(senators)
                targets.extend(house_reps)
                targets.sort()

        if order == ORDER_SHUFFLE:
            random.shuffle(targets)

        return targets
Esempio n. 4
0
from sunlight import openstates
from googlegeocoder import GoogleGeocoder

geocoder = GoogleGeocoder()

address = raw_input("please enter your address: ")

search = geocoder.get(address)

latitude = search[0].geometry.location.lat
longitude = search[0].geometry.location.lng

location = openstates.legislator_geo_search(latitude, longitude)

for legislators in location:
	print "%s %s" % (legislators['full_name'], legislators['photo_url'])