0

So I have a simple reddit bot set up which I wrote using the praw framework. The code is as follows:

import praw
import time
import numpy
import pickle

r = praw.Reddit(user_agent = "Gets the Daily General Thread from subreddit.")
print("Logging in...")
r.login()

words_to_match = ['sdfghm']

cache = []

def run_bot():
    print("Grabbing subreddit...")
    subreddit = r.get_subreddit("test")
    print("Grabbing thread titles...")
    threads = subreddit.get_hot(limit=10)
    for submission in threads:
        thread_title = submission.title.lower()
        isMatch = any(string in thread_title for string in words_to_match)
        if submission.id not in cache and isMatch:
            print("Match found! Thread ID is " + submission.id)
            r.send_message('FlameDraBot', 'DGT has been posted!', 'You are awesome!')
            print("Message sent!")
            cache.append(submission.id)
    print("Comment loop finished. Restarting...")


# Run the script
while True:
    run_bot()
    time.sleep(20)

I want to create a file (text file or xml, or something else) using which the user can change the fields for the various information being queried. For example I want a file with lines such as :

Words to Search for = sdfghm
Subreddit to Search in = text
Send message to = FlameDraBot

I want the info to be input from fields, so that it takes the value after Words to Search for = instead of the whole line. After the information has been input into the file and it has been saved. I want my script to pull the information from the file, store it in a variable, and use that variable in the appropriate functions, such as:

words_to_match = ['sdfghm']
subreddit = r.get_subreddit("test")
r.send_message('FlameDraBot'....

So basically like a config file for the script. How do I go about making it so that my script can take input from a .txt or another appropriate file and implement it into my code?

1

1 Answer 1

0

Yes, that's just a plain old Python config, which you can implement in an ASCII file, or else YAML or JSON.

Create a subdirectory ./config, put your settings in ./config/__init__.py Then import config. Using PEP-18 compliant names, the file ./config/__init__.py would look like:

search_string = ['sdfghm']
subreddit_to_search = 'text'
notify = ['FlameDraBot']

If you want more complicated config, just read the many other posts on that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.