Skip to content

Commit

Permalink
Uploaded scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
RodolfoFerro committed Jun 5, 2017
1 parent 73a0bbd commit 4fc22bd
Show file tree
Hide file tree
Showing 5 changed files with 909 additions and 1 deletion.
63 changes: 63 additions & 0 deletions Full/book_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import tweepy
from access import *
from random import randint
import re, time

def twitter_setup():
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
return api

def extract_status(path=None):
if not path:
return "No book opened!"
try:
with open(path, 'r', encoding='ascii', errors='surrogateescape') as book:
text = book.read()
if text:
return search_sentence(text)
except:
return "Book not found!"

def search_sentence(text):
# Initialize status:
status = 200

# While we have a long or very short status:
while not (5 < status < 125):
# Generate a random number:
index = randint(0,len(text))
# print(status)

#
init_index = text[index:].find('.') + index + 2
last_index = text[init_index:].find('.') + init_index + 2
status = len(text[init_index:last_index])

sentence = re.sub("\n", " ", text[init_index:last_index])
return sentence


if __name__ == '__main__':
# Setup Twitter API:
bot = twitter_setup()

# Set waiting time:
segs = 3

# Eternal posting:
while True:
# Extract status:
status = extract_status("texto.txt")
print(status)

# Try to post status:
try:
bot.update_status(status)
print("Done")
except tweepy.TweepError as e:
print(e.reason)

# Wait till next sentence extraction:
time.sleep(segs)
37 changes: 37 additions & 0 deletions Full/list_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import tweepy
from access import *
from random import randint
import re, time

def twitter_setup():
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
return api

if __name__ == '__main__':
# Setup Twitter API:
bot = twitter_setup()

# Set waiting time:
segs = 3

# Set wait time:
tweetlist = ["This is a #Python test for my @pythondaymx workshop. \n#PythonDayMX17",
"This is anoter automated tweet with #Python for my @pythondaymx workshop. \n#PythonDayMX17",
"Final automated tweet test for my @pythondaymx workshop. \n#PythonDayMX17"]

# Eternal posting:
for tweet in tweetlist:
# Extract status:
print(tweet)

# Try to post tweet:
try:
bot.update_status(tweet)
print("Done")
except tweepy.TweepError as e:
print(e.reason)

# Wait till next sentence extraction:
time.sleep(segs)
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# Crea_un_Twitterbot_en_30_minutos

> #### Quick info:
> **Taller:** ¡Crea un Twitterbot en 30 minutos!
> **Tallerista:** Rodolfo Ferro Pérez
> **Correo:** [ferro@cimat.mx](mailto:ferro@cimat.mx)
> **Twitter:** [@FerroRodolfo](https://twitter.com/FerroRodolfo)
> **Github:** [RodolfoFerro](https://github.com/RodolfoFerro)

-----


# ¡Crea un Twitterbot en 30 minutos!

## Abstract:

Los bots son relevantes para nosotros, pues automatizan tareas que eventualmente simplificarán trabajo a futuro. En esta taller _relámpago_ aprenderemos cómo crear un bot en Twitter usando Python, para de manera automática tuitear frases de un libro. ¡En sólo 30 minutos!

## Full description:

**¡Crea un Twitterbot en 30 minutos!**

Desde el apoyo en la administración de cuentas dando respuestas automáticas, hasta la realización de procesamientos más elaborados a través de redes sociales; los bots son relevantes para nosotros, pues automatizan tareas que eventualmente simplificarán trabajo futuro.

En este taller _relámpago_ (de sólo 30 minutos) aprenderemos cómo crear un bot muy básico en Twitter usando Python, para de manera automática tuitear frases de una lista de estados o directamente de un libro.

Para este taller necesitas conocimientos básicos sobre programación en Python y sobre el uso básico de Twitter. La idea es que tras tomar el taller, te interese adentrarte en el mundo de los bots para automatización de tareas, utilizando Python obviamente.

## Duración del taller:

##### Porque resulta increible hacer un bot en 30 minutos...

En general, este taller está diseñado para que –_como se menciona en la descripción_– tenga una duración de 30 minutos. Dicho tiempo sería repartido como sigue:

1. Introducción: Bots, utilidad y actualidad (~5 minutos)
2. Creando una Twitter App (~5 minutos)
3. Consumiendo el Twitter API vía Twitter (~5 minutos)
4. Tuiteando de una lista (~5 minutos)
5. Tuiteando de un libro (~10 minutos)

## Si vas a traer tu equipo:

#### Asegúrate de tener instalado lo siguiente:
* Python 3.x
* Tweepy


-----
10 changes: 10 additions & 0 deletions access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Credentials for your Twitter bot account
# @user

# Consumer:
CONSUMER_KEY = ''
CONSUMER_SECRET = ''

# Access:
ACCESS_TOKEN = ''
ACCESS_SECRET = ''
Loading

0 comments on commit 4fc22bd

Please sign in to comment.