def __process_following_fetch(self, user): #print("Processing friendship fetch for {} user".format(user)) #TODO: Check if it is needed to fetch following with more than 200 count base_url = 'https://api.twitter.com/1.1/friends/list.json' count = 200 cursor = -1 friendship = [] #set query params if user['id']: params = {'user_id': user['id'], 'count': count} else: print("User Id is missing and so using {} screen name".format( user['screen_name'])) params = {'screen_name': user['screen_name'], 'count': count} try: while cursor != 0: params['cursor'] = cursor #Check for ratelimit self.__handle_twitter_ratelimit() url = '%s?%s' % (base_url, urllib.parse.urlencode(params)) response_json = fetch_tweet_info(url) #print(type(response_json)) if 'next_cursor' not in response_json: print("Warning: Cursor not found in response [{}].".format( response_json)) print("cursor value is {}".format(cursor)) cursor = response_json["next_cursor"] if 'users' in response_json.keys(): print("adding {} users to list".format( len(response_json['users']))) friendship.extend(response_json['users']) except TwitterUserNotFoundError: print( "Twitter couldn't found user {} and so ignoring".format(user)) friendship = [] self.grandtotal += 1 except TwitterPageDoesnotExist as e: print( "Twitter couldn't found page < code: 34, page doesnot exist>") print(e) friendship = [] self.grandtotal += 1 except TwitterUnknownError as e: print( "Twitter unknown error happened for user {}. Error={}".format( user, e)) friendship = {"Error": "TwitterUnknownError"} self.grandtotal += 1 print(" Found {} followings for {}".format(len(friendship), user['screen_name'])) return friendship
def __process_tweets_fetch(self, tweet_id): print("Processing {} Tweet".format(tweet_id)) tweets = None base_url = 'https://api.twitter.com/1.1/statuses/show/' + tweet_id headers = {'accept': 'application/json'} params = {'result_type': 'recent', 'tweet_mode': 'extended'} tweet_url = '%s?%s' % (base_url, urllib.parse.urlencode(params)) tweet_json = fetch_tweet_info(tweet_url) print(type(tweet_json)) if (tweet_json): tweets = [tweet_json] return tweets
def __process_friendship_fetch(self, user): print("Processing friendship fetch for {} user".format(user)) base_url = 'https://api.twitter.com/1.1/friendships/show.json' params = { 'source_screen_name': self.source_screen_name, 'target_screen_name': user } url = '%s?%s' % (base_url, urllib.parse.urlencode(params)) response_json = fetch_tweet_info(url) print(type(response_json)) friendship = response_json return friendship
def __process_tweets_search(self, search_term, max_id=None, count=200): print("Processing [{}] Tweet search".format(search_term)) base_url = 'https://api.twitter.com/1.1/search/tweets.json' headers = {'accept': 'application/json'} params = {'q': search_term, 'count': count, 'result_type': 'recent'} if (max_id): params['max_id'] = max_id tweet_url = '%s?%s' % (base_url, urllib.parse.urlencode(params)) #print(tweet_url) response_json = fetch_tweet_info(tweet_url) # Keep status objects. tweets = response_json['statuses'] return tweets
def __process_retweets_fetch(self, tweet_id, count=100): print("Processing Retweet for {} Tweet".format(tweet_id)) base_url = "https://api.twitter.com/1.1/statuses/retweets/" + tweet_id + ".json" headers = {'accept': 'application/json'} tweets = None params = { 'count': count, 'result_type': 'recent', 'tweet_mode': 'extended' } tweet_url = '%s?%s' % (base_url, urllib.parse.urlencode(params)) tweet_json = fetch_tweet_info(tweet_url) print(type(tweet_json)) if (tweet_json): tweets = tweet_json return tweets
def __process_friendship_fetch(self, user): #print("Processing friendship fetch for {} user".format(user)) base_url = 'https://api.twitter.com/1.1/friendships/show.json' if user['id']: params = {'source_id': self.source_id, 'target_id': user['id']} else: print("User Id is missing and so using {} screen name".format( user['screen_name'])) params = { 'source_screen_name': self.source_screen_name, 'target_screen_name': user['screen_name'] } url = '%s?%s' % (base_url, urllib.parse.urlencode(params)) response_json = fetch_tweet_info(url) #print(type(response_json)) friendship = response_json return friendship