def load_data_elections_all(): elections = Elections.get_elections(Elections(), datafile=None) counter = 0 counter_not = 0 message = "Importing..." slackbot(message) for election_item in elections: election_id = election_item.electiondate.replace("-", "") election_mapping = { 'dataentry': 'automated', 'datasource': 'Associated Press', 'electiondate': election_item.electiondate, 'id': election_id, 'liveresults': election_item.liveresults, 'title': 'Automated election', ## e.g. extract state names and populate a /-separated string or something; or connect w/ FK like manual races 'testresults': election_item.testresults, } obj, created = Election.objects.update_or_create(**election_mapping) # print obj.electiondate counter += 1 message = "\nLoaded:\t\t\t" + str(counter) + " elections" slackbot(message) message = "Not loaded:\t\t" + str(counter_not) + " elections" slackbot(message)
def elections(self): """ ``elex elections`` Returns all elections known to the API. Command: .. code:: bash elex elections Example output: .. csv-table:: 2016-02-09,2016-02-09,True,False 2016-02-16,2016-02-16,True,False ... """ self.app.log.info('Getting election list') elections = Elections().get_elections( datafile=self.app.pargs.data_file ) self.app.render(elections)
def next_election(self): """ ``elex next-election <date-after>`` Returns data about the next election with an optional date to start searching. Command: .. code:: bash elex next-election Example output: .. csv-table:: id,electiondate,liveresults,testresults 2016-04-19,2016-04-19,False,True You can also specify the date to find the next election after, e.g.: .. code:: bash elex next-election 2016-04-15 This will find the first election after April 15, 2016. """ self.app.log.info('Getting next election') if len(self.app.pargs.date): electiondate = self.app.pargs.date[0] else: electiondate = None election = Elections().get_next_election( datafile=self.app.pargs.data_file, electiondate=electiondate) if election is None: self.app.log.error('No next election') self.app.close(1) self.app.render(election)
def load_data_elections(): ## AP elections elections_ap = Elections.get_elections(Elections(), datafile=None) elections_length = len(elections_ap) ## sort all automated elections by the most recent and return that one election_latest_imported_auto = Election.objects.filter( dataentry="automated").order_by("-electiondate")[0] election_latest_auto_date_formatted = str( election_latest_imported_auto.electiondate) election_latest_auto_date_unformatted = int( election_latest_auto_date_formatted.replace("-", "")) counter = 0 counter_not = 0 message = "Importing..." slackbot(message) for election_item in elections_ap: election_id = election_item.electiondate.replace("-", "") today_formatted = datetime.datetime.now().date() today_unformatted = int(str(today_formatted).replace("-", "")) electiondate_formatted = election_item.electiondate electiondate_unformatted = int(election_id) ## import anything today or after that if electiondate_unformatted > election_latest_auto_date_unformatted: election_check = True else: election_check = False if election_check: election_mapping = { 'dataentry': 'automated', 'datasource': 'Associated Press', 'electiondate': election_item.electiondate, 'id': election_id, 'liveresults': election_item.liveresults, 'title': 'Automated election', ## e.g. extract state names and populate a /-separated string or something; or connect w/ FK like manual races 'testresults': election_item.testresults, } obj, created = Election.objects.update_or_create( **election_mapping) # print obj.electiondate counter += 1 elif not election_check: counter_not += 1 message = "\nLoaded:\t\t\t" + str(counter) + " elections" slackbot(message) message = "Not loaded:\t\t" + str(counter_not) + " elections" slackbot(message)