예제 #1
0
def main():
    """main function"""
    sys.stdout.write(banner())
    version = "%(prog)s {version}".format(version=udemy.__version__)
    description = "A cross-platform python based utility to download courses from udemy for personal offline use."
    parser = argparse.ArgumentParser(description=description,
                                     conflict_handler="resolve")
    parser.add_argument(
        "course",
        help="Udemy course or file containing list of course URL(s).",
        type=str,
    )
    general = parser.add_argument_group("General")
    general.add_argument("-h", "--help", action="help", help="Shows the help.")
    general.add_argument("-v",
                         "--version",
                         action="version",
                         version=version,
                         help="Shows the version.")

    authentication = parser.add_argument_group("Authentication")
    authentication.add_argument(
        "-u",
        "--username",
        dest="username",
        type=str,
        help="Username in udemy.",
        metavar="",
    )
    authentication.add_argument(
        "-p",
        "--password",
        dest="password",
        type=str,
        help="Password of your account.",
        metavar="",
    )
    authentication.add_argument(
        "-k",
        "--cookies",
        dest="cookies",
        type=str,
        help="Cookies to authenticate with.",
        metavar="",
    )

    advance = parser.add_argument_group("Advance")
    advance.add_argument(
        "-o",
        "--output",
        dest="output",
        type=str,
        default=os.getcwd(),
        help="Download to specific directory.",
        metavar="",
    )
    advance.add_argument(
        "-q",
        "--quality",
        dest="quality",
        type=int,
        help="Download specific video quality.",
        metavar="",
    )
    advance.add_argument(
        "-c",
        "--chapter",
        dest="chapter",
        type=int,
        help="Download specific chapter from course.",
        metavar="",
    )
    advance.add_argument(
        "-l",
        "--lecture",
        dest="lecture",
        type=int,
        help="Download specific lecture from chapter(s).",
        metavar="",
    )
    advance.add_argument(
        "-s",
        "--sub-lang",
        dest="language",
        type=str,
        help="Download specific subtitle/caption (e.g:- en).",
        metavar="",
        default="en",
    )
    advance.add_argument(
        "--chapter-start",
        dest="chapter_start",
        type=int,
        help="Download from specific position within course.",
        metavar="",
    )
    advance.add_argument(
        "--chapter-end",
        dest="chapter_end",
        type=int,
        help="Download till specific position within course.",
        metavar="",
    )
    advance.add_argument(
        "--lecture-start",
        dest="lecture_start",
        type=int,
        help="Download from specific position within chapter(s).",
        metavar="",
    )
    advance.add_argument(
        "--lecture-end",
        dest="lecture_end",
        type=int,
        help="Download till specific position within chapter(s).",
        metavar="",
    )

    other = parser.add_argument_group("Others")
    other.add_argument(
        "--info",
        dest="info",
        action="store_true",
        help="List all lectures with available resolution.",
    )
    other.add_argument(
        "--cache",
        dest="cache_session",
        action="store_true",
        help="Cache your session to avoid providing again.",
    )
    other.add_argument(
        "--keep-vtt",
        dest="keep_vtt",
        action="store_true",
        help="Keep WebVTT caption(s).",
    )
    other.add_argument(
        "--sub-only",
        dest="caption_only",
        action="store_true",
        help="Download captions/subtitle only.",
    )
    other.add_argument(
        "--skip-sub",
        dest="skip_captions",
        action="store_true",
        help="Download course but skip captions/subtitle.",
    )
    other.add_argument(
        "--skip-hls",
        dest="skip_hls_stream",
        action="store_true",
        help="Download course but skip hls streams. (fast fetching).",
    )
    other.add_argument(
        "--assets-only",
        dest="assets_only",
        action="store_true",
        help="Download asset(s) only.",
    )
    other.add_argument(
        "--skip-assets",
        dest="skip_assets",
        action="store_true",
        help="Download course but skip asset(s).",
    )

    args = parser.parse_args()
    if args.cookies:
        f_in = open(args.cookies)
        with open(args.cookies) as f_in:
            cookies = "\n".join(
                [line for line in (l.strip() for l in f_in) if line])
        args.cookies = cookies
    if not args.username and not args.password and not args.cookies:
        # check if we already have a session..
        configs = load_configs()
        if not configs:
            # if not ask user for user/pass or access token (cookie)
            args.username = getpass.getuser(prompt="Username : "******"Password : "******"\n")
            if not args.username and not args.password:
                print("")
                args.cookies = getpass.get_access_token(
                    prompt="Access Token : ")
                if args.cookies:
                    print("\n")
        if configs:
            cookies = configs.get("cookies")
            if not cookies:
                args.username = configs.get("username")
                args.password = configs.get("password")
            if cookies:
                args.cookies = cookies
            args.quality = args.quality if args.quality else configs.get(
                "quality")
            args.output = args.output if args.output else configs.get("output")
            args.language = args.language if args.language else configs.get(
                "language")
    url_or_courses = extract_url_or_courses(args.course)
    if not args.username and not args.password and not args.cookies:
        print("\n")
        logger.error(
            msg=
            f"You should either provide fresh access token or username/password for udemy.."
        )
        sys.exit(0)
    udemy_obj = Udemy(
        url_or_courses=url_or_courses,
        username=args.username,
        password=args.password,
        cookies=args.cookies,
        cache_session=args.cache_session,
    )
    # setting the caching default so that we can avoid future login attemps.
    if args.cache_session:
        _ = to_configs(
            username=args.username,
            password=args.password,
            cookies=args.cookies,
            quality=args.quality,
            output=args.output,
            language=args.language,
        )
    dl_assets = dl_lecture = dl_subtitles = True
    if args.assets_only:
        dl_lecture = False
        dl_subtitles = False
        args.skip_hls_stream = True
    if args.skip_assets:
        dl_assets = False
    if args.caption_only:
        dl_lecture = False
        dl_assets = False
        args.skip_hls_stream = True
    if args.skip_captions:
        dl_subtitles = False
    if not args.info:
        if args.quality and args.quality > 720 and args.skip_hls_stream:
            args.quality = ""
            logger.warning(
                msg=
                "You cannot use --skip-hls and -q/--quality options togather, considering --skip-hls only.."
            )
        udemy_obj.course_download(
            path=args.output,
            quality=args.quality,
            language=args.language,
            dl_assets=dl_assets,
            dl_lecture=dl_lecture,
            dl_subtitles=dl_subtitles,
            chapter_number=args.chapter,
            chapter_start=args.chapter_start,
            chapter_end=args.chapter_end,
            lecture_number=args.lecture,
            lecture_start=args.lecture_start,
            lecture_end=args.lecture_end,
            keep_vtt=args.keep_vtt,
            skip_hls_stream=args.skip_hls_stream,
        )
    if args.info:
        udemy_obj.course_listdown(
            chapter_number=args.chapter,
            chapter_start=args.chapter_start,
            chapter_end=args.chapter_end,
            lecture_number=args.lecture,
            lecture_start=args.lecture_start,
            lecture_end=args.lecture_end,
            skip_hls_stream=args.skip_hls_stream,
        )
예제 #2
0
def main():
    ban = banner()
    print (ban)
    usage       = '''%prog [-h] [-u "username"] [-p "password"] COURSE_URL
                   [-s] [-l] [-r VIDEO_QUALITY] [-o OUTPUT]'''
    version     = "%prog version {}".format(__version__)
    description = 'A cross-platform python based utility to download courses from udemy for personal offline use.'
    parser = optparse.OptionParser(usage=usage,version=version,conflict_handler="resolve", description=description)

    general = optparse.OptionGroup(parser, 'General')
    general.add_option(
        '-h', '--help',
        action='help',
        help='Shows the help.')
    general.add_option(
        '-v', '--version',
        action='version',
    help='Shows the version.')

    downloader = optparse.OptionGroup(parser, "Advance")
    downloader.add_option(
        "-u", "--username", 
        action='store_true',
        dest='email',\
        help="Username in udemy.")
    downloader.add_option(
        "-p", "--password", 
        action='store_true',
        dest='password',\
        help="Password of your account.")
    downloader.add_option(
        "-s", "--save-links", 
        action='store_true',
        dest='save_links',\
        help="Do not download but save links to a file.")
    downloader.add_option(
        "-l", "--list-infos", 
        action='store_true',
        dest='list',\
        help="List all lectures with available resolution.")
    downloader.add_option(
        "-r", "--resolution", 
        action='store_true',
        dest='quality',\
        help="Download video resolution, default resolution is 720p.")
    downloader.add_option(
        "-o", "--output", 
        action='store_true',
        dest='output',\
        help="Output directory where the videos will be saved, default is current directory.")

    parser.add_option_group(general)
    parser.add_option_group(downloader)

    (options, args) = parser.parse_args()

    if not options.email and not options.password:
        parser.print_help()

    elif options.email and options.password and not options.save_links and not options.list and not options.output and not options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2]
        udemy =  UdemyDownload(url, email, passwd)
        udemy.ExtractAndDownload()
    elif options.email and options.password and options.save_links and not options.list and not options.output and not options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2] if 'http' in args[2] else args[3]
        links   = options.save_links
        udemy   =  UdemyDownload(url, email, passwd)
        udemy.SaveLinks()
    elif options.email and options.password and options.save_links and not options.list and not options.output and options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2]
        res     = args[3]
        links   = options.save_links
        udemy   =  UdemyDownload(url, email, passwd)
        udemy.SaveLinks(quality=res)
    elif options.email and options.password and options.save_links and not options.list and options.output and not options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2]
        outto   = args[3]
        links   = options.save_links
        udemy   =  UdemyDownload(url, email, passwd)
        udemy.SaveLinks(path=outto)
    elif options.email and options.password and options.save_links and not options.list and options.output and options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2]
        res     = args[3]
        outto   = args[4]
        links   = options.save_links
        udemy   =  UdemyDownload(url, email, passwd)
        udemy.SaveLinks(quality=res, path=outto)
    elif options.email and options.password and not options.save_links and options.list and not options.output and not options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2] if 'http' in args[2] else args[3]
        lists   = options.list
        udemy   =  UdemyDownload(url, email, passwd)
        udemy.ListDown()
    elif options.email and options.password and not options.save_links and not options.list and options.output and not options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2] 
        outto   = args[3] 
        udemy   =  UdemyDownload(url, email, passwd)
        udemy.ExtractAndDownload(path=outto)
    elif options.email and options.password and not options.save_links and not options.list and options.output and options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2]
        res     = args[3]
        outto   = args[4]
        udemy =  UdemyDownload(url, email, passwd)
        udemy.ExtractAndDownload(path=outto, quality=res)
    elif options.email and options.password and not options.save_links and not options.list and not options.output and options.quality:
        email   = args[0]
        passwd  = args[1]
        url     = args[2]
        res     = args[3]
        udemy =  UdemyDownload(url, email, passwd)
        udemy.ExtractAndDownload(quality=res)
    else:
        parser.print_help()
예제 #3
0
def main():
    ban = banner()
    print(ban)
    usage = '''%prog [-h] [-u "username"] [-p "password"] COURSE_URL
                   [-s] [-l] [-r VIDEO_QUALITY] [-o OUTPUT] [-d] 
                   [--configs]'''
    version = "%prog version {}".format(__version__)
    description = 'A cross-platform python based utility to download courses from udemy for personal offline use.'
    parser = optparse.OptionParser(usage=usage,
                                   version=version,
                                   conflict_handler="resolve",
                                   description=description)

    general = optparse.OptionGroup(parser, 'General')
    general.add_option('-h', '--help', action='help', help='Shows the help.')
    general.add_option('-v',
                       '--version',
                       action='version',
                       help='Shows the version.')

    downloader = optparse.OptionGroup(parser, "Advance")
    downloader.add_option(
        "-u", "--username",
        action='store_true',
        dest='email',\
        help="Username in udemy.")
    downloader.add_option(
        "-p", "--password",
        action='store_true',
        dest='password',\
        help="Password of your account.")
    downloader.add_option(
        "-c","--configs",
        action='store_true',
        dest='configurations',\
        help="Cache your credentials to use it later.")
    downloader.add_option(
        "-s", "--save-links",
        action='store_true',
        dest='save_links',\
        help="Do not download but save links to a file.")
    downloader.add_option(
        "-l", "--list-infos",
        action='store_true',
        dest='list',\
        help="List all lectures with available resolution.")
    downloader.add_option(
        "-r", "--resolution",
        action='store_true',
        dest='quality',\
        help="Download video resolution, default resolution is 720p.")
    downloader.add_option(
        "-d", "--get-default",
        action='store_true',
        default = False,
        dest='default',\
        help="Download default resolution if requested not there.")
    downloader.add_option(
        "-o", "--output",
        action='store_true',
        dest='output',\
        help="Output directory where the videos will be saved, default is current directory.")

    parser.add_option_group(general)
    parser.add_option_group(downloader)

    (options, args) = parser.parse_args()

    if not options.email and not options.password:
        try:
            url = args[0]
        except IndexError as e:
            parser.print_help()
        else:
            config = use_cached_creds()
            if isinstance(config, dict):
                email = config.get('username')
                passwd = config.get('password')
                resolution = config.get('resolution') or None
                output = config.get('output') or None

                if resolution != "" and resolution != None:
                    options.quality = True
                    options.default = True
                else:
                    options.quality = False

                if output != "" and output != None:
                    options.output = True
                else:
                    options.output = False

                print(fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg +
                      sd + "Using cached configurations..")
                if email != "" and passwd != "":
                    udemy = UdemyDownload(url, email, passwd)
                else:
                    print(
                        fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr +
                        sb +
                        "Username and password seems empty in 'configuration' file"
                    )
                    username = fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Username : "******"[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Password : "******"")
                    if email and passwd:
                        udemy = UdemyDownload(url, email, passwd)
                    else:
                        print(fc + sd + "[" + fr + sb + "-" + fc + sd +
                              "] : " + fr + sb +
                              "Username and password is required..")
                        exit(0)
                if options.configurations:
                    pass
                '''     Download course      '''
                if not options.save_links and not options.list and not options.output and not options.quality:
                    udemy.ExtractAndDownload()
                elif not options.save_links and not options.list and options.output and not options.quality:
                    outto = output
                    udemy.ExtractAndDownload(path=outto)
                elif not options.save_links and not options.list and options.output and options.quality:
                    res = resolution
                    outto = output
                    if options.default:
                        udemy.ExtractAndDownload(path=outto,
                                                 quality=res,
                                                 default=True)
                    else:
                        udemy.ExtractAndDownload(path=outto, quality=res)
                elif not options.save_links and not options.list and not options.output and options.quality:
                    res = resolution
                    if options.default:
                        udemy.ExtractAndDownload(quality=res, default=True)
                    else:
                        udemy.ExtractAndDownload(quality=res)
                    ''' Save course links '''
                elif options.save_links and not options.list and not options.output and not options.quality:
                    udemy.SaveLinks()
                elif options.save_links and not options.list and not options.output and options.quality:
                    res = resolution
                    if options.default:
                        udemy.SaveLinks(quality=res, default=True)
                    else:
                        udemy.SaveLinks(quality=res)
                elif options.save_links and not options.list and options.output and not options.quality:
                    outto = output
                    udemy.SaveLinks(path=outto)
                elif options.save_links and not options.list and options.output and options.quality:
                    res = resolution
                    outto = output
                    if options.default:
                        udemy.SaveLinks(quality=res, path=outto, default=True)
                    else:
                        udemy.SaveLinks(quality=res, path=outto)
                    ''' list down available formats of files and videos '''
                elif options.list:
                    udemy.ListDown()

            else:
                username = fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Username : "******"[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Password : "******"")
                if options.configurations:
                    print(fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " +
                          fg + sd + "Caching configuration...")
                    cached = cache_creds(email, passwd)
                    if cached == 'cached':
                        print(fc + sd + "[" + fm + sb + "*" + fc + sd +
                              "] : " + fg + sd +
                              "Configurations cached successfully...")

                if email and passwd:
                    udemy = UdemyDownload(url, email, passwd)
                else:
                    print(fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " +
                          fr + sb + "Username and password is required..")
                    exit(0)
                '''     Download course      '''
                if not options.save_links and not options.list and not options.output and not options.quality:
                    udemy.ExtractAndDownload()
                elif not options.save_links and not options.list and options.output and not options.quality:
                    outto = args[1]
                    udemy.ExtractAndDownload(path=outto)
                elif not options.save_links and not options.list and options.output and options.quality:
                    res = args[1]
                    outto = args[2]
                    if options.default:
                        udemy.ExtractAndDownload(path=outto,
                                                 quality=res,
                                                 default=True)
                    else:
                        udemy.ExtractAndDownload(path=outto, quality=res)
                elif not options.save_links and not options.list and not options.output and options.quality:
                    res = args[1]
                    if options.default:
                        udemy.ExtractAndDownload(quality=res, default=True)
                    else:
                        udemy.ExtractAndDownload(quality=res)
                    ''' Save course links '''
                elif options.save_links and not options.list and not options.output and not options.quality:
                    udemy.SaveLinks()
                elif options.save_links and not options.list and not options.output and options.quality:
                    res = args[1]
                    if options.default:
                        udemy.SaveLinks(quality=res, default=True)
                    else:
                        udemy.SaveLinks(quality=res)
                elif options.save_links and not options.list and options.output and not options.quality:
                    outto = args[1]
                    udemy.SaveLinks(path=outto)
                elif options.save_links and not options.list and options.output and options.quality:
                    res = args[1]
                    outto = args[2]
                    if options.default:
                        udemy.SaveLinks(quality=res, path=outto, default=True)
                    else:
                        udemy.SaveLinks(quality=res, path=outto)
                    ''' list down available formats of files and videos '''
                elif not options.save_links and options.list and not options.output and not options.quality:
                    udemy.ListDown()

    elif options.email and options.password:
        email = args[0]
        passwd = args[1]
        try:
            url = args[2]
        except IndexError as e:
            parser.print_usage()
        else:
            if options.configurations:
                print(fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fg +
                      sd + "Caching configurations...")
                cached = cache_creds(email, passwd)
                if cached == 'cached':
                    print(fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " +
                          fg + sd + "Configurations cached successfully...")

            udemy = UdemyDownload(url, email, passwd)
            '''     Download course      '''
            if not options.save_links and not options.list and not options.output and not options.quality:
                udemy.ExtractAndDownload()
            elif not options.save_links and not options.list and options.output and not options.quality:
                outto = args[3]
                udemy.ExtractAndDownload(path=outto)
            elif not options.save_links and not options.list and options.output and options.quality:
                res = args[3]
                outto = args[4]
                if options.default:
                    udemy.ExtractAndDownload(path=outto,
                                             quality=res,
                                             default=True)
                else:
                    udemy.ExtractAndDownload(path=outto, quality=res)
            elif not options.save_links and not options.list and not options.output and options.quality:
                res = args[3]
                if options.default:
                    udemy.ExtractAndDownload(quality=res, default=True)
                else:
                    udemy.ExtractAndDownload(quality=res)
                ''' Save course links '''
            elif options.save_links and not options.list and not options.output and not options.quality:
                udemy.SaveLinks()
            elif options.save_links and not options.list and not options.output and options.quality:
                res = args[3]
                if options.default:
                    udemy.SaveLinks(quality=res, default=True)
                else:
                    udemy.SaveLinks(quality=res)
            elif options.save_links and not options.list and options.output and not options.quality:
                outto = args[3]
                udemy.SaveLinks(path=outto)
            elif options.save_links and not options.list and options.output and options.quality:
                res = args[3]
                outto = args[4]
                if options.default:
                    udemy.SaveLinks(quality=res, path=outto, default=True)
                else:
                    udemy.SaveLinks(quality=res, path=outto)
                ''' list down available formats of files and videos '''
            elif not options.save_links and options.list and not options.output and not options.quality:
                udemy.ListDown()