iron-blogger/weekly-update.py

88 lines
2.2 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
import settings
2010-02-02 03:07:26 +00:00
config=settings.load_settings()
2010-02-02 03:07:26 +00:00
2010-06-01 00:54:47 +00:00
dry_run = False
quick_view = False
2010-06-01 00:54:47 +00:00
2010-06-01 00:40:47 +00:00
args = sys.argv[1:]
if args[0] == '-q':
dry_run = True
quick_view = True
args = args[1:]
2010-06-01 00:40:47 +00:00
if args[0] == '-n':
dry_run = True
args = args[1:]
date = args[0]
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("""\
%(date)s Punt
2010-06-01 00:54:47 +00:00
Pool:Owed:%(user)s $-%(debt)s
User:%(user)s
""" % {'user': user, 'debt': debt, 'date': date})
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)
x = xmlrpclib.ServerProxy(config['xmlrpc_endpoint'])
2012-09-29 20:50:30 +00:00
x.metaWeblog.newPost(config['blog_id'], config['username'], config['password'], page, True)
2012-01-24 21:48:30 +00:00
email = render.render_template('templates/email.txt', date, punt=punt,mail=config['mail'])
if quick_view:
2012-02-24 14:39:34 +00:00
print(render.render_template('templates/quick_view.tmpl',date,punt=punt))
if dry_run and not quick_view:
2010-06-01 00:40:47 +00:00
print email
if not dry_run:
2010-06-01 00:40:47 +00:00
p = subprocess.Popen(['mutt', '-H', '/dev/stdin'],
stdin=subprocess.PIPE)
p.communicate(email)
2010-06-01 00:54:47 +00:00
2012-05-23 19:15:57 +00:00
if punt and not dry_run:
2010-06-01 00:54:47 +00:00
with open('bloggers.yml') as b:
bloggers = yaml.safe_load(b)
for p in punt:
if 'end' not in bloggers[p]:
bloggers[p]['end'] = date
2010-06-01 00:54:47 +00:00
with open('bloggers.yml','w') as b:
yaml.safe_dump(bloggers, b)
subprocess.check_call(["git", "commit", "ledger", "bloggers.yml",
"-m", "Punts for %s" % (date,)])
# if it's a dry run, lets set the ledger back to the beginning state
if dry_run:
subprocess.check_call(["git", "checkout", "ledger"])