forked from sJohnsonStoever/redditPostArchiver
-
Notifications
You must be signed in to change notification settings - Fork 9
/
postids.py
147 lines (126 loc) · 4.9 KB
/
postids.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
#!/usr/bin/env python3
import os
import sys
import arrow
import praw
import requests
import yaml
from prawcore.exceptions import RequestException, NotFound
from requests.exceptions import HTTPError
"""
Customization Configuration
"""
# Default post_id: #
username = 'GallowBoob'
# Path to which to output the file #
# output_file_path = './'
# The Path to the stylesheet, relative to where the html file will be stored #
path_to_css = 'css/style.css'
"""
Reddit Post Archiver
By Samuel Johnson Stoever
"""
if len(sys.argv) == 1:
print('No username was provided. Using default username.')
elif len(sys.argv) > 2:
print('Too Many Arguments. Using default username.')
else:
username = sys.argv[1]
username = username.rstrip('/')
if '/u/' in username:
username = username.split('/u/')[1]
elif '/user/' in username:
username = username.split('/user/')[1]
print('Processing all posts submitted by', username)
cred_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'credentials.yml')
credentials = yaml.load(open(cred_path), Loader=yaml.SafeLoader)
r = praw.Reddit(client_id=credentials['client_id'],
client_secret=credentials['client_secret'],
user_agent=credentials['user_agent'])
def get_user_post_id_set(user, first_id, postcount):
user_post_id_set = set()
if first_id is not None:
params = dict(after=first_id, count=postcount)
postgenerators = user.submissions.new(params=params)
else:
postgenerators = user.submissions.new()
try:
for post in postgenerators:
post_id = "{}\n".format(post.id)
user_post_id_set.add(post_id)
postcount += 1
if postgenerators.yielded == 100:
try:
first_id = postgenerators.params['after']
except KeyError:
first_id = None
except NotFound:
print('User not found with Reddit API. Most likely deleted.')
return user_post_id_set, first_id, postcount
def get_reddit_submissions(reddituser):
try:
user = r.redditor(reddituser)
except HTTPError:
print('Unable to write post ids: Invalid username or login credentials')
return
first_id = None
postcount = 0
try:
user_post_id_set, first_id, postcount = get_user_post_id_set(user, first_id, postcount)
except RequestException:
return
post_id_set = user_post_id_set
subnumber = len(user_post_id_set)
print("Received", subnumber, "posts from", reddituser)
totalsubnumber = subnumber
while subnumber > 99:
try:
user_post_id_set, first_id, postcount = get_user_post_id_set(user, first_id, postcount)
except RequestException:
break
subnumber = len(user_post_id_set)
totalsubnumber += subnumber
post_id_set |= user_post_id_set
print("Received additional", subnumber, "posts from Reddit for", reddituser, " - Total posts received so far:",
totalsubnumber, "with", len(post_id_set), "in set.")
return post_id_set
def get_push_submissions(reddituser):
push_post_id_set = set()
now = int(arrow.utcnow().timestamp())
linktemplate = "https://api.pushshift.io/reddit/search/submission/?author={author}" \
"&before={timestamp}&sort=desc&size=500"
url = linktemplate.format(author=reddituser, timestamp=now)
rp = requests.get(url)
push = rp.json()
earliest = now
subnumber = len(push['data'])
totalsubnumber = 0
print("Received", subnumber, "pushshift.io posts from", reddituser)
while subnumber > 0:
totalsubnumber += subnumber
itemlist = push['data']
push['data'] = list()
for item in itemlist:
if item['created_utc'] < earliest:
earliest = item['created_utc']
post_id = "{}\n".format(item['id'])
push_post_id_set.add(post_id)
url = linktemplate.format(author=reddituser, timestamp=earliest)
rp = requests.get(url)
push = rp.json()
subnumber = len(push['data'])
print("Received additional", subnumber, "posts from", reddituser, " - Total posts received so far:",
totalsubnumber, "with", len(push_post_id_set), "in pushshift.io set.")
return push_post_id_set
def main():
reddit_post_id_set = get_reddit_submissions(username)
push_post_id_set = get_push_submissions(username)
post_id_set = reddit_post_id_set.union(push_post_id_set)
print("Total posts submitted by", username, "in set:", len(post_id_set))
filedate = int(arrow.now().timestamp())
basedir = "/rpa" if os.environ.get('DOCKER', '0') == '1' else '.'
output_file_path = "{basedir}/{username}_{timestamp}.txt".format(basedir=basedir, username=username, timestamp=filedate)
with open(output_file_path, 'w', encoding='UTF-8') as post_file:
post_file.writelines(post_id_set)
if __name__ == '__main__':
main()