-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreddit.py
172 lines (146 loc) · 7.15 KB
/
reddit.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
if __name__ == "__main__":
print("You are running the wrong script. Please run the main.py")
quit()
import praw
import re
import os
import validators
from dotenv import load_dotenv
#----------------------------------------------#
from random import randint as get_random
from utils import *
def reddit_instance():
# settings #
load_dotenv()
client_id = os.getenv('REDDIT_CLIENT_ID')
client_secret = os.getenv('REDDIT_CLIENT_SECRET')
subreddit = os.getenv('SUBREDDIT')
flair = os.getenv('FLAIR')
if flair == "False":
flair = False
post_scan_limit = int(os.getenv('POST_SCAN_LIMIT'))
comment_length_to_select = int(os.getenv('COMMENT_LENGTH_TO_SELECT'))
# setting fixes
subreddit = re.sub('r\/', '', subreddit)
reddit = praw.Reddit(
client_id = client_id,
client_secret = client_secret,
user_agent="comment explorer",
)
print("Searching for a random post...")
results = list(filter(is_self, list(reddit.subreddit(subreddit).hot(limit=post_scan_limit))))
if flair:
results = list(filter(is_self, reddit.subreddit(subreddit).search('flair:"{}"'.format(flair), limit=post_scan_limit)))
random = get_random(0, len(results)-1)
post = results[random]
comments_of_post = list(post.comments.replace_more(limit=0))
comments_of_post = clean_comments(comments_of_post)
focused_comments = comments_of_post[0:comment_length_to_select]
character_exceed_length = len(list(filter(control_character_length, list(map(get_comment_content, focused_comments)))))
# recreate the comments array if array length less than given length and post selftext longer than provided character limit
invalid_comments_length = len(list(filter(invalid_regex_control.match, list(map(get_comment_content, comments_of_post)))))
post_selftext_character_control = False
if post.selftext:
post_selftext_character_control = control_character_length(post.selftext)
while (len(comments_of_post) < comment_length_to_select + invalid_comments_length + character_exceed_length) or post_selftext_character_control:
random = get_random(0, len(results)-1)
post = results[random]
comments_of_post = list(post.comments)
comments_of_post = clean_comments(comments_of_post)
focused_comments = comments_of_post[0:comment_length_to_select]
if post.selftext:
post_selftext_character_control = control_character_length(post.selftext)
else:
post_selftext_character_control = False
# remove invalid and that's character length longer than provided character limit comments
to_control_comments = list(map(get_comment_content, focused_comments))
invalid_comments_length = len(list(filter(invalid_regex_control.match, to_control_comments)))
while invalid_comments_length != 0 or character_exceed_length != 0:
continuous_comment_position = len(focused_comments)
current_position = 0
for comment in focused_comments:
# remove invalid content
cleaned = re.sub(invalid_regex, '', comment.body)
# remove spaces for control
cleaned = re.sub('[\s]', '', cleaned)
# control if string is empty
if cleaned == "" or control_character_length(comment.body):
focused_comments.pop(current_position)
focused_comments.append(comments_of_post[continuous_comment_position])
invalid_comments_length-=1
continuous_comment_position+=1
current_position+=1
# update invalid comments
to_control_comments = list(map(get_comment_content, focused_comments))
invalid_comments_length = len(list(filter(invalid_regex_control.match, to_control_comments)))
# update character exceed length
character_exceed_length = len(list(filter(control_character_length, list(map(get_comment_content, focused_comments)))))
# print summary
summary(post, focused_comments)
# returning data
return post, focused_comments
def get_comments():
# settings #
load_dotenv()
client_id = os.getenv('REDDIT_CLIENT_ID')
client_secret = os.getenv('REDDIT_CLIENT_SECRET')
comment_length_to_select = int(os.getenv('COMMENT_LENGTH_TO_SELECT'))
reddit = praw.Reddit(
client_id = client_id,
client_secret = client_secret,
user_agent="comment explorer",
)
post_url = input("Enter post url: ")
post = False
while not validators.url(post_url):
post_url = input("Enter post url: ")
while not post:
try:
post = reddit.submission(url=post_url)
except:
post_url = input("Enter post url: ")
comments_of_post = post.comments
comments_of_post = clean_comments(comments_of_post)
focused_comments = comments_of_post[0:comment_length_to_select]
character_exceed_length = len(list(filter(control_character_length, list(map(get_comment_content, focused_comments)))))
post_selftext_character_control = False
if post.selftext:
post_selftext_character_control = control_character_length(post.selftext)
else:
post_selftext_character_control = False
invalid_comments_length = len(list(filter(invalid_regex_control.match, list(map(get_comment_content, comments_of_post)))))
if (len(comments_of_post) < comment_length_to_select + invalid_comments_length + character_exceed_length) or post_selftext_character_control:
warn_prompt = input("The post you entered does not meet the requirements. Continue anyway? (yes/no) > ")
if warn_prompt.lower() == "yes":
warn_prompt = False
else:
warn_prompt = True
if warn_prompt:
post = focused_comments = False
return post, focused_comments
# remove invalid and that's character length longer than provided character limit comments
to_control_comments = list(map(get_comment_content, focused_comments))
while invalid_comments_length != 0 or character_exceed_length != 0:
continuous_comment_position = len(focused_comments)
current_position = 0
for comment in focused_comments:
# remove invalid content
cleaned = re.sub(invalid_regex, '', comment.body)
# remove spaces for control
cleaned = re.sub('[\s]', '', cleaned)
# control if string is empty
if cleaned == "" or control_character_length(comment.body):
focused_comments.pop(current_position)
focused_comments.append(comments_of_post[continuous_comment_position])
invalid_comments_length-=1
continuous_comment_position+=1
current_position+=1
# update invalid comments
to_control_comments = list(map(get_comment_content, focused_comments))
invalid_comments_length = len(list(filter(invalid_regex_control.match, to_control_comments)))
# update character exceed length
character_exceed_length = len(list(filter(control_character_length, list(map(get_comment_content, focused_comments)))))
# print summary
summary(post, focused_comments)
# returning data
return post, focused_comments