import praw
import time
import database
# Create a user agent and log into Reddit
UA = 'OZBOZZ v0.1'
r = praw.Reddit(UA)
# Log UA into reddit with my personal user name and password
r.login('', '', disable_warning='True')
subreddit = r.get_subreddit('listentothis')
current_time = int(time.time())
user = ''
submission_list = []
results = []
count = 0
genres = ["Big Band Funk", "progressive Rock", "rock", "progressive metal"]
def botstuff():
# while condition is true...
while True:
# Print Working so That I know the Next iteration has begun
print("Working...")
# For submission in subreddit /r/listen to this get up to 20 new posts
for submission in subreddit.get_new(limit=20):
# Create a variable called Sub_age that is current time minus the submission creation time divided by 60
# divided by 60 divided by 12
sub_age = (current_time - submission.created_utc) / 60 / 60 / 24
# If the submission age is less than one (posted in last 24 hours)...
if sub_age < 1:
# Add that submission to the submission list. Each entry contains the title and Link to post
submission_list.append([submission.title, submission.permalink])
# Print the submission list, so I know I got something. Debugging
print(submission_list)
# For each list entry in submission_list
for x in submission_list:
# any of element in the list genre are found in the 1st... element property(?)
if any(a in x[1] for a in genres):
# Append the element with matching genre tags in to the results list.
results.append(x)
# Print the submission list, so I know I got something. Debugging
print(results)
# if the results list is empty
if not results:
# Send the user a message letting them know I havent found something
r.send_message(user, 'Nothing Yet', 'Hey, I didn\'t find anything')
# Otherwise...
else:
# Create a message variable from the bot with the list of results.
msg = 'Hey, Check these out. -oz \n' + "\n" +'[%s]'.strip('[]') % ''.join(map(str, results)).strip('[]')
# Sent the message to the user.
r.send_message(user, 'Suggestions', msg)
print("Message Sent")
count = count + 1
print("Sleep cycle " + str(count))
#wait 1560 minutes
time.sleep(3600)
So this is the code completely working code so far. what this bot accomplishes is scanning new posts on /r/listentothis that are less than 24 hours old and parsing the text of the submission post that matches my list of genres called genres. if a match is found it appends it to a results list. if the results list is empty it sends me a message stating that it didn't find anything, otherwise, it sends me a message with a suggestsion to check out some results and then prints out the results list.
- I'm looking for ways to multithread this bot,
- make this a modular function because the goal is to make it so that a reddit user can send this bot account a message with genres and it will put the uname and genres into a database and do a search for them every hour.
- CLEAN UP RESULTS OUT PUT SO EACH RESULT IS PRINTED ON A NEW LINE
- Improve the string search efficiency if possible.
- Search Specifically for ONLY the strings providied. So for example, "rock" will not return "Glam rock"
Thanks!