def add_untracked_friends(self): """Add previously untracked users. If a person is manually followed through the twitter website, this app doesn't know that a relationship exists. This method adds untracked people to our repository so we know not to refollow them and we won't multiple message them.""" self.log.debug("CHECK FOR UNTRACKED FRIENDS") friends_ids_api = self.api.friends_ids() targets = Target.objects.filter(hunter=self.user)\ .exclude(status__in=Target.ON_DECK) friends_ids_django = [t.hunted.twitter_id for t in targets] untracked_friends_ids = \ filter(lambda x: unicode(x) not in friends_ids_django, friends_ids_api) untracked_friends, remainder = lookup_users_by_id(self.api, untracked_friends_ids) for untracked_friend in untracked_friends: """These could be people who don't follow us, but we want to follow, for example to keep up with news of their company""" twitter_account, created = utils.get_or_create_twitter_account( untracked_friend) target, created = Target.objects.get_or_create( hunter=self.user, hunted=twitter_account) if created: target.reason = "External add." target.status = Target.FOLLOWER target.save() self.log.debug(" => add friend: %s" % twitter_account.screen_name) else: self.log.debug(" => we're following, but no reciprocation: %s" % twitter_account.screen_name)
def add_untracked_followers(self): """Add previously untracked users. These are people that are following us, but the django database doesn't know about. What that means is that they started following us since the last run of this function. There are 2 reasons that happens: 1) We followed them, then they started following us 2) They randomly decided to start following us or we tweeted at them and they started following us. In case 1, that means we had initially put them into purgatory and they responded favorably. We should tweet/DM them and then mark them as having reciprocated follow. In case 2, we're just awesome. Just add them as if they have reciprocated follow (basically, they're good, we just need them in the database) """ self.log.debug("CHECK FOR UNTRACKED FOLLOWERS") followers_ids_api = self.api.followers_ids() target = Target.objects.filter(hunter=self.user)\ .filter(status=Target.FOLLOWER) followers_ids_django = [t.hunted.twitter_id for t in target] untracked_followers_ids = filter( lambda x: unicode(x) not in followers_ids_django, followers_ids_api) untracked_followers, remainder = lookup_users_by_id(self.api, untracked_followers_ids) for untracked_follower in untracked_followers: twitter_account, created = \ utils.get_or_create_twitter_account(untracked_follower) target, created = Target.objects.get_or_create( hunter=self.user, hunted=twitter_account) if target.status == Target.PURGATORY: # Yay someone we targeted reciprocated follow self.follow_reciprocated(target) else: print target.status # Either a totally external follow, an ingrate changed mind, # or someone who we chatted became interested and followed # Either way the action is the same, follow him target.status = Target.FOLLOWER target.save() self.log.debug(" => Add follower: %s" % twitter_account.screen_name)
def find_new_followers(self): """Find new followers and dump them in the db""" api = self.api geocode = self.geocode queries = self.queries hits_per_query = self.hits_per_query self.log.debug("Initialize") self.log.debug("[ ********* FIND NEW FOLLOWERS *********** ]") if self.strategy == UserProfile.FOLLOW or self.strategy == UserProfile.TWEET: # Find statuses that match our interests self.log.debug("Strategy set to FOLLOW or TWEET") n = hits_per_query search_dict = dict() search_dict['lang'] = "en" if not geocode is None: search_dict['geocode'] = geocode statuses = list() self.log.debug("Queries:") for q in queries: search_dict['q'] = q results = [c for c in Cursor(api.search, **search_dict).items(n)] self.log.debug(" => %s: %s hits" % (q, len(results))) statuses.extend(results) #self.log.debug("Statuses: %s" % "\n".join([str(s.__dict__) for s in statuses])) # Get all the screen names of senders and receivers screen_names = ([t.from_user for t in statuses] + [t.to_user for t in statuses if t.to_user]) # Convert the strings to Tweepy user objects users, remainder = lookup_users_by_screen_name(self.api, screen_names) elif self.strategy == UserProfile.STEAL: users = [] stolen_from = {} for competitor in list(self.competitors): self.log.debug("[ ********* STEAL %s *********** ]" % competitor) try: competitor_friends_ids = self.api.friends_ids(competitor) competitor_followers_ids = self.api.followers_ids(competitor) filter_known_users_to_reduce_api_hits = False if filter_known_users_to_reduce_api_hits is True: new_competitor_friends_ids = [id for id in competitor_friends_ids if not len(TwitterAccount.objects.filter(twitter_id=id)) > 0 ] old_competitor_friends_ids = [id for id in competitor_friends_ids if len(TwitterAccount.objects.filter(twitter_id=id)) > 0 ] new_competitor_followers_ids = [id for id in competitor_followers_ids if not len(TwitterAccount.objects.filter(twitter_id=id)) > 0 ] old_competitor_followers_ids = [id for id in competitor_followers_ids if len(TwitterAccount.objects.filter(twitter_id=id)) > 0 ] # print new_competitor_friends_ids # print old_competitor_friends_ids # print new_competitor_followers_ids # print old_competitor_followers_ids print "start lookups" new_competitor_friends, remaining_friends = utils.lookup_users_by_id(self.api, new_competitor_friends_ids) new_competitor_followers, remaining_followers = utils.lookup_users_by_id(self.api, new_competitor_followers_ids) print "end lookups" else: # get all the tweepy users print "start lookups" new_competitor_friends, remaining_friends = utils.lookup_users_by_id(self.api, competitor_friends_ids) new_competitor_followers, remaining_followers = utils.lookup_users_by_id(self.api, competitor_followers_ids) print "end lookups" print "%s has %s friends" % (competitor, len(new_competitor_friends)) print "%s has %s followers" % (competitor, len(new_competitor_followers)) # holy crap this is so f****d up i'm ashamed that this code is getting written like this! for u in new_competitor_friends + new_competitor_followers: stolen_from.update({u.screen_name.lower(): competitor}) except Exception, e: print e # didn't get all the users, don't remove the competitor # from the competitor list pass else: # got all the competitors friends and followers and converted them # to tweepy users. users += new_competitor_friends users += new_competitor_followers # add them to the users list to be processed in the next block (for user in users) # then pop the name off the competitors list in the UserProfile # f**k it for now i'm going to just cycle the item to the bottom of the competitor list so we can start getting maximal coverage within api constraints by making sure the top person is new every time self.competitors.append(self.competitors.pop(0)) # return # for now self.profile.competitors = "\r\n".join(self.competitors) self.profile.save()