-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonForReddit.py
254 lines (222 loc) · 9.56 KB
/
PythonForReddit.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import praw
from enum import Enum
import os
from prawcore import NotFound
import datetime
import xml.dom.minidom
import sys
#https://www.geeksforgeeks.org/print-colors-python-terminal/
#https://stackoverflow.com/questions/2330245/python-change-text-color-in-shell
def hilite(string, color, bold=False):
attr = []
if color.value == Color.Green.value:
attr.append('32')
elif color.value == Color.Red.value:
attr.append('31')
elif color.value == Color.Orange.value:
attr.append('33')
elif color.value == Color.Grey.value:
attr.append('30')
elif color.value == Color.White.value:
attr.append('29')
elif color.value == Color.Blue.value:
attr.append('94')
elif color.value == Color.Purple.value:
attr.append('35')
if bold:
attr.append('1')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
class Option(Enum):
MyStats = 1
Preferences = 2
Random = 3
SearchSub = 4
Top10 = 5
SearchUser = 6
Quit = 9
class Color(Enum):
Red = 1
Green = 2
Orange = 3
Grey = 4
White = 5
Blue = 6
Purple = 7
def Clear():
input("Press Enter to continue...")
ClearWOMessage();
def ClearWOMessage():
os.system('cls' if os.name == 'nt' else 'clear');
def sub_exists(sub):
exists = True
try:
reddit.subreddits.search_by_name(sub, exact=True)
except NotFound:
exists = False
return exists
def user_exists(username):
exists = True
try:
reddit.redditor(username).link_karma
except NotFound:
exists = False
return exists
def PrintSub(SubName):
if (SubName != ""):
try:
while(True):
subobj = reddit.subreddit(SubName)
print("Name : %s" % subobj.stream.subreddit.display_name_prefixed)
print("Description : %s" % subobj.stream.subreddit.public_description)
print("Active users : %s" % subobj.stream.subreddit.active_user_count)
print("Quarantine : %s" % subobj.stream.subreddit.quarantine)
print("Subscribers : %s" % subobj.stream.subreddit.subscribers)
print("Subreddit Type : %s" % subobj.stream.subreddit.subreddit_type)
print("Created : %s" % datetime.datetime.fromtimestamp(subobj.stream.subreddit.created))
print("Advertiser category : %s" % subobj.advertiser_category)
print("Over 18 : %s" % subobj.over18)
if ViewPostAndComments(reddit.subreddit(SubName).hot(limit=10), reddit.subreddit(SubName).hot(limit=10)) == False:
break;
except Exception as e:
print("Error : %s" % str(e));
else:
print("Sub cannot be empty.");
def ViewPostAndComments(SubList1, SubList2):
var = input("View top 10 hottest posts? (Y/N)")
if var.upper() == "Y":
i = 1
for submission in SubList1:
ViewTop10(submission, i)
i += 1
else:
return False;
var = input("Enter number for the comments to view.")
i = 1
if var.isdigit() and int(var) <= 10 and int(var) >= 0:
GetToHere = int(var)
for submission in SubList2:
if (i == GetToHere):
ClearWOMessage();
ViewTop10(submission, i)
for Comment in submission.comments:
PrintComment(Comment)
i += 1
else:
return False;
Clear();
return True;
#https://www.2daygeek.com/rtv-reddit-terminal-viewer-a-simple-terminal-viewer-for-reddit/
def PrintComment(Comment, indent=""):
try:
print("")
if (Comment.author == None):
name = "[deleted]"
else:
name = Comment.author.name
print(hilite("%s%s" % (indent, name), Color.Blue))
print("%sScore: %d Upvotes: %d Downvotes: %d Controversiality: %d" % (indent, Comment.score, Comment.ups, Comment.downs, Comment.controversiality))
print("%sMessage : %s" % (indent, Comment.body))
print("%sPostdate : %s" % (indent,datetime.datetime.fromtimestamp(Comment.created_utc)))
print("%sPermalink : %s" % (indent,Comment.permalink))
for Replies in Comment.replies:
indent += " "
PrintComment(Replies, indent)
except Exception as e:
print("Error: %s" % str(e));
def ViewTop10(submission, i):
print("")
print(hilite("%s. %s" % (i, submission.title), Color.White, True))
print("%s" % submission.shortlink)
print("%s pts" % submission.score)
sys.stdout.write(hilite("%s " % submission.author, Color.Green, False))
print(hilite("%s" % submission.subreddit_name_prefixed, Color.Orange))
def ViewUserStats(Name):
redditor_obj = reddit.redditor(Name)
print("Username : %s" % redditor_obj.name)
print("Comment karma : %s" % redditor_obj.comment_karma)
print("Link karma : %s" % redditor_obj.link_karma)
print("Is Mod : %s" % redditor_obj.is_mod)
print("Is Gold : %s" % redditor_obj.is_gold)
print("Avatar URL : %s" % redditor_obj.icon_img)
print("Cake day : %s" % datetime.datetime.fromtimestamp(redditor_obj.created_utc))
var = input("View newest 10 comments? (Y/N)")
if var.upper() == "Y":
for comment in redditor_obj.comments.new(limit=10):
print("--------------------------------------------------------------------------")
print("Upvotes : %d Downvotes: %d, Controversiality: %d" % (comment.ups, comment.downs, comment.controversiality))
print("Comment : %s" % comment.body)
print("Permalink : %s" % comment.permalink)
doc = xml.dom.minidom.parse("Credentials.xml")
def ReadXML(nodename):
return doc.getElementsByTagName(nodename)[0].firstChild.data
reddit = praw.Reddit(client_id=ReadXML("client_id"),
client_secret=ReadXML("client_secret"),
password=ReadXML("password"),
user_agent=ReadXML("user_agent"),
username=ReadXML("username"))
ClearWOMessage();
while(True):
print(hilite("____ ____.__ ", Color.Orange));
print(hilite("\ \ / /|__| ____ __ _ __ ____ _______ ", Color.Orange));
print(hilite(" \ Y / | |_/ __ \ \ \/ \/ /_/ __ \ \_ __ \ ", Color.Orange));
print(hilite(" \ / | |\ ___/ \ / \ ___/ | | \/ ", Color.Orange));
print(hilite(" \___/ |__| \___ > \/\_/ \___ > |__| ", Color.Orange));
print(hilite(" \/ \/ ", Color.Orange));
print(hilite("___________ __________ .___ .___.__ __ ", Color.Orange));
print(hilite("\_ _____/ ____ _______ \______ \ ____ __| _/ __| _/|__|_/ |_ ", Color.Orange));
print(hilite(" | __) / _ \ \_ __ \ | _/_/ __ \ / __ | / __ | | |\ __\\", Color.Orange));
print(hilite(" | \ ( <_> ) | | \/ | | \\\\ ___/ / /_/ | / /_/ | | | | | ", Color.Orange));
print(hilite(" \___ / \____/ |__| |____|_ / \___ >\____ | \____ | |__| |__| ", Color.Orange));
print(hilite(" \/ \/ \/ \/ \/ ", Color.Orange));
print(hilite("By: Nicholas Bruno", Color.Green, False));
print("Made using the Python Reddit API Wrapper (PRAW)")
print("--------------------------------------------------------------------------")
print("Select an option:")
print(" 1. About me.")
print(" 2. My preferences.")
print(" 3. Random sub.")
print(" 4. Search sub.")
print(" 5. Top hot posts.")
print(" 6. Search for user.")
print(" 9. Quit.")
print("--------------------------------------------------------------------------")
var = input("Your choice: ")
if var.isdigit():
var = int(var)
if var == Option.MyStats.value:
ViewUserStats(reddit.user.me().name)
elif var == Option.Preferences.value:
preferences = reddit.user.preferences()
print("Label NSFW : %s" % preferences['label_nsfw'])
print("Include Over 18 : %s" % preferences['search_include_over_18'])
print("Accept PMs : %s" % preferences['accept_pms'])
elif var == Option.Random.value:
SubName = reddit.random_subreddit(False)
ClearWOMessage();
PrintSub(SubName.display_name)
elif var == Option.SearchSub.value:
SubName = input("Enter a subreddit name: ")
if sub_exists(SubName):
ClearWOMessage();
PrintSub(SubName)
else:
print("Subreddit doesn't exist.")
elif var == Option.SearchUser.value:
username = input("Enter a username: ")
if user_exists(username):
ClearWOMessage();
ViewUserStats(username)
else:
print("User not found.")
elif var == Option.Quit.value:
print("You chose to quit with the '%d' option." % var)
break
elif var == Option.Top10.value:
ClearWOMessage();
while(ViewPostAndComments(reddit.front.hot(limit=10), reddit.front.hot(limit=10)) == True):
no_op = 0
else:
print("Invalid entry number.")
else:
print("Please enter a digit.")
Clear();