analitics

Pages

Showing posts with label tweepy. Show all posts
Showing posts with label tweepy. Show all posts

Monday, May 20, 2019

Python 3.7.3 : Use the tweepy to deal with twitter api - part 002.

The tutorial for today is about with python 3.7.3
The development team comes with this intro:
An easy-to-use Python library for accessing the Twitter API.
You need to have an application on twitter with tokens and key, see here.
The first step is to install this python module:
C:\Python373\Scripts>pip install tweepy
...
Successfully built PySocks
Installing collected packages: PySocks, tweepy
Successfully installed PySocks-1.6.8 tweepy-3.7.0
This is the source code I used.
Is very simple to understand:
import tweepy

# set the keys strings
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

# get  the keys 
input("consumer_key :")
input("consumer_secret :")
input("access_token :")
input("access_token_secret :")
print("--------")

# authentification and get data 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.me()
print (user.name)
user = api.me()
print(user.name)
print(user.location)
search = input("search :")
numberOfTweets = int(input("number of tweets :"))
phrase = input("response phrase :")
for follower in tweepy.Cursor(api.followers).items():
    try:
        follower.follow()
    except tweepy.error.TweepError:
        pass

print("Followed everyone that is following " + user.name)
for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
    try:
    #Reply
        print('\nTweet by: @' + tweet.user.screen_name)
        print('ID: @' + str(tweet.user.id))
        tweetId = tweet.user.id
        username = tweet.user.screen_name
        api.update_status("@" + username + " " + phrase, in_reply_to_status_id = tweetId)
        print ("Replied with " + phrase)
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break

for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
    try:
        #Reply
        tweet.favorite()
        print('Favorited the tweet') 
        #Retweet
        tweet.retweet()
        print('Retweeted the tweet')   
        #Follow
        tweet.user.follow()
        print('Followed the user')        
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break
The result is this:
C:\Python373>python.exe tweepy_001.py
catafest
catafest
Romania, Suceava, Falticeni
search :Catalin
number of tweets :1
response phrase :Festila
Followed everyone that is following catafest

Tweet by: @rumeys1a
ID: @1103384469703196673
Replied with Festila
Favorited the tweet
Retweeted the tweet
Followed the user
You can see what are the technical follow limits for twitter here.
The tweepy Documentation can be read at this link.

Monday, December 12, 2016

Use the tweepy to deal with twitter api - part 001.

I will show you how to install the python module named tweepy  and how to make authentication into twitter webpage.
This will install the tweepy python module.
C:\>cd Python27
C:\Python27>cd Scripts
C:\Python27\Scripts>pip install tweepy
Collecting tweepy
Downloading tweepy-3.5.0-py2.py3-none-any.whl
Collecting requests>=2.4.3 (from tweepy)
Downloading requests-2.12.3-py2.py3-none-any.whl (575kB)
100% |################################| 583kB 556kB/s
Collecting requests-oauthlib>=0.4.1 (from tweepy)
Downloading requests_oauthlib-0.7.0-py2.py3-none-any.whl
Requirement already satisfied: six>=1.7.3 in c:\python27\lib\site-packages (from
tweepy)
Collecting oauthlib>=0.6.2 (from requests-oauthlib>=0.4.1->tweepy)
Downloading oauthlib-2.0.1.tar.gz (122kB)
100% |################################| 133kB 506kB/s
Installing collected packages: requests, oauthlib, requests-oauthlib, tweepy
Running setup.py install for oauthlib ... done
Successfully installed oauthlib-2.0.1 requests-2.12.3 requests-oauthlib-0.7.0 tweepy-3.5.0

To deal with twitter api then you need to create a new application into this webpage.
This webpage will give for authentication your date to connect to twitter:
Application Settings
consumer_key=""
consumer_secret=""

Your Access Token
access_token=""
access_token_secret=""

Let's start with a simple example, by using your application settings and access token:
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

consumer_key=""
consumer_secret=""

access_token=""
access_token_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

print(api.me().name)

class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
print(data)
return True

def on_error(self, status):
print(status)

if __name__ == '__main__':
lista = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

stream = Stream(auth, lista)
stream.filter(track=['internet'])

Using python shell to run this script will show all about 'internet'.
The output will come into raw format.
Let's try another example to update your status with this message:
I using OAuth authentication via Tweepy!
Just add this into my code before class definition:
api.update_status(status='I using OAuth authentication via Tweepy!')
You can rad more about streaming by reading this docs.
For example, I used track from here.