Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2017 20:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sedrubal/f384e84ff210a1dddf8313e215044115 to your computer and use it in GitHub Desktop.
Import posts from (Drupal) RSS feed to WordPress
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Import posts from RSS feed and bring it to WordPress export format.
This works for our Drupal 6.
It prints the content of the WordPress export xml (wpx) to stdout and
all images that have to be imported manually to stderr.
You need to install WordPress-importer plugin in WordPress and import the file.
Note that a RSS feed may not contain all posts but e.g. the last 30 entries.
BTW: This is dirty. Use it at your own risk TM
# "THE BEER-WARE LICENSE" (Revision 42):
# <> wrote this file. As long as you retain this
# notice you can do whatever you want with this stuff. If we meet some day,
# and you think this stuff is worth it, you can buy me a beer in return.
import sys
from datetime import datetime
from bs4 import BeautifulSoup
from dateutil.parser import parse as parse_date
from feedparser import parse as parse_feed
OLD_URL = ''
WP_URL = ''
AUTHOR = 'Sebastian Endres'
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->
<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the "WordPress" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!-- on the site. For each author, you may choose to map to an -->
<!-- existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->
<!-- generator="" created="{now}" -->
<rss version="2.0"
<guid isPermaLink="{guid_is_link}">{guid}</guid>
def main():
"""Load the RSS feed, parse it, detect images, and print wpx content."""
feed = parse_feed(FEED_URL)
items = ''
for post_id, entry in enumerate(feed['entries']):
content_soup = BeautifulSoup(
entry.get('summary', entry.get('description', '')), 'html.parser'
for img in content_soup.findAll('img'):
img_src = img.attrs.get('src', '').replace('http://', 'https://')
if img_src.startswith('/') or img_src.startswith(OLD_URL):
img_full_src = OLD_URL.rstrip('/') + img.attrs['src']
print(img_full_src, file=sys.stderr)
img.attrs['src'] = img_full_src
content = str(content_soup)
items += TEMPLATE_ITEM.format(
title=entry.get('title', ''),
link=entry.get('link', feed['feed'].get('link', OLD_URL)),
entry.get('published', str(
).strftime('%Y-%m-%d %H:%M:%S'),
creator=entry.get('author', ''),
guid_is_link=entry.get('guidislink', 'false'),
guid=entry.get('id', '{url} {pid}'.format(url=OLD_URL, pid=post_id)),
entry.get('description', entry.get('summary', '')), 'html.parser'
wpx = TEMPLATE.format(,
title=feed['feed'].get('title', ''),
page_url=feed['feed'].get('link', OLD_URL),
page_description=feed['feed'].get('subtitle', ''),
lang=feed['feed'].get('language', 'en_US'),
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment