-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit_tools.py
123 lines (105 loc) · 3.06 KB
/
reddit_tools.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
import sys
try:
import praw
except:
print("praw not installed")
install_command = sys.executable + " -m pip install praw"
print("Install command: " + install_command)
sys.exit()
version = '0.1'
username = 'Encanto_LyricBot'
owner = "00PT"
test_subreddit = '00PTBotTest'
reddit = praw.Reddit('bot1')
def get_comment(comment_id):
comment = reddit.comment(id=comment_id)
return comment
def get_comments(subreddit, limit=1000):
comments = list(subreddit.comments(limit=limit))
return comments
def get_submission(submission_id):
submission = reddit.submission(id=submission_id)
return submission
def get_submissions(subreddit, limit=1000):
submissions = list(subreddit.new(limit=limit))
return submissions
def get_mods(subreddit):
mods = subreddit.moderator()
result = []
for mod in mods:
result.append(mod.name)
return result
def is_root_comment(comment):
if not hasattr(comment, 'link_id'):
comment.refresh()
if comment.parent_id == comment.link_id:
return True
else:
return False
def did_reply_comment(comment, username=username, require_root=True):
comment.refresh()
replies = list(comment.replies)
for reply in replies:
if reply.author:
if reply.author.name == username:
return True
elif (not require_root) and did_reply_comment(reply, username, False):
return True
return False
def did_reply_submission(submission, username=username, require_root=True):
comments = list(submission.comments)
for comment in comments:
if comment.author:
if comment.author.name == username:
return True
elif (not require_root) and did_reply_comment(comment, username, False):
return True
return False
def get_notifications(type='comment', unread=False, mark_read=True):
notifications = []
if unread:
notifications = reddit.inbox.unread()
else:
notifications = reddit.inbox.all()
result = []
for notification in notifications:
if type == 'comment':
if isinstance(notification, praw.models.Comment):
result.append(notification)
if mark_read:
reddit.inbox.mark_read([notification])
elif type == 'submission':
if isinstance(notification, praw.models.Submission):
result.append(notification)
if mark_read:
reddit.inbox.mark_read([notification])
elif type == 'message':
if isinstance(notification, praw.models.Message):
result.append(notification)
if mark_read:
reddit.inbox.mark_read([notification])
elif type == 'all':
result.append(notification)
if mark_read:
reddit.inbox.mark_read([notification])
elif type == 'mention':
for mention in reddit.inbox.mentions():
result.append(mention)
if mark_read:
reddit.inbox.mark_read([mention])
else:
raise Exception('Invalid notification type: ' + type)
return list(result)
def reply_to_comment(comment, text):
return comment.reply(text)
def reply_to_submission(submission, text):
return submission.reply(text)
def reply_to_message(message, text):
return message.reply(text)
def get_comment_level(comment):
level = 0
parent = comment
while not is_root_comment(parent):
level += 1
parent = parent.parent()
return level