Example #1
0
def qvod_download_proc(config, qvod_url):
    """
    get task from downloaderMgr, 
    start downloader.download_proc,
    update db
    """
    cache_path = config["CACHE_PATH"]
    down_prex = config["DOWN_PREX"]
    cache_path = os.path.normpath(os.path.join(__HOME__, cache_path))

    if qvod_url.__class__ is unicode:
        qvod_url = qvod_url.encode("utf-8")
        
    trunks = downloader.verify_url(qvod_url)

    movie = ""
    if trunks:
        movie_len, hash_code, movie = trunks
    else:
        logger.error("ivalid qvod url:%s", qvod_url)
        return 

    movie = movie.replace(' ', "\ ").replace('(', "\(").replace(')', "\)")
    suffix = '.'.join(('', movie.split('.')[-1]))
    filename = hash_code + suffix
    ret = downloader.download_proc(qvod_url, filename)
    
    # download error move 4AD312D81D59DDBC7684139892E1A41C51C4C094/ to4AD312D81D59DDBC7684139892E1A41C51C4C094.err/ in cache dir
    if not ret:
        old_cache = os.path.normpath(os.path.join(cache_path, hash_code))
        err_cache = os.path.normpath(os.path.join(cache_path, hash_code + ".err"))
        cmd = ""
        if os.name == 'posix':
            cmd = "mv %s %s" % (old_cache, err_cache)
        elif os.name == 'nt':
            cmd = "move /y %s %s" % (old_cache, err_cache)
        if os.system(cmd):
            logger.error("move %s -> %s failed", old_cache, err_cache)
Example #2
0
def qvod_download_proc(instance):
    """
    get task from downloaderMgr, 
    start downloader.download_proc,
    update db
    """
    logger.info("start qvod download...")
    down_prex = ""
    if instance.config.has_key("DOWN_PREX"):
        down_prex = instance.config["DOWN_PREX"]

    dbhost = instance.config["db_host"]
    dbname = instance.config["db_name"]
    dbuser = instance.config["db_user"]
    dbpass = instance.config["db_pass"]

    while True:
        task = instance.taskQ.get()
        logger.info("task: %s", str(task))
        taskid = task.idx
        qvod_url = task.qvod_url
        hash_code = task.hash_code
        filename = task.filename
        status = task.status
        try:
            conn = torndb.Connection(dbhost, dbname, dbuser, dbpass)
            sql = "update qvod_tasks set updated_at = current_timestamp, status = 'processing' where id = %d" % taskid
            logger.debug("update status to processing, sql: %s", sql)
            conn.execute(sql)
            conn.close()
        except Exception, err:
            logger.error("update task status error!")
            logger.error("%s", str(traceback.format_exc()))

        if qvod_url.__class__ is unicode:
            qvod_url = qvod_url.encode("utf-8")
        trunks = downloader.verify_url(str(qvod_url))
        logger.debug("trunks is: %s", str(trunks))
        movie = ""
        if trunks:
            movie_len, hash_code, movie = trunks
        movie = movie.replace(' ', "\ ").replace('(', "\(").replace(')', "\)")
        suffix = '.'.join(('', movie.split('.')[-1]))
        filename = hash_code + suffix
        time.sleep(10)
        ret = downloader.download_proc(qvod_url, filename)

        time.sleep(5)

        download_url = down_prex + filename
        logger.info("%s, %s", down_prex, filename)
        try:
            conn = torndb.Connection(dbhost, dbname, dbuser, dbpass)
            sql = ""
            if ret:
                sql = "update qvod_tasks set updated_at = current_timestamp, status = 'succeed', download_url = '%s', filename = '%s'  where id = %d" % \
                        (download_url, movie, taskid)
            else:
                sql = "update qvod_tasks set updated_at = current_timestamp, status = 'error' where id = %d" % taskid
            logger.debug("update task status after download, sql: %s", sql)
            conn.execute(sql)
            conn.close()
        except Exception, err:
            logger.error("update task status after downloading error!")
            logger.error("%s", str(traceback.format_exc()))