Skip to content

Instantly share code, notes, and snippets.

@jbidoret

jbidoret/.zshrc Secret

Created March 7, 2022 10:42
Show Gist options
  • Save jbidoret/2660dd42719792cd3f9c5f592e3e0dbc to your computer and use it in GitHub Desktop.
Save jbidoret/2660dd42719792cd3f9c5f592e3e0dbc to your computer and use it in GitHub Desktop.
# permet de saisir dans la console `log` pour éditer le fichier HTML ou `log -p` pour le publier
alias log='/Users/julienbidoret/Code/log.py'
#!/usr/bin/env python
#
# accentgrave log
import os
import sys
import getopt
from datetime import datetime
import cgi
import io
from bs4 import BeautifulSoup
def print_help():
print("""log.py [options]
-h, --help Show this help message
-p, --publish Publish on server""")
def publish():
html_doc = '/Users/julienbidoret/Code/www/log/index.html'
xml_doc = '/Users/julienbidoret/Code/www/log/flux.xml'
xml_start = u"""<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Accentgrave ~ log</title>
<link href="https://accentgrave.net/log/flux.xml" rel="self"/>
<link href="https://accentgrave.net/log/"/>
<id>https://accentgrave.net/log/</id>
<updated>{}</updated>
<author>
<name>Julien Bidoret</name>
<email>julien@accentgrave.net</email>
<uri>https://accentgrave.net/</uri>
</author>\n""".format(datetime.now().isoformat('T') + "Z")
xml_end = u"""\n</feed>"""
with open(html_doc) as fp:
soup = BeautifulSoup(fp, 'html.parser')
articles = soup.find_all("article")
entries = xml_start
for article in articles:
if article.has_attr('data-title') :
title = article['data-title'] or None
time = article.time.extract().text
date = time or ""
content = article.encode_contents().replace("&", "&amp;")
content = cgi.escape(content)
updated = datetime.strptime(date, "%d/%m/%Y").isoformat('T') + "Z"
entry = u" <entry>\n"
entry += u" <title>{}</title>\n".format(title)
entry += " <link href='https://accentgrave.net/log/#{}'/>\n".format(date)
entry += " <id>https://accentgrave.net/log/#{}</id>\n".format(date)
entry += " <updated>{}</updated>\n".format(updated)
entry += " <content type='html'>{}</content>\n".format(content).decode('utf8')
entry += " </entry>\n"
entries += entry
entries += xml_end
f = io.open(xml_doc, "w", encoding='utf8')
f.write(entries)
f.close()
myCmd = 'rsync -az --progress /Users/julienbidoret/Code/www/log/ accentgrave@ssh-accentgrave.alwaysdata.net:/home/accentgrave/www/log/'
os.system(myCmd)
def main(argv, verbose=False):
try:
opts, args = getopt.getopt(argv, 'hp', ['help', 'publish'])
except getopt.GetoptError:
print_help()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print_help()
sys.exit()
if opt in ('-p', '--publish'):
publish()
sys.exit()
if not opts:
myCmd = 'code ~/Code/www/log/index.html'
os.system(myCmd)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment