⚡ - sauvegarde automatique de l'avancement du livre
This commit is contained in:
parent
de532abbb7
commit
375fbb3a7a
1814 changed files with 334236 additions and 0 deletions
80
app.py
Normal file
80
app.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
from flask import Flask, render_template, request, jsonify, url_for
|
||||
import os
|
||||
from datetime import datetime
|
||||
import json
|
||||
import re
|
||||
|
||||
app = Flask(__name__, static_folder='static')
|
||||
|
||||
def count_words_in_text(text):
|
||||
# Supprime les lignes commençant par * (titres org)
|
||||
text = re.sub(r'^\*+.*$', '', text, flags=re.MULTILINE)
|
||||
# Supprime les lignes commençant par # (commentaires)
|
||||
text = re.sub(r'^#.*$', '', text, flags=re.MULTILINE)
|
||||
# Compte les mots (séquences de caractères non-espaces)
|
||||
words = re.findall(r'\S+', text)
|
||||
return len(words)
|
||||
|
||||
def read_org_file():
|
||||
with open('livre.org', 'r', encoding='utf-8') as f:
|
||||
return f.read()
|
||||
|
||||
def update_word_count():
|
||||
try:
|
||||
with open('data.json', 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
data = {}
|
||||
|
||||
today = datetime.now().strftime('%Y-%m-%d')
|
||||
content = read_org_file()
|
||||
current_words = count_words_in_text(content)
|
||||
|
||||
# Si on a déjà un compteur pour aujourd'hui, on calcule la différence
|
||||
if today in data:
|
||||
previous_words = data[today]
|
||||
if current_words > previous_words:
|
||||
data[today] = current_words - previous_words
|
||||
else:
|
||||
data[today] = 0
|
||||
else:
|
||||
data[today] = current_words
|
||||
|
||||
with open('data.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
return data[today]
|
||||
|
||||
def count_words_today():
|
||||
try:
|
||||
with open('data.json', 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
today = datetime.now().strftime('%Y-%m-%d')
|
||||
return data.get(today, 0)
|
||||
except FileNotFoundError:
|
||||
return update_word_count()
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
content = read_org_file()
|
||||
words_today = count_words_today()
|
||||
return render_template('index.html', content=content, words_today=words_today)
|
||||
|
||||
@app.route('/update', methods=['POST'])
|
||||
def update():
|
||||
new_content = request.form.get('content')
|
||||
with open('livre.org', 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
# Met à jour le compteur de mots
|
||||
words_today = update_word_count()
|
||||
return jsonify({'status': 'success', 'words_today': words_today})
|
||||
|
||||
@app.route('/words_today')
|
||||
def get_words_today():
|
||||
return jsonify({'words': count_words_today()})
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Initialise le compteur de mots au démarrage si nécessaire
|
||||
count_words_today()
|
||||
app.run(debug=True)
|
Loading…
Add table
Add a link
Reference in a new issue