Commit f2d281d7 authored by Commandre Benjamin's avatar Commandre Benjamin
Browse files

Update

parent 45b53db7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import requests
class Telechargement:
def __init__(self, serveur, ident, mdp, proxy):
# Dictionnaire contenant les informations du proxy s'il existe
self.proxyDict = {
"http": "{0}".format(proxy),
"https": "{0}".format(proxy),
"ftp": "{0}".format(proxy)
}
self.url_auth = "{0}/services/authenticate/".format(serveur)
# Dictionnaire contenant les informations d'authentification
self.payload = {
'ident': "{0}".format(ident),
'pass': "{0}".format(mdp)
}
self.head = None
self.session = requests.session()
try:
# self.jeton_acces = self.get_jeton_acces()
self.get_jeton_acces()
# if self.jeton_acces is None:
if self.head is None:
raise Exception("Erreur lors de l'authentification.")
except Exception as e:
print(e)
else:
self.temps_limite_jeton = time.time()
def get_jeton_acces(self):
try:
# Jeton d'acces
# reponse = requests.post(self.url_auth, data=self.payload, proxies=self.proxyDict)
reponse = self.session.post(self.url_auth, data=self.payload, proxies=self.proxyDict)
reponse.raise_for_status()
except Exception as e:
raise e
else:
if "text" in reponse.headers["Content-Type"]:
# return reponse.text
jeton = reponse.text
elif "json" in reponse.headers["Content-Type"]:
# return reponse.json()["access_token"]
jeton = reponse.json()["access_token"]
else:
raise BaseException('Format non traité.')
self.head = {"Authorization": "Bearer {0}".format(jeton)}
class Decorators:
@staticmethod
def refresh_jeton(decorated):
def wrapper(telechargement, *args, **kwargs):
if time.time() > telechargement.temps_limite_jeton:
telechargement.get_jeton_acces()
return decorated(telechargement, *args, **kwargs)
return wrapper
@Decorators.refresh_jeton
def get_archive(self, url):
# Requête pour récupérer l'archive
# return requests.get(url, headers=self.head)
return self.session.get(url, headers=self.head)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment