70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
|
import os
|
||
|
import argparse
|
||
|
import re
|
||
|
import datetime
|
||
|
import shutil
|
||
|
|
||
|
|
||
|
def enlever_premier_tiret_ou_underscore(chaîne):
|
||
|
if chaîne.startswith('-') or chaîne.startswith('_'):
|
||
|
chaîne = chaîne[1:]
|
||
|
return chaîne
|
||
|
|
||
|
# Expression régulière pour extraire la date et le slug du nom de fichier org
|
||
|
regex = r"^(\d{4}(-?\d{2}){2}|\d{8})(-[a-zA-Z0-9_-]+)\.gmi$"
|
||
|
regex_orgroam = r"^(\d{14})_([a-zA-Z0-9_-]+)\.gmi$"
|
||
|
|
||
|
def find_year_and_slug(nom_de_fichier):
|
||
|
nom_de_fichier = nom_de_fichier.replace('..','.')
|
||
|
annee = ''
|
||
|
annee_presumed = nom_de_fichier[:4]
|
||
|
print(f"///////////////// /////////////////")
|
||
|
print(f"nom_de_fichier: {nom_de_fichier}")
|
||
|
if int(annee_presumed) >1970 and len(annee_presumed) == 4:
|
||
|
annee = str(annee_presumed)
|
||
|
print(f"année: {annee}")
|
||
|
match = re.match(regex_orgroam, nom_de_fichier)
|
||
|
if match:
|
||
|
date_str = match.group(1)
|
||
|
# annee = date_str[:4]
|
||
|
slug = match.group(2)
|
||
|
|
||
|
match = re.match(regex, nom_de_fichier)
|
||
|
if match:
|
||
|
date_str = match.group(1)
|
||
|
# Convertir la date en objet datetime
|
||
|
if "-" in date_str:
|
||
|
date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
|
||
|
else:
|
||
|
date = datetime.datetime.strptime(date_str, "%Y%m%d%H%M%S")
|
||
|
date_string_replaced = str(date).replace(' 00:00:00','')
|
||
|
slug = nom_de_fichier.replace('.gmi','')
|
||
|
slug = slug.replace(date_string_replaced,'')
|
||
|
slug = enlever_premier_tiret_ou_underscore(slug)
|
||
|
|
||
|
annee = str(date.year).replace(' 00:00:00','')
|
||
|
else:
|
||
|
print('find_year_and_slug : ERREUR aucun slug trouvé')
|
||
|
return [None,annee,None]
|
||
|
|
||
|
|
||
|
print(f"str(date): {str(date)}")
|
||
|
print(f"slug: {slug}")
|
||
|
print(f"chemin: {annee}/{slug}/")
|
||
|
return [date_str, annee,slug]
|
||
|
|
||
|
def test_find_year_and_slug_short_date():
|
||
|
date_string, année, slug = find_year_and_slug("2024-10-12-machin_truc-chose.gmi")
|
||
|
assert slug == "machin_truc-chose"
|
||
|
assert année == "2024"
|
||
|
assert date_string == "2024-10-12"
|
||
|
|
||
|
def test_find_year_and_slug_orgroam_date():
|
||
|
date_string, année, slug = find_year_and_slug("20060925105123_tkblog_879_by-the-wall-intersubkey.gmi")
|
||
|
# assert slug == "by-the-wall-intersubkey"
|
||
|
assert année == "2006"
|
||
|
# assert date_string == "20060925"
|
||
|
|
||
|
|
||
|
test_find_year_and_slug_short_date()
|
||
|
test_find_year_and_slug_orgroam_date()
|