-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_collector.py
141 lines (119 loc) · 4.31 KB
/
comment_collector.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
# Use pushshift to get posts, use collected post to go to reddit and get comments
import math
import json
import requests
import itertools
import numpy as np
import time
from datetime import datetime, timedelta
import sqlite3
import praw
config = {
"username" : "",
"password" : "",
"client_id" : "",
"client_secret" : "",
"user_agent" : ""
}
reddit = praw.Reddit(client_id = config['client_id'], \
client_secret = config['client_secret'], \
user_agent = config['user_agent'], \
username = config['username'], \
password = config['password'])
def make_request(uri, max_retries = 5):
def fire_away(uri):
response = requests.get(uri)
assert response.status_code == 200
return json.loads(response.content)
current_tries = 1
while current_tries < max_retries:
try:
time.sleep(1)
response = fire_away(uri)
return response
except:
time.sleep(1)
current_tries += 1
return fire_away(uri)
def pull_posts_for(subreddit, start_at, end_at):
def map_posts(posts):
return list(map(lambda post: { #add the post data that you want here
'id': post['id'],
'created_utc': post['created_utc'],
'prefix': 't4_',
'title': post['title']
}, posts))
SIZE = 500
URI_TEMPLATE = r'https://api.pushshift.io/reddit/search/submission?subreddit={}&after={}&before={}&size={}'
post_collections = map_posts( \
make_request( \
URI_TEMPLATE.format( \
subreddit, start_at, end_at, SIZE))['data'])
n = len(post_collections)
while n == SIZE:
last = post_collections[-1]
new_start_at = last['created_utc'] - (10)
more_posts = map_posts( \
make_request( \
URI_TEMPLATE.format( \
subreddit, new_start_at, end_at, SIZE))['data'])
n = len(more_posts)
post_collections.extend(more_posts)
return post_collections
def give_me_intervals(start_at, number_of_days_per_interval = 3):
end_at = math.ceil(datetime.utcnow().timestamp())
## 1 day = 86400,
period = (86400 * number_of_days_per_interval)
end = start_at + period
yield (int(start_at), int(end))
padding = 1
while end <= end_at:
start_at = end + padding
end = (start_at - padding) + period
yield int(start_at), int(end)
def getcomments(subreddit, posts):
TIMEOUT_AFTER_COMMENT_IN_SECS = .350
cmt_table = []
for submission_id in np.unique([ post['id'] for post in posts ]):
submission = reddit.submission(id=submission_id)
submission_title = submission.title
submission.comments.replace_more(limit=None)
for comment in submission.comments.list():
try:
author_name = comment.author.name
except:
author_name = "[deleted]"
cmt_row = [submission.id, submission_title, subreddit, comment.id, \
author_name, comment.body, comment.created_utc]
cmt_table.append(cmt_row)
if TIMEOUT_AFTER_COMMENT_IN_SECS > 0:
time.sleep(TIMEOUT_AFTER_COMMENT_IN_SECS)
return cmt_table
def getsubs(subfile):
subs = []
with open(subfile) as f:
for line in f:
subs.append(line.strip())
return subs
def writeout(cmt_table):
commentdb = "comments.db"
conn = sqlite3.connect(commentdb)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS comments (submission_id text, submission_title text, \
subreddit text, comment_id text, comment_author text, comment_body text, comment_created int)''')
for cmt_row in cmt_table:
c.execute('''INSERT INTO comments VALUES(?,?,?,?,?,?,?)''', tuple(cmt_row))
conn.commit()
## Main <-------------------
subreddits = getsubs("")
for subreddit in subreddits:
start_at = math.floor(\
(datetime.utcnow() - timedelta(days=365)).timestamp()) #past X days 1460
posts = []
for interval in give_me_intervals(start_at, 7):
pulled_posts = pull_posts_for(
subreddit, interval[0], interval[1])
posts.extend(pulled_posts)
time.sleep(.500)
cmt_table = getcomments(subreddit, posts)
#writeout(cmt_table)