2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Get wordpress posts from the past week

Last updated at Posted at 2013-03-17

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])

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?