-
Notifications
You must be signed in to change notification settings - Fork 0
/
populateJsDB.py
226 lines (173 loc) · 7.9 KB
/
populateJsDB.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
import tweepy
import re
import datetime
import time
import csv
import accessDB
import dbInserts
import dbQueries
import sys
from pytz import timezone
import pytz
def tweets_to_csv(alltweets, title="some"):
# transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
# write the csv
with open('%s_tweets.csv' % title, 'wb') as f:
writer = csv.writer(f)
writer.writerow(["id", "created_at", "text"])
writer.writerows(outtweets)
f.close()
pass
def parseTweet(tweet):
# https://regex101.com/#python
# Obviously we are just putting faith in the formatting of @tripleJplay
songRegex = re.compile(r"(?<=-\W).+?(?=\W\[)")
artistWithTwitterRegex = re.compile(r'(?<=@).+?(?=\W)') #
aristWithoutTwitterRegex = re.compile(r'.+(?=\W-)')
featureArtistTwitterRegex = re.compile(r'(?<=ft.\W\@).+?(?=\W)')
timeRegex = re.compile(r'(?<=\[)[0-9]{2}:[0-9]{2}(?=\])') # (?<=\[).+(?=\]) #THIS IS VERY SPECIFIC
song = songRegex.search(tweet.text)
artist = artistWithTwitterRegex.search(tweet.text)
feat = featureArtistTwitterRegex.search(tweet.text)
time = timeRegex.search(tweet.text)
day = tweet.created_at.day
month = tweet.created_at.month
year = tweet.created_at.year
if song:
song = song.group()
else:
song = "^Song Not Found!^"
if artist:
artist = artist.group()
else:
artistNoTwitter = aristWithoutTwitterRegex.search(tweet.text)
if artistNoTwitter:
artist = artistNoTwitter.group()
else:
artist = "!Artist Not Found!"
if feat:
feat = feat.group()
else:
feat = '*Feature Artist Not Found*'
if time:
time = time.group()
else:
time = "%Time Not Found!%"
#use pytz to put the time in sydney time
sydney = timezone('Australia/Sydney')
tweet.created_at = timezone('UTC').localize(tweet.created_at) #Attach UTC time to tweet
date_object = tweet.created_at.astimezone(sydney) #Convert to sydney time
date_object = date_object.replace(hour=int(str(time)[:2]), minute=int(str(time)[3:])) #get the hour and minute from the actual tweet
return {'song': song, 'artist': artist, 'feat': feat, 'time': date_object, 'raw': tweet.text,
'created_at': tweet.created_at, 'id': tweet.id}
def get_tweets(api, screen_name, limit=0, parseTweets=True): # asyncSendToDB=False, asyncBatchSize=100):
allTweets = []
for tweet in tweepy.Cursor(api.user_timeline, screen_name=screen_name).items(limit):
try:
# You can select the tweet format here
if parseTweets:
allTweets.append(parseTweet(tweet))
else:
allTweets.append(tweet)
print allTweets[-1], "\n"
except tweepy.TweepError:
print "\n\n\n********************\nHIT THE RATE LIMITER\n************************"
time.sleep(60 * 15) # WAIT FOR 15 MINUTES, THEN HAVE ANOTHER CRACK
continue
except StopIteration: # If there's nothing left
print "\n\n\n********************\nNO MORE TWEEETS\n************************"
break
print "tweets extracted....", len(allTweets)
return allTweets
def get_tweets_before(api, screen_name, max_id, limit=0):
allTweets = []
for tweet in tweepy.Cursor(api.user_timeline, screen_name=screen_name, max_id=max_id).items(limit):
try:
allTweets.append(parseTweet(tweet))
print allTweets[-1], "\n"
except tweepy.TweepError:
print "\n\n\n********************\nHIT THE RATE LIMITER\n************************"
time.sleep(60 * 15) # WAIT FOR 15 MINUTES, THEN HAVE ANOTHER CRACK
continue
except Exception, e:
print "Something failed: ", e
print "tweets extracted....", len(allTweets)
return allTweets
def get_tweets_after(api, screen_name, since_id, limit=0):
allTweets = []
for tweet in tweepy.Cursor(api.user_timeline, screen_name=screen_name, since_id=since_id).items(limit):
try:
allTweets.append(parseTweet(tweet))
print allTweets[-1], "\n"
except tweepy.TweepError:
print "\n\n\n********************\nHIT THE RATE LIMITER\n************************"
time.sleep(60 * 15) # WAIT FOR 15 MINUTES, THEN HAVE ANOTHER CRACK
continue
except Exception, e:
print "Something failed: ", e
print "tweets extracted....", len(allTweets)
return allTweets
# ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ **
if __name__ == "__main__":
if len(sys.argv) > 1:
key = "XXXX"
secret = "XXXX"
access_token = "XXXX"
access_token_secret = "XXXX"
# authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(key, secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
screen_name = 'triplejplays'
limit = int(sys.argv[2]) if len(sys.argv) > 2 else 0 # ternary lols.
db = accessDB.accessDatabase()
# ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ #
if str(sys.argv[1]) == "GET_ALL":
parsedTweets = get_tweets(api, screen_name, limit=limit)
# ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ #
elif str(sys.argv[1]) == "GET_NEW":
try:
queryOut = dbQueries.getNewestEntryInDB(db)
newestID = dbQueries.getEntryforRow(queryOut, 'id')
print 'newest id was:', newestID, '\n'
print queryOut, '\n************************\n'
parsedTweets = get_tweets_after(api, screen_name, since_id=newestID, limit=limit)
except Exception, e:
print "Couldn't get newer tweets (perhaps the db is empty? Or you're up to date!)"
print e
pass
# ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ #
elif str(sys.argv[1]) == "GET_OLD":
try:
queryOut = dbQueries.getOldestEntryInDB(db)
oldestID = dbQueries.getEntryforRow(queryOut, 'id')
print 'oldest id was:', oldestID, '\n'
print queryOut, '\n************************\n'
parsedTweets = get_tweets_before(api, screen_name, max_id=oldestID)
#parsedTweets = get_tweets_before(api, screen_name, max_id=oldestID, limit=limit)
except Exception, e:
print "Couldn't get older tweets (perhaps the db is empty? Or you've got the very first one!...)"
print e
pass
elif str(sys.argv[1]) == "NEWEST_ENTRY":
try:
queryOut = dbQueries.getNewestEntryInDB(db)
print queryOut, '\n************************\n'
except Exception, e:
print "Couldn't get older tweets (perhaps the db is empty? Or you've got the very first one!...)"
print e
# ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ #
else:
print "Your arguments were not recognised:\n"
for arg in sys.argv:
print arg
# ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ #
try:
dbInserts.batchInsert(db, parsedTweets)
except Exception, e:
print "\n *** BIG ERROR - your tweets were not inserted into the database *** \n"
print e
accessDB.closeConnection(db)
else:
print("Please provide some arguments hey")