Python tweepy.Cursor() Examples

The following are 30 code examples of tweepy.Cursor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tweepy , or try the search function .
Example #1
Source File: build_follower_network.py    From smappPy with GNU General Public License v2.0 6 votes vote down vote up
def ensure_users_edges_in_db(user, edges_collection, twitter_api):
    "Looks up a user's friends_ids and followers_ids on the twitter api, and stores the edges in db."

    logging.info(".. Fetching followers_ids for user {0}.".format(user['id']))
    logging.info(".... user has {0} followers.".format(user['followers_count']))
    cursor = Cursor(twitter_api.followers_ids, id=user['id'])
    edges = [{ 'from' : follower_id,
               'to'   : user['id']}
            for follower_id in cursor.items()]
    store_edges(edges_collection, edges)
    followers_ids = [edge['from'] for edge in edges]

    logging.info(".. Fetching friends_ids for user {0}.".format(user['id']))
    logging.info(".... user has {0} friends.".format(user['friends_count']))
    cursor = Cursor(twitter_api.friends_ids, id=user['id'])
    edges = [{ 'to'   : friend_id,
               'from' : user['id']}
            for friend_id in cursor.items()]
    store_edges(edges_collection, edges)
    friends_ids = [edge['to'] for edge in edges]

    return friends_ids, followers_ids 
Example #2
Source File: twit1.py    From TwitterAnalyser with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_trends(self):
        trends = self.api.trends_place(1)
        trend_data = []

        for trend in trends[0]["trends"]:
            #print(trend['name'])
            trend_tweets = []
            trend_tweets.append(trend['name'])
            tt = tweepy.Cursor(self.api.search, q = trend['name']).items(3)
         
            for t in tt:                
                trend_tweets.append(self.get_tweet_html(t.id))
                #print(tweet_html)

            trend_data.append(tuple(trend_tweets))

        self.c.executemany("INSERT INTO trend_data VALUES (?,?,?,?, DATETIME('now'))", trend_data)

        self.conn.commit() 
Example #3
Source File: twit1.py    From TwitterAnalyser with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_trends(self):
        trends = self.api.trends_place(1)
        trend_data = []

        for trend in trends[0]["trends"]:
            #print(trend['name'])
            trend_tweets = []
            trend_tweets.append(trend['name'])
            tt = tweepy.Cursor(self.api.search, q = trend['name']).items(3)
         
            for t in tt:
                
                trend_tweets.append(t.text)
                #print(tweet_html)

            trend_data.append(tuple(trend_tweets))

        print(trend_data) 
Example #4
Source File: twit2.py    From TwitterAnalyser with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_trends(self):
        trends = self.api.trends_place(1)
        trend_data = []

        for trend in trends[0]["trends"]:
            #print(trend['name'])
            trend_tweets = []
            trend_tweets.append(trend['name'])
            tt = tweepy.Cursor(self.api.search, q = trend['name']).items(3)
         
            for t in tt:                
                trend_tweets.append(self.get_tweet_html(t.id))
                #print(tweet_html)

            trend_data.append(tuple(trend_tweets))

        print(trend_data) 
Example #5
Source File: twitter.py    From trump2cash with MIT License 6 votes vote down vote up
def get_all_tweets(self):
        """Looks up metadata for the most recent Trump tweets."""

        tweets = []

        # Only the 3,200 most recent tweets are available through the API. Use
        # the @Trump2Cash account to filter down to the relevant ones.
        for status in Cursor(self.twitter_api.user_timeline,
                             user_id=TRUMP2CASH_USER_ID,
                             exclude_replies=True).items():

            # Extract the quoted @realDonaldTrump tweet, if available.
            try:
                quoted_tweet_id = status.quoted_status_id
            except AttributeError:
                self.logs.warn('Skipping tweet: %s' % status)
                continue

            # Get the tweet details and add it to the list.
            quoted_tweet = self.get_tweet(quoted_tweet_id)
            tweets.append(quoted_tweet)

        self.logs.debug("Got tweets: %s" % tweets)

        return tweets 
Example #6
Source File: bird.py    From harpoon with GNU General Public License v3.0 6 votes vote down vote up
def get_user_tweets(self, username, since_id=None):
        """
        Download all tweets for an user
        Max is around 3200 tweets
        """
        if self.api is None:
            self._authenticate()
        tweets = []
        if since_id:
            cursor = tweepy.Cursor(self.api.user_timeline, screen_name=username, since_id=since_id)
        else:
            cursor = tweepy.Cursor(self.api.user_timeline, screen_name=username)

        for item in cursor.items():
            tweets.append(item)

        return tweets 
Example #7
Source File: bird.py    From harpoon with GNU General Public License v3.0 6 votes vote down vote up
def get_searched_tweets(self, hashtag, since_id=None):
        """
        Search all tweets for a hashtag
        """
        if self.api is None:
            self._authenticate()

        tweets = []
        if since_id:
            cursor = tweepy.Cursor(self.api.search, q=hashtag, count=100, since_id=since_id)
        else:
            cursor = tweepy.Cursor(self.api.search, q=hashtag, count=100)
        try:
            for item in cursor.items():
                tweets.append(item)
        except tweepy.error.TweepError:
            print("Reached Twitter rate limit")
        return tweets 
Example #8
Source File: tweets.py    From Trusty-cogs-archive with MIT License 6 votes vote down vote up
def _add(self, ctx, account, channel:discord.Channel=None):
        """Adds a twitter account to the specified channel"""
        api = await self.authenticate()
        try:
            for status in tw.Cursor(api.user_timeline, id=account).items(1):
                user_id = str(status.user.id)
                screen_name = status.user.screen_name
                last_id = status.id
        
        except tw.TweepError as e:
            print("Whoops! Something went wrong here. \
                    The error code is " + str(e) + account)
            await self.bot.say("That account does not exist! Try again")
            return
        if channel is None:
            channel = ctx.message.channel
        added = await self.add_account(channel, user_id, screen_name)
        if added:
            await self.bot.say("{0} Added to {1}!".format(account, channel.mention))
        else:
            await self.bot.say("{} is already posting or could not be added to {}".format(account, channel.mention)) 
Example #9
Source File: tweets.py    From Trusty-cogs-archive with MIT License 6 votes vote down vote up
def _del(self, ctx, account, channel:discord.Channel=None):
        """Removes a twitter account to the specified channel"""
        account = account.lower()
        api = await self.authenticate()
        if channel is None:
            channel = ctx.message.channel
        try:
            for status in tw.Cursor(api.user_timeline, id=account).items(1):
                user_id = str(status.user.id)      
        except tw.TweepError as e:
            print("Whoops! Something went wrong here. \
                    The error code is " + str(e) + account)
            await self.bot.say("That account does not exist! Try again")
            return
        removed = await self.del_account(channel, user_id)
        if removed:
            await self.bot.say("{} has been removed from {}".format(account, channel.mention))
        else:
            await self.bot.say("{0} doesn't seem to be posting in {1}!"
                               .format(account, channel.mention)) 
Example #10
Source File: crawl_network.py    From twitterbots with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_friends(api, screen_name, max_connections=0):
    friends = []
    max_connections_reached = False
    try:
        for friend_ids in tweepy.Cursor(
                api.friends_ids, screen_name=screen_name).pages():
            if max_connections and (
                    len(friends) + len(friend_ids)) > max_connections:
                logger.info(
                    'Max connections reached... trimming final request')
                friend_ids = friend_ids[:max_connections - len(friends)]
                max_connections_reached = True
            friends.extend(lookup_users(api, friend_ids))
            if max_connections_reached:
                break
    except Exception as e:
        logger.error('Error fetching friends: {}'.format(e))
    return friends 
Example #11
Source File: oscrtwitter.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def dm_batch(t_api):
    print "You are about to Delete all RECEIEVED DMs from the account @%s." % t_api.verify_credentials().screen_name
    print "Does this sound ok? There is no undo! Type yes to carry out this action."
    do_delete = raw_input("> ")
    if do_delete.lower() == 'yes':
        direct_messages = tweepy.Cursor(t_api.direct_messages).items()
        for direct_message in itertools.islice(direct_messages,0,None):
        #for message in tweepy.Cursor(t_api.direct_messages).items():
            try:
                #t_api.destroy_direct_message(message.id)
                t_api.destroy_direct_message(direct_message.id)
                print "Deleted:", direct_message.id
            except:
                print "Failed to delete:", direct_message.id
            sleep(5)
    return 
Example #12
Source File: oscrtwitter.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def dm_sent(t_api):
    print "You are about to Delete all SENT DMs from the account @%s." % t_api.verify_credentials().screen_name
    print "Does this sound ok? There is no undo! Type yes to carry out this action."
    do_delete = raw_input("> ")
    if do_delete.lower() == 'yes':
        sent_direct_messages = tweepy.Cursor(t_api.sent_direct_messages).items()
        for direct_message in itertools.islice(sent_direct_messages,0,None):
        #for message in tweepy.Cursor(t_api.direct_messages).items():
            try:
                #t_api.destroy_direct_message(message.id)
                t_api.destroy_direct_message(direct_message.id)
                print "Deleted:", direct_message.id
            except:
                print "Failed to delete:", direct_message.id
            sleep(5)
    return 
Example #13
Source File: tweetlord.py    From tweetlord with GNU General Public License v3.0 6 votes vote down vote up
def _api_favorites(client, username, **kwargs):
	if username.startswith('id'):
		user_id = username[2:]
		return tweepy.Cursor(
			client.favorites,
			user_id=user_id,
			page=kwargs['page'],
			count=kwargs['count'],
			include_entities=False,
			tweet_mode=kwargs['tweet_extended']
		).pages(kwargs['pages_count'])

	screen_name = username
	return tweepy.Cursor(
		client.favorites,
		screen_name=screen_name,
		page=kwargs['page'],
		count=kwargs['count'],
		include_entities=False,
		tweet_mode=kwargs['tweet_extended']
	).pages(kwargs['pages_count']) 
Example #14
Source File: oscrtwitter.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def dm_sent(t_api):
    print "You are about to Delete all SENT DMs from the account @%s." % t_api.verify_credentials().screen_name
    print "Does this sound ok? There is no undo! Type yes to carry out this action."
    do_delete = raw_input("> ")
    if do_delete.lower() == 'yes':
        sent_direct_messages = tweepy.Cursor(t_api.sent_direct_messages).items()
        for direct_message in itertools.islice(sent_direct_messages,0,None):
        #for message in tweepy.Cursor(t_api.direct_messages).items():
            try:
                #t_api.destroy_direct_message(message.id)
                t_api.destroy_direct_message(direct_message.id)
                print "Deleted:", direct_message.id
            except:
                print "Failed to delete:", direct_message.id
            sleep(5)
    return 
Example #15
Source File: crawl_network.py    From twitterbots with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_followers(api, screen_name, max_connections=0):
    followers = []
    max_connections_reached = False
    try:
        for follower_ids in tweepy.Cursor(
                api.followers_ids, screen_name=screen_name).pages():
            if max_connections and (
                    len(followers) + len(follower_ids)) > max_connections:
                logger.info(
                    'Max connections reached... trimming final request')
                follower_ids = follower_ids[:max_connections - len(followers)]
                max_connections_reached = True
            followers.extend(lookup_users(api, follower_ids))
            if max_connections_reached:
                break
    except Exception as e:
        logger.error('Error fetching friends: {}'.format(e))
    return followers 
Example #16
Source File: get_tweets.py    From smappPy with GNU General Public License v2.0 6 votes vote down vote up
def user_tweets(api, user_id=None, screen_name=None, limit=None, **kwargs):
    """
    Queries Twitter REST API for user's tweets. Returns as many as possible, or
    up to given limit.
    Takes an authenticated API object (API or APIPool), one of user_id or screen_name 
    (not both), and an optional limit for number of tweets returned.
    Returns a cursor (iterator) over Tweepy status objects.

    Also takes variable collection of keyword argument to pass on to
    Tweepy/APIPool query methods, to support full API call parameterization.
    """
    if not (user_id or screen_name):
        raise Exception("Must provide one of user_id or screen_name")
    if user_id:
        cursor = Cursor(api.user_timeline, user_id=user_id, count=200,
            **kwargs)
    elif screen_name:
        cursor = Cursor(api.user_timeline, screen_name=screen_name,
            count=200, **kwargs)
    if limit:
        return cursor.items(_check_limit(limit))
    return cursor.items() 
Example #17
Source File: build_friends_followers.py    From smappPy with GNU General Public License v2.0 6 votes vote down vote up
def get_friends_ids(api, user_id):
    """
    Given a Tweepy/smappPy TweepyPool api, query twitter's rest API for friends of
    given user_id. Returns IDs only (much faster / more per request).
    Parameters:
        api     - fully authenticated Tweepy api or smappPy TweepyPool api
        user_id - twitter user id
    Returns tuple: return code, list of IDs or None (if API call fails)
    """
    cursor = Cursor(api.friends_ids, user_id=user_id)
    user_list, ret_code = call_with_error_handling(list, cursor.items())
   
    if ret_code != 0:
        logger.warning("User {0}: Friends request failed".format(user_id))
    
    # Return user list from API or None (call_with_error_handling returns None if
    # call fail)
    return ret_code, user_list 
Example #18
Source File: build_friends_followers.py    From smappPy with GNU General Public License v2.0 6 votes vote down vote up
def get_followers_ids(api, user_id):
    """
    Given a Tweepy/smappPy TweepyPool api, query twitter's rest API for followers of
    given user_id. Returns IDs only (much faster / more per request).
    Parameters:
        api     - fully authenticated Tweepy api or smappPy TweepyPool api
        user_id - twitter user id
    Returns tuple: return code, list of IDs or None (if API call fails)
    """
    cursor = Cursor(api.followers_ids, user_id=user_id)
    user_list, ret_code = call_with_error_handling(list, cursor.items())

    if ret_code != 0:
        logger.warning("User {0}: Followers request failed".format(user_id))
    
    # Return user list from API or None (call_with_error_handling returns None if
    # call fail)
    return ret_code, user_list 
Example #19
Source File: parser.py    From twitter_media_downloader with Apache License 2.0 6 votes vote down vote up
def get_medias(auth, user_id, include_retweets, image_size, since, since_id, until, until_id, likes):
    """Get all medias for a given Twitter user."""
    auth = tweepy.OAuthHandler(auth['consumer_token'], auth['consumer_secret'])
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

    results = {
        'tweets': 0,
        'retweets': 0,
        'media': []
    }
    capi = api.favorites if likes else api.user_timeline
    for tweet in tweepy.Cursor(capi, id=user_id, include_rts=include_retweets, include_entities=True, tweet_mode='extended', since_id=since_id, max_id=until_id).items():
        if since is not None and tweet.created_at < since:
            break
        if until is not None and tweet.created_at > until:
            continue
        parse_tweet(tweet, include_retweets, image_size, results)

    print('Medias for user {0}'.format(user_id))
    print('- Tweets: {0}'.format(results['tweets']))
    print('- Retweets: {0}'.format(results['retweets']))
    print('- Parsed: {0}'.format(len(results['media'])))

    return results 
Example #20
Source File: utils.py    From django-htk with MIT License 6 votes vote down vote up
def get_friends(screen_name):
    """
    https://dev.twitter.com/rest/reference/get/friends/list

    Requests / 15-min window (app auth): 30
    """
    api = get_tweepy_api()
    friends = []
    is_first = True
    for page in tweepy.Cursor(api.friends, screen_name=screen_name, count=200).pages():
        if not is_first:
            time.sleep(30)
        else:
            is_first = False
        friends.extend(page)
    return friends
#    api = get_twitter_api()
#    friends = api.GetFriends(screen_name=screen_name, count=200)
#    return friends 
Example #21
Source File: utils.py    From django-htk with MIT License 6 votes vote down vote up
def get_friends_ids(screen_name):
    api = get_tweepy_api()
    ids = []
    is_first = True
    try:
        for page in tweepy.Cursor(api.friends_ids, screen_name=screen_name, count=5000).pages():
            if not is_first:
                time.sleep(60)
            else:
                is_first = False
            ids.extend(page)
    except tweepy.RateLimitError:
        extra_data = {
            'screen_name' : screen_name,
        }
        rollbar.report_exc_info(extra_data=extra_data)
    return ids 
Example #22
Source File: utils.py    From django-htk with MIT License 6 votes vote down vote up
def get_followers_ids(screen_name):
    """
    https://dev.twitter.com/rest/reference/get/followers/ids

    Requests / 15-min window (app auth): 15
    """
    api = get_tweepy_api()
    ids = []
    is_first = True
    try:
        for page in tweepy.Cursor(api.followers_ids, screen_name=screen_name, count=5000).pages():
            if not is_first:
                time.sleep(60)
            else:
                is_first = False
            ids.extend(page)
    except tweepy.RateLimitError:
        extra_data = {
            'screen_name' : screen_name,
        }
        rollbar.report_exc_info(extra_data=extra_data)
    return ids 
Example #23
Source File: tweetlord.py    From tweetlord with GNU General Public License v3.0 6 votes vote down vote up
def _api_friends(client, username, **kwargs):
	if username.startswith('id'):
		user_id = username[2:]
		return tweepy.Cursor(
			client.friends,
			user_id=user_id,
			cursor=kwargs['page'],
			count=kwargs['count'],
			skip_status=True,
			include_user_entities=False
		).pages(kwargs['pages_count'])

	screen_name = username
	return tweepy.Cursor(
		client.friends,
		screen_name=screen_name,
		cursor=kwargs['page'],
		count=kwargs['count'],
		skip_status=True,
		include_user_entities=False
	).pages(kwargs['pages_count']) 
Example #24
Source File: tweetlord.py    From tweetlord with GNU General Public License v3.0 6 votes vote down vote up
def _api_followers(client, username, **kwargs):
	if username.startswith('id'):
		user_id = username[2:]
		return tweepy.Cursor(
			client.followers,
			user_id=user_id,
			cursor=kwargs['page'],
			count=kwargs['count'],
			skip_status=True,
			include_user_entities=False
		).pages(kwargs['pages_count'])

	screen_name = username
	return tweepy.Cursor(
		client.followers,
		screen_name=screen_name,
		cursor=kwargs['page'],
		count=kwargs['count'],
		skip_status=True,
		include_user_entities=False
	).pages(kwargs['pages_count']) 
Example #25
Source File: oscrtwitter.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def dm_batch(t_api):
    print "You are about to Delete all RECEIEVED DMs from the account @%s." % t_api.verify_credentials().screen_name
    print "Does this sound ok? There is no undo! Type yes to carry out this action."
    do_delete = raw_input("> ")
    if do_delete.lower() == 'yes':
        direct_messages = tweepy.Cursor(t_api.direct_messages).items()
        for direct_message in itertools.islice(direct_messages,0,None):
        #for message in tweepy.Cursor(t_api.direct_messages).items():
            try:
                #t_api.destroy_direct_message(message.id)
                t_api.destroy_direct_message(direct_message.id)
                print "Deleted:", direct_message.id
            except:
                print "Failed to delete:", direct_message.id
            sleep(5)
    return 
Example #26
Source File: tweets.py    From Trusty-cogs with MIT License 5 votes vote down vote up
def get_tweet_list(self, api: tw.API, owner: str, list_name: str) -> List[int]:
        cursor = -1
        list_members: list = []
        for member in tw.Cursor(
            api.list_members, owner_screen_name=owner, slug=list_name, cursor=cursor
        ).items():
            list_members.append(member)
        return list_members 
Example #27
Source File: geotweet.py    From GeoTweet with MIT License 5 votes vote down vote up
def search_tag():
    c = tweepy.Cursor(api.search, q=TAG)
    for tweet in c.items(int(number)):
        print "------------------------------------------------"
        printout("[NAME:]   ", GREEN)
        print tweet.user.name.encode('utf-8')
        printout("[ALLIAS:]   ", BLUE)
        print "@" + tweet.user.screen_name.encode('utf-8')
        printout("[TWEET:] ", YELLOW)
        print " " + tweet.text.encode('utf-8')
        printout("[ID:] ", RED)
        print tweet.user.id
        printout("[GEOLOCATION:]   ", MAGENTA)
        if tweet.coordinates == None:
           print tweet.coordinates
        else:
            print tweet.coordinates['coordinates']
            location = geolocator.reverse(tweet.coordinates['coordinates'],timeout=20)
            print location.address.encode('utf-8')
        printout("[SOURCE:]   ", BLACK)
        print tweet.source.encode('utf-8')
        printout("[TWEET ID:]   ", WHITE)
        print tweet.id
        printout("[NUMBER RT:]   ", YELLOW)
        print tweet.retweet_count
    if MAPS == "y":
        print "------------------------------------------------"
        printout("[!] ", RED)
        print "in search tag, maps are not abilited"
    else:
        sys.exit(2)
    credit() 
Example #28
Source File: tweets_analyzer.py    From hackerbot with GNU General Public License v3.0 5 votes vote down vote up
def get_tweets(api, username, limit):
    """ Download Tweets from username account """
    for status in tqdm(tweepy.Cursor(api.user_timeline, screen_name=username).items(limit),
                       unit="tw", total=limit):
        process_tweet(status) 
Example #29
Source File: tweets_analyzer.py    From hackerbot with GNU General Public License v3.0 5 votes vote down vote up
def get_friends(api, username, limit):
    """ Download friends and process them """
    for friend in tqdm(tweepy.Cursor(api.friends, screen_name=username).items(limit), unit="friends", total=limit):
        process_friend(friend) 
Example #30
Source File: fetch.py    From harley_the_bot with Apache License 2.0 5 votes vote down vote up
def get_all_follows(screen_name):
    try:
        for follower in limit_handled(tweepy.Cursor(api.followers, screen_name=screen_name).items(), screen_name):
            try:
                taskdb.insert_inputusers(follower.screen_name, following=screen_name)
            except sqlite3.IntegrityError as e: 
                if 'UNIQUE constraint' in str(e):
                    print("skip..")
        return True
    except tweepy.error.TweepError as e: 
        if 'Not authorized' in str(e):
            print("Not authorized. May the account has been suspended. input args: " + screen_name)
            taskdb.mark_task_done(2, screen_name)
            taskdb.commit()
            return False