#!/usr/bin/python2.3 import urllib, socket, re, os, time, sys """Script to ping various blog trackers when my Blosxom blog changes. Suitable for running as a cron job. Does a find across a directory for .txt files. If any of them are newer than the last time we pinged a weblog spot, then we ping it again. By Nelson Minar http://www.nelson.monkey.org/~nelson/ 2003-08-24. I place this work in the public domain. """ sys.stderr.write("This script won't run without modification.\n") sys.exit(0) # Sorry, but it's not bulletproof yet - if you know enough to edit # this script, then maybe it's safe for you to run it. # Directory of your Blosxom install blosxomDir="/home/nelson/blosxom" # Places to ping - you can generate these at the sites urls = [ ('blo.gs', 'FILL THIS IN'), ('weblogs.com', 'FILL THIS IN') ] # Socket timeouts - sometimes these hosts are slow socketTimeout = 25 # Enable verbose output verbose = True ### End of configuration directives entryRE = re.compile(r'^[^.].*txt$') stateFilePrefix = ".lastPing" # Set timeouts socket.setdefaulttimeout(socketTimeout) # Change to the Blosxom directory os.chdir(blosxomDir) # Find the newest Blosxom entry newestEntry = None def findNewest(arg, dirname, names): "Callback for os.path.walk() that finds the newest file" global newestEntry for n in names: if not entryRE.search(n): continue fn = os.path.normpath(os.path.join(dirname, n)) mtime = os.path.getmtime(fn) if not newestEntry or mtime > newestEntry[1]: newestEntry = (fn, mtime) os.path.walk("entries", findNewest, None) if verbose: sys.stderr.write("Newest entry: %s %s\n" % \ (newestEntry[0], time.ctime(newestEntry[1]))) # Loop through each URL, testing and pinging for url in urls: if verbose: sys.stderr.write("\nProcessing %s\n" % url[0]) stateFile = "%s-%s" % (stateFilePrefix, url[0]) # Read the last ping time (make the file if necessary) if os.path.exists(stateFile): lastPingTime = os.path.getmtime(stateFile) else: open(stateFile, "w").close() os.utime(stateFile, (0, 0)) lastPingTime = 0 if verbose: sys.stderr.write("Last ping: %s\n" % time.ctime(lastPingTime)) # Test: do we need to ping? if newestEntry[1] < lastPingTime: if verbose: sys.stderr.write("No new entries, skipping\n") continue # Do the actual ping and touch the file success = True if verbose: sys.stderr.write("Pinging %s\n" % url[1]) try: fp = urllib.urlopen(url[1]) output = fp.read() fp.close() os.utime(stateFile, None) # if verbose: sys.stderr.write("Output: %s\n" % output) except Error, e: if verbose: sys.stderr.write("Ping error: %s\n" % e)