-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
52 lines (38 loc) · 1.25 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import tweepy, os, re
from dotenv import load_dotenv
from random import randint
load_dotenv()
def twitter_setup():
auth = tweepy.OAuthHandler(os.getenv('CONSUMER_KEY'), os.getenv('CONSUMER_SECRET'))
auth.set_access_token(os.getenv('ACCESS_TOKEN'), os.getenv('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='utf-8', errors="surrogateescape") as book:
text = book.read()
if text:
return search_sentence(text)
except:
return "Book not found."
def search_sentence(text):
status = 200
while not (5 < status < 125):
index = randint(0, len(text))
init_index = text[index:].find(".") + 2 + index
last_index = text[init_index:].find(".") + 2 + index
status = len(text[init_index:last_index])
sentence = text[init_index:last_index]
sentence = re.sub("\n", " ", sentence)
return sentence
def make_tweet():
bot = twitter_setup()
status = extract_status("texto.txt")
try:
print(status)
bot.update_status(status)
print("Successfuly posted!")
except tweepy.TweepError as e:
print(e.reason)