-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYouTuberBot.py
More file actions
executable file
·153 lines (117 loc) · 3.78 KB
/
Copy pathYouTuberBot.py
File metadata and controls
executable file
·153 lines (117 loc) · 3.78 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
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
#! /usr/bin/Python
import praw, os, time, re
import pprint
import bisect
import pickle
from util import success, warn, log, fail, special, bluelog
### Uncomment to debug
import logging
logging.basicConfig(level=logging.DEBUG)
### Main program execution path
def YouTuberBot():
# Define globals
global r
global processedComments
# Load Configurations
OpenConfigFile()
# Load previously processed comments List
processedComments = LoadListFromFile("ProcessedComments.inf")
# Connect to reddit
r = LoginToReddit()
# Work horse section
try:
#SearchSubredditTitlesForKeywords("test", ['test', 'hello'])
SearchSubmisson('242i2k', ['hello'])
finally:
# Save newly processed comments
SaveListToFile(processedComments, "ProcessedComments.inf")
### Opens a config file that contains configuration settings.
def OpenConfigFile():
### Set root directory to script directory
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
with open ('datafile.inf', 'r') as myfile:
datafile_lines=myfile.readlines()
return datafile_lines
### Login to reddit
def LoginToReddit():
datafile_lines = OpenConfigFile()
r = praw.Reddit("LearnRedditAPI - Alpha .1")
USERNAME = datafile_lines[0].strip()
PASSWORD = datafile_lines[1].strip()
Trying = True
while Trying:
try:
r.login(USERNAME, PASSWORD)
success("LOGGED IN")
Trying = False
return r
except praw.errors.InvalidUserPass:
fail("WRONG USERNAME OR PASSWORD")
exit()
except Exception as e:
fail("%s"%e)
time.sleep(5)
def SearchSubredditTitlesForKeywords(subredditName, keyWords):
already_done = []
#Starting with a single subreddit. This may later be changed to look at all comments
subreddit = r.get_subreddit(subredditName)
for submission in subreddit.get_hot(limit=10):
op_text = submission.selftext.encode('ascii', 'ignore').lower()
has_keyWords = any(string in op_text for string in keyWords)
# Test if it contains a PRAW-related question
if submission.id not in already_done and has_keyWords:
msg = '[Keyword found](%s)' % submission.short_link
success(msg)
already_done.append(submission.id)
def SearchSubmisson(submissionId, keyWords):
submission = r.get_submission(submission_id=submissionId)
flat_comments = praw.helpers.flatten_tree(submission.comments)
for comment in flat_comments:
if comment.body.lower() == "hello" and comment.id not in processedComments:
#comment.reply('Goodbye')
InsertToList(processedComments, comment.id)
### Posts a reply to a comment
### Input - commentId of comment to reply to, message to reply with
def ReplyToComment(commentId, msg):
### To be implemented
return
### Retrieves video information.
### Input - youtube link
### Output - Metadata for video
def GetVideoInformation(link):
### To be implemented
return
### Determines if a comment has already been processed.
### Input - reddit commentId
### Output - bool indicating processed status.
def IsCommentAlreadyProcessed(commentId):
### To be implemented
return
def IsYouTubeLink(link):
pattern = '^(http\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$'
return bool(re.match(pattern, link))
###region List Functions###
### Load a list from file
def LoadListFromFile(FileName):
L= []
try:
L = pickle.load(open(FileName, "rb"))
except EOFError:
L = []
return L
### Save a list to file
def SaveListToFile(L, FileName):
pickle.dump(L, open(FileName, "wb"))
### Determines if value is in list
def IsInList(L, x, lo=0, hi=None):
hi = hi if hi is not None else len(L) # hi defaults to len(a)
pos = bisect_left(L,x,lo,hi) # find insertion position
return (pos if pos != hi and L[pos] == x else -1) # don't walk off the end
### Insert to list
def InsertToList(L, x):
bisect.insort(L, x)
###endregion List Functions###
if __name__ == "__main__":
YouTuberBot()