所以我有一个简单的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_to_match = ['sdfghm']
subreddit = r.get_subreddit("test")
r.send_message('FlameDraBot'....
基本上就像脚本的配置文件。我该怎么做呢?这样我的脚本就可以从一个.txt或其他合适的文件中获取输入,并将其实现到我的代码中?你知道吗
是的,这只是一个普通的老Python配置,您可以can implement in an ASCII file, or else YAML or JSON。你知道吗
创建一个子目录
./config
,将设置放在./config/__init__.py
然后import config
。 使用符合PEP-18的名称,文件./config/__init__.py
看起来像:如果你想要更复杂的配置,只需阅读许多其他文章。你知道吗
相关问题 更多 >
编程相关推荐