-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
73 lines (52 loc) · 2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
""" Generate word clouds and post tweet image """
from datetime import datetime
import os
import logging
import sys
from tweepy import OAuthHandler, API
from pytagcloud import create_tag_image, make_tags
from pytagcloud.lang.counter import get_tag_counts
from skatewords import TextBuilder
import sw_auth as swa
LOG_FILENAME = 'the.log'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
# get textbuilder, which will help get text from database
tb = TextBuilder(cutoff_age=1)
num_tweets = len(tb.data)
def make_cloud(cloud_type,time,maxsize=80,cutoff=65,layout=4):
""" Make word cloud. Returns path to image file"""
text = tb.build_text(cloud_type)
filename = "images/" + cloud_type + "_" + time + ".png"
tags = make_tags(get_tag_counts(text), minsize=30, maxsize=maxsize)
if len(tags) >= cutoff:
tags = tags[:cutoff]
else:
return None
if tags:
create_tag_image(tags, filename, background=(0,0,0),size=(900, 600), layout=layout)
return filename
else:
return None
# go
time = datetime.now().strftime("%Y-%m-%d--%H-%M-%S-%f")
logging.info(" " + time + " --- Making tag clouds")
hashtag_img = make_cloud("hashtag",time,maxsize=60,cutoff=50,layout=2)
tweet_img = make_cloud("tweet",time)
if len(sys.argv) == 2 and sys.argv[1] == 'test':
logging.info(" This is a test, not tweeting images.")
else:
logging.info(" Tweeting images.")
# authorize bot
auth = OAuthHandler(swa.consumer_key, swa.consumer_secret)
auth.set_access_token(swa.access_token, swa.access_token_secret)
api = API(auth)
# send the tweet with photo
if tweet_img:
status = 'top skatewords of the day from %d tweets' % num_tweets
api.update_with_media(tweet_img, status=status)
os.remove(tweet_img)
if hashtag_img:
status = 'top skateboarding hashtags of the day from %d tweets' % num_tweets
api.update_with_media(hashtag_img, status=status)
os.remove(hashtag_img)
logging.info(" Done! \n")