def addToUsers(self): most_recent_id = self.followers[0].id u = User() saved = u.saveFromFollowers(self.TWITTER_BOT, most_recent_id) if saved: return most_recent_id else: return False
from sortingHat.models import User, TrainingData with open('sortingHat/TweetDataRedo.csv') as file: reader = csv.reader(file) for row in reader: if row[3] == '?': row[3] = None new_user = User( screen_name = row[0], #screen_name user_id = row[1], #user_id contributors_enabled = row[2], #contributors_enabled hours_since_last_tweet = row[3], #hours_since_last_tweet declared_blogger = row[4], #declared_blogger declared_company = row[5], #declared_company num_entities = row[6], #num_profile_entities tweets_favorited = row[7], #tweets_favorited num_followers = row[8], #num_followers num_friends = row[9], #num_friendships geo_enabled = row[10], #geo_enabled is_translator = row[11], #is_translator listed_count = row[12], #listed_count protected = row[13], #protected num_tweets = row[14], #num_tweets has_profile_url = row[15], #has_profile_url verified = row[16]) #verifed new_user.save() new_classified_user = TrainingData( user = new_user, classification = row[17]) new_classified_user.save()
def main(): if 'HEROKU_CONSUMER_KEY' in os.environ: CONSUMER_KEY = os.environ['HEROKU_CONSUMER_KEY'] CONSUMER_SECRET = os.environ['HEROKU_CONSUMER_SECRET'] ACCESS_TOKEN = os.environ['HEROKU_ACCESS_TOKEN'] ACCESS_SECRET = os.environ['HEROKU_ACCESS_SECRET'] twitter_api = API(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token=ACCESS_TOKEN, access_token_secret=ACCESS_SECRET) else: twitter_api = API() TWITTER_BOT = twitter_api.authenticate() did_ff = False ind_adv = False while True: ####create the needed API Objects searcher = Search(TWITTER_BOT) followers = Follower(TWITTER_BOT) classifier = tree.DecisionTreeClassifier() advertiser = TeaAdvertiser(TWITTER_BOT) #wda = WeekDayAware() #### check to see if theres a new follower to follow #if new follower does not already exist #i guess i could try/except IntegrityError instead: if not User.objects.filter(user_id=followers.most_recent_id).exists(): newUser = followers.arrangeUserData() try: newUser.save() print(newUser) except Exception as e: print(newUser, e) ### if adding was successful (new user) #print(user_id) if newUser.user_id: ##retrive him from user database most_recent_user_object = User.objects.get(user_id=newUser.user_id) ## set up the classifier with all the training data u = User td = TrainingData target = TrainingData.get_targets(td) data = TrainingData.get_data(td) classifier = classifier.fit(data, target) ## predict the class of the new user new_data_to_predict_on = User.get_data(u, user_object=most_recent_user_object) prediction = classifier.predict(new_data_to_predict_on) ### save the new prediction in the TrainingData database try: #prediction is a numpy array type, whose first index is the (string) number i want. newTD = TrainingData.saveClassified(td, newUser, prediction[0]) print(newTD) print('updated training data and user table') except Exception as e: print(newUser, prediction, e) #get the trianing data we just saved and tweet at them if they are an individual if TrainingData.objects.filter(user=most_recent_user_object).exists(): ## if it was a blogger or individual, tweet at them! newFollower = TrainingData.objects.get(user=followers.most_recent_id) if newFollower.classification == '0' or newFollower.classification == '2': followers.mention_new_follower() ## if it was a blogger or individual, tweet at them! elif newFollower.classification == '1': #steal followers followers.poach_followers(newFollower.user.screen_name, 20) ##if the user was a dupe, we've already done this. else: print('already following most recent') #### done with following/mentioning #begin favoriting! Favorite no matter what. statuses = searcher.get_statuses(query="#genmaicha", count=5) for status in statuses: try: TWITTER_BOT.create_favorite(status.id) print('favorited ' + str(status.id)) except tweepy.TweepError as e: print(e) print("favorite failed on status: " + str(status.id)) print(datetime.datetime.now()) continue statuses = searcher.get_statuses(query="#teasontheloose", count=5) for status in statuses: try: TWITTER_BOT.create_favorite(status.id) print('favorited ' + str(status.id)) except tweepy.TweepError as e: print(e) print("favorite failed on status: " + str(status.id)) print(datetime.datetime.now()) continue #advertise advertiser.advertise_this_months() advertiser.advertise_last_months() ##follow friday shout-outs, # if its friday, # and we havent already done it: # if (wda.today_int == 4) and (not did_ff): # followers.ff_tweet(wda=wda) # did_ff = True # #reset if we've done ff on a non-friday # #so it will be ready next week # elif wda.today_int != 4: # did_ff = False # ## done, wait and go again. print('done') time.sleep(1800)