Browse Source

Use JogoBot StatusAPI to check if Bot/Task is active

Jonathan Golder 3 years ago
parent
commit
d76f914615
2 changed files with 63 additions and 37 deletions
  1. 2
    0
      .gitignore
  2. 61
    37
      charts.py

+ 2
- 0
.gitignore View File

@@ -62,3 +62,5 @@ target/
62 62
 
63 63
 # Test
64 64
 test.py
65
+
66
+disabled

+ 61
- 37
charts.py View File

@@ -11,7 +11,7 @@
11 11
 #
12 12
 #  modified by:
13 13
 #
14
-#  Copyright 2015 GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
14
+#  Copyright 2016 GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
15 15
 #
16 16
 #  This program is free software; you can redistribute it and/or modify
17 17
 #  it under the terms of the GNU General Public License as published by
@@ -46,6 +46,8 @@ The following parameters are supported:
46 46
 
47 47
 
48 48
 import locale
49
+import os
50
+import sys
49 51
 
50 52
 import pywikibot
51 53
 from pywikibot import pagegenerators
@@ -199,43 +201,65 @@ def main(*args):
199 201
     @param args: command line arguments
200 202
     @type args: list of unicode
201 203
     """
202
-    # Process global arguments to determine desired site
203
-    local_args = pywikibot.handle_args(args)
204
-
205
-    # This factory is responsible for processing command line arguments
206
-    # that are also used by other scripts and that determine on which pages
207
-    # to work on.
208
-    genFactory = pagegenerators.GeneratorFactory()
209
-    # The generator gives the pages that should be worked upon.
210
-    gen = None
211
-
212
-    # If always is True, bot won't ask for confirmation of edit (automode)
213
-    always = False
214
-
215
-    # If force_reload is True, bot will always parse Countrylist regardless of
216
-    # parsing is needed or not
217
-    force_reload = False
218
-
219
-    # Parse command line arguments
220
-    for arg in local_args:
221
-        if arg.startswith("-always"):
222
-            always = True
223
-        elif arg.startswith("-force-reload"):
224
-            force_reload = True
225
-        else:
226
-            genFactory.handleArg(arg)
227
-
228
-    if not gen:
229
-        gen = genFactory.getCombinedGenerator()
230
-    if gen:
231
-        # The preloading generator is responsible for downloading multiple
232
-        # pages from the wiki simultaneously.
233
-        gen = pagegenerators.PreloadingGenerator(gen)
234
-        bot = ChartsBot(gen, always, force_reload)
235
-        if bot:
236
-            bot.run()
204
+
205
+    # Get the jogobot-task_slug (basename of current file without ending)
206
+    task_slug = os.path.basename(__file__)[:-len(".py")]
207
+
208
+    # Before run, we need to check wether we are currently active or not
209
+    try:
210
+        # Will throw Exception if disabled/blocked
211
+        jogobot.is_active( task_slug )
212
+
213
+    except jogobot.jogobot.Blocked:
214
+        (type, value, traceback) = sys.exc_info()
215
+        jogobot.output( "\03{lightpurple} %s (%s)" % (value, type ),
216
+                        "CRITICAL" )
217
+
218
+    except jogobot.jogobot.Disabled:
219
+        (type, value, traceback) = sys.exc_info()
220
+        jogobot.output( "\03{red} %s (%s)" % (value, type ),
221
+                        "ERROR" )
222
+
223
+    # Bot/Task is active
237 224
     else:
238
-        pywikibot.showHelp()
225
+        # Process global arguments to determine desired site
226
+        local_args = pywikibot.handle_args(args)
227
+
228
+        # This factory is responsible for processing command line arguments
229
+        # that are also used by other scripts and that determine on which pages
230
+        # to work on.
231
+        genFactory = pagegenerators.GeneratorFactory()
232
+        # The generator gives the pages that should be worked upon.
233
+        gen = None
234
+
235
+        # If always is True, bot won't ask for confirmation of edit (automode)
236
+        always = False
237
+
238
+        # If force_reload is True, bot will always parse Countrylist regardless
239
+        # if parsing is needed or not
240
+        force_reload = False
241
+
242
+        # Parse command line arguments
243
+        for arg in local_args:
244
+            if arg.startswith("-always"):
245
+                always = True
246
+            elif arg.startswith("-force-reload"):
247
+                force_reload = True
248
+            else:
249
+                pass
250
+                genFactory.handleArg(arg)
251
+
252
+        if not gen:
253
+            gen = genFactory.getCombinedGenerator()
254
+        if gen:
255
+            # The preloading generator is responsible for downloading multiple
256
+            # pages from the wiki simultaneously.
257
+            gen = pagegenerators.PreloadingGenerator(gen)
258
+            bot = ChartsBot(gen, always, force_reload)
259
+            if bot:
260
+                bot.run()
261
+        else:
262
+            pywikibot.showHelp()
239 263
 
240 264
 if( __name__ == "__main__" ):
241 265
     main()