-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
executable file
·171 lines (149 loc) · 5.88 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
#
#
import webapp2
import tweepy
import ConfigParser
from tweepy import *
from time import ctime, gmtime, mktime, strftime
import csv
import random
class BotHandler(webapp2.RequestHandler):
def runBot(self):
config = ConfigParser.RawConfigParser()
config.read('settings.cfg')
# https://dev.twitter.com/apps/myappid
CONSUMER_KEY = config.get('Twitter', 'CONSUMER_KEY')
CONSUMER_SECRET = config.get('Twitter', 'CONSUMER_SECRET')
# https://dev.twitter.com/apps/myappid/my_token
ACCESS_TOKEN_KEY = config.get('Twitter', 'ACCESS_TOKEN_KEY')
ACCESS_TOKEN_SECRET = config.get('Twitter', 'ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
# If the authentication was successful, you should
# see the name of the account print out
#Iam = api.me().name
# Frequency calc
freq = random.randint(1, 100)
#freq = 3
if freq <= 5:
# Tweet a random quote
self.tweetQuote(api)
# Follow back a follower
##self.followBackFollower(api)
elif freq <= 30:
# Unfollow a non-follower
self.dropNonFollower(api)
elif freq <= 60:
# Add a random follower
self.addNewFollower(api)
else:
# RT
self.searchRT(api, config)
self.response.write(freq)
def tweet(self, api, theTweet):
api.update_status(theTweet)
def retweet(self, api, theTweetID):
print(theTweetID)
api.retweet(id=theTweetID)
def searchRT(self, api, config):
searchTerms = config.get('Twitter', 'SEARCH_TERMS')
termsList = searchTerms.split(",")
term = random.choice(termsList)
self.response.write(term)
results = api.search(term, "en")
#resultsSorted = sorted(results, key=lambda tweet: tweet.retweet_count, reverse=True)
randTweetNum = random.randint(0, len(results))
if results[randTweetNum]:
theTweet = results[randTweetNum].text
theTweetID = results[randTweetNum].id
#print(theTweet)
self.response.write(theTweet)
self.retweet(api, theTweetID)
def tweetQuote(self, api):
# read csv
with open('quotes.csv', 'rU') as f:
reader = csv.reader(f)
quotes = list(reader)
randQuoteNum = random.randint(0, len(quotes))
quote = quotes[randQuoteNum]
# Format quote
quoteText = quote[0]
quoteAuthor = quote[1]
# QuoteSource: Quote
quoteSource = ", "+ quote[2]
quoteWho = quote[3] +": " if quote[3] else ""
quoteHandle = quote[4]
theTweet = "\""+ quoteWho + quoteText +"\""+ quoteSource
# Tweet a quote
if len(theTweet) < 140:
self.tweet(api, theTweet)
self.response.write(theTweet)
def dropNonFollower(self, api):
myID = api.me().id
# Get followers
followersList = api.followers_ids(myID)
# Get friends
friendsList = api.friends_ids(myID)
# non-friend list
nonFollowerList = list(set(friendsList) - set(followersList))
if len(nonFollowerList) > 0:
#self.response.write(nonFollowerList)
# Get non-followers in descending order and pick the last one
nonFollowerID = nonFollowerList[-1]
#nonFollower = api.get_user(nonFollowerID)
if nonFollowerID:
nonFollower = api.destroy_friendship(nonFollowerID)
self.response.write(nonFollower)
def saveFollowers(self, api, config):
myID = api.me().id
# Get followers
followersList = api.followers_ids(myID)
# Mongo API keys
apiURL = config.get('Mongolab', 'apiURL')
apiKey = config.get('Mongolab', 'apiKey')
database = config.get('Mongolab', 'database')
# Set API call headers
headers = {'content-type': 'application/json'}
def followBackFollower(self, api):
myID = api.me().id
# Get followers
followersList = api.followers_ids(myID)
# Get friends
friendsList = api.friends_ids(myID)
# non-friend list
nonFollowingList = list(set(followersList) - set(friendsList))
# Find non-follwers and pick a random one
randNonFollowingID = random.choice(nonFollowingList)
#randNonFollowingID = nonFollowingList[random.randint(0, len(nonFollowingList)-1)]
#randNonFollowing = api.get_user(randNonFollowingID)
if randNonFollowingID:
randNonFollowing = api.create_friendship(randNonFollowingID)
self.response.write(randNonFollowing)
def addNewFollower(self, api):
myID = api.me().id
# Get followers
followerList = api.followers_ids(myID)
# Get a random follower
randFollowerID = followerList[random.randint(0, len(followerList)-1)]
# Get list of riends of the random follower
friendList = api.friends_ids(randFollowerID)
#newFollowerID = friendList[random.randint(0, len(friendList)-1)]
newFollowerID = random.choice(friendList)
#newFollower = api.get_user(newFollowerID)
if newFollowerID:
newFollower = api.create_friendship(newFollowerID)
self.response.write(newFollower)
def log(self, message):
timestamp = strftime("%Y %b %d %H:%M:%S UTC: ", gmtime())
print (timestamp + message + '\n')
def get(self):
try:
self.runBot()
print("Ran Bot")
except TweepError as te:
print te.message
app = webapp2.WSGIApplication([
('/bot', BotHandler)
], debug=False)