コード例 #1
0
 def test_make_dir(self):
     if os.name == "nt":
         save_dir = ["con", ":"]
         for name in save_dir:
             with pytest.raises(NameError):
                 utils.make_dir(name)
     else:
         with pytest.raises(NameError):
             utils.make_dir("/{}/downloads".format(__name__))
コード例 #2
0
ファイル: nicodown.py プロジェクト: kaimisou/niconico-tools
    def start(self, glossary, save_dir, chunk_size=1024 * 50):
        """

        :param dict[str, dict[str, int | str]] | list[str] glossary:
        :param str | Path save_dir:
        :param int chunk_size: 一度にサーバーに要求するファイルサイズ
        :rtype: bool
        """
        utils.check_arg(locals())
        self.logger.debug("Directory to save in: %s", save_dir)
        self.logger.debug("Dictionary of Videos: %s", glossary)
        self.save_dir = utils.make_dir(save_dir)
        if isinstance(glossary, list):
            glossary = get_infos(glossary, self.logger)
        self.glossary = glossary
        self.logger.info(Msg.nd_start_dl_video, len(self.glossary),
                         list(self.glossary))

        for index, video_id in enumerate(self.glossary.keys()):
            self.logger.info(Msg.nd_download_video, index + 1, len(glossary),
                             video_id, self.glossary[video_id][KeyGTI.TITLE])
            self._download(video_id, chunk_size)
            if len(glossary) > 1:
                time.sleep(1)
        return True
コード例 #3
0
    def show(self, list_id, file_name=None, table=False, survey=False):
        """
        そのマイリストに登録された動画を一覧する。

        :param int | str list_id: マイリストの名前またはID。0で「とりあえずマイリスト」。
        :param str | Path | None file_name: ファイル名。ここにリストを書き出す。
        :param bool table: Trueで表形式で出力する。
        :param bool survey: Trueで全てのマイリストの情報をまとめて出力する。
        :rtype: str
        """
        utils.check_arg({"list_id": list_id, "table": table, "survey": survey})
        if file_name:
            file_name = utils.make_dir(file_name)
        if table:  # 表形式の場合
            if list_id == utils.ALL_ITEM:
                if survey:
                    cont = self._construct_table(self.fetch_all())
                else:
                    cont = self._construct_table(self.fetch_meta())
            else:
                cont = self._construct_table(self.fetch_one(list_id))
        else:  # タブ区切りテキストの場合
            if list_id == utils.ALL_ITEM:
                if survey:
                    cont = self._construct_tsv(self.fetch_all())
                else:
                    cont = self._construct_tsv(self.fetch_meta())
            else:
                cont = self._construct_tsv(self.fetch_one(list_id))
        return self._writer(cont, file_name)
コード例 #4
0
ファイル: nicodown.py プロジェクト: kaimisou/niconico-tools
def main(args):
    """
    メイン。

    :param args: ArgumentParser.parse_args() によって解釈された引数
    :rtype: bool
    """
    is_debug = int(os.getenv("PYTHON_TEST", 0))
    mailadrs = args.mail[0] if args.mail else None
    password = args.password[0] if args.password else None

    #
    # エラーの除外
    #
    if hasattr(args, "dmc"):
        sys.exit(Err.unexpected_commands.format("--dmc"))
    if hasattr(args, "smile"):
        sys.exit(Err.unexpected_commands.format("--smile"))
    videoid = utils.validator(args.VIDEO_ID)
    if not videoid:
        sys.exit(Err.invalid_videoid)
    if not (args.getthumbinfo or args.thumbnail or args.comment or args.video):
        sys.exit(
            Err.not_specified.format("--thumbnail、 --comment、 --video のいずれか"))

    if args.getthumbinfo:
        file_name = args.out[0] if isinstance(args.out, list) else None
        return utils.print_info(videoid, file_name)

    #
    # 本筋
    #
    log_level = "DEBUG" if is_debug else args.loglevel
    logger = utils.NTLogger(log_level=log_level, file_name=utils.LOG_FILE_ND)
    destination = utils.make_dir(args.dest[0])
    database = get_infos(videoid, logger=logger)

    res_t = False
    if args.thumbnail:
        res_t = Thumbnail(logger=logger).start(database, destination)
        if not (args.comment or args.video):
            # サムネイルのダウンロードだけならここで終える。
            return res_t

    session = utils.LogIn(mail=mailadrs, password=password).session

    res_c = False
    if args.comment:
        res_c = Comment(logger=logger,
                        session=session).start(database, destination, args.xml)

    res_v = False
    if args.video:
        res_v = Video(logger=logger,
                      session=session).start(database, destination)

    return res_c | res_v | res_t
コード例 #5
0
 def test_make_dir(self):
     save_dir = ["test", "foo", "foo/bar", "some/thing/text.txt"]
     paths = [utils.make_dir(name) for name in save_dir]
     try:
         for participant, result in zip(save_dir, paths):
             assert str(result).replace("\\", "/").replace(
                 "//", "/").endswith(participant)
     finally:
         try:
             for _parh in {item.split("/")[0] for item in save_dir}:
                 shutil.rmtree(_parh)
         except FileNotFoundError:
             pass
コード例 #6
0
    def _writer(self, text, file_name=None):
        """
        ファイルまたは標準出力に書き出す。

        :param str text: 内容。
        :param str | Path | None file_name: ファイル名またはそのパス
        :rtype: str
        """
        utils.check_arg({"text": text})
        if file_name:
            file_name = utils.make_dir(file_name)
            _text = "{}\n".format(text)
            with file_name.open(mode="w", encoding="utf-8") as fd:
                fd.write(_text)
            self.logger.info(Msg.ml_exported, file_name)
        else:
            enco = utils.get_encoding()
            _text = text.encode(enco, utils.BACKSLASH).decode(enco) + "\n"
            print(_text)
        return _text
コード例 #7
0
    def export(self, list_id, file_name=None, survey=False):
        """
        そのマイリストに登録された動画のIDを一覧する。

        :param int | str list_id: マイリストの名前またはID。0で「とりあえずマイリスト」。
        :param str | Path | None file_name: ファイル名。ここにリストを書き出す。
        :param bool survey: Trueで全てのマイリストの情報をまとめて出力する。
        :rtype: str
        """
        utils.check_arg({"list_id": list_id, "survey": survey})
        if file_name:
            file_name = utils.make_dir(file_name)
        if list_id == utils.ALL_ITEM:
            if survey:
                cont = self._construct_id(self.fetch_all(False))
            else:
                cont = self._construct_id_name(self.fetch_meta(False))
        else:
            cont = self._construct_id(self.fetch_one(list_id, False))
        return self._writer(cont, file_name)
コード例 #8
0
ファイル: nicodown.py プロジェクト: kaimisou/niconico-tools
    def start(self, glossary, save_dir, xml=False):
        """

        :param dict[str, dict[str, int | str]] | list[str] glossary:
        :param str | Path save_dir:
        :param bool xml:
        """
        utils.check_arg(locals())
        self.logger.debug("Directory to save in: %s", save_dir)
        self.logger.debug("Dictionary of Videos: %s", glossary)
        self.logger.debug("Download XML? : %s", xml)
        if isinstance(glossary, list):
            glossary = get_infos(glossary, self.logger)
        self.glossary = glossary
        self.save_dir = utils.make_dir(save_dir)
        self.logger.info(Msg.nd_start_dl_comment, len(self.glossary),
                         list(self.glossary))
        for index, video_id in enumerate(self.glossary.keys()):
            self.logger.info(Msg.nd_download_comment, index + 1, len(glossary),
                             video_id, self.glossary[video_id][KeyGTI.TITLE])
            self._download(video_id, xml)
            if len(self.glossary) > 1:
                time.sleep(1.5)
        return True
コード例 #9
0
ファイル: nicodown.py プロジェクト: kaimisou/niconico-tools
    def start(self, glossary, save_dir, is_large=True):
        """

        :param dict[str, dict[str, int | str]] | list[str] glossary:
        :param str | Path save_dir:
        :param bool is_large: 大きいサムネイルを取りに行くかどうか
        :rtype: bool
        """
        utils.check_arg(locals())
        self.logger.debug("Directory to save in: %s", save_dir)
        self.logger.debug("Dictionary of Videos: %s", glossary)
        if isinstance(glossary, list):
            glossary = get_infos(glossary, self.logger)
        self.glossary = glossary
        self.save_dir = utils.make_dir(save_dir)

        self.logger.info(Msg.nd_start_dl_pict, len(self.glossary),
                         list(self.glossary))

        for index, video_id in enumerate(self.glossary.keys()):
            self.logger.info(Msg.nd_download_pict, index + 1, len(glossary),
                             video_id, self.glossary[video_id][KeyGTI.TITLE])
            self._download(video_id, is_large)
        return True
コード例 #10
0
def test_okatadsuke():
    for _parh in (SAVE_DIR_1, SAVE_DIR_2):
        shutil.rmtree(str(utils.make_dir(_parh)))