Last active
September 1, 2018 17:49
-
-
Save Maxime-Favier/1e344fa0b9b88368b8f5d6f0111b204c to your computer and use it in GitHub Desktop.
met dans postgresql toutes les fic (copier le script dans le dossier parent de /Fics donné par anno59)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import psycopg2 | |
import re | |
import datetime | |
idmax = 2462 | |
try: | |
conn = psycopg2.connect("dbname='noelfic' user='postgres' host='localhost' password='root'") | |
except: | |
raise ConnectionError | |
fic = [] | |
cur = conn.cursor() | |
for i in range(idmax + 1): | |
name = 'Fics/Fic-' + str(i) + ".txt" | |
# print(name) | |
try: | |
with open(name, 'r') as fichier: | |
lines = list(filter(lambda x: x != '\n' and x != '@#@#@#@#@#\n' and x != '\r', fichier.readlines())) | |
#print(lines) | |
oldId = str(re.search('%s(.*)%s' % ("ID : ", "\n"), lines[0]).group(1)).replace("\r", "") | |
# print(oldId) | |
titre = str(re.search('%s(.*)%s' % ("TITRE : ", "\n"), lines[1]).group(1)).replace("\r", "") | |
# print(titre) | |
auteur = str(re.search('%s(.*)%s' % ("AUTEUR : ", "\n"), lines[2]).group(1)).replace("\r", "") | |
# print(auteur) | |
genre = str(re.search('%s(.*)%s' % ("GENRE : ", "\n"), lines[3]).group(1)).replace("\r", "") | |
# print(genre) | |
statut = str(re.search('%s(.*)%s' % ("STATUT : ", "\n"), lines[4]).group(1)).replace("\r", "") | |
# print(statut) | |
chapitre = int(re.search('%s(.*)%s' % ("/", "\n"), lines[5]).group(1)) | |
# print(chapitre) | |
fic.append({'oldId': oldId, 'titre': titre, 'auteur': auteur, 'genre': genre, 'statut': statut, | |
'chapitre': chapitre}) | |
currentFic = [] | |
for j in range(5, 5 + 6 * chapitre, 6): | |
#print(j) | |
numChapitre = str(re.search('%s(.*)%s' % ("CHAPITRE : ", "/"), lines[j]).group(1)).replace("\r", "") | |
#print(numChapitre) | |
titreChapitre = str(re.search('%s(.*)%s' % ("TITRE : ", "\n"), lines[j + 1]).group(1)).replace("\r", "") | |
# print(titreChapitre) | |
date = str(re.search('%s(.*)%s' % ("DATE : ", "\n"), lines[j + 2]).group(1)).replace("\r", "") | |
# print(date) | |
heure = str(re.search('%s(.*)%s' % ("HEURE : ", "\n"), lines[j + 3]).group(1)).replace("\r", "") | |
# print(heure) | |
auteur = str(re.search('%s(.*)%s' % ("AUTEUR : ", "\n"), lines[j + 4]).group(1)).replace("\r", "") | |
# print(auteur) | |
contenu = str(re.search('%s(.*)' % ("CONTENU : "), lines[j + 5]).group(1)).replace("\n","").replace("\r", "") | |
# print(contenu) | |
#print(lines[130]) | |
datetimechap = datetime.datetime.strptime(date + " " + heure, '%d/%m/%Y %H:%M:%S') | |
#print(datetimechap) | |
currentFic.append({"ficId": oldId, "titre":titreChapitre, "datetime": datetimechap, "contenu": contenu, "auteur": auteur, "chapitre":numChapitre}) | |
cur.executemany("""INSERT INTO chapitres(fic_id, titre, datetime, contenu, auteur, num_chapitre) VALUES (%(ficId)s, %(titre)s, %(datetime)s, %(contenu)s, %(auteur)s, %(chapitre)s)""", currentFic) | |
conn.commit() | |
print("Fic numéro " + str(oldId) + " importé avec succes") | |
except FileNotFoundError: | |
print("le fichier " + str(i) + " n'a pas ete trouvé") | |
print("importation de la table FIC...") | |
cur.executemany("""INSERT INTO fic(old_id, titre, auteur, genre, statut, nchapitre) VALUES (%(oldId)s, %(titre)s, %(auteur)s, %(genre)s, %(statut)s, %(chapitre)s)""", fic) | |
conn.commit() | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ne pas oublier d'installer la lib psycopg2 avec pip et mettre les bons identifiants de connection ! et creer deux tables