Skip to content

Instantly share code, notes, and snippets.

@Maxime-Favier
Last active September 1, 2018 17:49
Show Gist options
  • Save Maxime-Favier/1e344fa0b9b88368b8f5d6f0111b204c to your computer and use it in GitHub Desktop.
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)
#!/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")
@Maxime-Favier
Copy link
Author

Ne pas oublier d'installer la lib psycopg2 avec pip et mettre les bons identifiants de connection ! et creer deux tables

`CREATE TABLE fic
(
id serial PRIMARY KEY,
old_id int,
titre TEXT,
auteur TEXT,
genre TEXT,
statut TEXT,
nchapitre int
);
COMMENT ON COLUMN fic.old_id IS 'id du fichier extrait par ano59';
COMMENT ON COLUMN fic.titre IS 'titre de la fic';
COMMENT ON COLUMN fic.auteur IS 'nom de l''auteur';
COMMENT ON COLUMN fic.genre IS 'genre de la fic';
COMMENT ON COLUMN fic.statut IS 'statut de la fic';

CREATE TABLE chapitres
(
id serial PRIMARY KEY,
fic_id int,
titre TEXT,
datetime TIMESTAMP,
contenu TEXT,
auteur TEXT,
num_chapitre int
);
COMMENT ON COLUMN chapitres.fic_id IS 'numero de la fic';
COMMENT ON COLUMN chapitres.titre IS 'titre du chapitre';
COMMENT ON COLUMN chapitres.datetime IS 'date de publication';
COMMENT ON COLUMN chapitres.contenu IS 'texte du chapitre de la fic';
COMMENT ON COLUMN chapitres.auteur IS 'pseudo de l auteur';
COMMENT ON TABLE chapitres IS 'chapitres de toutes les fics';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment