ScoDoc/misc/example-api-python2.py

100 lines
3.0 KiB
Python
Raw Permalink Normal View History

2020-09-26 16:19:37 +02:00
#!/usr/bin/env python
# -*- mode: python -*-
# -*- coding: utf-8 -*-
"""Exemple connexion sur ScoDoc et utilisation de l'API
Attention: cet exemple est en Python 2.
Voir example-api-1.py pour une version en Python3 plus moderne.
"""
import urllib, urllib2
# A modifier pour votre serveur:
BASEURL = "https://scodoc.xxx.net/ScoDoc/RT/Scolarite"
USER = "XXX"
PASSWORD = "XXX"
2021-08-10 17:12:10 +02:00
values = {
"__ac_name": USER,
"__ac_password": PASSWORD,
}
2020-09-26 16:19:37 +02:00
# Configure memorisation des cookies:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
data = urllib.urlencode(values)
2021-08-10 17:12:10 +02:00
req = urllib2.Request(BASEURL, data) # this is a POST http request
2020-09-26 16:19:37 +02:00
response = urllib2.urlopen(req)
# --- Use API
# Affiche la liste des formations en format XML
req = urllib2.Request(BASEURL + "/Notes/formation_list?fmt=xml")
2020-09-26 16:19:37 +02:00
response = urllib2.urlopen(req)
2021-08-10 17:12:10 +02:00
print response.read()[:100] # limite aux 100 premiers caracteres...
2020-09-26 16:19:37 +02:00
# Recupere la liste de tous les semestres:
req = urllib2.Request(BASEURL + "/Notes/formsemestre_list?fmt=json") # format json
2020-09-26 16:19:37 +02:00
response = urllib2.urlopen(req)
js_data = response.read()
# Plus amusant: va retrouver le bulletin de notes du premier etudiant (au hasard donc) du premier semestre (au hasard aussi)
try:
2021-08-10 17:12:10 +02:00
import json # Attention: ceci demande Python >= 2.6
2020-09-26 16:19:37 +02:00
except:
2021-08-10 17:12:10 +02:00
import simplejson as json # python2.4 with simplejson installed
2020-09-26 16:19:37 +02:00
2021-08-10 17:12:10 +02:00
data = json.loads(js_data) # decode la reponse JSON
2020-09-26 16:19:37 +02:00
if not data:
print "Aucun semestre !"
else:
2021-08-10 17:12:10 +02:00
formsemestre_id = str(data[0]["formsemestre_id"])
2020-09-26 16:19:37 +02:00
# Obtient la liste des groupes:
2021-08-10 17:12:10 +02:00
req = urllib2.Request(
BASEURL
+ "/Notes/formsemestre_partition_list?fmt=json&formsemestre_id="
2021-08-10 17:12:10 +02:00
+ str(formsemestre_id)
) # format json
response = urllib2.urlopen(req)
2020-09-26 16:19:37 +02:00
js_data = response.read()
data = json.loads(js_data)
2021-08-10 17:12:10 +02:00
group_id = data[0]["group"][0][
"group_id"
] # premier groupe (normalement existe toujours)
2020-09-26 16:19:37 +02:00
# Liste les étudiants de ce groupe:
2021-08-10 17:12:10 +02:00
req = urllib2.Request(
BASEURL + "/Notes/group_list?fmt=json&with_codes=1&group_id=" + str(group_id)
2021-08-10 17:12:10 +02:00
) # format json
response = urllib2.urlopen(req)
2020-09-26 16:19:37 +02:00
js_data = response.read()
data = json.loads(js_data)
# Le code du premier étudiant:
if not data:
2021-08-10 17:12:10 +02:00
print ("pas d'etudiants dans ce semestre !")
2020-09-26 16:19:37 +02:00
else:
2021-08-10 17:12:10 +02:00
etudid = data[0]["etudid"]
2020-09-26 16:19:37 +02:00
# Récupère bulletin de notes:
2021-08-10 17:12:10 +02:00
req = urllib2.Request(
BASEURL
+ "/Notes/formsemestre_bulletinetud?formsemestre_id="
+ str(formsemestre_id)
+ "&etudid="
+ str(etudid)
+ "&fmt=xml"
2021-08-10 17:12:10 +02:00
) # format XML ici !
2020-09-26 16:19:37 +02:00
response = urllib2.urlopen(req)
xml_bulletin = response.read()
2021-08-10 17:12:10 +02:00
print "----- Bulletin de notes en XML:"
2020-09-26 16:19:37 +02:00
print xml_bulletin
# Récupère la moyenne générale:
import xml.dom.minidom
2021-08-10 17:12:10 +02:00
2020-09-26 16:19:37 +02:00
doc = xml.dom.minidom.parseString(xml_bulletin)
2021-08-10 17:12:10 +02:00
moy = doc.getElementsByTagName("note")[0].getAttribute(
"value"
) # une chaine unicode
print "\nMoyenne generale: ", moy