-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetCTT.py
312 lines (249 loc) · 11.9 KB
/
GetCTT.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
print("###Keep This window open. Close if you want to change settings on Streamlabs Chatbot###")
#---------------------------------------
# Import Libraries
#---------------------------------------
import sys, json, os, codecs, time, winsound
#from TwitterSearch import *
from ast import literal_eval
from datetime import datetime, timedelta
import colorama
from colorama import init
init()
from colorama import Fore, Back, Style
from twython import TwythonStreamer
import csv
#---------------------------------------
# [Required] Script Information
#---------------------------------------
ScriptName = "CTTupdate"
Website = ""
Creator = "Yaz12321"
Version = "2.0"
Description = "Part 1: Create a list of viewers who CTT"
#---------------------------------------
# Version Information
#---------------------------------------
# Version:
# > 2.0 <
# Changed tweet retreival approach to streaming.
# > 1.0<
# Official Release
class g:
#Get local time difference to UTC
DTS = time.localtime().tm_isdst
TZ = time.timezone / 3600
TD = DTS - TZ
#Get variables from Settings file
path = os.path.dirname(os.path.abspath(__file__))
Settingsf = codecs.open("{}\settings.json".format(path),encoding='utf-8-sig',mode="r+")
Settings = dict()
Settings = json.load(Settingsf, encoding='utf-8-sig')
Settingsf.close()
CTTMsg = Settings['CTTMsg'].split(",")
RefreshTime = Settings['RefreshTime']
MentionsOn = Settings['MentionsOn']
Mention = CTTMsg[0]
CTTColour = Settings['CTTColour']
CTTBG = Settings['CTTBackground']
MentionColour = Settings['MentionColour']
MentionBG = Settings['MentionBG']
sound = Settings['Sound']
#Set text and background colours
#Running code in Python Shell will not show colours, but codes. Colours only work in CMD
def Tcolour(colour):
if colour == "RED":
return(Fore.RED)
elif colour == "BLACK":
return(Fore.BLACK)
elif colour == "BLUE":
return(Fore.BLUE)
elif colour == "CYAN":
return(Fore.CYAN)
elif colour == "GREEN":
return(Fore.GREEN)
elif colour == "MAGENTA":
return(Fore.MAGENTA)
elif colour == "WHITE":
return(Fore.WHITE)
elif colour == "YELLOW":
return(Fore.YELLOW)
def Bcolour(colour):
if colour == "RED":
return(Back.RED)
elif colour == "BLACK":
return(Back.BLACK)
elif colour == "BLUE":
return(Back.BLUE)
elif colour == "CYAN":
return(Back.CYAN)
elif colour == "GREEN":
return(Back.GREEN)
elif colour == "MAGENTA":
return(Back.MAGENTA)
elif colour == "WHITE":
return(Back.WHITE)
elif colour == "YELLOW":
return(Back.YELLOW)
#Print colour codes for user
print Tcolour(g.CTTColour) + Bcolour(g.CTTBG) + "This is a CTT" + Fore.RESET + Back.RESET
print Tcolour(g.MentionColour) + Bcolour(g.MentionBG) + "This is a Mention" + Fore.RESET + Back.RESET
#Get Twitter keys
Keysf = open("{}\keys.txt".format(g.path),"r+")
Keysr = Keysf.read()
Keys = dict()
Keys = literal_eval(Keysr)
Keysf.close()
#Create Mentions list
global mentions
mentions = dict()
mentions = {'Empty': 2}
##global path
##path = os.path.dirname(os.path.abspath(__file__))
def process_tweet(tweet):
d = {}
d['hashtags'] = [hashtag['text'] for hashtag in tweet['entities']['hashtags']]
d['text'] = tweet['text']
d['user'] = tweet['user']['screen_name']
d['user_loc'] = tweet['user']['location']
TweetLocalTime = datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y') + timedelta(hours = g.TD)
pendingf = open('{}\Pending.txt'.format(g.path),'r+')
Tread = pendingf.read()
tweeters = dict()
tweeters = literal_eval(Tread)
pendingf.close()
if all(w.lower() in tweet['text'].lower() for w in g.CTTMsg):
if tweet['user']['screen_name'].lower() in tweeters.keys():
if tweeters[tweet['user']['screen_name'].lower()] == 0:
tweeters[tweet['user']['screen_name'].lower()] = 1
print Tcolour(g.CTTColour) + Bcolour(g.CTTBG) + "({}) CTT: @{}".format(TweetLocalTime,tweet['user']['screen_name']) + Fore.RESET + Back.RESET
if g.sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
else:
tweeters[tweet['user']['screen_name'].lower()] = 1
print Tcolour(g.CTTColour) + Bcolour(g.CTTBG) + "({}) CTT: @{}".format(TweetLocalTime,tweet['user']['screen_name']) + Fore.RESET + Back.RESET
if g.sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
elif g.MentionsOn:
print Tcolour(g.MentionColour) + Bcolour(g.MentionBG) + "({}) {} @{} tweeted: {}".format(TweetLocalTime,tweet['created_at'],tweet['user']['screen_name'],tweet['text'].encode('ascii','ignore')) + Fore.RESET + Back.RESET
if g.sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
return tweeters
# Create a class that inherits TwythonStreamer
class MyStreamer(TwythonStreamer):
# Received data
def on_success(self, data):
# Only collect tweets in English
tweet_data = process_tweet(data)
self.save_to_csv(tweet_data)
#print tweet_data
# Problem with the API
def on_error(self, status_code, data):
print(status_code, data)
self.disconnect()
winsound.SND_ALIAS
# Save each tweet to csv file
def save_to_csv(self, tweet):
pendingf = open("{}\Pending.txt".format(g.path),"w+") # add Services/Scripts/CTT to file
pendingf.write(str(tweet))
pendingf.close()
stream = MyStreamer(Keys['consumer_key'], Keys['consumer_secret'],
Keys['access_token'], Keys['access_token_secret'])
# Start the stream
stream.statuses.filter(track = g.Mention)
#Old approach using iterative search. Disabled.
while False:
try:
tso = TwitterSearchOrder() # create a TwitterSearchOrder object
tso.set_keywords([CTTMsg]) # let's define all words we would like to have a look for
tso.set_language('en') # we want to see English tweets only
tso.set_include_entities(False) # and don't give us all those entity information
## tso.set_since(datetime.date.today())
## tso.set_result_type('recent')
# Deal with authentication
ts = TwitterSearch(
consumer_key = Keys['consumer_key'],
consumer_secret = Keys['consumer_secret'],
access_token = Keys['access_token'],
access_token_secret = Keys['access_token_secret']
)
#Get Today
day = time.strftime("%a %b %d",time.gmtime())
year = time.strftime("%Y",time.gmtime())
#Get CTT List
path = os.path.dirname(os.path.abspath(__file__))
pendingf = open('{}\Pending.txt'.format(path),'r+')
Tread = pendingf.read()
tweeters = dict()
tweeters = literal_eval(Tread)
pendingf.close()
# Search Twitter
for tweet in ts.search_tweets_iterable(tso):
TweetLocalTime = datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y') + timedelta(hours = TD)
# if user is not marked 1 (CTT but not reveived points), add to list marked 1.
if tweet['created_at'].startswith(day) and tweet['created_at'].endswith(year):
if tweet['user']['screen_name'].lower() in tweeters.keys():
if tweeters[tweet['user']['screen_name'].lower()] == 0:
tweeters[tweet['user']['screen_name'].lower()] = 1
print Tcolour(CTTColour) + Bcolour(CTTBG) + "({}) CTT: @{}".format(TweetLocalTime,tweet['user']['screen_name']) + Fore.RESET + Back.RESET
if sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
else:
tweeters[tweet['user']['screen_name'].lower()] = 1
print Tcolour(CTTColour) + Bcolour(CTTBG) + "({}) CTT: @{}".format(TweetLocalTime,tweet['user']['screen_name']) + Fore.RESET + Back.RESET
if sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
#Return CTT list to file
pendingf = open("{}\Pending.txt".format(path),"w+") # add Services/Scripts/CTT to file
pendingf.write(str(tweeters))
pendingf.close()
except TwitterSearchException as e: # take care of all errors
print(e)
#Parent.SendTwitchMessage("e")
if MentionsOn == True:
try:
tso = TwitterSearchOrder() # create a TwitterSearchOrder object
tso.set_keywords([Mention]) # set key word to be searched for
tso.set_language('en') # we want to see English tweets only
tso.set_include_entities(False) # and don't give us all those entity information
# Deal with authentication
ts = TwitterSearch(
consumer_key = Keys['consumer_key'],
consumer_secret = Keys['consumer_secret'],
access_token = Keys['access_token'],
access_token_secret = Keys['access_token_secret']
)
# Search twitter:
for tweet in ts.search_tweets_iterable(tso):
TweetLocalTime = datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y') + timedelta(hours = TD)
# if new mention, display on window, and add to mentions list.
if tweet['created_at'].startswith(day) and tweet['created_at'].endswith(year) and CTTMsg not in tweet['text']:
if tweet['id'] in mentions.keys():
if mentions[tweet['id']] == 0:
mentions[tweet['id']] = 1
print Tcolour(MentionColour) + Bcolour(MentionBG) + "({}) {} @{} tweeted: {}".format(TweetLocalTime,tweet['created_at'],tweet['user']['screen_name'],tweet['text'].encode('ascii','ignore')) + Fore.RESET + Back.RESET
if sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
else:
mentions[tweet['id']] = 1
print Tcolour(MentionColour) + Bcolour(MentionBG) + "({}) {} @{} tweeted: {}".format(TweetLocalTime,tweet['created_at'],tweet['user']['screen_name'],tweet['text'].encode('ascii','ignore')) + Fore.RESET + Back.RESET
if sound == True:
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
except TwitterSearchException as e: # take care of all errors
print(e)
#Parent.SendTwitchMessage("e")
## Delay next refresh
time.sleep(RefreshTime*60)
###### OTHER PARAMETERS THAT CAN BE USED FROM TWEET:
###### tweet['text'] : text of tweet
###### tweet['id'] : id of tweet
###### tweet['retweet_count']: number of retweets
###### tweet['favorite_count'] : number of likes
###### tweet['created_at'] : date and time of creating tweet (in UTC)
###### tweet['user']['name'] : Display name
###### tweet['user']['screen_name'] : @name
###### tweet['user']['id'] : user id
###### tweet['user']['statuses_count'] : number of tweets by user
###### tweet['user']['friend_count'] : number of user's friends
###### tweet['user']['location'] : location of user
###### tweet['user']['following'] : whether is following authenticator
###### tweet['user']['created_at'] : date and time of creating user account (in UTC)