From 3a46ea8ddca1e1c0bc1ad7607160e3af2b76558b Mon Sep 17 00:00:00 2001 From: Jonathan Golder Date: Mon, 24 Sep 2018 20:31:07 +0200 Subject: [PATCH] Add update frequenzy in days to job Check against timestamp of latest file revision --- euroexange/euroexange.py | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/euroexange/euroexange.py b/euroexange/euroexange.py index d4d21c4..5528ed1 100644 --- a/euroexange/euroexange.py +++ b/euroexange/euroexange.py @@ -40,6 +40,7 @@ class EuroExangeBotJob(): self.image = kwargs['image'] self.script = kwargs['script'] + self.freq = kwargs['freq'] class EuroExangeBot( pywikibot.bot.BaseBot ): @@ -53,7 +54,9 @@ class EuroExangeBot( pywikibot.bot.BaseBot ): csv_file = "eurofxref-hist.csv" upload_comment = "Bot: ([[User:Jogobot/Euroexange|euroexange]]) update chart" - jobs = [ EuroExangeBotJob( image="TEST_Euro_exchange_rate_to_TRY_-_Turkish_Currency_and_Debt_Crisis_2018.svg", script="Euro_exchange_rate_to_TRY_-_Turkish_Currency_and_Debt_Crisis_2018" ) ] + jobs = [ + EuroExangeBotJob( image="TEST_Euro_exchange_rate_to_TRY_-_Turkish_Currency_and_Debt_Crisis_2018.svg", script="Euro_exchange_rate_to_TRY_-_Turkish_Currency_and_Debt_Crisis_2018", freq=1 ) + ] def __init__( self, genFactory, **kwargs ): @@ -219,9 +222,40 @@ class EuroExangeBot( pywikibot.bot.BaseBot ): "ERROR" ) raise pywikibot.NoPage( filepage ) - self.call_gnuplot( job ) + # Check if update is necessary + if self.image_update_needed(): + self.call_gnuplot( job ) - self.upload_file( job ) + self.upload_file( job ) + + # Nothing to do + else: + jogobot.output( "No update needed for Job {}".format( + self.current_job.image) ) + + def image_update_needed( self ): + """ + Checks weather image update intervall is reached. + + @returns True if update needed + @rtype bool + """ + + return True + + # Get datetime of last update + last_update = self.current_job.filepage.latest_file_info.timestamp + + # Get current time + now = pywikibot.Site().getcurrenttime() + + # Calculate allowed delta (with tolerance) + delta = datetime.timedelta( days=self.current_job.freq, hours=-2 ) + + if now >= last_update + delta: + return True + else: + return False def call_gnuplot( self, job ): """