def main(): args = get_args() pollster = Pollster() chart = pollster.charts(topic=args.topic) if args.run_all: for state in chart: polls = state.polls() for poll in polls: process_poll(poll) else: if args.state: state = get_state_from_chart(chart, args.state) else: state = chart[int(args.index)] polls = state.polls() for poll in polls: process_poll(poll)
def update_polls(): pollster = Pollster() for state in State.select().where(State.electoral_votes > 1): charts = pollster.charts(topic='2012-president', state=state.id) if charts: chart = charts[0] else: print 'NO DATA FOR %s' % state.id.upper() continue obama = 0 romney = 0 if chart.estimates: for estimate in chart.estimates: if estimate['choice'] == "Obama": obama = estimate['value'] elif estimate['choice'] == "Romney": romney = estimate['value'] else: print 'NO ESTIMATES FOR %s' % state.id.upper() continue prediction = "t" if abs(obama - romney) > 15: if obama > romney: prediction = "sd" else: prediction = "sr" elif abs(obama - romney) > 7.5: if obama > romney: prediction = "ld" else: prediction = "lr" uq = State.update(prediction=prediction).where(State.id == state) uq.execute()
# Will download data from Huffington Post and save it as data.csv # Run once before trying predict.py, but you shouldn't need to run it frequently. from csv import DictWriter import re from pollster import Pollster pollster = Pollster() kDATE = re.compile("[0-9]*-[0-9][0-9]-[0-9][0-9]") kFIELDS = ['YEAR', 'DATE', 'TOPIC', 'NAME', 'MOE', 'SUBPOP', 'SUBPOPID', 'CHOICE', 'PARTY', 'VALUE', 'OBS', 'STATE'] if __name__ == "__main__": o = DictWriter(open("data.csv", 'w'), kFIELDS) o.writeheader() for year in [2012, 2016]: line = {} line['YEAR'] = year entry = pollster.charts(topic='%i-president' % year) for chart in entry: for poll in chart.polls(): line['DATE'] = kDATE.findall(str(poll))[-1] for question in poll.questions: line['TOPIC'] = question['topic'] line['NAME'] = question['name'] line['STATE'] = question['state'] subpop_id = 0 for subpop in question['subpopulations']: subpop_id += 1 line['SUBPOPID'] = subpop_id line['SUBPOP'] = subpop['name']
# Will download data from Huffington Post and save it as data.csv # Run once before trying predict.py, but you shouldn't need to run it frequently. from csv import DictWriter import re from pollster import Pollster pollster = Pollster() kDATE = re.compile("[0-9]*-[0-9][0-9]-[0-9][0-9]") kFIELDS = [ 'YEAR', 'DATE', 'TOPIC', 'NAME', 'MOE', 'SUBPOP', 'SUBPOPID', 'CHOICE', 'PARTY', 'VALUE', 'OBS', 'STATE' ] if __name__ == "__main__": o = DictWriter(open("data.csv", 'w'), kFIELDS) o.writeheader() for year in [2012, 2016]: line = {} line['YEAR'] = year entry = pollster.charts(topic='%i-president' % year) for chart in entry: for poll in chart.polls(): line['DATE'] = kDATE.findall(str(poll))[-1] for question in poll.questions: line['TOPIC'] = question['topic'] line['NAME'] = question['name'] line['STATE'] = question['state'] subpop_id = 0 for subpop in question['subpopulations']:
def mock(): producer = ProducerThread(producers) producer.start() plugins = registry.setup_check_plugin() pollster = Pollster(plugins, 15) pollster.run()
# pip install pollster from pollster import Pollster pollster = Pollster() # current estimate of president's job approval chart = pollster.charts(topic='obama-job-approval')[20] chart.estimates example_topics = ['2012-gop-primary', '2012-senate', '2012-governor', '2012-president', '2012-house'] # list charts about Texas pollster.charts(state='TX') # calculate margin between Obama and Romney from poll poll = pollster.polls(chart='2012-general-election-romney-vs-obama')[0] question = [ x['subpopulations'][0] for x in poll.questions if x['chart'] == '2012-general-election-romney-vs-obama'][0] obama = [x for x in question['responses'] if x['choice'] == 'Obama'][0] romney = [x for x in question['responses'] if x['choice'] == 'Romney'][0] print obama['value'] - romney['value'] # check methodology in recent polls about the house chart = pollster.chart(slug='us-health-bill') print [[x.pollster, x.method] for x in chart.polls()] # TODO: compare favorability of Trump and Clinton from a recent poll # use info at http://elections.huffingtonpost.com/pollster/api/ new_poll = pollster.polls(chart = '2016-general-election-trump-vs-clinton')[0] new_question = [x['subpopulations'][0] for x in new_poll.questions if x['chart'] == '2016-general-election-trump-vs-clinton'][0] trump = [x for x in new_question['responses'] if x['choice'] == 'Donald Trump'][0] clinton = [x for x in new_question['responses'] if x['choice'] == 'Hillary Clinton'][0]
except ImportError: from numpy import random from pollster import Pollster # Get state names from us.STATES states = {state.abbr: state.name for state in us.STATES} # Read electoral college votes from csv file # https://raw.githubusercontent.com/chris-taylor/USElection/master/data/electoral-college-votes.csv with open('electoral-college-votes.csv') as csvfile: reader = csv.reader(csvfile) college = {row[0]: int(row[1]) for row in reader} # Initialize Pollster API pollster = Pollster() date = '2016-11-01' # 1 weeks of polls between 2016-11-01 and 2016-11-07 polls = [] page = 1 query = pollster.polls(topic='2016-president', after=date, page=page) while query: polls.extend(query) query = pollster.polls(topic='2016-president', after=date, page=page) page += 1 # Save polls by pickle pickle.dump(polls, open('polls.p', 'wb')) # Keep only state polls, igore national polls
# pip install pollster from pollster import Pollster pollster = Pollster() # current estimate of president's job approval chart = pollster.charts(topic='obama-job-approval')[20] chart.estimates example_topics = [ '2012-gop-primary', '2012-senate', '2012-governor', '2012-president', '2012-house' ] # list charts about Texas pollster.charts(state='TX') # calculate margin between Obama and Romney from poll poll = pollster.polls(chart='2012-general-election-romney-vs-obama')[0] question = [ x['subpopulations'][0] for x in poll.questions if x['chart'] == '2012-general-election-romney-vs-obama' ][0] obama = [x for x in question['responses'] if x['choice'] == 'Obama'][0] romney = [x for x in question['responses'] if x['choice'] == 'Romney'][0] print obama['value'] - romney['value'] # check methodology in recent polls about the house chart = pollster.chart(slug='us-health-bill') print[[x.pollster, x.method] for x in chart.polls()]
from pollster import Pollster import sys pollsterList = [] pollster = Pollster() pollCharts = pollster.charts() #iterate through every category of polls for x in pollCharts: #iterate through all the polls in the category for y in x.polls(): if(y.pollster not in pollsterList): pollsterList.append(y.pollster) print pollsterList
from pollster import Pollster pollster = Pollster() print('\npollster.charts(): ----------------------------------------------') print(pollster.charts()) print('\npollster.charts(topics = \'obama-job-approval\'))--------------------') print(pollster.charts(topic = 'obama-job-approval')) print('\npollster.charts()[0]: -------------------------------------------') print(pollster.charts()[0]) print('\npollster.charts()[0].estimates: ---------------------------------') print(pollster.charts()[0].estimates) print('\npollster.charts()[0].estimates_by_date()[1]: --------------------') print(pollster.charts()[0].estimates_by_date()[1]) #def stateVsTopic(): # stateCharts = pollster.charts(state='CO') # presCharts = pollster.charts(topic='2016-president') # # for stateChart in stateCharts: # for presChart in presCharts: # if presChart == stateChart: # print presChart #def stateAndTopic(): # print pollster.charts(topic='2016-president', state='WI')
from pollster import Pollster pollster = Pollster() print('\npollster.charts(): ----------------------------------------------') print(pollster.charts()) print( '\npollster.charts(topics = \'obama-job-approval\'))--------------------') print(pollster.charts(topic='obama-job-approval')) print('\npollster.charts()[0]: -------------------------------------------') print(pollster.charts()[0]) print('\npollster.charts()[0].estimates: ---------------------------------') print(pollster.charts()[0].estimates) print('\npollster.charts()[0].estimates_by_date()[1]: --------------------') print(pollster.charts()[0].estimates_by_date()[1])
# pip install pollster from pollster import Pollster pollster = Pollster() # current estimate of president's job approval chart = pollster.charts(topic='obama-job-approval')[20] chart.estimates example_topics = ['2012-gop-primary', '2012-senate', '2012-governor', '2012-president', '2012-house'] # list charts about Texas pollster.charts(state='TX') # calculate margin between Obama and Romney from poll poll = pollster.polls(chart='2012-general-election-romney-vs-obama')[0] question = [ x['subpopulations'][0] for x in poll.questions if x['chart'] == '2012-general-election-romney-vs-obama'][0] obama = [x for x in question['responses'] if x['choice'] == 'Obama'][0] romney = [x for x in question['responses'] if x['choice'] == 'Romney'][0] print obama['value'] - romney['value'] # check methodology in recent polls about the house chart = pollster.chart(slug='us-health-bill') print [[x.pollster, x.method] for x in chart.polls()] # TODO: compare favorability of Trump and Clinton from a recent poll # use info at http://elections.huffingtonpost.com/pollster/api/ chart = pollster.charts(topic='favorable-ratings') print chart ### See list of favorable ratings which exist chart[] ### Select index of Clinton and Trump
def fetchPolls(): #This function will fetch all new polls from Pollster and injects them into the SQLite database #First, we call updateTableStructure() to make sure that the SQLite table has the right structure and so we can get the list of candidates that we are concerned with dems, repubs = updateTableStructure() demString = str(dems).replace('[', '').replace(']', '').replace('\'', '`').replace('"', '`').replace('O`M', 'O\'M') repubString = str(repubs).replace('[', '').replace(']', '').replace('\'', '`').replace('"', '`') #Now we initialize everything for the SQLite database cursor = conn.cursor() #Now we initialize the pollster object pollster = Pollster() #Now we query the DemPrimary table to find the most recent poll that was inserted cursor.execute("SELECT EndDate FROM DemPrimary ORDER BY EndDate DESC;") testDemDates = cursor.fetchall() if testDemDates: #We need to make sure that there testDemDates has some entry, otherwise that means absolutely no polls have been injected, so we set the date to 2012-11-06, the day of the 2012 presidential election latestDemDate = testDemDates[0][0] + datetime.timedelta(1) else: latestDemDate = datetime.date(2012, 11, 06) #The next step is to get all the new polls. In order to do this, we need to first get the first page of polls and then every subsequent page of polls after. demPolls = [] newDemPolls = pollster.polls(chart = '2016-national-democratic-primary', page=1, after=str(latestDemDate)) i = 2 while newDemPolls: demPolls.extend(newDemPolls) newDemPolls = pollster.polls(chart = '2016-national-democratic-primary', page=i, after=str(latestDemDate)) i += 1 #Now we loop through every poll and inject it into the database. for p in demPolls: #pollProps are true of all subpopulations pollProps = [str(p.pollster), str(p.start_date), str(p.end_date), str(p.partisan), str(p.affiliation), str(p.method)] relevantQuestion = [x for x in p.questions if x['topic'] == '2016-president-dem-primary'][0] #Loop through subpopulations and inject each separately (treat each as its own poll) for s in relevantQuestion['subpopulations']: values = pollProps + [s['observations'] if s['observations'] else 0, str(s['name'])] resp = s['responses'] for c in dems: relevantResp = [x for x in resp if x['last_name']==c] val = relevantResp[0]['value'] if relevantResp else 0 values.append(val) cursor.execute("INSERT INTO DemPrimary (Pollster, StartDate, EndDate, Partisan, Affiliation, Method, Sample, SampleType, %(cands)s) VALUES (%(val)s);" %{"val": str(values).replace('[', '').replace(']', ''), "cands":demString}) #We now follow the same process for the Republicans cursor.execute("SELECT EndDate FROM RepubPrimary ORDER BY EndDate DESC;") testRepubDates = cursor.fetchall() if testRepubDates: latestRepubDate = testRepubDates[0][0] + datetime.timedelta(1) else: latestRepubDate = datetime.date(2012, 11, 06) repubPolls = [] newRepubPolls = pollster.polls(chart = '2016-national-gop-primary', page=1, after=str(latestRepubDate)) i = 2 while newRepubPolls: repubPolls.extend(newRepubPolls) newRepubPolls = pollster.polls(chart = '2016-national-gop-primary', page=i, after=str(latestRepubDate)) i += 1 for p in repubPolls: pollProps = [str(p.pollster), str(p.start_date), str(p.end_date), str(p.partisan), str(p.affiliation), str(p.method)] relevantQuestion = [x for x in p.questions if x['topic'] == '2016-president-gop-primary'][0] for s in relevantQuestion['subpopulations']: values = pollProps + [s['observations'] if s['observations'] else 0, str(s['name'])] resp = s['responses'] for c in repubs: relevantResp = [x for x in resp if x['last_name']==c] val = relevantResp[0]['value'] if relevantResp else 0 values.append(val) cursor.execute("INSERT INTO RepubPrimary (Pollster, StartDate, EndDate, Partisan, Affiliation, Method, Sample, SampleType, %(cands)s) VALUES (%(val)s);" %{"val": str(values).replace('[', '').replace(']', ''), "cands":repubString}) conn.commit() return dems, repubs
from pollster import Pollster from datetime import date import sys pollster = Pollster() def presPollsByDate(start, end): try: start = start.split('-', 2) start = date(int(start[0]), int(start[1]), int(start[2])) end = end.split('-', 2) end = date(int(end[0]), int(end[1]), int(end[2])) except: print 'Date could not be parsed correctly' print 'Make sure it is in the format YYYY-MM-DD' return pollCat = pollster.charts()[0].estimates_by_date() for x in range(1, len(pollCat)): poll = pollCat[x] pollDate = poll['date'].split('-', 2) pollDate = date(int(pollDate[0]), int(pollDate[1]), int(pollDate[2])) if (pollDate > start and pollDate < end): #print '\nPoll:\n' + str(poll) + '\n' #figure out who the winner was highestVal = 0 winner = '' print 'Poll Values:' for y in poll['estimates']: print y if (y['value'] > highestVal):