#!/usr/bin/env python
#
# $URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/bin/bad_links.py $
# $Id: bad_links.py 31107 2008-09-16 13:57:47Z dbinger $

import sys
from qp.lib.site import Site
from dulcinea.util import urlopen

def try_url(link):
    try:
        file = urlopen(link.get_link_url())
    except IOError:
        err = sys.exc_info()[1]
        sys.stderr.write("Link %d Unable to fetch %s: %s\n" % (
            link.get_key(), link.get_link_url(), err.strerror))

def main (prog, args):
    usage = "usage: %s [site]" % prog

    if len(args) != 1:
        sys.exit(usage)

    link_db = Site(args[0]).get_connection().get_root()['link_db']
    for link in link_db.get_all_links():
        try_url(link)

if __name__ == '__main__':
    main(sys.argv[0], sys.argv[1:])
