所以我有一个简单的 reddit 机器人设置,我使用 praw 框架编写。代码如下:
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)
我想创建一个文件(文本文件或 xml 或其他文件),用户可以使用该文件更改正在查询的各种信息的字段。例如,我想要一个包含以下行的文件:
Words to Search for = sdfghm
Subreddit to Search in = text
Send message to = FlameDraBot
我希望从字段中输入信息,以便它采用 Words 之后的值来搜索 = 而不是整行。信息输入文件并保存后。我希望我的脚本从文件中提取信息,将其存储在一个变量中,并在适当的函数中使用该变量,例如:
words_to_match = ['sdfghm']
subreddit = r.get_subreddit("test")
r.send_message('FlameDraBot'....
所以基本上就像脚本的配置文件。我该如何做才能使我的脚本可以从 .txt 或其他适当的文件中获取输入并将其实现到我的代码中?