Updated de translation, made sure the script reacts properly when the Internet connection is down

This commit is contained in:
Frisk 2018-06-18 15:34:27 +02:00
parent 5f39940971
commit dfdd4c1110
No known key found for this signature in database
GPG key ID: 0E9A7D3C0A01586C
4 changed files with 38 additions and 15 deletions

View file

@ -16,12 +16,21 @@ Recent changes Gamepedia compatible Discord webhook is a project made from earli
### settings.json ### ### settings.json ###
Explanation for settings: 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) `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. `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). `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 `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 `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 ### ### How to use ###
Make sure you have installed all of dependencies and filled settings.json properly. 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. 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.

Binary file not shown.

View file

@ -6,8 +6,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"POT-Creation-Date: 2018-06-18 11:12+0200\n" "POT-Creation-Date: 2018-06-18 11:12+0200\n"
"PO-Revision-Date: 2018-06-18 11:24+0200\n" "PO-Revision-Date: 2018-06-18 13:49+0200\n"
"Last-Translator: Frisk <piotrex43@protonmail.ch>\n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
"Language: de\n" "Language: de\n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
@ -298,4 +298,4 @@ msgstr "Verbindungsstatus"
#: rcgcdw.py:485 #: rcgcdw.py:485
msgid "{wiki} seems to be down or unreachable." msgid "{wiki} seems to be down or unreachable."
msgstr "{wiki} scheint unerreichbar zu sein." msgstr "Das {wiki} scheint unerreichbar zu sein."

View file

@ -5,24 +5,29 @@ import time, logging, json, requests, datetime, re, gettext, math, random, os.pa
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from collections import defaultdict from collections import defaultdict
from urllib.parse import quote_plus from urllib.parse import quote_plus
logging.basicConfig(level=logging.DEBUG)
#logging.warning('Watch out!') #logging.warning('Watch out!')
#DEBUG, INFO, WARNING, ERROR, CRITICAL #DEBUG, INFO, WARNING, ERROR, CRITICAL
with open("settings.json") as sfile: with open("settings.json") as sfile:
settings = json.load(sfile) settings = json.load(sfile)
if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"]!=-1: if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"]!=-1:
settings["limitrefetch"] = settings["limit"] settings["limitrefetch"] = settings["limit"]
logging.basicConfig(level=settings["verbose_level"])
if settings["limitrefetch"] != -1 and os.path.exists("lastchange.txt") == False: if settings["limitrefetch"] != -1 and os.path.exists("lastchange.txt") == False:
with open("lastchange.txt", 'w') as sfile: with open("lastchange.txt", 'w') as sfile:
sfile.write("") sfile.write("")
logging.info("Current settings: {settings}".format(settings=settings)) logging.info("Current settings: {settings}".format(settings=settings))
if settings["lang"] != "en" or settings["lang"] == "":
lang = gettext.translation('rcgcdw', localedir='locale', languages=[settings["lang"]]) lang = gettext.translation('rcgcdw', localedir='locale', languages=[settings["lang"]])
lang.install() lang.install()
#_ = lambda s: s else:
_ = lambda s: s
def send(message, name, avatar): def send(message, name, avatar):
try:
req = requests.post(settings["webhookURL"], data={"content": message, "avatar_url": avatar, "username": name}, timeout=10) req = requests.post(settings["webhookURL"], data={"content": message, "avatar_url": avatar, "username": name}, timeout=10)
except:
pass
def safe_read(request, *keys): def safe_read(request, *keys):
if request is None: if request is None:
@ -415,7 +420,8 @@ class recent_changes(object):
if len(self.ids) > settings["limit"]+5: if len(self.ids) > settings["limit"]+5:
self.ids.pop(0) self.ids.pop(0)
def fetch(self, amount=settings["limit"]): 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: if settings["limitrefetch"] != -1 and self.recent_id != self.file_id:
self.file_id = self.recent_id self.file_id = self.recent_id
with open("lastchange.txt", "w") as record: with open("lastchange.txt", "w") as record:
@ -454,16 +460,16 @@ class recent_changes(object):
try: try:
request = requests.get(url, timeout=10, headers=settings["header"]) request = requests.get(url, timeout=10, headers=settings["header"])
except requests.exceptions.Timeout: 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() self.downtime_controller()
return None return None
except requests.exceptions.ConnectionError: 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() self.downtime_controller()
return None return None
else: else:
return request return request
def check_connection(self): def check_connection(self, looped=False):
online = 0 online = 0
for website in ["https://google.com", "https://instagram.com", "https://steamcommunity.com"]: for website in ["https://google.com", "https://instagram.com", "https://steamcommunity.com"]:
try: try:
@ -475,14 +481,22 @@ class recent_changes(object):
pass pass
if online < 1: 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()))) 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 False
return True return True
def downtime_controller(self): def downtime_controller(self):
if settings["show_updown_messages"] == False:
return
if self.downtimecredibility<60: if self.downtimecredibility<60:
self.downtimecredibility+=15 self.downtimecredibility+=15
else: 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 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"]) send(_("{wiki} seems to be down or unreachable.").format(wiki=settings["wikiname"]), _("Connection status"), settings["avatars"]["connection_failed"])
self.last_downtime = time.time() self.last_downtime = time.time()
recent_changes = recent_changes() recent_changes = recent_changes()