analitics

Pages

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.

Python 3.7.3 : The google-cloud-vision python module - part 002.

I used Windows 8.1 and python 3.7.3 version.
The first step is to install the python module.
C:\Python373\Scripts>pip install --upgrade google-cloud-vision
You can see another tutorial about this python module here.
Let's test the python module.
C:\Python373>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import vision
>>> from google.cloud.vision import types
>>> from PIL import Image, ImageDraw
You need to have billing enabled for your Google Cloud Platform project.
Evaluated prices can vary and Google gives us this online calculator, or let us test with a FREE account with 300$.
To change the billing account go to the Google Cloud Platform Console.
Open the console left side menu and select Billing.
If you have more than one billing account then you need to solve this issue, see this webpage.
The Cloud Vision API integrates vision features, like: including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications.
You need to set the authentification for your google account and your project, see this page.
The next step is to instantiate a client:
client = vision.ImageAnnotatorClient()
Google provides this tutorial with the same steps as into this tutorial.
You can test and read more about Google Vision on the official page.