moodle-updates-notifications.py (3418B) - raw


      1 #!/usr/bin/env python3
      2 # Moodle Updates Notifications
      3 # Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
      4 # Copyright (C) 2020 Ernesto Lanchares <e.lancha98@gmail.com>
      5 # Copyright (C) 2020 Ferran López <flg@tuta.io>
      6 #
      7 # This program is free software: you can redistribute it and/or modify
      8 # it under the terms of the GNU Affero General Public License as
      9 # published by the Free Software Foundation, either version 3 of the
     10 # License, or (at your option) any later version.
     11 #
     12 # This program is distributed in the hope that it will be useful,
     13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 # GNU Affero General Public License for more details.
     16 #
     17 # You should have received a copy of the GNU Affero General Public License
     18 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     19 
     20 # Get notified when new documents are uploaded to a Moodle instance. Makes use
     21 # of a Gotify server.
     22 #
     23 # The script assumes there is a file on the same directory named
     24 # "mun_config.json" with the configuration. Example configuration file:
     25 #
     26 #     {
     27 #       "moodle_domain": "my.moodle.domain",
     28 #       "gotify_domain": "my.gotify.domain",
     29 #       "time_interval": 120,
     30 #       "moodle_api_token": "myMoodleAPIToken",
     31 #       "gotify_token": "myGotifyToken",
     32 #       "course_ids": {
     33 #         "56145": "GD",
     34 #         "56152": "EDPS"
     35 #       }
     36 #     }
     37 #
     38 # Note: the script assumes Moodle's web service is found at
     39 #     https://moodle_domain/webservice
     40 # If that is not the case, change the moodle_domain variable so that it does.
     41 
     42 
     43 import os
     44 import requests
     45 import json
     46 import time
     47 
     48 
     49 with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mun_config.json'), 'r') as f:
     50     cg = json.load(f)
     51 
     52 
     53 def get_updates(id):
     54     parameters = {
     55         'wstoken': cg['moodle_api_token'],
     56         'moodlewsrestformat': 'json',
     57         'wsfunction': 'core_course_get_updates_since',
     58         'courseid': id,
     59         'since': int(time.time()) - (cg['time_interval'] + 20)
     60     }
     61     response = requests.get('https://' + cg['moodle_domain'] + '/webservice/rest/server.php', params=parameters)
     62     return response.json()['instances']
     63 
     64 
     65 def get_course_docs(id):
     66     parameters = {
     67         'wstoken': cg['moodle_api_token'],
     68         'moodlewsrestformat': 'json',
     69         'wsfunction': 'core_course_get_contents',
     70         'courseid': id,
     71     }
     72     return requests.get('https://' + cg['moodle_domain'] + '/webservice/rest/server.php', params=parameters)
     73 
     74 
     75 def find_document(docs, doc_id):
     76     for module in docs:
     77         for doc in module['modules']:
     78             if doc['id'] == doc_id:
     79                 return doc
     80 
     81 
     82 def send_notification(doc, course_name):
     83     if doc['modname'] == 'resource':
     84         message = 'URL: ' + doc['contents'][0]['fileurl'] + '&token=' + cg['moodle_api_token']
     85     else:
     86         message = doc['modplural']
     87 
     88     data = {
     89         'title': course_name + ': ' + doc['name'],
     90         'message': message,
     91         'priority': 5
     92     }
     93     requests.post('https://' + cg['gotify_domain'] + '/message?token=' + cg['gotify_token'], data = data)
     94 
     95 for id, course_name in cg['course_ids'].items():
     96     updates = get_updates(id)
     97 
     98     if updates != []:
     99         course_docs = get_course_docs(id)
    100 
    101     for update in updates:
    102         doc = find_document(course_docs.json(), update['id'])
    103         send_notification(doc, course_name)