Merge branch 'sched' into 'testing'

Added #152

See merge request piotrex43/RcGcDw!100
This commit is contained in:
Frisk 2022-06-15 17:58:23 +00:00
commit e935fe846c
7 changed files with 43 additions and 16 deletions

View file

@ -14,7 +14,6 @@
* **Python 3.7+**
* requests 2.18.4+
* beautifulsoup 4.6.0+
* schedule 0.5.0+
* lxml 4.2.1+
### settings.json ###

View file

@ -104,6 +104,7 @@ Client consists of the following methods:
- `make_api_request(params: Union[str, OrderedDict], *json_path: str, timeout: int = 10, allow_redirects: bool = False)` - allows to make a request to the wiki with parameters specified in params argument, json_path additionally allows to provide a list of strings that will be iterated over and json path of the result of this iteration returned. Timeout in float (seconds) can be added to limit the time for response, allow_redirects can be set to disallow or allow redirects
- `get_formatters()` - returns a dictionary of all formatters in format of `{'eventtype': func}`
- `get_ipmapper()` - returns ip mapper which tracks edit counts of IP editors
- `schedule(function: Callable, *args, every: float, at: str, priority=5, **kwargs)` schedules a periodic task that executes *function*. Either every or at should be defined. *every* is float amount of seconds every which tasks should be ran, *at* should be HH:MM formatted time at which task should be ran. Function returns sched event, but only for first execution.
### Context
**Path**: `src.api.context.Context`

View file

@ -1,5 +1,4 @@
beautifulsoup4 >= 4.6.0;
python_version >= '3.7'
requests >= 2.18.4
schedule >= 0.5.0
lxml >= 4.2.1

View file

@ -10,6 +10,6 @@ setup(
description='A set od scripts to fetch recent changes from MediaWiki wiki to a Discord channel using a webhook',
keywords=['MediaWiki', 'recent changes', 'Discord', 'webhook'],
package_dir={"": "src"},
install_requires=["beautifulsoup4 >= 4.6.0", "requests >= 2.18.4", "schedule >= 0.5.0", "lxml >= 4.2.1"],
install_requires=["beautifulsoup4 >= 4.6.0", "requests >= 2.18.4", "lxml >= 4.2.1"],
python_requires=">=3.7"
)

View file

@ -15,8 +15,10 @@
from __future__ import annotations
from datetime import datetime
import src.misc
from typing import Union
import sched
from typing import Union, Callable
from collections import OrderedDict
from typing import TYPE_CHECKING, Optional
@ -37,8 +39,39 @@ class Client:
self.content_parser = src.misc.ContentParser
self.tags = self.__recent_changes.tags
self.LinkParser: type(src.misc.LinkParser) = src.misc.LinkParser
self.scheduler: sched.scheduler = sched.scheduler()
#self.make_api_request: src.rc.wiki.__recent_changes.api_request = self.__recent_changes.api_request
def schedule(self, function: Callable, *args: list, every: Optional[float] = None, at: Optional[str] = None,
priority: int = 5, **kwargs: dict):
"""Schedules a function indefinitely, does not execute function immediately
Parameters:
function (callable): a function to call each scheduled execution
*args: arguments provided to callable function
every (float): float of time between each execution
at (str): string of time
priority (int): priority of the task (lower - more important, RcGcDw tasks are executed at 5)
**kwargs: key-value provided to callable function
Returns:
sched.event
"""
def return_delay(given_time: Union[float, str]) -> float:
if isinstance(given_time, float) or isinstance(given_time, int):
return float(given_time)
now = datetime.utcnow()
then = datetime(now.year, now.month, now.day, *(map(int, given_time.split(':'))), 0, 0)
return float((then - now).seconds)
def wrap_reschedule(function, period: float, *args, **kwargs):
self.schedule(function, every=period, *args, **kwargs)
function(*args, **kwargs)
if not any([every, at]) or all([every, at]):
raise ValueError("Either every or at (and not both) has to be set for client.schedule function.")
return self.scheduler.enter(return_delay(every or at), priority, wrap_reschedule, argument=(function, every or 86400.0, *args), kwargs=kwargs)
def refresh_internal_data(self):
"""Refreshes internal storage data for wiki tags and MediaWiki messages."""
self.__recent_changes.init_info()

View file

@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import logging, schedule, requests
import logging, requests
from typing import Optional
from src.configloader import settings
@ -158,5 +158,5 @@ def safe_request(url) -> Optional[requests.Response]:
return request
schedule.every(settings["fandom_discussions"]["cooldown"]).seconds.do(fetch_discussions)
client.schedule(fetch_discussions, every=settings["fandom_discussions"]["cooldown"])

View file

@ -19,7 +19,7 @@
# WARNING! SHITTY CODE AHEAD. ENTER ONLY IF YOU ARE SURE YOU CAN TAKE IT
# You have been warned
import time, logging.config, requests, datetime, math, os.path, schedule, sys, re, importlib
import time, logging.config, requests, datetime, math, os.path, sys, importlib
import src.misc
import src.configloader
@ -316,21 +316,16 @@ time.sleep(3.0) # this timeout is to prevent timeouts. It seems Fandom does not
if settings["rc_enabled"]:
logger.info("Script started! Fetching newest changes...")
wiki.fetch(amount=settings["limitrefetch"] if settings["limitrefetch"] != -1 else settings["limit"])
schedule.every(settings["cooldown"]).seconds.do(wiki.fetch)
client.schedule(wiki.fetch, every=settings["cooldown"])
if settings["overview"]:
try:
overview_time = time.strptime(settings["overview_time"], '%H:%M')
schedule.every().day.at("{}:{}".format(str(overview_time.tm_hour).zfill(2),
str(overview_time.tm_min).zfill(2))).do(day_overview)
client.schedule(day_overview, at="{}:{}".format(str(overview_time.tm_hour).zfill(2), str(overview_time.tm_min).zfill(2)))
del overview_time
except schedule.ScheduleValueError:
logger.error("Invalid time format! Currently: {}:{}".format(
time.strptime(settings["overview_time"], '%H:%M').tm_hour,
time.strptime(settings["overview_time"], '%H:%M').tm_min))
except ValueError:
logger.error("Invalid time format! Currentely: {}. Note: It needs to be in HH:MM format.".format(
settings["overview_time"]))
schedule.every().day.at("00:00").do(wiki.clear_cache)
client.schedule(wiki.clear_cache, at="00:00")
else:
logger.info("Script started! RC is disabled however, this means no recent changes will be sent :c")
@ -349,4 +344,4 @@ if TESTING:
while 1:
time.sleep(1.0)
schedule.run_pending()
client.scheduler.run()