forked from voussoir/reddit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadreader.py
More file actions
97 lines (78 loc) · 2.65 KB
/
Copy paththreadreader.py
File metadata and controls
97 lines (78 loc) · 2.65 KB
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
import praw
import textwrap
import time
import datetime
FALLBACKID = ""
USERAGENT = ""
try:
pid=input('Thread ID: ')
except EOFError:
if FALLBACKID == '':
print('\nCannot run in this environment. Your device is not accepting input')
print('You may run this program another way, or edit the FALLBACKID variable with the ID')
time.sleep(99999)
else:
pid = FALLBACKID
print('Connecting to reddit')
r=praw.Reddit(USERAGENT)
print('Getting post')
post = r.get_info(thing_id='t3_' + pid)
print('Getting all comments')
post.replace_more_comments(limit=None, threshold=1)
comments = post.comments
print('Creating file')
outfile = open('t3_' + pid + '.txt', 'w', encoding='utf-8')
print('/r/' + post.subreddit.display_name, file=outfile)
print(post.title + '\n', file=outfile)
if post.selftext != "":
selftext = post.selftext
pbody = post.selftext.replace('\n\n', '\n')
pfinal = ''
for paragraph in pbody.split('\n\n'):
pfinal += '\n'.join(textwrap.wrap(paragraph))
pfinal += '\n'
else:
pfinal = post.url
print(pfinal, file=outfile)
print(post.permalink + '\n\n\n', file=outfile)
DEPTHSYMBOL = " "
def recursivereplies(inlist, depth):
for reply in inlist:
print(' ', file=outfile)
try:
cauthor = reply.author.name
except AttributeError:
cauthor = "[DELETED]"
print(DEPTHSYMBOL * depth + '/u/' + cauthor + ', ' + reply.id + ', ' + humanize(reply.created_utc), file=outfile)
cbody = reply.body
cbody = DEPTHSYMBOL*depth + cbody
#cbody = cbody.replace('\n\n', '\n')
cfinal = ''
for paragraph in cbody.split('\n'):
cfinal += ('\n').join(textwrap.wrap(paragraph, 120))
cfinal += '\n'
cfinal = cfinal[:-1]
cfinal = cfinal.replace('\n', '\n' + DEPTHSYMBOL*depth)
print(cfinal, file=outfile)
print(DEPTHSYMBOL*depth + '-'*10, file=outfile)
recursivereplies(reply.replies, depth+1)
def humanize(instamp):
date = datetime.datetime.utcfromtimestamp(instamp)
date = datetime.datetime.strftime(date, "%b %d %Y %H:%M:%S UTC")
return date
for comment in comments:
try:
cauthor = comment.author.name
except AttributeError:
cauthor = "[DELETED]"
print('/u/' + cauthor + ', ' + comment.id + ', ' + humanize(comment.created_utc), file=outfile)
cbody = comment.body
#cbody = cbody.replace('\n\n', '\n')
cbody = '\n'.join(textwrap.wrap(cbody))
print(cbody, file=outfile)
print('-'*10, file=outfile)
recursivereplies(comment.replies, 1)
print('*'*50, file=outfile)
outfile.close()
print('Done')
input()