this is a log website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

159 lines
5.2 KiB

import os
import subprocess
from flask import Flask , send_from_directory , request , jsonify
from flask_flatpages import FlatPages
from flask import render_template
import os
FLATPAGES_EXTENSION = '.md'
FLATPAGES_AUTO_RELOAD = True
app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/tdtdt'
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
FLATPAGES_MARKDOWN_EXTENSIONS = ['extra']
FLATPAGES_EXTENSION_CONFIGS = {
'codehilite': {
'linenums': 'True'
}
}
app.config.from_object(__name__)
pages = FlatPages(app)
application = app
pages.get('foo')
def Liste_cat():
articles = (p for p in pages if 'published' in p.meta)
catList = []
for a in articles:
catList.append(a.meta['cat'])
catList = list(dict.fromkeys(catList))
return catList
def imagelist(articlename):
dir_path = os.path.dirname(os.path.realpath(articlename))+'/pages/'
gallery_path = os.path.join(dir_path, articlename)
if os.path.exists(gallery_path):
images = [f for f in os.listdir(gallery_path) if f.endswith('.jpg') or f.endswith('.jpeg') or f.endswith('.png') or f.endswith('.gif')]
return gallery_path ,images
else:
return None, None
# this is chatgpt quick fix no love !
def is_time(value):
try:
datetime.strptime(value, '%H:%M:%S') # Adjust the format as needed
return True
except ValueError:
return False
@app.route('/')
def index():
# Articles are pages with a publication date
articles = (p for p in pages if 'published' in p.meta)
latest = sorted(articles, reverse=True,
key=lambda p: p.meta['published'])
catList = Liste_cat()
return render_template('index.html', articles=latest , catList=catList )
@app.route('/<path:path>')
def page(path):
page = pages.get_or_404(path)
catList = Liste_cat()
g_path, imgs = imagelist(path)
# print(list(page.meta.values()))
if imgs:
return render_template('single.html', page=page ,catList=catList , g_path=g_path, imgs = imgs)
else :
return render_template('single.html', page=page ,catList=catList)
@app.route('/info')
def info():
page = pages.get_or_404('info')
catList = Liste_cat()
return render_template('staticpage.html', page=page , catList=catList)
@app.route('/cat/<catname>')
def catPage(catname):
articles = (p for p in pages if 'published' in p.meta and 'cat' in p.meta and p.meta['cat']==catname )
latest = sorted(articles, reverse=True,
key=lambda p: p.meta['published'])
catList = Liste_cat()
return render_template('index.html', articles=latest , catList=catList )
@app.route('/author/<authorname>')
def authorPage(authorname):
articles = (p for p in pages if 'published' in p.meta and 'author' in p.meta and p.meta['author']==authorname )
latest = sorted(articles, reverse=True,
key=lambda p: p.meta['published'])
catList = Liste_cat()
return render_template('index.html', articles=latest , catList=catList )
@app.route('/pages/<path:path>')
def serve_pages(path):
return send_from_directory('pages', path)
@app.route('/search', methods=['POST'])
def search():
query = request.form['query']
print(query)
catList = Liste_cat()
# articles = (p for p in pages if 'published' in p.meta and query.lower() in metastr= lambda m : [item.lower() for item in list(p.meta.values())])
articles = (p for p in pages if 'published' in p.meta and any(isinstance(value,str) and query.lower() in value.lower() for value in p.meta.values()))
return render_template('index.html', articles=articles ,catList=catList)
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
repo_path = "http://git.tdtdt.net:3003/zvevqx/tdtdt.git" # Replace with the actual path
subprocess.run(['git', 'pull', repo_path])
return "Webhook received!"
# # Verify the secret (if you used one in Gitea)
# secret = "salut" # Replace with your actual secret
# if secret:
# if 'X-Gitea-Signature' not in request.headers:
# return "Unauthorized", 401
# received_signature = request.headers['X-Gitea-Signature']
# expected_signature = f"sha256={compute_signature(request.data, secret)}"
# if received_signature != expected_signature:
# return "Unauthorized", 401
#
# # Perform Git pull to update the website's code
# # repo_path = "/path/to/your/website/repo" # Replace with the actual path
# # subprocess.run(['git', 'pull'], cwd=repo_path)
# # Restart the Flask app
# #restart_flask_app()
# print("YOLOLYOYLOOLLYYYOOOLLOO il y a un push")
# return "Webhook received and processed successfully", 200
def compute_signature(data, secret):
import hmac
import hashlib
return hmac.new(secret.encode('utf-8'), data, hashlib.sha256).hexdigest()
def restart_flask_app():
# Customize this function to restart your Flask app (e.g., using gunicorn, systemd, etc.)
# Example: subprocess.run(['systemctl', 'restart', 'your-flask-app.service'])
pass
@app.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return "NOPE NOTHING HERE plz leave now 🛸"
if __name__ == "__main__":
app.run(host='0.0.0.0')