[PYTHON] Holen Sie sich WordPress-Beiträge aus der letzten Woche

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

Holen Sie sich WordPress-Beiträge aus der letzten Woche
Holen Sie sich eine Adresse aus einer Postleitzahl
Holen Sie sich die Adresse von Längen- und Breitengrad
[Python] Holen Sie sich die Hauptfarbe aus dem Screenshot
Holen Sie sich nur den Text aus dem Django-Formular.
Holen Sie sich den Inhalt von Git Diff aus Python
Holen Sie sich einen Tag von Zellers gemeinsamer Zeremonie ~ Übrigens der ewige Kalender ~
[Python] Holen Sie sich den Tag (Englisch & Japanisch)
[Python] Ruft den Gesetzestext aus der e-GOV-Gesetz-API ab
Holen Sie sich den Rückkehrcode eines Python-Skripts von bat
Holen Sie sich AKB48 Google+ Beiträge
Holen Sie sich die GNOME-Version
Holen Sie sich den MIME-Typ
Holen Sie sich die Paketversion, um sich bei PyPI von Git zu registrieren
Rufen Sie den Wert ab, während Sie den Standardwert aus dict in Python angeben
Ich habe versucht, verschiedene Informationen von der Codeforces-API abzurufen