-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
333 lines (283 loc) · 12.8 KB
/
database.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from time import gmtime, strftime
import json
import os
from database import *
config_location = "configs/database.config.json"
config = json.load(open(config_location, "r"))
database_location = config["databaseLocation"]
def time():
return strftime("%Y-%m-%d--%H-%M-%S", gmtime())
def getLatestTweets():
latest = []
if os.path.exists(database_location + "latest.json"):
with open(database_location + "latest.json", "r") as datafile:
latest = json.load(datafile)
return latest
def writeLatestTweet(diction):
latest = [diction]
latest.extend(getLatestTweets())
latest = sorted(latest, key=lambda k: k["id"], reverse=True)
with open(database_location + "latest.json", "w") as datafile:
json.dump(latest[:50], datafile)
def initializeAccountData(id):
if not os.path.exists(database_location +
"accounts/" + str(id)):
os.mkdir(database_location + "accounts/" + str(id))
if not os.path.exists(database_location +
"accounts/" + str(id) + "/archive/"):
os.mkdir(database_location + "accounts/" + str(id) + "/archive/")
def hasTweetArchive(id):
return os.path.exists(database_location +
"accounts/" + str(id) + "/tweets/")
def markTweetAsDeleted(userid, tweetid):
data = []
with open(database_location + "accounts/" + str(userid) + "/tweets/" + str(tweetid) + ".json", "r") as tweetfile:
data = json.load(tweetfile)
data["deleted"] = True
with open(database_location + "accounts/" + str(userid) + "/tweets/" + str(tweetid) + ".json", "w") as tweetfile:
json.dump(data, tweetfile)
deleted = {}
userid = str(userid)
tweetid = int(tweetid)
if os.path.exists(database_location + "deleted.json"):
with open(database_location + "deleted.json", "r") as deleted_file:
deleted = json.load(deleted_file)
if userid in deleted.keys():
if tweetid not in deleted[userid]:
deleted[userid].append(tweetid)
else:
deleted[userid] = [tweetid]
with open(database_location + "deleted.json", "w") as deleted_file:
json.dump(deleted, deleted_file)
def getTweet(userid, tweetid):
with open(database_location + "accounts/" + str(userid) + "/tweets/" + str(tweetid) + ".json", "r") as tweetfile:
data = json.load(tweetfile)
if "deleted" not in data:
data["deleted"] = False # edge cases
if "retrieved" not in data and "retreived" not in data:
data["retrieved"] = "n/a"
return data
def initializeTweetArchive(id):
if not os.path.exists(database_location +
"accounts/" + str(id) + "/tweets/"):
os.mkdir(database_location + "accounts/" + str(id) + "/tweets/")
def getAccountFromDatabase(id):
if os.path.exists(database_location +
"accounts/" + str(id) + "/account.json"):
with open(database_location + "accounts/" + str(id) +
"/account.json", "r") as account_file:
data = json.load(account_file)
metadata = getAccountMetadata(id)
for key in metadata.keys():
data[key] = metadata[key]
if "deleted" not in data:
data["deleted"] = False # edge cases
if "url" not in data:
data["url"] = "https://twitter.com/" + data["screen_name"] # edge cases
return data
def hasAccountData(id):
return os.path.exists(database_location + "accounts/" +
str(id) + "/account.json")
deleted_tweet_data = {}
def getAllDeletedTweets():
deleted = getDeletedTweetsMap()
tweet_data = []
for user in deleted.keys():
for tweet in deleted[user]:
if str(user) not in deleted_tweet_data:
deleted_tweet_data[str(user)] = {str(tweet): getTweet(user, tweet)}
if str(user) in deleted_tweet_data and str(tweet) not in deleted_tweet_data[str(user)]:
deleted_tweet_data[str(user)][str(tweet)] = getTweet(user, tweet)
data = deleted_tweet_data[str(user)][str(tweet)]
if "user" not in data or "profile_image_url" not in data["user"]:
data["user"] = getAccountFromDatabase(user)
print(data["user"])
deleted_tweet_data[str(user)][str(tweet)] = data
tweet_data.append(deleted_tweet_data[str(user)][str(tweet)])
tweets = sorted(tweet_data, key=lambda k: int(k["id"]), reverse=True)
return tweets
def getDeletedTweets(id):
deleted = getDeletedTweetsMap()
if str(id) in deleted:
return deleted[str(id)]
return []
def getDeletedTweetsData(id):
ids = getDeletedTweets(id)
data = [getTweet(id, tweet) for tweet in ids] # I think i'm starting to get the hang of this
return data
def getDeletedTweetsMap():
deleted = {}
if os.path.exists(database_location + "deleted.json"):
with open(database_location + "deleted.json", "r") as deleted_file:
deleted = json.load(deleted_file)
return deleted
def getAllAccountsInDatabase():
accounts = []
for directory in os.listdir(database_location + "accounts/"):
if directory.startswith("."):
continue
accounts.append(int(directory))
return accounts
def writeAccountToDatabase(account):
with open(database_location + "accounts/" + str(account.id) +
"/account.json", "w") as account_file:
account_file.write(account.AsJsonString())
def writeAccountMetadata(id, metadata):
with open(database_location + "accounts/" + str(id) +
"/metadata.json", "w") as meta_file:
json.dump(metadata, meta_file)
def hasAccountMetadata(id):
return os.path.exists(database_location + "accounts/" + str(id) +
"/metadata.json")
def writeTweet(account, tweet_id, jsondata):
initializeTweetArchive(account)
with open(database_location + "accounts/" + str(account) + "/tweets/" + str(tweet_id) + ".json", "w") as tweet_archive:
data = json.loads(jsondata)
data["retrieved"] = time()
json.dump(data, tweet_archive)
def getHighestLowestArchivedStatus(id):
lowest_archived_status = -1 # for the max_id parameter for subsequent searches
highest_archived_status = -1
for dirname, dirnames, filenames in os.walk(database_location + "accounts/" + str(id) + "/tweets/"):
for filename in filenames:
try:
status = int(filename.replace(".json", "")) # could be cleaner but it'll do
if status < lowest_archived_status or lowest_archived_status == -1:
lowest_archived_status = status
if status > highest_archived_status or highest_archived_status == -1:
highest_archived_status = status
except Exception:
pass
# is a .file
return [highest_archived_status, lowest_archived_status]
def getAllTweetsInDatabase(id):
tweets = []
for dirname, dirnames, filenames in os.walk(database_location + "accounts/" + str(id) + "/tweets/"):
for filename in filenames:
try:
tweets.append(getTweet(id, int(filename.replace(".json", ""))))
except Exception:
pass
# is a .file
return tweets
def getAccountMetadata(id):
if not hasAccountMetadata(id):
return {}
with open(database_location + "accounts/" + str(id) +
"/metadata.json", "r") as meta_file:
return json.load(meta_file)
def archiveAccountData(account):
if not os.path.exists(database_location + "accounts/" + str(account.id) + "/archive/"):
os.mkdir(database_location + "accounts/" + str(account.id) + "/archive/")
with open(database_location + "accounts/" + str(account.id) + "/archive/" + time() + ".json", "w") as archive_file:
archive_file.write(account.AsJsonString())
def getFollowing():
return json.load(open(database_location + "following.json", "r"))
def hasFollowingData():
return os.path.exists(database_location + "following.json")
def writeFollowingData(following):
with open(database_location + "following.json", "w") as f:
json.dump(following, f)
"""
Clean all tweets in database.
"""
def cleanAllTweets():
total = len(os.listdir(database_location + "accounts/"))
ind = 0.0
affected = 0
for dirname in os.listdir(database_location + "accounts/"):
if dirname.startswith("."):
continue
ind += 1.0
print(str(ind/total*100) + "%: " + dirname)
try:
for filename in os.listdir(database_location + "accounts/" + dirname+"/tweets/"):
try:
if filename.startswith("."):
continue
with open(database_location + "accounts/" + dirname+"/tweets/" + filename, "r") as tweet:
#print("Scanning " + database_location + "accounts/" + dirname+"/tweets/" + filename)
twdata = json.load(tweet)
if "deleted" not in twdata.keys() or "retrieved" not in twdata.keys():
if "deleted" not in twdata.keys():
twdata["deleted"] = False
if "retrieved" not in twdata.keys():
twdata["retrieved"] = time()
if "retreived" in twdata.keys():
del twdata["retreived"]
with open(database_location + "accounts/" + dirname+"/tweets/" + filename, "w") as tweet_write:
json.dump(twdata, tweet_write)
affected += 1
print("Cleaned tweet " + twdata["id_str"])
except Exception as e:
print(e)
continue
except Exception as e:
print(e)
continue
#except Exception as exception:
# print("Error: " + str(exception))
print("Cleaned " + str(affected) + " tweets.")
"""
Collect all media!
"""
def findAllMediaInDatabase(debug=True):
media = []
total = len(os.listdir(database_location + "accounts/"))
ind = 0.0
for dirname in os.listdir(database_location + "accounts/"):
if dirname.startswith("."):
continue
ind += 1.0
print(str(ind/total*100) + "%: " + dirname)
try:
for filename in os.listdir(database_location + "accounts/" + dirname+"/tweets/"):
try:
if filename.startswith("."):
continue
with open(database_location + "accounts/" + dirname+"/tweets/" + filename, "r") as tweet:
#print("Scanning " + database_location + "accounts/" + dirname+"/tweets/" + filename)
twdata = json.load(tweet)
if "media" in twdata.keys():
i = 0
for item in twdata["media"]:
media.append(twdata["media"][i]["media_url_https"])
if debug:
print(str(ind/total*100) + "%: ...found media: " + twdata["media"][i]["media_url_https"])
i += 1
except Exception as e:
print(e)
continue
except Exception as e:
print(e)
continue
#except Exception as exception:
# print("Error: " + str(exception))
print("Found " + str(len(media)) + " pieces of media.")
if os.path.exists(database_location + "media.json"):
with open(database_location + "media.json", "r") as media_file:
media_old = json.load(media_file)
for item in media_file:
if item not in media:
media.append(item)
with open(database_location + "media.json", "w") as media_file:
json.dump(media, media_file)
return media
def markAsHasDeletedTweet(id):
metadata = getAccountMetadata(id)
metadata["has_deleted_tweet"] = True
writeAccountMetadata(id, metadata)
def retreiveAllStatusesFromDatabase(id):
statuses = []
if os.path.exists(database_location + "accounts/" + str(id) + "/tweets/"):
for dirname, dirnames, filenames in os.walk(database_location + "accounts/" + str(id) + "/tweets/"):
for filename in filenames:
if filename.startswith("."):
continue
try:
fp = database_location + "accounts/" + str(id) + "/tweets/" + filename
with open(fp, "r") as tweetfile:
statuses.append(json.load(tweetfile))
except Exception:
print("Error: " + str(exception))
return statuses