Minimal configuration for reddit comment callback

This commit is contained in:
Simon 2018-08-08 21:24:55 -04:00
parent 1ff1e039f5
commit 458641654c
3 changed files with 29 additions and 20 deletions

9
app.py
View File

@ -13,6 +13,7 @@ import config
from flask_caching import Cache from flask_caching import Cache
from tasks import TaskManager, Task, TaskResult from tasks import TaskManager, Task, TaskResult
from search.search import ElasticSearchEngine from search.search import ElasticSearchEngine
from callbacks import PostCrawlCallbackFactory
app = Flask(__name__) app = Flask(__name__)
if config.CAPTCHA_SUBMIT or config.CAPTCHA_LOGIN: if config.CAPTCHA_SUBMIT or config.CAPTCHA_LOGIN:
@ -572,7 +573,11 @@ def api_complete_task():
if filename and os.path.exists(filename): if filename and os.path.exists(filename):
os.remove(filename) os.remove(filename)
# TODO: handle callback here # Handle task callback
callback = PostCrawlCallbackFactory.get_callback(task)
if callback:
callback.run(task_result, searchEngine)
return "Successfully logged task result and indexed files" return "Successfully logged task result and indexed files"
else: else:
@ -666,7 +671,7 @@ def api_task_enqueue():
request.json["url"], request.json["url"],
request.json["priority"], request.json["priority"],
request.json["callback_type"], request.json["callback_type"],
request.json["callback_args"] json.dumps(request.json["callback_args"])
) )
taskManager.queue_task(task) taskManager.queue_task(task)
return "" return ""

View File

@ -1,6 +1,7 @@
from tasks import Task from tasks import Task, TaskResult
from crawl_server.reddit_bot import RedditBot from reddit_bot import RedditBot
import praw import praw
from search.search import SearchEngine
class PostCrawlCallback: class PostCrawlCallback:
@ -8,7 +9,7 @@ class PostCrawlCallback:
def __init__(self, task: Task): def __init__(self, task: Task):
self.task = task self.task = task
def run(self): def run(self, task_result: TaskResult, search: SearchEngine):
raise NotImplementedError raise NotImplementedError
@ -36,26 +37,30 @@ class RedditCallback(PostCrawlCallback):
user_agent='github.com/simon987/od-database (by /u/Hexahedr_n)') user_agent='github.com/simon987/od-database (by /u/Hexahedr_n)')
self.reddit_bot = RedditBot("crawled.txt", reddit) self.reddit_bot = RedditBot("crawled.txt", reddit)
def run(self): def run(self, task_result: TaskResult, search: SearchEngine):
raise NotImplementedError raise NotImplementedError
class RedditPostCallback(RedditCallback): class RedditPostCallback(RedditCallback):
def run(self): def run(self, task_result: TaskResult, search: SearchEngine):
print("Reddit post callback for task " + str(self.task)) print("Reddit post callback for task " + str(self.task))
pass
class RedditCommentCallback(RedditCallback): class RedditCommentCallback(RedditCallback):
def run(self): def run(self, task_result: TaskResult, search: SearchEngine):
print("Reddit comment callback for task " + str(self.task))
pass comment_id = self.task.callback_args["comment_id"]
print("Replying to comment " + comment_id)
stats = search.get_stats(self.task.website_id)
message = self.reddit_bot.get_comment(stats, self.task.website_id)
print(message)
self.reddit_bot.reply(self.reddit_bot.reddit.comment(comment_id), message)
class DiscordCallback(PostCrawlCallback): class DiscordCallback(PostCrawlCallback):
def run(self): def run(self, task_result: TaskResult, search: SearchEngine):
print("Discord callback for task " + str(self.task)) print("Discord callback for task " + str(self.task))
pass

View File

@ -54,14 +54,13 @@ class RedditBot:
@staticmethod @staticmethod
def get_comment(stats: dict, website_id, message: str = ""): def get_comment(stats: dict, website_id, message: str = ""):
comment = message + " \n" if len(message) > 0 else "" comment = message + " \n" if message else ""
for stat in stats: comment += RedditBot.format_stats(stats)
comment += stat + " \n" if len(stat) > 0 else ""
comment += RedditBot.format_stats(stats[stat])
comment += "[Full Report](https://od-database.simon987.net/website/" + str(website_id) + "/)" comment += "[Full Report](https://od-db.the-eye.eu/website/" + str(website_id) + "/)"
comment += " | [Link list](https://od-database.simon987.net/website/" + str(website_id) + "/links) \n" comment += " | [Link list](https://od-db.the-eye.eu/website/" + str(website_id) + "/links)"
comment += " | [Source](https://github.com/simon987/od-database) \n"
comment += "*** \n" comment += "*** \n"
comment += RedditBot.bottom_line comment += RedditBot.bottom_line
@ -74,7 +73,7 @@ class RedditBot:
result += "File types | Count | Total Size\n" result += "File types | Count | Total Size\n"
result += ":-- | :-- | :-- \n" result += ":-- | :-- | :-- \n"
counter = 0 counter = 0
for mime in stats["mime_stats"]: for mime in stats["ext_stats"]:
result += mime[2] result += mime[2]
result += " | " + str(mime[1]) result += " | " + str(mime[1])
result += " | " + humanfriendly.format_size(mime[0]) + " \n" result += " | " + humanfriendly.format_size(mime[0]) + " \n"