forked from jakkdl/necro_score_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsb_twitter.py
89 lines (61 loc) · 2.33 KB
/
nsb_twitter.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
import twitter as twitter_api
import os.path
import math
class twitter:
def readConfig(self, file):
f = open(file)
result = f.read()
f.close()
return result.rstrip()
def initTwitterAgent(self, file, consumer_key, consumer_secret):
oauth_token, oauth_secret = twitter_api.read_token_file(file)
return twitter_api.Twitter(auth=twitter_api.OAuth(
oauth_token, oauth_secret, consumer_key, consumer_secret))
def __init__(self, configDir):
key = self.readConfig(configDir + 'consumer_key')
secret = self.readConfig(configDir + 'consumer_secret')
credentialsFile = configDir + 'credentials'
self.agent = self.initTwitterAgent(credentialsFile, key, secret)
self.tweetCount = 0
self.blockFile = configDir + 'blockFile'
self.block = self.blockCheck()
def createBlock(self):
open(self.blockFile, 'w')
def blockExists(self):
return os.path.isfile(self.blockFile)
def blockCheck(self):
if self.tweetCount > 10:
self.createBlock()
if self.blockExists():
raise LookupError('Tweeting is blocked')
def incrementCount(self):
self.tweetCount += 1
def checkTwitterHandle(self, handle):
try:
self.agent.users.show(screen_name=handle)
return handle
except:
return None
def postTweet(self, text):
self.incrementCount()
self.blockCheck()
self.agent.statuses.update(status=text)
def userTweetCount(self, name):
return self.agent.users.show(screen_name=name)['statuses_count']
def timeline(self, name, count=-1, max_id=-1):
if count == -1:
count = self.userTweetCount(name)
result = []
for i in range(math.ceil(count/200)):
print(max_id)
if max_id == -1:
tmp = self.agent.statuses.user_timeline(
screen_name=name, count=count)
else:
tmp = self.agent.statuses.user_timeline(
screen_name=name, count=count, max_id = max_id)
#extract id of last tweet gathered, and get tweets at least
#one id smaller(older
max_id = tmp[-1]['id']-1
result += tmp
return result