예제 #1
0
def run_download(args, session, video, url, fmt):
    """ actually download a video """
    make_dirs_to(args, "videos_folder")
    
    folder      = args.db.getOptionValue("videos_folder")
    basename    = gen_videofn(video, fmt)
    filename    = ".".join((basename, fmtext[fmt]))
    path        = os.path.join(folder, filename)
    fullpath    = make_absolute(path, args.root)
    
    progress    = SimpleFileProgress("{position}/{total} {bar} {percent} {speed} ETA: {eta}")
    retry       = 0
    while retry < 5:
        try:
            downloadFile(url, fullpath, progress, 2, bytecount)
        except Exception:
            import traceback
            print("[ERROR] " + "".join(traceback.format_exception_only(*sys.exc_info()[:2])))
            retry += 1
        else:
            break
    else:
        print("[ERROR] Cannot Download. Continuing")
        return
    
    # Save DB
    localvideo = yfdb.LocalVideo(video_id=video.id, fmt=fmt, location=path,
                                 created=datetime.datetime.utcnow().isoformat())
    session.add(localvideo)
    session.commit()
    
    return localvideo
예제 #2
0
def _download(args,meta,target,fmt_list,video_item):
    try:
        video_title = video_item["title"]
        video_id    = video_item["id"]
        if video_id == "":
            print("[VIDEO] Found Deleted Video in Playlist. please clean up!")
            return
        print("[VIDEO] Found new Video: \"{0}\" (ID='{1}')".format(video_title,video_id))
        print("[VIDEO] Preparing to Download...",end="\r")
        try:
            url,fmt     = _recursive_resolve(video_id,fmt_list)
        except YouTubeResolveError:
            print("[VIDEO] Video could not be resolved.")
            return
        print("[VIDEO] Downloading with Quality level {0}".format(fmtdesc[fmt]))
    except Exception:
        import traceback
        print("-"*40)
        print("Exception while Resolving Video:")
        if args.verbose:
            traceback.print_exc(limit=None,chain=True)
        else:
            print("".join(traceback.format_exception_only(*sys.exc_info()[:2])),end="")
        print("-"*40)
        return meta
    if not args.dummy:
        video       = resolve3(video_id)
        filename    = tofilename(video.title)+"."+fmtext[fmt]
        path        = target
        fullpath    = os.path.join(path,filename)
        progress = SimpleFileProgress("{position}/{total} {bar} {percent} {speed} ETA: {eta}")
        retry = 0
        while retry<5:
            try:
                downloadFile(url,fullpath,progress,2,bytecount)
            except Exception:
                import traceback
                traceback.print_exception(*sys.exc_info())
                #print("[ERROR] " + "".join(traceback.format_exception_only(*sys.exc_info()[:2])))
                retry+=1
            else:
                break
        else:
            print("[ERROR] Cannot Download. Continuing")
            return meta
        meta["items"].append(video_id)
        meta["local"].append(video_item)
        idx         = meta["local"].index(video_item)
        meta["local_id"][video_id] = idx
        meta["local_title"][video_title] = idx
        meta["downloads"][video_id] = {
                                       "path": fullpath,
                                       "location": path,
                                       "filename": filename,
                                       "type": fmtext[fmt],
                                       "fmt": fmt,
                                       "quality": fmtdesc[fmt]
                                       }
    else:
        print("[ URL ] '{0}'".format(url))
    return meta