-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
101 lines (92 loc) · 2.99 KB
/
bot.py
File metadata and controls
101 lines (92 loc) · 2.99 KB
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
import tweepy #https://github.com/tweepy/tweepy
import json
from pprint import pprint
from time import sleep
import os
def twitter_api(consumer_key, consumer_secret, access_token, access_token_secret):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
def creds():
with open('creds.json') as data_file:
data = json.load(data_file)
consumer_key = data['creds'][0]['consumer_key']
consumer_secret = data['creds'][0]['consumer_secret']
access_token = data['creds'][0]['access_token']
access_token_secret = data['creds'][0]['access_token_secret']
#return consumer_key, consumer_secret
return consumer_key, consumer_secret, access_token, access_token_secret
def listen(consumer_key, consumer_secret, access_key, access_secret,since_id):
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
mentions = api.mentions_timeline(count=200,since_id=since_id)
dms = api.direct_messages(count=200,since_id=since_id)
return mentions, dms
def clap(tweet,user):
clap_emoji = u"\U0001F44F"
tweet = tweet.lower().replace('@clapbot','').strip()
words = tweet.split(' ')
first_word = words[0]
if first_word[0] == '@':
user = first_word
if first_word[0] == '-':
first_word = first_word.replace('-','@')
clap_tweet = first_word
words.pop(0)
for w in words:
clap_tweet = clap_tweet + clap_emoji + w
clap_tweet = clap_tweet + clap_emoji
if len(clap_tweet) < 141:
if clap_tweet[0] == '@':
return clap_tweet
else:
return clap_tweet + ' @' + user
else:
return 'cannot' + clap_emoji + 'clap' + clap_emoji + 'dat @' + user
def process(i, user):
tweet_id = i.id
request_id = i.id_str
seen.append(int(request_id)+1)
log.write(request_id + '\n')
sender = user.screen_name
tweet = clap(i.text,sender)
api = twitter_api(consumer_key, consumer_secret, access_key, access_secret)
try:
api.update_status(status=tweet)
if hasattr(i, 'retweeted'):
api.create_favorite(tweet_id)
print 'tweeted '+tweet
sleep(30)
except Exception:
print 'couldnt tweet'
sleep(30)
pass
consumer_key, consumer_secret, access_key, access_secret = creds()
seen = []
log = open('log.txt','r')
for l in log:
last = int(l)
seen.append(last+1)
log.close()
log = open('log.txt','ab')
while len(seen) > 0:
last_mention = max(seen)
try:
mentions, dms = listen(consumer_key, consumer_secret, access_key, access_secret,last_mention)
except Exception as e:
print str(e)
print 'some sort of drama when listening'
sleep(420)
pass
if len(mentions) < 1 and len(dms) < 1:
sleep(420)
print 'no new mentions, taking a 420 second break'
else:
for i in mentions:
if i.user.screen_name <> 'ClapBot':
process(i, i.user)
for d in dms:
process(d, d.sender)