#!/usr/bin/env python

import xmlrpclib

VERBOSE = 0

server = 'http://your.server.here/downhill.cgi/xmlrpc'
server = 'http://crummy.com/devel/dh2/downhill.cgi/xmlrpc/'
server = xmlrpclib.ServerProxy(server, verbose=VERBOSE)

NO_SUCH_URL = 'http://nonexistent.nope.'

def line(): print '\n%s\n' % ('-' * 80)

print "START EXAMPLE"

print "EXAMPLE 0: basic field retrieval"

print
print 'Information about NYCB follows:'
url = "http://crummy.com/"
for i in ('getCanonicalURL', 'getName', 'getOutgoingLinks',
          'getIncomingLinks'):
    print "", getattr(server.downhill, i)(url)

print
url = server.downhill.getCanonicalURL('')
print 'Random blog: "%s" at %s' % (server.downhill.getName(url), url)

print
print 'Will attempt to get info for URL not in dataset, should throw exception...'
try:
    print server.downhill.getCanonicalURL(NO_SUCH_URL)
    raise 'Uh-oh, it went through.'
except xmlrpclib.ResponseError:
    print ' Good.'

line()

print "EXAMPLE 1: getMatchingURLs"

print 'URLs for "funky.com"'
print "", server.downhill.getMatchingURLs("funky.com")

print
print 'URLs for "f" (10 maximum)'
print "", server.downhill.getMatchingURLs("f")

print
print 'Will attempt to match the empty string, should throw exception...'
try:
    print server.downhill.getMatchingURLs("")
    raise "Uh-oh, it went through."
except xmlrpclib.ResponseError:
    print " Good."

line()

print "EXAMPLE 2: findPath"

print
print 'Crummy->Camworld'
print server.downhill.shortestPath('http://www.crummy.com/',
                                   'http://www.camworld.com')

print
print 'Crummy->Camworld, special "I\'m cranky and I don\'t like BoingBoing" edition'
print server.downhill.shortestPath('http://www.crummy.com/',
                                   'http://www.camworld.com',
                                   ['http://www.boingboing.net/'])

print
print 'xenu->xsim, this should be a pretty long path.'
print "", server.downhill.shortestPath('http://www.xenu.net/',
                                       'http://www.xsim.net')

print
print "Crummy->Crummy"
print "", server.downhill.shortestPath('http://www.crummy.com/', 'http://crummy.com')

print
print "random->Crummy"
print "", server.downhill.shortestPath('', 'http://crummy.com')

print
print "random->random"
print "", server.downhill.shortestPath('', '')

print
try:
    print "Will try to find path from a URL not in the database, should throw exception."
    print server.downhill.shortestPath(NO_SUCH_URL, 'http://crummy.com')
    raise "Uh-oh, it went through."
except xmlrpclib.ResponseError:
    print " Good."

line()

print "END EXAMPLE"

   
