iron-blogger/weekly-update.py

90 lines
2.3 KiB
Python
Raw Normal View History

2010-02-02 03:07:26 +00:00
#!/usr/bin/python
# This Python file uses the following encoding: utf-8
2010-02-02 03:07:26 +00:00
import render
import os
import sys
import xmlrpclib
import subprocess
2010-06-01 00:54:47 +00:00
import datetime
import yaml
2010-02-02 03:07:26 +00:00
XMLRPC_ENDPOINT = 'http://blog-URL.tld/xmlrpc.php' # Wordpress RPC-URL - e.g. http://chaosblog.wordpress.com/xmlrpc.php
USER = 'username' # wordpress-username
BLOG_ID = 0
2010-02-02 03:07:26 +00:00
2010-06-01 00:54:47 +00:00
dry_run = False
2010-06-01 00:40:47 +00:00
args = sys.argv[1:]
if args[0] == '-n':
dry_run = True
args = args[1:]
date = args[0]
2010-06-01 00:54:47 +00:00
today = str(datetime.date.today())
2010-02-02 03:07:26 +00:00
with open('ledger', 'a') as f:
f.write("\n")
2010-06-01 00:40:47 +00:00
f.write(render.render_template('templates/ledger', date))
if not dry_run:
subprocess.check_call(["git", "commit", "ledger",
"-m", "Update for %s" % (date,)])
2010-06-01 00:54:47 +00:00
debts = render.get_debts()
punt = []
2010-02-02 03:07:26 +00:00
2010-06-01 00:54:47 +00:00
with open('ledger', 'a') as f:
f.write("\n")
for (user, debt) in debts:
if debt < 30: continue
punt.append(user)
f.write("""\
%(today)s Punt
Pool:Owed:%(user)s $-%(debt)s
User:%(user)s
""" % {'user': user, 'debt': debt, 'today': today})
2010-02-02 03:07:26 +00:00
2010-06-01 00:40:47 +00:00
if not dry_run:
2010-06-01 00:54:47 +00:00
text = render.render_template('templates/week.tmpl', date, punt=punt)
lines = text.split("\n")
title = lines[0]
body = "\n".join(lines[1:])
page = dict(title = title, description = body)
try:
subprocess.call(['stty', '-echo'])
passwd = raw_input("Password for %s: " % (USER,))
print
finally:
subprocess.call(['stty', 'echo'])
2010-06-01 00:40:47 +00:00
x = xmlrpclib.ServerProxy(XMLRPC_ENDPOINT)
x.metaWeblog.newPost(BLOG_ID, USER, passwd, page, True)
2010-06-01 00:54:47 +00:00
email = render.render_template('templates/email.txt', date, punt=punt)
2010-06-01 00:40:47 +00:00
if dry_run:
print email
else:
p = subprocess.Popen(['mutt', '-H', '/dev/stdin'],
stdin=subprocess.PIPE)
p.communicate(email)
2010-06-01 00:54:47 +00:00
if punt:
with open('bloggers.yml') as b:
bloggers = yaml.safe_load(b)
for p in punt:
if 'end' not in bloggers[p]:
bloggers[p]['end'] = today
with open('bloggers.yml','w') as b:
yaml.safe_dump(bloggers, b)
subprocess.check_call(["git", "commit", "ledger", "bloggers.yml",
"-m", "Punts for %s" % (today,)])
# if it's a dry run, lets set the ledger back to the beginning state
if dry_run:
subprocess.check_call(["git", "checkout", "ledger"])