示例#1
0
def get_old_style_syllabus(session, class_name, local_page=False, preview=False):
    """
    Get the old style course listing webpage.

    If we are instructed to use a local page and it already exists, then
    that page is used instead of performing a download.  If we are
    instructed to use a local page and it does not exist, then we download
    the page and save a copy of it for future use.
    """

    if not (local_page and os.path.exists(local_page)):
        url = get_syllabus_url(class_name, preview)
        page, final_url = get_page_and_url(session, url)
        logging.info('Downloaded %s (%d bytes)', url, len(page))

        if "/learn/" in final_url:
            # got redirected to a on-demand course page,
            # abort and let on_demand download run
            raise ClassNotFound

        # cache the page if we're in 'local' mode
        if local_page:
            with open(local_page, 'w') as f:
                f.write(page.encode("utf-8"))
    else:
        with open(local_page) as f:
            page = decode_input(f.read())
        logging.info('Read (%d bytes) from local file', len(page))

    return page
示例#2
0
def parseArgs(args=None):
    """
    Parse the arguments/options passed to the program on the command line.
    """

    parser = argparse.ArgumentParser(
        description='Download Coursera.org lecture material and resources.')

    # positional
    parser.add_argument('class_names',
                        action='store',
                        nargs='+',
                        help='name(s) of the class(es) (e.g. "nlp")')

    parser.add_argument('-c',
                        '--cookies_file',
                        dest='cookies_file',
                        action='store',
                        default=None,
                        help='full path to the cookies.txt file')
    parser.add_argument('-u',
                        '--username',
                        dest='username',
                        action='store',
                        default=None,
                        help='coursera username')
    parser.add_argument('-n',
                        '--netrc',
                        dest='netrc',
                        nargs='?',
                        action='store',
                        const=True,
                        default=False,
                        help='use netrc for reading passwords, uses default'
                        ' location if no path specified')

    parser.add_argument('-p',
                        '--password',
                        dest='password',
                        action='store',
                        default=None,
                        help='coursera password')

    # optional
    parser.add_argument('--about',
                        dest='about',
                        action='store_true',
                        default=False,
                        help='download "about" metadata. (Default: False)')
    parser.add_argument('-b',
                        '--preview',
                        dest='preview',
                        action='store_true',
                        default=False,
                        help='get preview videos. (Default: False)')
    parser.add_argument('-f',
                        '--formats',
                        dest='file_formats',
                        action='store',
                        default='all',
                        help='file format extensions to be downloaded in'
                        ' quotes space separated, e.g. "mp4 pdf" '
                        '(default: special value "all")')
    parser.add_argument('-sf',
                        '--section_filter',
                        dest='section_filter',
                        action='store',
                        default=None,
                        help='only download sections which contain this'
                        ' regex (default: disabled)')
    parser.add_argument('-lf',
                        '--lecture_filter',
                        dest='lecture_filter',
                        action='store',
                        default=None,
                        help='only download lectures which contain this regex'
                        ' (default: disabled)')
    parser.add_argument('-rf',
                        '--resource_filter',
                        dest='resource_filter',
                        action='store',
                        default=None,
                        help='only download resources which match this regex'
                        ' (default: disabled)')
    parser.add_argument('--wget',
                        dest='wget',
                        action='store',
                        nargs='?',
                        const='wget',
                        default=None,
                        help='use wget for downloading,'
                        'optionally specify wget bin')
    parser.add_argument('--curl',
                        dest='curl',
                        action='store',
                        nargs='?',
                        const='curl',
                        default=None,
                        help='use curl for downloading,'
                        ' optionally specify curl bin')
    parser.add_argument('--aria2',
                        dest='aria2',
                        action='store',
                        nargs='?',
                        const='aria2c',
                        default=None,
                        help='use aria2 for downloading,'
                        ' optionally specify aria2 bin')
    parser.add_argument('--axel',
                        dest='axel',
                        action='store',
                        nargs='?',
                        const='axel',
                        default=None,
                        help='use axel for downloading,'
                        ' optionally specify axel bin')
    # We keep the wget_bin, ... options for backwards compatibility.
    parser.add_argument('-w',
                        '--wget_bin',
                        dest='wget_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --wget')
    parser.add_argument('--curl_bin',
                        dest='curl_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --curl')
    parser.add_argument('--aria2_bin',
                        dest='aria2_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --aria2')
    parser.add_argument('--axel_bin',
                        dest='axel_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --axel')
    parser.add_argument('-o',
                        '--overwrite',
                        dest='overwrite',
                        action='store_true',
                        default=False,
                        help='whether existing files should be overwritten'
                        ' (default: False)')
    parser.add_argument('-l',
                        '--process_local_page',
                        dest='local_page',
                        help='uses or creates local cached version of syllabus'
                        ' page')
    parser.add_argument('--skip-download',
                        dest='skip_download',
                        action='store_true',
                        default=False,
                        help='for debugging: skip actual downloading of files')
    parser.add_argument('--path',
                        dest='path',
                        action='store',
                        default='',
                        help='path to save the file')
    parser.add_argument('--verbose-dirs',
                        dest='verbose_dirs',
                        action='store_true',
                        default=False,
                        help='include class name in section directory name')
    parser.add_argument('--debug',
                        dest='debug',
                        action='store_true',
                        default=False,
                        help='print lots of debug information')
    parser.add_argument('--quiet',
                        dest='quiet',
                        action='store_true',
                        default=False,
                        help='omit as many messages as possible'
                        ' (only printing errors)')
    parser.add_argument('--add-class',
                        dest='add_class',
                        action='append',
                        default=[],
                        help='additional classes to get')
    parser.add_argument('-r',
                        '--reverse',
                        dest='reverse',
                        action='store_true',
                        default=False,
                        help='download sections in reverse order')
    parser.add_argument('--combined-section-lectures-nums',
                        dest='combined_section_lectures_nums',
                        action='store_true',
                        default=False,
                        help='include lecture and section name in final files')
    parser.add_argument('--hook',
                        dest='hooks',
                        action='append',
                        default=[],
                        help='hooks to run when finished')
    parser.add_argument('-pl',
                        '--playlist',
                        dest='playlist',
                        action='store_true',
                        default=False,
                        help='generate M3U playlists for course weeks')
    parser.add_argument('--clear-cache',
                        dest='clear_cache',
                        action='store_true',
                        default=False,
                        help='clear cached cookies')
    parser.add_argument('--unrestricted-filenames',
                        dest='intact_fnames',
                        action='store_true',
                        default=False,
                        help='Do not limit filenames to be ASCII-only')

    args = parser.parse_args(args)

    # Initialize the logging system first so that other functions
    # can use it right away
    if args.debug:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(name)s[%(funcName)s] %(message)s')
    elif args.quiet:
        logging.basicConfig(level=logging.ERROR,
                            format='%(name)s: %(message)s')
    else:
        logging.basicConfig(level=logging.INFO, format='%(message)s')

    # turn list of strings into list
    args.file_formats = args.file_formats.split()

    # decode path so we can work properly with cyrillic symbols on different
    # versions on Python
    args.path = decode_input(args.path)

    for bin in ['wget_bin', 'curl_bin', 'aria2_bin', 'axel_bin']:
        if getattr(args, bin):
            logging.error('The --%s option is deprecated, please use --%s',
                          bin, bin[:-4])
            sys.exit(1)

    # check arguments
    if args.cookies_file and not os.path.exists(args.cookies_file):
        logging.error('Cookies file not found: %s', args.cookies_file)
        sys.exit(1)

    if not args.cookies_file:
        try:
            args.username, args.password = get_credentials(
                username=args.username,
                password=args.password,
                netrc=args.netrc)
        except CredentialsError as e:
            logging.error(e)
            sys.exit(1)

    return args
示例#3
0
    def classify(self, text=u''):
        """ Predicts the Language of a given text.

            :param text: Unicode text to be classified.
        """

        text = self.lm.normalize(text)
        tokenz = LM.tokenize(text, mode='c')
        result = self.lm.calculate(doc_terms=tokenz)
        #print 'Karbasa:', self.karbasa(result)
        if self.unk and self.lm.karbasa(result) < self.min_karbasa:
            lang = 'unk'
        else:
            lang = result['calc_id']
        return lang

if __name__ == '__main__':

    l = LangID(unk=False)
    l.train()

    if len(sys.argv) > 1:
        text_in = decode_input(sys.argv[1:])
    else:
        text_in = u'hello, world'

    lang_res = l.classify(text_in)
    print text_in, '[ Language:', lang_res, ']'

def parse_args(args=None):
    """
    Parse the arguments/options passed to the program on the command line.
    """

    parser = argparse.ArgumentParser(description="Download Coursera.org lecture material and resources.")

    # Basic options
    group_basic = parser.add_argument_group("Basic options")

    group_basic.add_argument("class_names", action="store", nargs="+", help='name(s) of the class(es) (e.g. "ml-005")')

    group_basic.add_argument(
        "-u", "--username", dest="username", action="store", default=None, help="coursera username"
    )

    group_basic.add_argument(
        "-p", "--password", dest="password", action="store", default=None, help="coursera password"
    )

    group_basic.add_argument(
        "--on-demand",
        dest="on_demand",
        action="store_true",
        default=False,
        help="[DEPRECATED] get on-demand videos. Do not use"
        " this option, it is deprecated. The script will"
        " try to detect course type automatically.",
    )

    group_basic.add_argument(
        "-b",  # FIXME: kill this one-letter option
        "--preview",
        dest="preview",
        action="store_true",
        default=False,
        help="get videos from preview pages. (Default: False)",
    )

    group_basic.add_argument(
        "--path",
        dest="path",
        action="store",
        default="",
        help="path to where to save the file. (Default: current directory)",
    )

    group_basic.add_argument(
        "-sl",  # FIXME: deprecate this option
        "--subtitle-language",
        dest="subtitle_language",
        action="store",
        default="en",
        help="Choose language to download subtitles. (Default: en)",
    )

    # Selection of material to download
    group_material = parser.add_argument_group("Selection of material to download")

    group_material.add_argument(
        "--about",  # FIXME: should be --about-course
        dest="about",
        action="store_true",
        default=False,
        help='download "about" metadata. (Default: False)',
    )

    group_material.add_argument(
        "-f",
        "--formats",
        dest="file_formats",
        action="store",
        default="all",
        help="file format extensions to be downloaded in"
        ' quotes space separated, e.g. "mp4 pdf" '
        '(default: special value "all")',
    )

    group_material.add_argument(
        "--ignore-formats",
        dest="ignore_formats",
        action="store",
        default=None,
        help="file format extensions of resources to ignore" " (default: None)",
    )

    group_material.add_argument(
        "-sf",  # FIXME: deprecate this option
        "--section_filter",
        dest="section_filter",
        action="store",
        default=None,
        help="only download sections which contain this" " regex (default: disabled)",
    )

    group_material.add_argument(
        "-lf",  # FIXME: deprecate this option
        "--lecture_filter",
        dest="lecture_filter",
        action="store",
        default=None,
        help="only download lectures which contain this regex" " (default: disabled)",
    )

    group_material.add_argument(
        "-rf",  # FIXME: deprecate this option
        "--resource_filter",
        dest="resource_filter",
        action="store",
        default=None,
        help="only download resources which match this regex" " (default: disabled)",
    )

    group_material.add_argument(
        "--video-resolution",
        dest="video_resolution",
        action="store",
        default="540p",
        help="video resolution to download (default: 540p); "
        "only valid for on-demand courses; "
        "only values allowed: 360p, 540p, 720p",
    )

    # Selection of material to download
    group_external_dl = parser.add_argument_group("External downloaders")

    group_external_dl.add_argument(
        "--wget",
        dest="wget",
        action="store",
        nargs="?",
        const="wget",
        default=None,
        help="use wget for downloading," "optionally specify wget bin",
    )
    group_external_dl.add_argument(
        "--curl",
        dest="curl",
        action="store",
        nargs="?",
        const="curl",
        default=None,
        help="use curl for downloading," " optionally specify curl bin",
    )
    group_external_dl.add_argument(
        "--aria2",
        dest="aria2",
        action="store",
        nargs="?",
        const="aria2c",
        default=None,
        help="use aria2 for downloading," " optionally specify aria2 bin",
    )
    group_external_dl.add_argument(
        "--axel",
        dest="axel",
        action="store",
        nargs="?",
        const="axel",
        default=None,
        help="use axel for downloading," " optionally specify axel bin",
    )

    parser.add_argument(
        "--resume",
        dest="resume",
        action="store_true",
        default=False,
        help="resume incomplete downloads (default: False)",
    )

    parser.add_argument(
        "-o",
        "--overwrite",
        dest="overwrite",
        action="store_true",
        default=False,
        help="whether existing files should be overwritten" " (default: False)",
    )

    parser.add_argument(
        "--verbose-dirs",
        dest="verbose_dirs",
        action="store_true",
        default=False,
        help="include class name in section directory name",
    )

    parser.add_argument(
        "--quiet",
        dest="quiet",
        action="store_true",
        default=False,
        help="omit as many messages as possible" " (only printing errors)",
    )

    parser.add_argument(
        "-r", "--reverse", dest="reverse", action="store_true", default=False, help="download sections in reverse order"
    )

    parser.add_argument(
        "--combined-section-lectures-nums",
        dest="combined_section_lectures_nums",
        action="store_true",
        default=False,
        help="include lecture and section name in final files",
    )

    parser.add_argument(
        "--unrestricted-filenames",
        dest="intact_fnames",
        action="store_true",
        default=False,
        help="Do not limit filenames to be ASCII-only",
    )

    parser.add_argument(
        "-v",
        "--version",
        help="Display the version of udemy-dl and exit",
        action="version",
        version="%(prog)s {version}".format(version=__version__),
    )

    # Advanced authentication
    group_adv_auth = parser.add_argument_group("Advanced authentication options")

    group_adv_auth.add_argument(
        "-c",
        "--cookies_file",
        dest="cookies_file",
        action="store",
        default=None,
        help="full path to the cookies.txt file",
    )

    group_adv_auth.add_argument(
        "-n",
        "--netrc",
        dest="netrc",
        nargs="?",
        action="store",
        const=True,
        default=False,
        help="use netrc for reading passwords, uses default" " location if no path specified",
    )

    group_adv_auth.add_argument(
        "-k",
        "--keyring",
        dest="use_keyring",
        action="store_true",
        default=False,
        help="use keyring provided by operating system to " "save and load credentials",
    )

    group_adv_auth.add_argument(
        "--clear-cache", dest="clear_cache", action="store_true", default=False, help="clear cached cookies"
    )

    # Advanced miscellaneous options
    group_adv_misc = parser.add_argument_group("Advanced miscellaneous options")

    group_adv_misc.add_argument("--hook", dest="hooks", action="append", default=[], help="hooks to run when finished")

    group_adv_misc.add_argument(
        "-pl",
        "--playlist",
        dest="playlist",
        action="store_true",
        default=False,
        help="generate M3U playlists for course weeks",
    )

    # Debug options
    group_debug = parser.add_argument_group("Debugging options")

    group_debug.add_argument(
        "--skip-download",
        dest="skip_download",
        action="store_true",
        default=False,
        help="for debugging: skip actual downloading of files",
    )

    group_debug.add_argument(
        "--debug", dest="debug", action="store_true", default=False, help="print lots of debug information"
    )

    group_debug.add_argument(
        "-l",  # FIXME: remove short option from rarely used ones
        "--process_local_page",
        dest="local_page",
        help="uses or creates local cached version of syllabus" " page",
    )

    # Final parsing of the options
    args = parser.parse_args(args)

    # Initialize the logging system first so that other functions
    # can use it right away
    if args.debug:
        logging.basicConfig(level=logging.DEBUG, format="%(name)s[%(funcName)s] %(message)s")
    elif args.quiet:
        logging.basicConfig(level=logging.ERROR, format="%(name)s: %(message)s")
    else:
        logging.basicConfig(level=logging.INFO, format="%(message)s")

    # turn list of strings into list
    args.file_formats = args.file_formats.split()

    # decode path so we can work properly with cyrillic symbols on different
    # versions on Python
    args.path = decode_input(args.path)

    # check arguments
    if args.use_keyring and args.password:
        logging.warning("--keyring and --password cannot be specified together")
        args.use_keyring = False

    if args.use_keyring and not keyring:
        logging.warning("The python module `keyring` not found.")
        args.use_keyring = False

    if args.cookies_file and not os.path.exists(args.cookies_file):
        logging.error("Cookies file not found: %s", args.cookies_file)
        sys.exit(1)

    if not args.cookies_file:
        try:
            args.username, args.password = get_credentials(
                username=args.username, password=args.password, netrc=args.netrc, use_keyring=args.use_keyring
            )
        except CredentialsError as e:
            logging.error(e)
            sys.exit(1)

    return args
示例#5
0
def parse_args(args=None):
    """
    Parse the arguments/options passed to the program on the command line.
    """

    parser = argparse.ArgumentParser(
        description='Download Coursera.org lecture material and resources.')

    # Basic options
    group_basic = parser.add_argument_group('Basic options')

    group_basic.add_argument('class_names',
                             action='store',
                             nargs='+',
                             help='name(s) of the class(es) (e.g. "ml-005")')

    group_basic.add_argument('-u',
                             '--username',
                             dest='username',
                             action='store',
                             default=None,
                             help='coursera username')

    group_basic.add_argument('-p',
                             '--password',
                             dest='password',
                             action='store',
                             default=None,
                             help='coursera password')

    group_basic.add_argument('--on-demand', # FIXME: remove this option
                             dest='on_demand',
                             action='store_true',
                             default=False,
                             help='[DEPRECATED] get on-demand videos. Do not use'
                             ' this option, it is deprecated. The script will'
                             ' try to detect course type automatically.')

    group_basic.add_argument('-b',  # FIXME: kill this one-letter option
                             '--preview',
                             dest='preview',
                             action='store_true',
                             default=False,
                             help='get videos from preview pages. (Default: False)')

    group_basic.add_argument('--path',
                             dest='path',
                             action='store',
                             default='',
                             help='path to where to save the file. (Default: current directory)')

    group_basic.add_argument('-sl',  # FIXME: deprecate this option
                             '--subtitle-language',
                             dest='subtitle_language',
                             action='store',
                             default='all',
                             help='Choose language to download subtitles and transcripts. (Default: all)'
                             'Use special value "all" to download all available.')

    # Selection of material to download
    group_material = parser.add_argument_group('Selection of material to download')

    group_material.add_argument('--about',  # FIXME: should be --about-course
                                dest='about',
                                action='store_true',
                                default=False,
                                help='download "about" metadata. (Default: False)')

    group_material.add_argument('-f',
                                '--formats',
                                dest='file_formats',
                                action='store',
                                default='all',
                                help='file format extensions to be downloaded in'
                                ' quotes space separated, e.g. "mp4 pdf" '
                                '(default: special value "all")')

    group_material.add_argument('--ignore-formats',
                                dest='ignore_formats',
                                action='store',
                                default=None,
                                help='file format extensions of resources to ignore'
                                ' (default: None)')

    group_material.add_argument('-sf',  # FIXME: deprecate this option
                                '--section_filter',
                                dest='section_filter',
                                action='store',
                                default=None,
                                help='only download sections which contain this'
                                ' regex (default: disabled)')

    group_material.add_argument('-lf',  # FIXME: deprecate this option
                                '--lecture_filter',
                                dest='lecture_filter',
                                action='store',
                                default=None,
                                help='only download lectures which contain this regex'
                                ' (default: disabled)')

    group_material.add_argument('-rf',  # FIXME: deprecate this option
                                '--resource_filter',
                                dest='resource_filter',
                                action='store',
                                default=None,
                                help='only download resources which match this regex'
                                ' (default: disabled)')

    group_material.add_argument('--video-resolution',
                                dest='video_resolution',
                                action='store',
                                default=None,
                                help='video resolution to download (default: None); '
                                'only valid for on-demand courses; '
                                'only values allowed: 360p, 540p, 720p')

    group_material.add_argument('--disable-url-skipping',
                                dest='disable_url_skipping',
                                action='store_true',
                                default=False,
                                help='disable URL skipping, all URLs will be '
                                'downloaded (default: False)')

    # Selection of material to download
    group_external_dl = parser.add_argument_group('External downloaders')

    group_external_dl.add_argument('--wget',
                                   dest='wget',
                                   action='store',
                                   nargs='?',
                                   const='wget',
                                   default=None,
                                   help='use wget for downloading,'
                                   'optionally specify wget bin')
    group_external_dl.add_argument('--curl',
                                   dest='curl',
                                   action='store',
                                   nargs='?',
                                   const='curl',
                                   default=None,
                                   help='use curl for downloading,'
                                   ' optionally specify curl bin')
    group_external_dl.add_argument('--aria2',
                                   dest='aria2',
                                   action='store',
                                   nargs='?',
                                   const='aria2c',
                                   default=None,
                                   help='use aria2 for downloading,'
                                   ' optionally specify aria2 bin')
    group_external_dl.add_argument('--axel',
                                   dest='axel',
                                   action='store',
                                   nargs='?',
                                   const='axel',
                                   default=None,
                                   help='use axel for downloading,'
                                   ' optionally specify axel bin')
    group_external_dl.add_argument('--downloader-arguments',
                                   dest='downloader_arguments',
                                   default='',
                                   help='additional arguments passed to the'
                                   ' downloader')

    parser.add_argument('--resume',
                        dest='resume',
                        action='store_true',
                        default=False,
                        help='resume incomplete downloads (default: False)')

    parser.add_argument('-o',
                        '--overwrite',
                        dest='overwrite',
                        action='store_true',
                        default=False,
                        help='whether existing files should be overwritten'
                             ' (default: False)')

    parser.add_argument('--verbose-dirs',
                        dest='verbose_dirs',
                        action='store_true',
                        default=False,
                        help='include class name in section directory name')

    parser.add_argument('--quiet',
                        dest='quiet',
                        action='store_true',
                        default=False,
                        help='omit as many messages as possible'
                             ' (only printing errors)')

    parser.add_argument('-r',
                        '--reverse',
                        dest='reverse',
                        action='store_true',
                        default=False,
                        help='download sections in reverse order')

    parser.add_argument('--combined-section-lectures-nums',
                        dest='combined_section_lectures_nums',
                        action='store_true',
                        default=False,
                        help='include lecture and section name in final files')

    parser.add_argument('--unrestricted-filenames',
                        dest='unrestricted_filenames',
                        action='store_true',
                        default=False,
                        help='Do not limit filenames to be ASCII-only')

    # Advanced authentication
    group_adv_auth = parser.add_argument_group('Advanced authentication options')

    group_adv_auth.add_argument('-c',
                                '--cookies_file',
                                dest='cookies_file',
                                action='store',
                                default=None,
                                help='full path to the cookies.txt file')

    group_adv_auth.add_argument('-n',
                                '--netrc',
                                dest='netrc',
                                nargs='?',
                                action='store',
                                const=True,
                                default=False,
                                help='use netrc for reading passwords, uses default'
                                ' location if no path specified')

    group_adv_auth.add_argument('-k',
                                '--keyring',
                                dest='use_keyring',
                                action='store_true',
                                default=False,
                                help='use keyring provided by operating system to '
                                'save and load credentials')

    group_adv_auth.add_argument('--clear-cache',
                                dest='clear_cache',
                                action='store_true',
                                default=False,
                                help='clear cached cookies')

    # Advanced miscellaneous options
    group_adv_misc = parser.add_argument_group('Advanced miscellaneous options')

    group_adv_misc.add_argument('--hook',
                                dest='hooks',
                                action='append',
                                default=[],
                                help='hooks to run when finished')

    group_adv_misc.add_argument('-pl',
                                '--playlist',
                                dest='playlist',
                                action='store_true',
                                default=False,
                                help='generate M3U playlists for course weeks')

    # Debug options
    group_debug = parser.add_argument_group('Debugging options')

    group_debug.add_argument('--skip-download',
                             dest='skip_download',
                             action='store_true',
                             default=False,
                             help='for debugging: skip actual downloading of files')

    group_debug.add_argument('--debug',
                             dest='debug',
                             action='store_true',
                             default=False,
                             help='print lots of debug information')

    group_debug.add_argument('--version',
                             dest='version',
                             action='store_true',
                             default=False,
                             help='display version and exit')

    group_debug.add_argument('-l',  # FIXME: remove short option from rarely used ones
                             '--process_local_page',
                             dest='local_page',
                             help='uses or creates local cached version of syllabus'
                             ' page')

    # Final parsing of the options
    args = parser.parse_args(args)

    # Initialize the logging system first so that other functions
    # can use it right away
    if args.debug:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(name)s[%(funcName)s] %(message)s')
    elif args.quiet:
        logging.basicConfig(level=logging.ERROR,
                            format='%(name)s: %(message)s')
    else:
        logging.basicConfig(level=logging.INFO,
                            format='%(message)s')

    # show version?
    if args.version:
        # we use print (not logging) function because version may be used
        # by some external script while logging may output excessive information
        print(__version__)
        sys.exit(0)

    # turn list of strings into list
    args.downloader_arguments = args.downloader_arguments.split()

    # turn list of strings into list
    args.file_formats = args.file_formats.split()

    # decode path so we can work properly with cyrillic symbols on different
    # versions on Python
    args.path = decode_input(args.path)

    # check arguments
    if args.use_keyring and args.password:
        logging.warning('--keyring and --password cannot be specified together')
        args.use_keyring = False

    if args.use_keyring and not keyring:
        logging.warning('The python module `keyring` not found.')
        args.use_keyring = False

    if args.cookies_file and not os.path.exists(args.cookies_file):
        logging.error('Cookies file not found: %s', args.cookies_file)
        sys.exit(1)

    return args
示例#6
0
def create_and_send_tx(client_socket: socket.socket, user):
    sender = utils.get_public_key(user)
    command = gui_metadata(TX_COMMAND)
    client_socket.send(command)

    inputs = []
    N_inputs = int.from_bytes(client_socket.recv(4), 'little')
    for _ in range(N_inputs):
        input_data = client_socket.recv(159)
        inputs.append(utils.decode_input(input_data))

    input_choices = [
        {
            'name': f'txID: {input.where_created} value: {input.value}',
        }
        for index, input in enumerate(inputs)
    ]

    recipient_choices = [
        {
            'name': 'Alice',
        }, {
            'name': 'Bob',
        }, {
            'name': 'Charlie',
        }, {
            'name': 'No more pays',
        },
    ]

    actions = [{
        'type': 'checkbox',
        'name': INPUT_INDEX,
        'message': 'Selecciona los inputs a ingresar en la transacción',
        'choices': input_choices,
    }]

    recipient_actions = [{
        'type': 'list',
        'name': 'recipient',
        'message': 'Selecciona a la persona que le pagarás',
        'choices': recipient_choices,
    }]

    value_actions = [{
        'type': 'input',
        'name': 'value',
        'message': '¿Cuánto le vas a pagar?',
    }]

    answers = prompt(actions)
    inputs_selected = answers.get(INPUT_INDEX)
    inputs_to_pay = []
    for inputs_selected in inputs_selected:
        index = input_choices.index({'name': inputs_selected})
        inputs_to_pay.append(inputs[index])

    outputs = []
    while True:
        answers = prompt(recipient_actions)
        answer = answers['recipient']
        if answer == 'No more pays':
            break

        recipient = utils.get_public_key(answer)
        answers = prompt(value_actions)
        value = int(answers['value'])
        output = Output(value, recipient)
        outputs.append(output)

    unsigned = UnsignedTransaction(PAYCOINS_TYPE, inputs_to_pay, outputs)
    to_sign = unsigned.DataForSigs()
    signs = {}
    signs[sender.export_key(format='DER')] = sign(to_sign, utils.get_private_key(user))
    transaction = Transaction(unsigned, signs)
    client_socket.send(transaction.serialize())
示例#7
0
def parseArgs(args=None):
    """
    Parse the arguments/options passed to the program on the command line.
    """

    parser = argparse.ArgumentParser(
        description='Download Coursera.org lecture material and resources.')

    # positional
    parser.add_argument('class_names',
                        action='store',
                        nargs='+',
                        help='name(s) of the class(es) (e.g. "nlp")')

    parser.add_argument('-c',
                        '--cookies_file',
                        dest='cookies_file',
                        action='store',
                        default=None,
                        help='full path to the cookies.txt file')
    parser.add_argument('-u',
                        '--username',
                        dest='username',
                        action='store',
                        default=None,
                        help='coursera username')
    parser.add_argument('-n',
                        '--netrc',
                        dest='netrc',
                        nargs='?',
                        action='store',
                        const=True,
                        default=False,
                        help='use netrc for reading passwords, uses default'
                             ' location if no path specified')

    parser.add_argument('-p',
                        '--password',
                        dest='password',
                        action='store',
                        default=None,
                        help='coursera password')

    # optional
    parser.add_argument('--about',
                        dest='about',
                        action='store_true',
                        default=False,
                        help='download "about" metadata. (Default: False)')
    parser.add_argument('-b',
                        '--preview',
                        dest='preview',
                        action='store_true',
                        default=False,
                        help='get preview videos. (Default: False)')
    parser.add_argument('-f',
                        '--formats',
                        dest='file_formats',
                        action='store',
                        default='all',
                        help='file format extensions to be downloaded in'
                             ' quotes space separated, e.g. "mp4 pdf" '
                             '(default: special value "all")')
    parser.add_argument('-sf',
                        '--section_filter',
                        dest='section_filter',
                        action='store',
                        default=None,
                        help='only download sections which contain this'
                             ' regex (default: disabled)')
    parser.add_argument('-lf',
                        '--lecture_filter',
                        dest='lecture_filter',
                        action='store',
                        default=None,
                        help='only download lectures which contain this regex'
                             ' (default: disabled)')
    parser.add_argument('-rf',
                        '--resource_filter',
                        dest='resource_filter',
                        action='store',
                        default=None,
                        help='only download resources which match this regex'
                             ' (default: disabled)')
    parser.add_argument('--wget',
                        dest='wget',
                        action='store',
                        nargs='?',
                        const='wget',
                        default=None,
                        help='use wget for downloading,'
                             'optionally specify wget bin')
    parser.add_argument('--curl',
                        dest='curl',
                        action='store',
                        nargs='?',
                        const='curl',
                        default=None,
                        help='use curl for downloading,'
                             ' optionally specify curl bin')
    parser.add_argument('--aria2',
                        dest='aria2',
                        action='store',
                        nargs='?',
                        const='aria2c',
                        default=None,
                        help='use aria2 for downloading,'
                             ' optionally specify aria2 bin')
    parser.add_argument('--axel',
                        dest='axel',
                        action='store',
                        nargs='?',
                        const='axel',
                        default=None,
                        help='use axel for downloading,'
                             ' optionally specify axel bin')
    # We keep the wget_bin, ... options for backwards compatibility.
    parser.add_argument('-w',
                        '--wget_bin',
                        dest='wget_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --wget')
    parser.add_argument('--curl_bin',
                        dest='curl_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --curl')
    parser.add_argument('--aria2_bin',
                        dest='aria2_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --aria2')
    parser.add_argument('--axel_bin',
                        dest='axel_bin',
                        action='store',
                        default=None,
                        help='DEPRECATED, use --axel')
    parser.add_argument('-o',
                        '--overwrite',
                        dest='overwrite',
                        action='store_true',
                        default=False,
                        help='whether existing files should be overwritten'
                             ' (default: False)')
    parser.add_argument('-l',
                        '--process_local_page',
                        dest='local_page',
                        help='uses or creates local cached version of syllabus'
                             ' page')
    parser.add_argument('--skip-download',
                        dest='skip_download',
                        action='store_true',
                        default=False,
                        help='for debugging: skip actual downloading of files')
    parser.add_argument('--path',
                        dest='path',
                        action='store',
                        default='',
                        help='path to save the file')
    parser.add_argument('--verbose-dirs',
                        dest='verbose_dirs',
                        action='store_true',
                        default=False,
                        help='include class name in section directory name')
    parser.add_argument('--debug',
                        dest='debug',
                        action='store_true',
                        default=False,
                        help='print lots of debug information')
    parser.add_argument('--quiet',
                        dest='quiet',
                        action='store_true',
                        default=False,
                        help='omit as many messages as possible'
                             ' (only printing errors)')
    parser.add_argument('--add-class',
                        dest='add_class',
                        action='append',
                        default=[],
                        help='additional classes to get')
    parser.add_argument('-r',
                        '--reverse',
                        dest='reverse',
                        action='store_true',
                        default=False,
                        help='download sections in reverse order')
    parser.add_argument('--combined-section-lectures-nums',
                        dest='combined_section_lectures_nums',
                        action='store_true',
                        default=False,
                        help='include lecture and section name in final files')
    parser.add_argument('--hook',
                        dest='hooks',
                        action='append',
                        default=[],
                        help='hooks to run when finished')
    parser.add_argument('-pl',
                        '--playlist',
                        dest='playlist',
                        action='store_true',
                        default=False,
                        help='generate M3U playlists for course weeks')
    parser.add_argument('--clear-cache',
                        dest='clear_cache',
                        action='store_true',
                        default=False,
                        help='clear cached cookies')
    parser.add_argument('--unrestricted-filenames',
                        dest='intact_fnames',
                        action='store_true',
                        default=False,
                        help='Do not limit filenames to be ASCII-only')

    args = parser.parse_args(args)


    # Initialize the logging system first so that other functions
    # can use it right away
    if args.debug:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(name)s[%(funcName)s] %(message)s')
    elif args.quiet:
        logging.basicConfig(level=logging.ERROR,
                            format='%(name)s: %(message)s')
    else:
        logging.basicConfig(level=logging.INFO,
                            format='%(message)s')

    # turn list of strings into list
    args.file_formats = args.file_formats.split()

    # decode path so we can work properly with cyrillic symbols on different
    # versions on Python
    args.path = decode_input(args.path)

    for bin in ['wget_bin', 'curl_bin', 'aria2_bin', 'axel_bin']:
        if getattr(args, bin):
            logging.error('The --%s option is deprecated, please use --%s',
                          bin, bin[:-4])
            sys.exit(1)

    # check arguments
    if args.cookies_file and not os.path.exists(args.cookies_file):
        logging.error('Cookies file not found: %s', args.cookies_file)
        sys.exit(1)

    if not args.cookies_file:
        try:
            args.username, args.password = get_credentials(
                username=args.username, password=args.password,
                netrc=args.netrc)
        except CredentialsError as e:
            logging.error(e)
            sys.exit(1)

    return args