Exemplo n.º 1
0
    def check_metalink(self, src):
        '''
        Decode a metalink file, can be local or remote
        First parameter, file to download, URL or file path to download from
        Returns the results of the check in a dictonary
        '''
        self.running = True
        src = download.complete_url(src)
        
        try:
            metalinkobj = download.parse_metalink(src, nocheck=False)
        except:
            print _("ERROR parsing XML.")
            raise
            
        if not metalinkobj:
            return False
        
        if metalinkobj.type == "dynamic":
            origin = metalinkobj.origin
            if origin != src and origin != "":
                try:
                    result = self.check_metalink(origin)
                    self.running = True
                    return result
                except:
                    print "Error downloading from origin %s, not using." % origin
        
        urllist = metalinkobj.files
        if len(urllist) == 0:
            print _("No urls to download file from.")
            self.running = False
            return False

        #results = {}
        for filenode in urllist:
            #size = filenode.size
            #name = filenode.filename
            #print "=" * 79
            #print _("File") + ": %s " % name + _("Size") + ": %s" % size
            myheaders = {}
            if download.is_remote(src):
                myheaders = {'referer': src}
            self.check_file_node(filenode, myheaders)

        self.running = False
Exemplo n.º 2
0
def run():
    '''
    Start a console version of this application.
    '''
    # Command line parser options.
    usage = "usage: %prog [-c|-d|-j|--convert|--rconvert] [options] arg1 arg2 ..."
    parser = optparse.OptionParser(version=metalinkc.ABOUT, usage=usage)
    parser.add_option("--download", "-d", action="store_true", dest="download", help=_("Actually download the file(s) in the metalink"))
    parser.add_option("--check", "-c", action="store_true", dest="check", help=_("Check the metalink file URLs"))
    #parser.add_option("--file", "-f", dest="filevar", metavar="FILE", help=_("Metalink file to check or file to download"))
    parser.add_option("--timeout", "-t", dest="timeout", metavar="TIMEOUT", help=_("Set timeout in seconds to wait for response (default=10)"))
    parser.add_option("--os", "-o", dest="os", metavar="OS", help=_("Operating System preference"))
    parser.add_option("--no-segmented", "-s", action="store_true", dest="nosegmented", help=_("Do not use the segmented download method"))
    parser.add_option("--lang", "-l", dest="language", metavar="LANG", help=_("Language preference (ISO-639/3166)"))
    parser.add_option("--country", dest="country", metavar="LOC", help=_("Two letter country preference (ISO 3166-1 alpha-2)"))
    parser.add_option("--pgp-keys", "-k", dest="pgpdir", metavar="DIR", help=_("Directory with the PGP keys that you trust (default: working directory)"))
    parser.add_option("--pgp-store", "-p", dest="pgpstore", metavar="FILE", help=_("File with the PGP keys that you trust (default: ~/.gnupg/pubring.gpg)"))
    parser.add_option("--gpg-binary", "-g", dest="gpg", help=_("(optional) Location of gpg binary path if not in the default search path"))
    parser.add_option("--convert-jigdo", "-j", action="store_true", dest="jigdo", help=_("Convert Jigdo format file to Metalink"))
    parser.add_option("--port", dest="port", help=_("Streaming server port to use (default: No streaming server)"))
    parser.add_option("--html", dest="html", help=_("Extract links from HTML webpage"))
    parser.add_option("--convert", dest="convert", action="store_true", help="Conversion from 3 to 4 (IETF RFC)")
    parser.add_option("--rconvert", dest="rev", action="store_true", help="Reverses conversion from 4 (IETF RFC) to 3")
    parser.add_option("--output", dest="output", metavar="OUTFILE", help=_("Output conversion result to this file instead of screen"))
    parser.add_option("--rss", "-r", action="store_true", dest="rss", help=_("RSS/Atom Feed Mode, implies -d"))
    parser.add_option("--testable", action="store_true", dest="only_testable", help=_("Limit tests to only the URL types we can test (HTTP/HTTPS/FTP)"))
    parser.add_option("-w", dest="writedir", default=os.getcwd(), help=_("Directory to write output files to (default: current directory)"))
    (options, args) = parser.parse_args()
    
    #if options.filevar != None:
    #   args.append(options.filevar)

    if len(args) == 0:
        parser.print_help()
        return

    socket.setdefaulttimeout(10)
    proxy.set_proxies()
    if options.os != None:
        download.OS = options.os
    if options.language != None:
        download.LANG = [].extend(options.language.lower().split(","))
    if options.country != None:
        download.COUNTRY = options.country
    if options.pgpdir != None:
        download.PGP_KEY_DIR = options.pgpdir
    if options.pgpstore != None:
        download.PGP_KEY_STORE = options.pgpstore
    if options.port != None:
        download.PORT = int(options.port)
    if options.gpg != None:
        GPG.DEFAULT_PATH.insert(0, options.gpg)
        
    if options.timeout != None:
        socket.setdefaulttimeout(int(options.timeout))

    if options.country != None and len(options.country) != 2:
        print _("Invalid country length, must be 2 letter code")
        return

    if options.jigdo:
        print download.convert_jigdo(args[0])
        return
        
    if options.convert:
        text = download.parse_metalink(args[0], ver=4).generate()
        if options.output:
            handle = open(options.output, "w")
            handle.write(text)
            handle.close()
        else:
            print text
        return
        
    if options.rev:
        text = download.parse_metalink(args[0], ver=3).generate()
        if options.output:
            handle = open(options.output, "w")
            handle.write(text)
            handle.close()
        else:
            print text
        return

    if options.html:
        handle = download.urlopen(options.html)
        text = handle.read()
        handle.close()

        page = metalinkc.Webpage()
        page.set_url(options.html)
        page.feed(text)
        
        for item in page.urls:
            if item.endswith(".metalink"):
                print "=" * 79
                print item
                mcheck = metalinkc.Checker()
                mcheck.check_metalink(item)
                results = mcheck.get_results()
                print_totals(results)
        return

    if options.check:
        failure = False
        for item in args:
            print "=" * 79
            print item
            mcheck = metalinkc.Checker(options.only_testable)
            mcheck.check_metalink(item)
            results = mcheck.get_results()
            result = print_totals(results)
            failure |= result
        sys.exit(int(failure))
            
    if options.download:
        for item in args:
            progress = ProgressBar()
            result = download.get(item, options.writedir, handlers={"status": progress.download_update, "bitrate": progress.set_bitrate, "time": progress.set_time}, segmented = not options.nosegmented)
            progress.download_end()
            if not result:
                sys.exit(-1)
                
    if options.rss:
        for feed in args:
            progress = ProgressBar()
            result = download.download_rss(feed, options.writedir, handlers={"status": progress.download_update, "bitrate": progress.set_bitrate, "time": progress.set_time}, segmented = not options.nosegmented)
            progress.download_end()
            if not result:
                sys.exit(-1)
                        
    sys.exit(0)