From dfdd4c1110ecb63dd5b47b7c564784ad595bca8f Mon Sep 17 00:00:00 2001 From: Frisk Date: Mon, 18 Jun 2018 15:34:27 +0200 Subject: [PATCH] Updated de translation, made sure the script reacts properly when the Internet connection is down --- README.md | 11 +++++++++- locale/de/LC_MESSAGES/rcgcdw.mo | Bin 6029 -> 6033 bytes locale/de/LC_MESSAGES/rcgcdw.po | 6 +++--- rcgcdw.py | 36 ++++++++++++++++++++++---------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1351b03..6b15b17 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,21 @@ Recent changes Gamepedia compatible Discord webhook is a project made from earli ### settings.json ### Explanation for settings: `cooldown` – interval for how often changes are retrieved from the wiki (due to used solutions, the real interval is ~1 sec longer) -`wiki` – wiki prefix the bot is supposed to work with (for example, for English Minecraft Wiki it's minecraft (https://**minecraft**.gamepedia.com) and for Polish Minecraft Wiki minecraft-pl (https://**minecraft-pl**.gamepedia.com) +`wiki` – wiki prefix the bot is supposed to work with (for example, for English Minecraft Wiki it's minecraft (https://**minecraft**.gamepedia.com) and for Polish Minecraft Wiki minecraft-pl (https://**minecraft-pl**.gamepedia.com +`lang` – language for the messages, currently available options are: de, en, pl `header` – it's recommended to leave this value as it is, it's a header the script will use to communicate with Gamepedia. Please note that without it, no communication will be possible. `limit` – amount of actions retrieved every `cooldown` amount of seconds. The higher it is the more network data will be used and the data will be processed longer, setting it to higher values is not recommended, but if you want to make sure no edit is omitted (which only happen if there are more actions in last `cooldown` seconds than this value). `webhookURL` – webhook URL you can get using channel settings in Discord `limitrefetch` – limit of how many changes can be retrieved when refetch happens, cannot be lower than limit. -1 if you want to disable auto-refetch +`wikiname` – a name of the wiki the bot will work on, required in some messages +`avatars` – this section makes specific types of messages overwrite the default webhook avatar +`connection_failed` – message printed when script fails connection with the wiki several times +`no_event` – error message when the event couldn't be recognized by the script +`embed` – every embed message showing changes ### How to use ### Make sure you have installed all of dependencies and filled settings.json properly. When you are sure, use `python rcgcdw.py` command to run the script. + +### Other ### +Script seem to use about 17MB of RAM and negligible amount of CPU when fetching changes. diff --git a/locale/de/LC_MESSAGES/rcgcdw.mo b/locale/de/LC_MESSAGES/rcgcdw.mo index 8883c5f2df37e8787d008e9cf9838572fee645bc..40e44d22883aba9e6bc9015724bbf3bc58e9055f 100644 GIT binary patch delta 33 ocmeCxpQyi~g@sXLb1O>(CyTL_iRI>pyzQJUE{VkolQYCs0kLff{Qv*} delta 28 jcmbQJ->bi&g@sXNb1O>(CySw#k;&$VyzQKmbHr5vg5U_a diff --git a/locale/de/LC_MESSAGES/rcgcdw.po b/locale/de/LC_MESSAGES/rcgcdw.po index 16bf80f..4a90ab8 100644 --- a/locale/de/LC_MESSAGES/rcgcdw.po +++ b/locale/de/LC_MESSAGES/rcgcdw.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: \n" "POT-Creation-Date: 2018-06-18 11:12+0200\n" -"PO-Revision-Date: 2018-06-18 11:24+0200\n" -"Last-Translator: Frisk \n" +"PO-Revision-Date: 2018-06-18 13:49+0200\n" +"Last-Translator: \n" "Language-Team: \n" "Language: de\n" "MIME-Version: 1.0\n" @@ -298,4 +298,4 @@ msgstr "Verbindungsstatus" #: rcgcdw.py:485 msgid "{wiki} seems to be down or unreachable." -msgstr "{wiki} scheint unerreichbar zu sein." +msgstr "Das {wiki} scheint unerreichbar zu sein." diff --git a/rcgcdw.py b/rcgcdw.py index 4d4d2e9..d67a6d7 100644 --- a/rcgcdw.py +++ b/rcgcdw.py @@ -5,24 +5,29 @@ import time, logging, json, requests, datetime, re, gettext, math, random, os.pa from bs4 import BeautifulSoup from collections import defaultdict from urllib.parse import quote_plus -logging.basicConfig(level=logging.DEBUG) #logging.warning('Watch out!') #DEBUG, INFO, WARNING, ERROR, CRITICAL with open("settings.json") as sfile: settings = json.load(sfile) if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"]!=-1: settings["limitrefetch"] = settings["limit"] +logging.basicConfig(level=settings["verbose_level"]) if settings["limitrefetch"] != -1 and os.path.exists("lastchange.txt") == False: with open("lastchange.txt", 'w') as sfile: sfile.write("") logging.info("Current settings: {settings}".format(settings=settings)) -lang = gettext.translation('rcgcdw', localedir='locale', languages=[settings["lang"]]) -lang.install() -#_ = lambda s: s +if settings["lang"] != "en" or settings["lang"] == "": + lang = gettext.translation('rcgcdw', localedir='locale', languages=[settings["lang"]]) + lang.install() +else: + _ = lambda s: s def send(message, name, avatar): - req = requests.post(settings["webhookURL"], data={"content": message, "avatar_url": avatar, "username": name}, timeout=10) + try: + req = requests.post(settings["webhookURL"], data={"content": message, "avatar_url": avatar, "username": name}, timeout=10) + except: + pass def safe_read(request, *keys): if request is None: @@ -415,7 +420,8 @@ class recent_changes(object): if len(self.ids) > settings["limit"]+5: self.ids.pop(0) def fetch(self, amount=settings["limit"]): - self.recent_id = self.fetch_changes(amount=amount) + last_check = self.fetch_changes(amount=amount) + self.recent_id = last_check if last_check is not None else self.recent_id if settings["limitrefetch"] != -1 and self.recent_id != self.file_id: self.file_id = self.recent_id with open("lastchange.txt", "w") as record: @@ -454,16 +460,16 @@ class recent_changes(object): try: request = requests.get(url, timeout=10, headers=settings["header"]) except requests.exceptions.Timeout: - logging.warning("Reached timeout error for request on link {url}") + logging.warning("Reached timeout error for request on link {url}".format(url=url)) self.downtime_controller() return None except requests.exceptions.ConnectionError: - logging.warning("Reached connection error for request on link {url}") + logging.warning("Reached connection error for request on link {url}".format(url=url)) self.downtime_controller() return None else: return request - def check_connection(self): + def check_connection(self, looped=False): online = 0 for website in ["https://google.com", "https://instagram.com", "https://steamcommunity.com"]: try: @@ -475,14 +481,22 @@ class recent_changes(object): pass if online < 1: logging.error("Failure when checking Internet connection at {time}".format(time=time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()))) + self.downtimecredibility = 0 + if looped == False: + while 1: #recursed loop, check for connection (every 10 seconds) as long as three services are down, don't do anything else + if self.check_connection(looped=True): + break + time.sleep(10) return False return True def downtime_controller(self): + if settings["show_updown_messages"] == False: + return if self.downtimecredibility<60: self.downtimecredibility+=15 else: - if (time.time() - self.last_downtime)>1800 and self.check_connection(): #check if last downtime happened within 30 minutes, if yes, don't send a message - send(_("{wiki} seems to be down or unreachable.").format(wiki=settings["wiki"]), _("Connection status"), settings["avatars"]["connection_failed"]) + if(time.time() - self.last_downtime)>1800 and self.check_connection(): #check if last downtime happened within 30 minutes, if yes, don't send a message + send(_("{wiki} seems to be down or unreachable.").format(wiki=settings["wikiname"]), _("Connection status"), settings["avatars"]["connection_failed"]) self.last_downtime = time.time() recent_changes = recent_changes()