[PYTHON] Get wordpress posts from the past week

This is a simple script for fetching a list of your posts for the last seven days, or since the previous Monday. I sometimes use this to generate weekly summary posts over on SD Japan.

It should output to .md file or something, but for now I just copy the output from terminal and bring into Textmate, rendering Markdown with Marked.

Here's the code. Feedback and improvements welcome.

(I'm a beginner programmer, so this code is very ugly and really sucks. I'm mostly just testing out Kobito/Qiita here.)

get_weekly_posts.py



#!/usr/bin/env python

"""a script for fetching posts from wordpress, and then autogenerating markdown files for end-of-week-posts."""


from __future__ import division
import xmlrpclib
import datetime
from bs4 import BeautifulSoup
 
MAX_POSTS = 30
#adjust for your own domain
url = 'http://www.example.com/wp/xmlrpc.php'
#input your blog username and pw
myusername = 'USERNAME'
mypassword = 'PASSWORD'
#write tag to fetch
#tag_to_fetch = 'ADD ANY TAG'
category1 = 'SAMPLECATEGORY1'
category2 = 'SAMPLECATEGORY2'
category3 = 'SAMPLECATEGORY3'
category4 = 'SAMPLECATEGORY4'
category5 = 'SAMPLECATEGORY5'


#for weekly roundup posts, figure out date for when the past week started, i.e. last monday
today = datetime.date.today()
last_monday = str(today - datetime.timedelta(days=today.weekday()))
lastmonday_fourdigit = last_monday[5:7] + last_monday[8:10]
 
server = xmlrpclib.ServerProxy(url)
result = server.metaWeblog.getRecentPosts(url, myusername, mypassword, MAX_POSTS)

print """###""", category1
for post in result:
  post_title = post['title']
  post_date = str(post['date_created_gmt'])
  tags = post['mt_keywords']
  categories = post['categories']
  description = post['description']
  desc_minus_image = BeautifulSoup(description).get_text()
  permalink = post['permaLink']
#  if tag_to_fetch in tags and post_date[4:8] >= lastmonday_fourdigit:
  if category1 in categories and post_date[4:8] >= lastmonday_fourdigit:
    print """* [%s](%s)%s/%s"""%(post_title,permalink,post_date[4:6],post_date[6:8])
   
print ''

print """###""", category2
for post in result:
  post_title = post['title']
  post_date = str(post['date_created_gmt'])
  tags = post['mt_keywords']
  categories = post['categories']
  description = post['description']
  desc_minus_image = BeautifulSoup(description).get_text()
  permalink = post['permaLink']
#  if tag_to_fetch in tags and post_date[4:8] >= lastmonday_fourdigit:
  if category2 in categories and post_date[4:8] >= lastmonday_fourdigit:
    print """* [%s](%s)%s/%s"""%(post_title,permalink,post_date[4:6],post_date[6:8])

print ''

print """###""", category3
for post in result:
  post_title = post['title']
  post_date = str(post['date_created_gmt'])
  tags = post['mt_keywords']
  categories = post['categories']
  description = post['description']
  desc_minus_image = BeautifulSoup(description).get_text()
  permalink = post['permaLink']
#  if tag_to_fetch in tags and post_date[4:8] >= lastmonday_fourdigit:
  if category3 in categories and post_date[4:8] >= lastmonday_fourdigit:
    print """* [%s](%s)%s/%s"""%(post_title,permalink,post_date[4:6],post_date[6:8])

print ''

print """###""", category4
for post in result:
  post_title = post['title']
  post_date = str(post['date_created_gmt'])
  tags = post['mt_keywords']
  categories = post['categories']
  description = post['description']
  desc_minus_image = BeautifulSoup(description).get_text()
  permalink = post['permaLink']
#  if tag_to_fetch in tags and post_date[4:8] >= lastmonday_fourdigit:
  if category4 in categories and post_date[4:8] >= lastmonday_fourdigit:
    print """* [%s](%s)%s/%s"""%(post_title,permalink,post_date[4:6],post_date[6:8])

print ''

print """###""", category5
for post in result:
  post_title = post['title']
  post_date = str(post['date_created_gmt'])
  tags = post['mt_keywords']
  categories = post['categories']
  description = post['description']
  desc_minus_image = BeautifulSoup(description).get_text()
  permalink = post['permaLink']
#  if tag_to_fetch in tags and post_date[4:8] >= lastmonday_fourdigit:
  if category5 in categories and post_date[4:8] >= lastmonday_fourdigit:
    print """* [%s](%s)%s/%s"""%(post_title,permalink,post_date[4:6],post_date[6:8])



Recommended Posts

Get wordpress posts from the past week
Get the value from the [Django] Form
Get the address from the zip code
Get the address from latitude and longitude
[Python] Get the main color from the screenshot
Get only the text from the Django form.
Get the contents of git diff from python
Get one day of the week from Zeller's joint ceremony ~ Incidentally, the perpetual calendar ~
[Python] Get the day of the week (English & Japanese)
[Python] Get the text of the law from the e-GOV Law API
Get the return code of the Python script from bat
Get AKB48 Google+ Posts
Get the GNOME version
Get the MIME Type
Get the package version to register with PyPI from Git
Get the value while specifying the default value from dict in Python
I tried to get various information from the codeforces API