forked from Fitzy1293/reddit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmissionComments.py
More file actions
73 lines (54 loc) · 2.66 KB
/
submissionComments.py
File metadata and controls
73 lines (54 loc) · 2.66 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
import praw
from praw.models import MoreComments
from pprint import pprint
import csv,os
reddit = praw.Reddit('Your authentification info')
def main():
print('This program creates .csv files of comments for current posts in a subreddt.\n')
subreddit = input('Enter a subreddit >> ')
limit = int(input('Enter the number of hot posts you want comments for >> '))
print('\nGetting data from reddit.')
submissionComments = getComments(reddit.subreddit(subreddit), limit)
directory = input('\nEnter a directory to create .csv files containing all comments in that post >> ')
createCsvFiles(directory, submissionComments)
print('\n' + str(len(submissionComments)) + ' .csv file(s) were created in ' + directory)
createCombinedCsvFiles(directory, submissionComments)
def getComments(subreddit, limit):
for submission in subreddit.hot(limit=5): #Bypassing the stickies and changing user's limit +1 for each sticky
if submission.stickied:
limit = limit+1
subredditRange = subreddit.hot(limit = limit)
postComments = []
for submission in subredditRange:
if not submission.stickied:
authorInfo = []
submission.comments.replace_more(limit=None) #Reaches all comments.
comments = submission.comments.list()
for comment in comments:
authorInfo.append([comment.author, #Most useful fields.
comment.body,
comment.score,
comment.id])
postComments.append((subreddit, [submission.id, authorInfo]))
return postComments
#Creates separate .csv files for each post. Each row is a comment in the thread.
def createCsvFiles(directory, submissionComments):
for submission in submissionComments:
path = os.path.join(directory, submission[1][0] + '.csv') #Path for nw .csv.
spreadsheet = open(path + '.', 'w', encoding = 'UTF-8-sig', newline = '')
with spreadsheet:
writer = csv.writer(spreadsheet)
writer.writerow(['Author', 'Comment', 'Score', 'ID'])
writer.writerows(submission[1][1])
#Creates a .csv of every comment.
def createCombinedCsvFiles(directory, submissionComments):
path = os.path.join(directory, 'All_Comments.csv')
spreadsheet = open(path + '.', 'w', encoding = 'UTF-8-sig', newline = '')
with spreadsheet:
writer = csv.writer(spreadsheet)
writer.writerow(['Author', 'Comment', 'Score', 'ID'])
for submission in submissionComments:
writer.writerows(submission[1][1])
while True:
main()
print()