-
Notifications
You must be signed in to change notification settings - Fork 2
/
subreddit.py
226 lines (181 loc) · 7.1 KB
/
subreddit.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
import calendar
import json
import sys
import time
from ast import main
from datetime import datetime, timedelta
from encodings.utf_8 import encode
from mimetypes import init
import praw
from peewee import *
import config
subreddit_name = "AmItheAsshole"
reddit = None
db = SqliteDatabase(subreddit_name + '.sqlite')
UPDATE = True
POST_LIMIT = 50
class BaseModel(Model):
class Meta:
database = db
class Submission(BaseModel):
id = AutoField()
submission_id = CharField(unique=True)
title = TextField()
selftext = TextField()
created_utc = DateField()
permalink = TextField()
score = IntegerField()
class Comment(BaseModel):
id = AutoField()
submission = ForeignKeyField(Submission, backref='comments')
message = TextField()
comment_id = CharField(unique=True)
parent_id = CharField()
created_utc = DateField()
score = IntegerField()
def create_database():
db = SqliteDatabase(subreddit_name + '.sqlite')
db.connect()
db.create_tables([Submission, Comment])
def get_submissions():
reddit: praw.Reddit = None
agent = "Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion"
client_id = config.client_id
client_secret = config.client_secret
if client_id == None or client_secret == None:
exit(1)
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
user_agent=agent
)
display = 1
for submission in reddit.subreddit(subreddit_name).hot(limit=POST_LIMIT):
print(display)
display = display + 1
submission.comments.replace_more(limit=0)
# submission.comments.replace_more(limit=None, threshold=0)
query = Submission.select().where(Submission.submission_id == submission.id)
if query.exists() == False:
Submission.create(
submission_id = submission.id,
title = submission.title,
selftext = submission.selftext,
created_utc = submission.created_utc,
permalink = submission.permalink,
score = submission.score
)
else:
if UPDATE is False:
continue
else:
if submission.selftext == "[removed]":
continue
# print("Updating submission")
update_query = Submission.update(title = submission.title,
selftext = submission.selftext,
created_utc = submission.created_utc,
permalink = submission.permalink,
score = submission.score).where(Submission.submission_id == submission.id)
update_query.execute()
# Submission.update()
comments = submission.comments.list()
for comment in comments:
query = Comment.select().where(Comment.comment_id == comment.id)
if query.exists() == False:
Comment.create(submission_id=submission.id,
message = comment.body,
comment_id = comment.id,
parent_id = comment.parent_id,
created_utc = comment.created_utc,
score = comment.score)
else:
if UPDATE is False:
continue
else:
update_query = Comment.update(submission_id = submission.id,
message = comment.body,
parent_id = comment.parent_id,
created_utc = comment.created_utc,
score = comment.score).where(Comment.comment_id == comment.id)
update_query.execute()
def main():
create_database()
get_submissions()
create_warehouse()
# A super simple warehouse that is not even a warehouse, basically it qets all submissions for a day and builds up
# a json and also the indexes
def create_warehouse():
# This function will make indices in for the API to consume with the submission IDs and the file name is the unix timestamp
# This does not need to be date aware
today = datetime.today()
start = datetime(today.year, today.month, today.day) + timedelta(1)
yesterday = start - timedelta(2)
# Convert time to UTC time
start_utc = calendar.timegm(start.timetuple())
yesterday_utc = calendar.timegm(yesterday.timetuple())
# query = Submission.select().order_by(Submission.score.desc())
query = Submission.select().where(Submission.created_utc.between(yesterday_utc, start_utc))
submissions = list(query.dicts())
sys.stdout.reconfigure(encoding='utf-8')
results = []
indexes = []
created_utcs = []
scores = []
entries = []
for index in range(0,len(submissions)):
_submission_id = (submissions[index]["submission_id"])
_submission_pk = submissions[index]["id"]
_score = submissions[index]["score"]
_created_utc = submissions[index]["created_utc"]
# indexes.append(_submission_pk)
# scores.append(_score)
# created_utcs.append(_created_utc)
entry = {
"id": _submission_pk,
"scores": _score,
"created_utc": _created_utc
}
_comment_query = Comment.select(Comment.message).where(Comment.submission == _submission_id)
_replies_list = list(_comment_query.dicts())
# print((replies))
replies = []
for reply in _replies_list:
replies.append(reply["message"])
submissions[index]["replies"] = replies
# This is the summary of the submission
write_to_file(json.dumps(submissions[index]), "./submissions/" + str(_submission_pk))
entries.append(entry)
# print(json.dumps(submissions[0]))
# json_indexes = {
# "indexes": indexes,
# "scores": scores,
# "created_utcs": created_utcs
# }
# Create indexes for query
# write_to_file(json.dumps(entries), "./indexes/" + "indexes")
data = update_index(entries)
write_to_file(json.dumps(data), "./indexes/" + "indexes")
def update_index(entries):
with open("./docs/api/indexes/indexes.json", 'r') as json_file:
data = json.load(json_file)
used = []
for d in data:
for entry in entries:
if d["id"] == entry["id"]:
print("updating")
d.update(entry)
used.append(entry)
for entry in entries:
if entry not in used:
data.append(entry)
print("new entry")
return data
def write_to_file(json, file_name):
f = open("./docs/api/" + str(file_name) + ".json", "w")
f.write(json)
f.close()
if __name__ == '__main__':
get_submissions()
create_warehouse()
# main()