Bot de Twitter amb Python i Tweepy
Contingut
Introducció
(TBD)
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
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
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.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()
Servidor remot, automatització
(TBD)
creat per Joan Quintana Compte, setembre 2021