def update_area(self,tweets,area,name,motherstate=None): if (area == 'state'): # Query the database for the state with id its name. AreaEntry = self.CollState.find_one({'_id':name}) elif (area == 'county'): # Query the database for the state with id the motherstate # By taking the name field the proper collection for the # the county leaderboard is selected. state = self.CollState.find_one({'_id':,motherstate}) CollCounty = db[state['name']] AreaEntry = collCounty.find_one('name':name) #Grad the date of the last update, create an object of the Game #class and call the method for the new points lastdate = date(AreaEntry['lastUpdate'][0],AreaEntry['lastUpdate'][1],AreaEntry['lastUpdate'][2]) #extract last update date GamingArea = Game(area,AreaEntry['name'],AreaEntry['points'],tweets,AreaEntry['lastTweettVal'],lastdate,AreaEntry['totalTweets']) GamingArea.NewPoints() #Change the values of the database entry based on the decisions #made by the Game class object AreaEntry['points']=GamingArea.__get__('points') AreaEntry['lastTweetVal'] = GamingArea.__get__('lastTweetVal') AreaEntry['lastUpdate'] = GamingArea.__get__('lastUpdate') AreaEntry['totalTweets'] = GamingArea.__get__('totalTweets') #Update the entry in the correct collection for the state or the county. if area == 'state': CollState.update({'_id':AreaEntry['_id']},AreaEntry,True) elif area == 'county': CollCounty.update({'_id':AreaEntry['_id']},AreaENtry,True)
def update_areas(self): ''' This method is responsible to go through the two area dictionaries, create a Game object of the Game Class for a given area and run the game. At the end it should update the database for that area. ''' #First update States for state in self.TweetedStates: print state # Query the database for the state with id its name. AreaEntry = self.CollState.find_one({'_id':state}) #Grad the date of the last update, create an object of the Game #class and call the method for the new points lastdate = date(AreaEntry['lastUpdate'][0],AreaEntry['lastUpdate'][1],AreaEntry['lastUpdate'][2]) #extract last update date GamingArea = Game('state',AreaEntry['name'],AreaEntry['points'],self.TweetedStates[state],AreaEntry['lastTweetVal'], lastdate,AreaEntry['totalTweets']) GamingArea.NewPoints() #Change the values of the database entry based on the decisions #made by the Game class object AreaEntry['points']=GamingArea.__get__('points') AreaEntry['lastTweetVal'] = GamingArea.__get__('lastTweetVal') AreaEntry['lastUpdate'] = GamingArea.__get__('lastUpdate') AreaEntry['totalTweets'] = GamingArea.__get__('totalTweets') self.CollState.update({'_id':AreaEntry['_id']},AreaEntry,True) for city in self.TweetedCities: motherstate = city[-2:] name = city[0:-4] # Query the database for the state with id the motherstate # By taking the name field the proper collection for the # the City leaderboard is selected. state = self.CollState.find_one({'_id':motherstate}) CollCity = self.mongoclient.search_results[motherstate] AreaEntry = CollCity.find_one({'name':name}) adding_entry = 0 # If the city does not exist create a new entry with the necessary starting data if not AreaEntry: AreaEntry = dict() AreaEntry['area'] = 'city' AreaEntry['name'] = name AreaEntry['points'] = 0 AreaEntry['lastTweetVal'] = 0 AreaEntry['lastUpdate'] = [date.today().year,date.today().month,date.today().day] AreaEntry['totalTweets'] = 0 AreaEntry['motherState'] = motherstate adding_entry = 1 #Grad the date of the last update, create an object of the Game #class and call the method for the new points lastdate = date(AreaEntry['lastUpdate'][0],AreaEntry['lastUpdate'][1],AreaEntry['lastUpdate'][2]) #extract last update date GamingArea = Game('city',AreaEntry['name'],AreaEntry['points'],self.TweetedCities[city],AreaEntry['lastTweetVal'],lastdate,AreaEntry['totalTweets'],motherstate) GamingArea.NewPoints() #Change the values of the database entry based on the decisions #made by the Game class object AreaEntry['points']=GamingArea.__get__('points') AreaEntry['lastTweetVal'] = GamingArea.__get__('lastTweetVal') AreaEntry['lastUpdate'] = GamingArea.__get__('lastUpdate') AreaEntry['totalTweets'] = GamingArea.__get__('totalTweets') #If the entry is new insert it to MongoDB, else update it if adding_entry: CollCity.insert(AreaEntry) else: CollCity.update({'_id':AreaEntry['_id']},AreaEntry,True)