root / plugins / change.org / changeorg_signature_count @ a7139bca
Historique | Voir | Annoter | Télécharger (3,15 ko)
| 1 | a7139bca | Lars Kruse | #!/usr/bin/env python3 |
|---|---|---|---|
| 2 | 4804aea0 | Raphaël Droz | ''' |
| 3 | =head1 NAME |
||
| 4 | |||
| 5 | Munin Plugin to grab signature count for change.org petition given its ID |
||
| 6 | You need a valid API key for this petition |
||
| 7 | |||
| 8 | =head2 CONFIGURATION |
||
| 9 | |||
| 10 | c6f88968 | Lars Kruse | Example: |
| 11 | |||
| 12 | [changeorg_signature_count] |
||
| 13 | env.APIkey xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
||
| 14 | env.petitions 1727001 1727053 |
||
| 15 | 4804aea0 | Raphaël Droz | |
| 16 | =head1 AUTHOR |
||
| 17 | |||
| 18 | (c) 2017 Raphaël Droz <raphael.droz+floss@gmail.com> |
||
| 19 | |||
| 20 | =head1 LICENSE |
||
| 21 | |||
| 22 | c6f88968 | Lars Kruse | General Public Licence v3 or later |
| 23 | |||
| 24 | SPDX-License-Identifier: GPL-3.0-or-later |
||
| 25 | 4804aea0 | Raphaël Droz | |
| 26 | =head1 MAGIC MARKERS |
||
| 27 | |||
| 28 | c6f88968 | Lars Kruse | #%# family=auto |
| 29 | #%# capabilities=autoconf |
||
| 30 | |||
| 31 | =cut |
||
| 32 | 4804aea0 | Raphaël Droz | ''' |
| 33 | |||
| 34 | from sys import argv, exit |
||
| 35 | 97d4922b | Raphaël Droz | import sys |
| 36 | import codecs |
||
| 37 | 4804aea0 | Raphaël Droz | from os import environ, path, umask |
| 38 | import re |
||
| 39 | import urllib.request |
||
| 40 | import json |
||
| 41 | |||
| 42 | if len(argv) > 1 and argv[1] == 'autoconf': |
||
| 43 | ok = True |
||
| 44 | if not environ.get('APIkey') or not re.match('[a-fA-F0-9]{64}$', environ.get('APIkey')):
|
||
| 45 | print("no (env.APIkey not defined or bad format)")
|
||
| 46 | ok = False |
||
| 47 | for i in environ.get("petitions"):
|
||
| 48 | if not re.match("[0-9]+$", i):
|
||
| 49 | print("no ($i isn't a valid petition ID")
|
||
| 50 | ok = False |
||
| 51 | if ok: |
||
| 52 | print("yes")
|
||
| 53 | exit(0) |
||
| 54 | |||
| 55 | |||
| 56 | petition_titles = {}
|
||
| 57 | write_cache = False |
||
| 58 | |||
| 59 | if environ.get('MUNIN_PLUGSTATE'):
|
||
| 60 | d47fc3ad | Raphaël Droz | petition_cache_names = path.join(environ['MUNIN_PLUGSTATE'], path.basename(argv[0]) + '-petition_names') |
| 61 | 4804aea0 | Raphaël Droz | try: |
| 62 | with open(petition_cache_names, 'r') as f: |
||
| 63 | petition_titles = json.load(f) |
||
| 64 | except FileNotFoundError: |
||
| 65 | pass |
||
| 66 | |||
| 67 | for i in environ.get('petitions').split():
|
||
| 68 | if i in petition_titles: |
||
| 69 | continue |
||
| 70 | # NB: user-agent's tweak is needed to avoid a 403 |
||
| 71 | req = urllib.request.Request("https://api.change.org/v1/petitions/{}?api_key={}&fields=title".format(i, environ.get('APIkey')),
|
||
| 72 | 17f78427 | Lars Kruse | data=None, |
| 73 | 4804aea0 | Raphaël Droz | headers={ 'User-Agent': 'curl/7.38.0' })
|
| 74 | response = urllib.request.urlopen(req).read().decode('utf-8')
|
||
| 75 | petition_titles[i] = json.loads(response) |
||
| 76 | write_cache = True |
||
| 77 | |||
| 78 | if environ.get('MUNIN_PLUGSTATE') and write_cache:
|
||
| 79 | umask(0o077) |
||
| 80 | with open(petition_cache_names, 'w') as outfile: |
||
| 81 | json.dump(petition_titles, outfile) |
||
| 82 | 97d4922b | Raphaël Droz | |
| 83 | # equivalent of passing PYTHONIOENCODING=utf-8 to munin |
||
| 84 | sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
|
||
| 85 | 4804aea0 | Raphaël Droz | |
| 86 | if len(argv) > 1 and argv[1] == 'config': |
||
| 87 | print('''graph_title change.org signature count
|
||
| 88 | graph_args --base 1000 -l 0 |
||
| 89 | graph_vlabel Signatures |
||
| 90 | graph_category other |
||
| 91 | graph_info change.org signature count |
||
| 92 | graph_period minute |
||
| 93 | ''') |
||
| 94 | |||
| 95 | for i in petition_titles: |
||
| 96 | print('''signcount_{pid}.label {title}
|
||
| 97 | signcount_{pid}.info Total signatures for {title}
|
||
| 98 | signcount_{pid}.draw LINE3
|
||
| 99 | signcount_{pid}.min 0'''.format(pid=i, title=petition_titles[i]["title"]))
|
||
| 100 | exit(0) |
||
| 101 | |||
| 102 | for i in environ.get("petitions").split():
|
||
| 103 | req = urllib.request.Request("https://api.change.org/v1/petitions/{}?api_key={}&fields=signature_count".format(i, environ.get('APIkey')),
|
||
| 104 | data=None, |
||
| 105 | headers={ 'User-Agent': 'curl/7.38.0' })
|
||
| 106 | response = urllib.request.urlopen(req).read().decode('utf-8')
|
||
| 107 | print("signcount_%s.value %s" % (i, json.loads(response)["signature_count"])) |
