-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikibot.py
More file actions
134 lines (96 loc) · 4.23 KB
/
wikibot.py
File metadata and controls
134 lines (96 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import sys
import time
import praw
from prawcore import PrawcoreException
from constants import Constants
from parse_helper import NameParser
from fetching import Fetcher
import logging as log
class RedditBot:
def __init__(self):
self.LOCK_FILE = 'lockfile.lock'
self._subs_to_check = "onepiece+memepiece+rickandmorty"
self.fetcher = Fetcher()
self._subs_to_check = self.fetcher.get_subs_to_check()
"""self.constants = Constants()
self.fetcher_dict = self.constants.fetchers"""
# self._subs_to_check = "+".join(self.fetcher_dict.keys())
log.basicConfig(
filename='bot_logging.log',
format='%(asctime)s %(levelname)-8s %(message)s',
level=log.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
def create_response_string(self, info_dict):
response_string = ""
all_titles = list()
for page in info_dict:
curr_title = page["title"]
curr_url = page["url"]
curr_image_url = page["image_url"]
curr_summary = page["summary"]
all_titles.append(curr_title)
response_string += ("#[{title}]({image_url})\n\n"
"#####*{summary}*\n\n"
"{url}\n\n".
format(title=curr_title, url=curr_url,
image_url=curr_image_url,
summary=curr_summary))
log.info("Commenting about:" + (",".join(all_titles)))
if response_string == "":
response_string = "##*Didn't find any results.*\n\n"
#response_string += "---\n\n^(*For any feedback on this bot,*) [^(*send a DM to u/Zylvian.*)](https://www.reddit.com/message/compose?to=Zylvian&subject=Wikia Bot feedback&message=ay suck my dick)"
#response_string += "---\n\n^[^(*Github page.*)](https://github.com/Zylvian/RedditWikiaBot)"
response_string += "---\n\n ^^I ^^am ^^a ^^wiki ^^bot. ^^Call ^^me ^^by ^^typing ^^`::something::` ^^in ^^a ^^comment! [^(*^Github ^page.*)](https://github.com/Zylvian/RedditWikiaBot)"
return response_string
def _comment_responder(self):
reddit = praw.Reddit('bot1')
# answeredDB = commentDB.DB()
subreddit = reddit.subreddit(self._subs_to_check)
print(self._subs_to_check)
parser = NameParser()
print("Comment checker started!")
for comment in subreddit.stream.comments(skip_existing=True):
if os.path.isfile(self.LOCK_FILE):
# Parse the comment
text = comment.body
# Finds all text within brackets.
names = parser.parse_text(text)
if names and comment.author != "Wikia_Bot":
curr_sub = comment.subreddit.display_name
info_dict = self.fetcher.get_wiki_info(curr_sub, names)
response_string = self.create_response_string(info_dict) + "\n"
try:
comment.reply(response_string)
# log.info("replying to {user}: {response}".format(
# user=comment.author.name, response=response_string))
except praw.exceptions.APIException as e:
log.info(str(e))
else:
return
# for comment in subreddit.stream.comments():
# if not answeredDB.exists(comment.parent_id, cards):
def run(self):
with open(self.LOCK_FILE, 'w'): pass
print("Lock file made (presumably)")
log.info("STARTED")
# remove to kill
self.run_cont()
def run_cont(self):
try:
self._comment_responder()
except PrawcoreException as e:
log.info(e)
log.info("Sleeping for 1 minute...")
time.sleep(60)
self.run_cont()
except KeyboardInterrupt:
raise
except Exception as e:
secs = 5
log.info("Something random happened, sleeping for {} sec.".format(secs))
log.info(e)
time.sleep(secs)
self.run_cont()
if __name__ == '__main__':
RedditBot().run()