FrontPage SiteMap RecentChanges RecentNearChanges HowTo Blog RSS
United States of America, Independence Day

SocialBookmarkingToWikiCode

This code is hereby released into the public domain by me, the author, LionKimbro.

appender.py

The code requires:

There’s a running instance, pointed straight at CommunityWiki, which you may take advantage of.

 """Append text to a wiki page.
 
 This module requires xrcgi.py and the WikiGateway.
 
 Install it in a directory with the Automatic XML-RPC CGI. Make sure
 Apache allows CGI to execute in the directory.
 
 Customize the variables at the top. You may want to choose an
 XMLRPC_namespace to reflect the wiki.
 
 append -- append to wiki page
 overwrite -- overwrite wiki page
 """
 
 WIKI_URL = "http://www.emacswiki.org/cw/wiki.pl"
 WIKI_TYPE = "oddmuse1"
 XMLRPC_namespace = "cw"
 
 
 def append(pagename, text):
     """Append text to a page in a wiki."""
     import WikiGateway
     wg = WikiGateway.WikiGateway(WIKI_URL, WIKI_TYPE)
     wg.putPage(pagename, wg.getPage(pagename) + text)
     return 1
 
 
 def overwrite(pagename, text):
     """Write text to a page in a wiki."""
     import WikiGateway
     wg = WikiGateway.WikiGateway(WIKI_URL, WIKI_TYPE)
     wg.putPage(pagename, text)
     return 1

cw_bookmarking.py

Put this script in /etc/cron.hourly.

Requires feedparser and Python 2.4. Trivially adjusted to earlier Pythons.

Unfortunately, this script is hard coded to the XMLRPC_namespace used above. Fixing it up isn’t high priority atm.

 #!/usr/bin/env python2.4
 """Automatically transfer social bookmarks to wiki.
 
 Regularly scan a social bookmarking RSS feed, and divvy up the contents
 to wiki pages.
 
 wikimark.py avoids re-posting by keeping a timestamps of the latest date
 seen in the RSS feed.
 """
 
 import operator
 
 import time
 import re
 import cPickle as pickle
 import xmlrpclib
 
 import feedparser
 
 
 DEFAULT_RSS_URL = "http://unalog.com/rss"
 """RSS feed to read for links.
 
 Presently, the default is the main Unalog server.
 """
 
 DEFAULT_TIMESTAMP_FILE = "/etc/cron.hourly/timestamp.pickle"
 """Path to timestamp file.
 
 The timestamp file stores the most recent time observed in the RSS
 feed. It is accurate before and after the script runs.
 """
 
 WIKI_APPENDER_XMLRPC = "http://taoriver.net/xmlrpc/xrcgi.py"
 """URL of "wiki appender" XML-RPC interface.
 
 A "wiki appender" supports a function meeting the following interface:
   wiki.append(page, text)
 
 "page" is the string name of a page.
 "text" is text to append to the page.
 """
 
 re_pagename = re.compile('CW:\[\[([a-zA-Z][a-zA-Z0-9 ]*)\]\]')
 """Pagename capture regex.
 
 Regular expression that captures the name of the wiki page to append the
 link to.
 """
 
 
 if __name__ == "__main__":
 
     timestamp_file = DEFAULT_TIMESTAMP_FILE
     rss_url = DEFAULT_RSS_URL
 
     try:
         f = open(timestamp_file, "r")
         last_read = pickle.load(f)
         f.close()
     except IOError:
         last_read = time.gmtime(0)
 
     feed = feedparser.parse(rss_url)
 
     entries_list = []
     for entry in feed["items"]:
         time_tuple = entry["modified_parsed"]
         if time_tuple <= last_read:
             continue
         pagenames_list = re_pagename.findall(entry["summary"])
         if len(pagenames_list) == 0:
             continue
         entries_list.append({"title": entry["title"],
                              "link": entry["link"],
                              "summary_html": entry["summary"],
                              "categories": entry["category"].split(),
                              "time_text": entry["modified"],
                              "time_tuple": time_tuple,
                              "pagenames_list": pagenames_list})
 
     entries_list.sort(key=operator.itemgetter("time_tuple"))
 
     server = xmlrpclib.ServerProxy(WIKI_APPENDER_XMLRPC)
 
     for entry in entries_list:
         post = "* [%s %s]" % (entry["link"], entry["title"])
         for pagename in entry["pagenames_list"]:
             server.cw.append(pagename, post)
         f = open(DEFAULT_TIMESTAMP_FILE, "w")
         pickle.dump(entry["time_tuple"], f)
         f.close()

See Also

EditNearLinks: MeatballWiki PatrickAnderson

Languages: