Bot de Twitter amb Python i Tweepy

De wikijoan
Salta a la navegació Salta a la cerca

Introducció

compte de twitter: @CatalaRevifat

Es tracta de fer un bot que envïi cada 1 o 2 dies un tuit sobre català, a partir d'informació que hi ha a la base de dades. Amb un caire humorístic i picant.

Referències

Tweepy is an open source Python package that gives you a very convenient way to access the Twitter API with Python.

Instal·lació de Tweepy

Previ:

$ sudo apt-get install python3-venv

$ cd /home/joan/projectes/CatalaRevifat
$ mkdir tweepy-bots
$ cd tweepy-bots
$ python3 -m venv venv

The commands above create the virtual environment inside the project directory.

Then you can install the Tweepy package. First, you need to activate the newly created virtual environment and then use pip to do the installation:

$ source ./venv/bin/activate
(env) $ pip install tweepy

Now that Tweepy is installed, let’s create a requirements.txt file containing the names of your dependencies. You can use the pip command freeze for this task:

(env) $ pip freeze > requirements.txt

Creating Twitter API Authentication Credentials

As we have previously seen, the Twitter API requires that all requests use OAuth to authenticate. So you need to create the required authentication credentials to be able to use the API. These credentials are four text strings:

1. Consumer key
2. Consumer secret
3. Access token
4. Access secret

Step 1: Apply for a Twitter Developer Account

Go to the Twitter developer site to apply for a developer account.

joanqc+catalarevifat@gmail.com -> és el compte de correu associat a @CatalaRevifat, que es rep a joanqc@gmail.com

Ja he fet un apply per al developer account, i ara entrem en el procés de verificació.

#ApplicationReceived
Your email joanqc+catalarevifat@gmail.com has been verified and your application is officially under review!

Després de 24h:

Your Twitter developer account application has been approved!

Step 2: Create an Application

Twitter grants authentication credentials to apps, not accounts. An app can be any tool or bot that uses the Twitter API. So you need to register your an app to be able to make API calls.

Em deixa escollir entre Project o Standalone app, i escullo aquesta segona.

  • https://developer.twitter.com/en/portal/projects-and-apps
  • App name: CatalaRevifat
  • Application description: Bot for sending tweets (from a database) related with the promotion of Catalan language.
  • Your or your application's website URL: http://www.joanillo.org
  • Use of the app: This app is a bot that sends tweet every two days, and does not interact with users.
  • App id: 22033212
  • App permissions: read and write and direct messages

Step 3: Create the Authentication Credentials

1rtweepy.png

Consumer keys:

  • Api Key: ***
  • Api key secret: ***
  • bearer token: ***

Autenthication tokens:

  • Access Token: ***
  • Access Token secret: ***

You can test the credentials using the following snippet:

import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("***","***")
auth.set_access_token("***","***")

api = tweepy.API(auth)

try:
    api.verify_credentials()
    print("Authentication OK")
except:
    print("Error during authentication")
(env)$  python prova.py 
Authentication OK

I per enviar el primer tweet només cal posar a sota, després de l'autenticació:

api.update_status("1a prova. Test tweet from Tweepy Python")

Envia tuit de català revifat

versió v1

I ara que ja sé enviar tuits, aquí va el primer codi que funciona que agafa la informació de la bd, i envia el tuit:

script envia_tuit_catala_revifat_v1.py:

# pip install mysql-connector-python

from __future__ import print_function
import tweepy
import mysql.connector
import datetime

hostname = 'localhost'
username = '***'
password = '***'
database = 'catalarevifat'

# Authenticate to Twitter
#auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
#auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")
auth = tweepy.OAuthHandler("***", "***")
auth.set_access_token("***", "***")

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

try:
	api.verify_credentials()
	print("Authentication OK")
except:
	print("Error during authentication")

conn = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
cur = conn.cursor(dictionary=True) # dictionary per tal de què l'array sigui associatiu
cur.execute( "select P.id_paraula, paraula, P.notes, F.id_frase, frase from PARAULA P INNER JOIN FRASE F ON P.id_paraula=F.id_paraula INNER JOIN FRASE_TWITTER FT ON F.id_frase=FT.id_frase WHERE twitter=true and estat_twitter=0 ORDER BY RAND() LIMIT 1" )
row = cur.fetchone()
if row is not None:
	cad = "Aquí ve el tuit de #catalarevifat i humor picant:\n\n"
	cad = cad + row['paraula']
	if row['notes']:
		cad = cad + " (" + row['notes'] + ")"
	cad = cad + "\n\n"
	cad = cad + row['frase']
	print(cad)
	id_frase = row['id_frase']
	print(id_frase)
	cur.execute("update FRASE_TWITTER set estat_twitter=1, dia_twitter='" + str(datetime.datetime.now()) + "', comptador = comptador+1 WHERE id_frase = " + str(id_frase))
	conn.commit()
	api.update_status(cad)
conn.close()

versió v2

I ara que ja sé enviar tuits, aquí va el primer codi que funciona que agafa la informació de la bd, i envia el tuit:

script envia_tuit_catala_revifat_v2.py:

# pip install mysql-connector-python

from __future__ import print_function

import tweepy
import mysql.connector
import datetime

hostname = 'localhost'
username = '****'
password = '****'
database = 'catalarevifat'

# Authenticate to Twitter
#auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
#auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")
auth = tweepy.OAuthHandler("FUH3e3mXNAe8cEFex52GnxWPI", "alReVslFjTkjhPreYvkIcF25pHHxpgKDBqLxUFCgI9VJvbBXIC")
auth.set_access_token("1441337047369740293-VJnMGSya6TMYZQxmV1dpQYYKHPfVdA", "PlYpZpJVNje19tslsICqHOeggbminq2bo6Dzq3GWLKjr9")

#api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
api = tweepy.API(auth)
api.verify_credentials()
'''
try:
        api.verify_credentials()
        print("Authentication OK")
except:
        print("Error during authentication")
'''
conn = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
cur = conn.cursor(dictionary=True) # dictionary per tal de què l'array sigui associatiu
cur.execute( "SELECT distinct F.id_frase FROM FRASE F LEFT OUTER JOIN FRASE_TWITTER FT USING(id_frase) WHERE estat_twitter IS NULL ORDER BY RAND() LIMIT 1");
row = cur.fetchone()
if row is not None:
        id_frase = row['id_frase']
        cur.execute( "SELECT F.id_frase, frase, P.id_paraula, paraula, exemple FROM FRASE F INNER JOIN FRASE_PARAULA FP USING(id_frase) INNER JOIN PARAULA P USING (id_paraula) LEFT OUTER JOIN EXEMPLE E USING (id_paraula) WHERE F.id_frase = " + str(id_frase));
        myresult = cur.fetchall()
        frase = ""
        i = 0
        cad = "Aquí ve el tuit diari de #catalarevifat i humor picant:\n\n"
        for x in myresult:
                i = i + 1
                if frase == "":
                        frase = frase + x['frase']
                        cad = cad + "\"" + frase + "\"" + "\n\n"

                
                if x['exemple'] is None:
                        cad = cad + str(i) + ". " + x['paraula'] + "\n"
                else:
                        cad = cad + str(i) + ". " + x['paraula'] + ": " + x['exemple'] + "\n"

        cur.execute("INSERT INTO FRASE_TWITTER VALUES (" + str(id_frase) + ",1,'" + str(datetime.datetime.now()) + "')")
        conn.commit()
        #print(cad)
        # i ara ja podem enviar el tweet
        api.update_status(cad)              

conn.close()

Servidor remot, automatització

Instal·lem tota aquesta funcionalitat al servidor OVH. Encara no hi tenim instal·lat el Python:

$ sudo apt-get install python3.9
$ python3 --version
Python 3.8.10

$ sudo apt-get install python3-pip
$ pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

Instal·lem ara les llibreries que necessitarem:ç

$ pip install tweepy
$ pip freeze > requirements.txt

$ pip install mysql-connector-python

creem i omplim la base de dades

cron

En el servidor, com a root, he de tornar a instal·lar les llibreries tweepy i mysql-connector-python.

$ sudo joe /etc/crontab

#diumenge, dilluns, dimecres, divendres a les 8:00
0 6 * * 0 root /usr/bin/python3 /home/ubuntu/CatalaRevifat/tweepy-bots/enviar_tuit_catala_revifat_v1.py >/dev/null 2>&1
0 6 * * 1 root /usr/bin/python3 /home/ubuntu/CatalaRevifat/tweepy-bots/enviar_tuit_catala_revifat_v1.py >/dev/null 2>&1
0 6 * * 3 root /usr/bin/python3 /home/ubuntu/CatalaRevifat/tweepy-bots/enviar_tuit_catala_revifat_v1.py >/dev/null 2>&1
0 6 * * 5 root /usr/bin/python3 /home/ubuntu/CatalaRevifat/tweepy-bots/enviar_tuit_catala_revifat_v1.py >/dev/null 2>&1

Reiniciem el servei:

$ sudo /etc/init.d/cron restart

creat per Joan Quintana Compte, setembre 2021