-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_bot.py
53 lines (39 loc) · 1.17 KB
/
twitter_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
53
import time
import random
import logging
import tweepy
from conf import (
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_SECRET,
MINUTES
)
logger = logging.getLogger('nojam_twitter')
class Twitter:
def __init__(self):
# Init twitter bot config
self.auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
self.auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
self.api = tweepy.API(self.auth)
# Load nojam gag
with open('no-jam-gag.txt', 'r') as nojam:
self.items = nojam.readlines()
# Init default msg
self.msg = ''
self.logger = logging.getLogger('nojam_twitter')
self.logger.setLevel(logging.INFO)
def extract_message(self):
self.msg = random.choice(self.items)
def send_message(self):
try:
self.api.update_status(self.msg)
except Exception as e:
self.logger.error(e)
return
self.logger.info('Uploaded message: {}'.format(self.msg))
def execute_bot(self):
while True:
self.extract_message()
self.send_message()
time.sleep(MINUTES * 60)