WordPress Book Posting Scripts

Module 2: dategen.py (Generating sequences of dates)

I wanted to make sure I could schedule the posts in advance instead of making them all show up at once, but I didn’t want to take down all of the chapters I’d already posted and start scheduling from ‘now.’ My answer to this was to make a script that would take a start date and then yield dates after that start date as long as they corresponded to the desired update schedule. I set it up so I could specify an update schedule because I wanted to make sure I could use a more frequent schedule for future books.

import datetime
import xmlrpclib

MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
SATURDAY = 5
SUNDAY = 6
THREE_UPDATES = (MONDAY, WEDNESDAY, FRIDAY)
TWO_UPDATES = (TUESDAY, THURSDAY)
ONE_UPDATE = (WEDNESDAY,)

def sequenced_dates(year, month, day,
                    hour=12, minute=0,
                    second=0, microsecond=0,
                    accepted_days=THREE_UPDATES,):
    """
    Given a start date and time in year, month, day, hour, minute, second and
    microsecond and an iterable describing an update schedule, this will yield
    date/time strings formatted for use in the auto update scripts.

    """
    start_date = datetime.datetime(year, month, day,
                                   hour, minute, second, microsecond)
    while True:
        start_date += datetime.timedelta(days=1)
        if start_date.weekday() in accepted_days:
            yield xmlrpclib.DateTime(datetime.datetime.strptime(
                                     datetime.datetime.strftime(
                                                            start_date,
                                                            "%Y-%m-%d %H:%M"),
                                                            "%Y-%m-%d %H:%M"))

if __name__ == '__main__':

    dates = sequenced_dates(2014, 3, 17)
    for x in range(0, 44):
        print x, dates.next()

I used a generator function (it ‘yields’ the results each time .next() is called, instead of returning them) so that I could generate as many dates as I needed – again, I was thinking ahead to future books, which might have more or fewer chapters than my current one.

This script was made with reference to the python 2.7 documentation for the datetime module.

Utility Scripts , ,

Leave a Reply

Your email address will not be published. Required fields are marked *