groupementliens/util/manip.py
Rick dc9d398409
Nouvelle structure uniquement basé sur Flask
Enregistrement des liens dans le fichier listeLiens.json. Affichage
dynamique avec Flask et Jinja.
2020-10-07 11:42:30 +02:00

52 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
import json
from util.status import Status
class Manip():
def __init__(self, cheminFichier: str):
self.fichierJson = cheminFichier
def ajoutLienJson(self, infoLiens) -> Status:
"""
Insère dans le fichier json le nouveau lien
:param infoLiens dic: dictionnaire avec le lien qui sera
inséré dans le fichier json. Le dictionnaire doit contenir
toutes les informations !
:rtype Status: le status de linstertion (réussie, échouée…)
"""
ret = Status.ERREUR_LIEN
if self.valideUrl(infoLiens["url"]):
try:
with open(self.fichierJson, "r+") as file:
temp = json.load(file)
temp["liens"].append(infoLiens)
file.seek(0)
json.dump(temp, file, indent=4)
ret = Status.BON
except Exception as err:
ret = Status.ERREUR_INSERTION
#TODO ajouté logging pour retrouer lerreur
return ret
def valideUrl(self, url: str) -> bool:
"""
Vérifie si une url est valide
:param url str: lurl à vérifier
:rtype bool: true si lurl est bonne
false sinon
"""
# thx django
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return bool(re.search(regex, url))