我正在尝试编写一段代码,将 ID 添加到集合中,以便我可以查看它是否已被使用,并将集合存储在文件中。我一直在尝试通过使用 Shelve 模块来做到这一点,但我遇到了一些麻烦。到目前为止我有这个代码
import praw
import datetime
import shelve
user_agent ='Removed'
r = praw.Reddit(user_agent=user_agent)
submission = r.get_submission(submission_id='11v36o')
r.login('Removed','Removed')
files = shelve.open("PrawTest3.dat", writeback=True)
print "Opened!"
already_done = {}
files["already_done"] = ["a","b"]
files.close()
done = set()
print "Running"
while True:
subreddit = r.get_subreddit('mobilebot')
all_comments = subreddit.get_comments()
files = shelve.open("PrawTest2.dat", writeback=True)
already_done = files["already_done"]
files.close()
for comment in all_comments:
if (comment.body == "Hello") and (comment.id not in already_done) and (comment.id not in done):
files = shelve.open("PrawTest2.dat", writeback=True)
comment.reply(' world!')
already_done = files["already_done"]
already_done.append(comment.id)
files[already_done] = already_done
print "Shelves working"
a = datetime.datetime.now()
b = "%s:%s:%s" % (a.hour,a.minute, a.second)
print "["+b+"]"+"Comment sent!"
files.sync()
files.close()