22
33from __future__ import unicode_literals
44from pprint import pprint
5- import urllib2
65import simplejson as json
6+ import sys
7+ import urllib2
8+
9+ #This allows printing to the console, including being able to pipe ( | ) the input
10+ reload (sys )
11+ sys .setdefaultencoding ("utf-8" )
712
8- #user_local_data = json.load(urllib2.urlopen('http://freegeoip.net/json/'))
13+ #Making sure that there is at least one command line argument, if not, is none
14+ if len (sys .argv ) == 1 :
15+ command_line_argument = None
16+ else :
17+ command_line_argument = sys .argv [1 ]
18+
19+ #Finds out the user's IP address for lookup
920user_local_data = json .load (urllib2 .urlopen ('http://api.ipinfodb.com/v3/ip-city/?key=a648bf3844359d401197bcaa214dd01e0f8c0c6d623ec57f3716fbcafc8262bd&format=json' ))
1021
22+ #Set variables for detected lat/long
1123user_lat = user_local_data ['latitude' ]
1224user_long = user_local_data ['longitude' ]
1325
14- #print gathered lat/long
15- #print user_lat,user_long
16-
17- #print """
18- #
19- #You can get your latitude and longitude from http://www.latlong.net/
20- #
21- #"""
22-
23- #user_lat = raw_input('Please enter your Latitude: \n')
24- #user_long = raw_input('Please enter your Longitude: \n')
25-
26- #user_lat = '47.653098'
27- #user_long = '-122.353731'
28-
29- lat_long_url = 'https://congress.api.sunlightfoundation.com/districts/locate?latitude={}&longitude={}&apikey=15f4679bdc124cd6a2c6be8666253000' .format (user_lat , user_long )
30-
31- congressional_district = json .load (urllib2 .urlopen (lat_long_url ))
32-
26+ #lat_long_url = 'https://congress.api.sunlightfoundation.com/districts/locate?latitude={}&longitude={}&apikey=15f4679bdc124cd6a2c6be8666253000'.format(user_lat, user_long)
3327
28+ #Look up legislators from API with given lat/long, then store in a var. for later use
3429legislators = json .load (urllib2 .urlopen ('https://congress.api.sunlightfoundation.com/legislators/locate?latitude={}&longitude={}&apikey=15f4679bdc124cd6a2c6be8666253000' .format (user_lat , user_long )))
3530
36- for i in legislators ['results' ]:
37- print i ['last_name' ] + ' ' + i ['bioguide_id' ]
38-
39- #All Legislators, irrespective of location
40-
41- #House only
42- #legislators = json.load(urllib2.urlopen('https://congress.api.sunlightfoundation.com/legislators?chamber=house&per_page=all&apikey=15f4679bdc124cd6a2c6be8666253000'.format(user_lat, user_long)))
43-
44- #All Legislators
45- #legislators = json.load(urllib2.urlopen('https://congress.api.sunlightfoundation.com/legislators?per_page=all&apikey=15f4679bdc124cd6a2c6be8666253000'.format(user_lat, user_long)))
46-
47- #print 'Based on the latitude and longitude provided, your United States Congresspeople are: \n'
48-
49-
50- #Prints Congressman/Congresswoman + First, Last
5131
32+ #Remnant of abandoned functionality
5233def find_legislators ():
53-
34+ """This function returns the legislator's last name and unique bioguide id, this would be useful for further, unimplemented features"""
5435 for i in legislators ['results' ]:
55- print i ['last_name' ] + ' ' + i ['bioguide_id' ]
56- #legislator_ids.append(i)
36+ print '{} {}' .format (i ['last_name' ], i ['bioguide_id' ])
5737
58- #find_legislators()
38+ def print_all_legislators ():
39+ """This function prints all the legislators, then formats according to gender and title"""
40+
41+ legislators = json .load (urllib2 .urlopen ('https://congress.api.sunlightfoundation.com/legislators?per_page=all&apikey=15f4679bdc124cd6a2c6be8666253000' ))
42+ for i in legislators ['results' ]:
5943
60- #votes_url = json.load(urllib2.urlopen('https://congress.api.sunlightfoundation.com/votes?voter_ids.{}__exists=true&apikey=15f4679bdc124cd6a2c6be8666253000')).format( __ THIS IS WHERE THE UNIQUE ID OF THE LEGISLATOR NEEDS TO BE __ )
44+ if i ['chamber' ] == 'house' and i ['gender' ] == 'M' :
45+ print 'Congressman {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
46+ if i ['chamber' ] == 'house' and i ['gender' ] == 'F' :
47+ print 'Congresswoman {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
48+ elif i ['chamber' ] == 'senate' :
49+ print 'Senator {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
6150
62- #pprint(votes_url)
51+ def print_manual_legislators ():
52+ """This function prints the legislators of the given lat/long, then formats according to gender and title"""
53+
54+ print """
55+
56+ You can get your latitude and longitude from http://www.latlong.net/
57+
58+ """
6359
64- #def recent_votes():
60+ user_lat = raw_input ('Please enter your Latitude: \n ' )
61+ user_long = raw_input ('Please enter your Longitude: \n ' )
6562
66- ##THIS IS WHERE YOU ARE GOING TO RETURN THE LAST 10 VOTES BY var = LEGISLATOR
63+ legislators = json .load (urllib2 .urlopen ('https://congress.api.sunlightfoundation.com/legislators/locate?latitude={}&longitude={}&apikey=15f4679bdc124cd6a2c6be8666253000' .format (user_lat , user_long )))
64+
65+ for i in legislators ['results' ]:
6766
68- def print_legislators ():
67+ if i ['chamber' ] == 'house' and i ['gender' ] == 'M' :
68+ print 'Congressman {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
69+ if i ['chamber' ] == 'house' and i ['gender' ] == 'F' :
70+ print 'Congresswoman {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
71+ elif i ['chamber' ] == 'senate' :
72+ print 'Senator {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
6973
74+ def print_detected_legislators ():
75+ """This function prints the legislators local to the IP address of the user, then formats according to gender and title"""
7076 for i in legislators ['results' ]:
7177
7278 if i ['chamber' ] == 'house' and i ['gender' ] == 'M' :
@@ -76,4 +82,13 @@ def print_legislators():
7682 elif i ['chamber' ] == 'senate' :
7783 print 'Senator {} {} - {} \n Phone: {}\n Website: {}\n ' .format (i ['first_name' ],i ['last_name' ],i ['party' ],i ['phone' ],i ['website' ] )
7884
79- #print_legislators()
85+ if command_line_argument == '--all-legislators' :
86+ print_all_legislators ()
87+
88+ elif command_line_argument == None :
89+ print 'Based on the latitude and longitude detected, your United States Congresspeople are: \n '
90+ print_detected_legislators ()
91+
92+ elif command_line_argument == '--manual' :
93+ print_manual_legislators ()
94+
0 commit comments