Skip to main content
Capitalization, No need to shout.
Source Link
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65

So this is the code completely working code so far. whatWhat 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. ifIf a match is found, it appends it to a results list. ifIf 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.

  1. I'm looking for ways to multithread this bot,.
  2. makeMake 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.
  3. CLEAN UP RESULTS OUT PUT SO EACH RESULT IS PRINTED ON A NEW LINEClean up results output so each result is printed on a new line.
  4. Improve the string search efficiency if possible.
  5. Search Specifically for ONLYonly the strings providied. So for example, "rock" will not return "Glam rock"

Thanks!

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.

  1. I'm looking for ways to multithread this bot,
  2. 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.
  3. CLEAN UP RESULTS OUT PUT SO EACH RESULT IS PRINTED ON A NEW LINE
  4. Improve the string search efficiency if possible.
  5. Search Specifically for ONLY the strings providied. So for example, "rock" will not return "Glam rock"

Thanks!

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.

  1. I'm looking for ways to multithread this bot.
  2. 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.
  3. Clean up results output so each result is printed on a new line.
  4. Improve the string search efficiency if possible.
  5. Search Specifically for only the strings providied. So for example, "rock" will not return "Glam rock"
Added description and some areas of improvement.
Source Link
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.

  1. I'm looking for ways to multithread this bot,
  2. 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.
  3. CLEAN UP RESULTS OUT PUT SO EACH RESULT IS PRINTED ON A NEW LINE
  4. Improve the string search efficiency if possible.
  5. Search Specifically for ONLY the strings providied. So for example, "rock" will not return "Glam rock"

Thanks!

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 15 minutes
    time.sleep(3600)
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 60 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.

  1. I'm looking for ways to multithread this bot,
  2. 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.
  3. CLEAN UP RESULTS OUT PUT SO EACH RESULT IS PRINTED ON A NEW LINE
  4. Improve the string search efficiency if possible.
  5. Search Specifically for ONLY the strings providied. So for example, "rock" will not return "Glam rock"

Thanks!

edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

How can Increase the efficiency of this code? Reddit Botbot to gather listening suggestions

Source Link
Loading