forked from wrought/Whitehouse-Signature-Tweeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweeter.py
executable file
·139 lines (119 loc) · 5.46 KB
/
tweeter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python
import tweepy
import threading
import Queue
import logging
logger = logging.getLogger('tweeter')
max_msg_length = 120
class Tweeter(threading.Thread):
# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key=""
consumer_secret=""
# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located
# under "Your access token")
access_token=""
access_token_secret=""
auth = ""
api = ""
#get logger
def __init__(self, c_key, c_sec, a_tok, a_tok_s, msg_preamble, msg_postamble,
q, signature_count, exit_event, delay, live_mode = False):
threading.Thread.__init__(self)
self.consumer_key = c_key
self.consumer_secret = c_sec
self.access_token = a_tok
self.access_token_secret = a_tok_s
self.auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
self.auth.set_access_token(self.access_token, self.access_token_secret)
self.api = tweepy.API(self.auth)
self.q = q
self.signature_count = signature_count
self.exit_event = exit_event
self.exitflag = False
self.delay = delay
self.live_mode = live_mode
self.msg_preamble = msg_preamble
self.msg_postamble = msg_postamble
logger.debug("constructed")
# @TODO
def run(self):
old_next_person = None
while not self.exitflag:
people = ""
rightlength = False
while not rightlength and not self.exit_event.wait(0):
next_person = None
if old_next_person == None:
logger.debug("checking name queue . . .")
if not self.q.empty():
next_person = self.q.get()
logger.debug(". . . retrieved %s" % next_person)
else:
logger.debug(". . . nothing in queue")
pass
#print "DEBUG: tweeter: nothing in q"
else:
logger.debug("bypassing queue and using %s" % old_next_person)
next_person = old_next_person
old_next_person = None
#throw out names that are too long
#short circuits if == None
if (next_person == None) or (len(next_person) > 40):
logger.debug("name too long or None, throwing away")
next_person = None
if next_person != None:
logger.debug("checking adding person to msg: " + next_person)
currentlength = len((self.msg_preamble % self.signature_count.get())
+ people + self.msg_postamble)
logger.debug("current msg length: %s" % currentlength)
nextlength = len((self.msg_preamble % self.signature_count.get())
+ self.add_to_msg(people, next_person) + self.msg_postamble)
logger.debug("possible next msg length: %s" % nextlength)
if nextlength > max_msg_length:
logger.debug("msg was already the correct length. Getting ready to tweet . . .")
logger.debug("stowing %s" % old_next_person)
old_next_person = next_person
rightlength = True
else:
logger.debug("adding %s to msg" % next_person)
people = self.add_to_msg(people, next_person)
else:
if self.exit_event.wait(1):
self.exit()
logger.debug('current tweet build: %s' % (self.msg_preamble % self.signature_count.get()) + people + self.msg_postamble)
logger.debug('current tweet length: %s' % str(len((self.msg_preamble % self.signature_count.get()) + people + self.msg_postamble)))
#print "DEBUG: tweeter: msg = " + self.msg_preamble + people + self.msg_postamble
if not self.exitflag:
self.tweet((self.msg_preamble % self.signature_count.get())
+ people + self.msg_postamble)
if self.exit_event.wait(self.delay):
self.exit()
logger.info('exiting thread')
print "Exiting: tweeter"
def add_to_msg(self, items, next_item):
if len(items) == 0:
return next_item
else:
return items + ', ' + next_item
def exit(self):
logger.debug("initializing exit procedure")
self.exitflag = True
def tweet(self, message):
# Debugging
logger.info('tweeting msg: %s' % message)
logger.debug('tweeting msg length: %s' % str(len(message)))
if len(message) > max_msg_length:
logger.error('tweet too long at %s characters.' % str(len(message)))
return -1
#if live_mode is true, actually tweet, otherwise just write tweet to SO
if self.live_mode:
try:
self.api.update_status(message)
except:
logging.error("something broke with the update_status_call. Deal with it.")
else:
print "Your tweet: " + message
logger.debug('tweet complete')
return 0