From 81792315abf04e00a8a615b6764a15534babacb3 Mon Sep 17 00:00:00 2001 From: Frisk Date: Sun, 18 Oct 2020 11:39:16 +0200 Subject: [PATCH] Added #143 and #136 --- settings.json.example | 1 + src/misc.py | 15 +++++++++++++-- src/rc.py | 16 ---------------- src/rcgcdw.py | 6 ++---- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/settings.json.example b/settings.json.example index 256b2fa..02879f7 100644 --- a/settings.json.example +++ b/settings.json.example @@ -31,6 +31,7 @@ "show_added_categories": true, "show_bots": false, "show_abuselog": false, + "discord_message_cooldown": 0, "logging": { "version": 1, "disable_existing_loggers": false, diff --git a/src/misc.py b/src/misc.py index 0681ed0..04227f9 100644 --- a/src/misc.py +++ b/src/misc.py @@ -38,6 +38,7 @@ WIKI_API_PATH: str = "" WIKI_ARTICLE_PATH: str = "" WIKI_SCRIPT_PATH: str = "" WIKI_JUST_DOMAIN: str = "" +rate_limit = 0 profile_fields = {"profile-location": _("Location"), "profile-aboutme": _("About me"), "profile-link-google": _("Google link"), "profile-link-facebook":_("Facebook link"), "profile-link-twitter": _("Twitter link"), "profile-link-reddit": _("Reddit link"), "profile-link-twitch": _("Twitch link"), "profile-link-psn": _("PSN link"), "profile-link-vk": _("VK link"), "profile-link-xbl": _("XBL link"), "profile-link-steam": _("Steam link"), "profile-link-discord": _("Discord handle"), "profile-link-battlenet": _("Battle.net handle")} @@ -124,7 +125,6 @@ class MessageQueue: str(item))) if send_to_discord_webhook(item) < 2: misc_logger.debug("Sending message succeeded") - time.sleep(2.5) else: misc_logger.debug("Sending message failed") break @@ -324,12 +324,24 @@ def send_simple(msgtype, message, name, avatar): send_to_discord(discord_msg) +def update_ratelimit(request): + """Updates rate limit time""" + global rate_limit + rate_limit = 0 if int(request.headers.get('x-ratelimit-remaining', "-1")) > 0 else int(request.headers.get( + 'x-ratelimit-reset-after', 0)) + rate_limit += settings.get("discord_message_cooldown", 0) + + def send_to_discord_webhook(data): + global rate_limit header = settings["header"] header['Content-Type'] = 'application/json' try: + time.sleep(rate_limit) + rate_limit = 0 result = requests.post(data.webhook_url, data=repr(data), headers=header, timeout=10) + update_ratelimit(result) except requests.exceptions.Timeout: misc_logger.warning("Timeouted while sending data to the webhook.") return 3 @@ -362,7 +374,6 @@ def send_to_discord(data): time.sleep(5.0) messagequeue.add_message(data) elif code < 2: - time.sleep(2.0) pass class DiscordMessage(): diff --git a/src/rc.py b/src/rc.py index 01e45cc..a3b5727 100644 --- a/src/rc.py +++ b/src/rc.py @@ -104,16 +104,6 @@ class Recent_Changes_Class(object): storage["abuse_log_id"] = last_check[1] if last_check[1] else storage["abuse_log_id"] storage.save_datafile() self.initial_run_complete = True - # If the request succeeds the last_check will be the last rcid from recentchanges query - # if last_check is not None: - # self.recent_id = last_check - # # Assigns self.recent_id the last rcid if request succeeded, otherwise set the id from the file - # if settings["limitrefetch"] != -1 and self.recent_id != self.file_id and self.recent_id != 0: # if saving to database is disabled, don't save the recent_id - # self.file_id = self.recent_id - # storage["rcid"] = self.recent_id - # datafile.save_datafile() - # logger.debug("Most recent rcid is: {}".format(self.recent_id)) - # return self.recent_id def fetch_recentchanges_request(self, amount): """Make a typical MW request for rc/abuselog @@ -221,12 +211,6 @@ class Recent_Changes_Class(object): abuselog_processing(entry, self) return entry["id"] - # def filter_logic(self, clean_status, change_id, file_id): - # """Function that filers which changes should be sent and which not. Returns True if to send, False otherwise""" - # if clean_status and not change_id > file_id: - # return False - # return True - def fetch_changes(self, amount): """Fetches the :amount: of changes from the wiki. Returns None on error and int of rcid of latest change if succeeded""" diff --git a/src/rcgcdw.py b/src/rcgcdw.py index 2c57df2..3d17407 100644 --- a/src/rcgcdw.py +++ b/src/rcgcdw.py @@ -46,7 +46,7 @@ logging.config.dictConfig(settings["logging"]) logger = logging.getLogger("rcgcdw") logger.debug("Current settings: {settings}".format(settings=settings)) -storage = datafile.data +storage = datafile # Remove previous data holding file if exists and limitfetch allows @@ -247,9 +247,7 @@ if 1 == 2: # additional translation strings in unreachable code if TESTING: logger.debug("DEBUGGING ") - recent_changes.recent_id -= 5 - recent_changes.file_id -= 5 - recent_changes.ids = [1] + storage["rcid"] = 1 recent_changes.fetch(amount=5) day_overview() import src.discussions