I am trying to follow reddit u/busterroni's video on how to make a reddit bot, but while his program runs fine, mine keeps getting hung up on "filename" on the line below that says...
usernames = get_usernames(filename)
...under main(). Can anyone tell me what the issue is? BTW I am at 12:11 in the video in case that helps at all.
import praw
import sys
def main():
reddit = authenticate()
usernames = get_usernames(filename) #ERROR
print(usernames)
def authenticate():
print('Authenticating...')
reddit = praw.Reddit(
'notifier',
user_agent="BlueWizard3's joketest v0.1")
print('Authenticated as {}'.format(reddit.user.me()))
return reddit
def get_usernames(filename):
try:
with open(filename, 'r') as f:
usernames = f.read()
usernames = usernames.split('\n')
usernames = filter(None, usernames)
except IOError:
print('Error: file ', filename, " not found in the current directory.")
quit()
return usernames
main()
def send_message(r, username, subject, body):
try:
r.redditor(username).message(subject, body)
except praw.exceptions.APIException as e:
if 'USER_DOESNT_EXIST' in e.args[0]:
print(e.args[0])
if len(sys.argv) != 4:
print('usage: notifier.py file "subject" "body"')
filename = sys.argv[1] # ADDED
subject = sys.argv[2] # ADDED
body = sys.argv[3] # ADDED
for username in usernames:
send_message(r, username, subject, body)
First time posting on Stack Overflow so please be patient.
Your code is missing the part where it assigns a value to the variable 'filename' in the code from the referenced video
this is done on line 47 in the end code, line 37 at around 11m in:
47: filename = sys.argv[1]
48: subject = sys.argv[2]
49: body = sys.argv[3]
50:
51: r = bot_login()
52: usernames = get_usernames(filename)
EDIT: Now we've fixed that, we need to address the next problem, which is that you've got stuff in main that should just be straight after the values are passed in via sys.argv. That means the below code should not be in the 'main' method earlier in the program, but after line 50
reddit = authenticate()
usernames = get_usernames(filename) #ERROR
print(usernames)