iron-blogger/daily-update.py

59 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python
# This Python file uses the following encoding: utf-8
import render
import os
import sys
import xmlrpclib
import subprocess
import datetime
import yaml
import settings
config=settings.load_settings()
dry_run = False
send_mail = True
quick_view = False
args = sys.argv[1:]
if len(args)>0:
if args[0] == '-q':
dry_run = True
quick_view = True
send_mail = False
if args[0] == '-n':
dry_run = True
send_mail = False
START = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=1)
date = START.strftime("%Y-%m-%d")
email = render.render_template('templates/email.txt', date, start=START, mail=config['mail'])
if len(email)==0:
print("No posts.")
exit(0)
if not dry_run:
text = render.render_template('templates/week.tmpl', date, start=START)
lines = text.split("\n")
title = lines[0]
body = "\n".join(lines[1:])
page = dict(title = title, description = body)
x = xmlrpclib.ServerProxy(config['xmlrpc_endpoint'])
x.metaWeblog.newPost(config['blog_id'], config['username'], config['password'], page, True)
if quick_view:
print(render.render_template('templates/quick_view.tmpl',date,start=START))
if dry_run and not quick_view:
print email
if send_mail:
p = subprocess.Popen(['/usr/sbin/sendmail', '-oi', '-t'],
stdin=subprocess.PIPE)
p.communicate(email)