Created
April 18, 2009 23:42
-
-
Save jcsalterego/97826 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # | |
| # Generates OPML from Mail.app RSS Feeds | |
| # | |
| # Usage: | |
| # $ python mailrss2opml.py > export.opml | |
| # | |
| import os | |
| import stat | |
| import sys | |
| OPML_TITLE = "Mail.app RSS Generated List" | |
| RSS_RELPATH = "Library/Mail/RSS" | |
| OPML_SKEL = \ | |
| """<?xml version="1.0" encoding="iso-8859-1"?> | |
| <opml version="1.0"> | |
| <head> | |
| <title>%s</title> | |
| </head> | |
| <body> | |
| %s | |
| </body> | |
| </opml>""" | |
| def main(): | |
| opml_feeds = [] | |
| home_dir = os.getenv('HOME') | |
| if not home_dir: | |
| return 1 | |
| rss_path = "%s/%s" % (home_dir, RSS_RELPATH) | |
| if not os.path.isdir(rss_path): | |
| return 1 | |
| # extract only subdirectories | |
| feeds = [feed for feed in os.listdir(rss_path) | |
| if os.path.isdir("%s/%s" % (rss_path, feed))] | |
| for feed in feeds: | |
| info_file = "%s/%s/Info.plist" % (rss_path, feed) | |
| try: | |
| contents = file(info_file).read() | |
| url = extract_url(contents) | |
| if not url: | |
| # silently error if no URL was extracted | |
| continue | |
| title = feed.replace(".rssmbox", "") | |
| opml_feeds.append((url, title)) | |
| except Exception, e: | |
| # silently error | |
| pass | |
| contents = [] | |
| for url, title in opml_feeds: | |
| feed = {"title": title, | |
| "text": title, | |
| "type": "rss", | |
| "version": "RSS", | |
| "xmlUrl": url} | |
| contents.append(" %s" % dict_to_elem("outline", feed)) | |
| contents = "\n".join(contents) | |
| opml = OPML_SKEL % (OPML_TITLE, | |
| contents) | |
| print opml | |
| def dict_to_elem(tagname, d): | |
| out = ["<%s" % tagname] | |
| for k, v in d.items(): | |
| out.append(' %s="%s"' % (k, v)) | |
| out.append(" />") | |
| return "".join(out) | |
| def extract_url(contents): | |
| matches = [line for line in contents.split("\n") | |
| if "<string>" in line and | |
| ("http://" in line or "https://" in line)] | |
| if len(matches) != 1: | |
| return "" | |
| else: | |
| m = matches[0].replace("<string>", "").replace("</string>", "") | |
| return m.strip() | |
| if __name__ == '__main__': | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment