コード例 #1
0
    def get_videos(self, raw_path):
        """
        传入视频名称或路径,返回 Video 对象列表
        若指定 store_path ,则是否存在字幕会在 store_path 中查找

        params:
            raw_path: str, video path/name or a directory path
        return:
            videos: list of "Video" objects
        """

        raw_path = raw_path.replace('"', "")

        videos = []

        if path.isdir(raw_path):  # directory
            for root, dirs, files in os.walk(raw_path):
                for file in files:
                    v_type = path.splitext(file)[-1]
                    if v_type not in VIDEO_FORMATS:
                        continue
                    video = Video(
                        path.join(root, file),
                        sub_store_path=self.sub_store_path,
                        identifier=self.sub_identifier,
                    )
                    videos.append(video)
        elif path.isabs(raw_path):  # video's absolute path
            v_type = path.splitext(raw_path)[-1]
            if v_type in VIDEO_FORMATS:
                video = Video(
                    raw_path,
                    sub_store_path=self.sub_store_path,
                    identifier=self.sub_identifier,
                )
                videos.append(video)
        else:  # single video name, no path
            s_path = os.getcwd(
            ) if not self.sub_store_path else self.sub_store_path
            video = Video(raw_path,
                          sub_store_path=s_path,
                          identifier=self.sub_identifier)
            videos.append(video)

        return videos
コード例 #2
0
 def test_choose_subtitle(self, mock_input):
     create_test_directory(
         TestProcessArchive.test_dir_structure,
         parent_dir=TestProcessArchive.test_dir,
     )
     with open(path.join(assets_path, "archive.zip"), "rb") as f:
         data = f.read()
     test_video = Video(path.join(TestProcessArchive.test_dir, "sub1.mkv"))
     process_archive = get_function(single=True)
     err, subnames = process_archive(test_video, data, ".zip")
     self.assertEqual((err, subnames), ("", [["dir1/sub1.ass", ".ass"]]))
コード例 #3
0
 def test_save_both_subtitles_fail(self):
     create_test_directory(
         TestProcessArchive.test_dir_structure,
         parent_dir=TestProcessArchive.test_dir,
     )
     with open(path.join(assets_path, "archive.zip"), "rb") as f:
         data = f.read()
     process_archive = get_function(both=True)
     test_video = Video(path.join(TestProcessArchive.test_dir, "sub1.mkv"))
     err, subnames = process_archive(test_video, data, ".zip")
     self.assertTrue(
         "sub1.ass" in os.listdir(TestProcessArchive.test_dir)
         and "sub1.srt" not in os.listdir(TestProcessArchive.test_dir)
     )
コード例 #4
0
 def test_identifier(self):
     dir_structure = copy.copy(TestProcessArchive.test_dir_structure)
     dir_structure["sub1.ass"] = None
     dir_structure["sub1.zh.srt"] = None
     create_test_directory(
         dir_structure, parent_dir=TestProcessArchive.test_dir,
     )
     with open(path.join(assets_path, "archive.zip"), "rb") as f:
         data = f.read()
     process_archive = get_function()
     test_video = Video(
         path.join(TestProcessArchive.test_dir, "sub1.mkv"), identifier=".zh"
     )
     err, subnames = process_archive(test_video, data, ".zip")
     self.assertTrue(
         "sub1.ass" in os.listdir(TestProcessArchive.test_dir)
         and "sub1.zh.ass" in os.listdir(TestProcessArchive.test_dir)
         and "sub1.zh.srt" not in os.listdir(TestProcessArchive.test_dir)
     )
コード例 #5
0
    def test_get_keywords(self):
        """
        Test video info extracting
        """

        names = (
            "Show.S01E01.ShowName.1080p.AMZN.WEB-DL.DDP5.1.H.264-GRP.mkv",
            "Hanzawa.Naoki.Ep10.Final.Chi_Jap.BDrip.1280X720-ZhuixinFan.mp4",
            "Homeland.S02E12.PROPER.720p.HDTV.x264-EVOLVE.mkv",
            "La.La.Land.2016.1080p.BluRay.x264.Atmos.TrueHD.7.1-HDChina.mkv",
        )
        results = (
            ["Show", "s01", "e01", "Web", "GRP", "amzn", "1080p"],
            ["Hanzawa%20Naoki", "e10", "Bluray", "ZhuixinFan", "720p"],
            ["Homeland", "s02", "e12", "HDTV", "EVOLVE", "720p"],
            ["La%20La%20Land", "2016", "Bluray", "HDChina", "1080p"],
        )
        for n, r in zip(names, results):
            video = Video(n)
            self.assertEqual(Downloader.get_keywords(video), r)
コード例 #6
0
 def test_season_filter(self):
     video_name = "supernatural.s08.mkv"
     video = Video(video_name)
     results = ZimukuDownloader().get_subtitles(video, sub_num=1)
     self.assertEqual(len(results), 1)
コード例 #7
0
 def test_get_season_from_subtitle(self):
     # from issue #68 and pr #66
     video_name = "The.Morning.Show.S01E01.mkv"
     video = Video(video_name)
     results = ZimukuDownloader().get_subtitles(video, sub_num=1)
     self.assertEqual(len(results), 1)
コード例 #8
0
 def test_shooter_page(self):
     video_name = "supernatural.s08e10.mkv"
     ZimukuDownloader.search_url = ZimukuDownloader.site_url + "/search?t=onlyst&q="
     video = Video(video_name)
     results = ZimukuDownloader().get_subtitles(video, sub_num=1)
     self.assertEqual(len(results), 1)
コード例 #9
0
 def test_no_redirect_url(self):
     # from issue #72
     video_name = "the.expanse.s01e01.mkv"
     video = Video(video_name)
     results = ZimukuDownloader().get_subtitles(video, sub_num=1)
     self.assertEqual(len(results), 1)
コード例 #10
0
 def test_redirect_url(self):
     # from issue #72
     video_name = "The.Flash.S01E01.mkv"
     video = Video(video_name)
     results = ZimukuDownloader().get_subtitles(video, sub_num=1)
     self.assertEqual(len(results), 1)
コード例 #11
0
class TestProcessArchive(unittest.TestCase):

    test_dir = "testPA"
    test_video = Video(path.join(test_dir, "video.mkv"), test_dir)
    test_dir_structure = {}

    def tearDown(self):
        if path.exists(TestProcessArchive.test_dir):
            shutil.rmtree(TestProcessArchive.test_dir)

    def test_unsupported_archive(self):
        process_archive = get_function()
        err, subnames = process_archive(TestProcessArchive.test_video, b"", ".tar")
        self.assertEqual((err, subnames), ("unsupported file type .tar", []))

    def test_invalid_archive(self):
        process_archive = get_function()
        self.assertRaises(
            rarfile.BadRarFile,
            process_archive,
            TestProcessArchive.test_video,
            b"",
            ".7z",
        )

    def test_empty_archive(self):
        with open(path.join(assets_path, "empty.zip"), "rb") as f:
            data = f.read()
        process_archive = get_function()
        err, subnames = process_archive(TestProcessArchive.test_video, data, ".zip")
        self.assertEqual((err, subnames), ("no subtitle in this archive", []))

    def test_fail_guess(self):
        with open(path.join(assets_path, "archive.zip"), "rb") as f:
            data = f.read()
        process_archive = get_function()
        err, subnames = process_archive(TestProcessArchive.test_video, data, ".zip")
        self.assertEqual((err, subnames), ("no guess result in auto mode", []))

    @mock.patch("builtins.input", side_effect=["1"])
    def test_choose_subtitle(self, mock_input):
        create_test_directory(
            TestProcessArchive.test_dir_structure,
            parent_dir=TestProcessArchive.test_dir,
        )
        with open(path.join(assets_path, "archive.zip"), "rb") as f:
            data = f.read()
        test_video = Video(path.join(TestProcessArchive.test_dir, "sub1.mkv"))
        process_archive = get_function(single=True)
        err, subnames = process_archive(test_video, data, ".zip")
        self.assertEqual((err, subnames), ("", [["dir1/sub1.ass", ".ass"]]))

    def test_save_both_subtitles_success(self):
        create_test_directory(
            TestProcessArchive.test_dir_structure,
            parent_dir=TestProcessArchive.test_dir,
        )
        with open(path.join(assets_path, "archive.zip"), "rb") as f:
            data = f.read()
        test_video = Video(path.join(TestProcessArchive.test_dir, "sub.mkv"))
        process_archive = get_function(both=True)
        err, subnames = process_archive(test_video, data, ".zip")
        self.assertTrue(
            "sub.ass" in os.listdir(TestProcessArchive.test_dir)
            and "sub.srt" in os.listdir(TestProcessArchive.test_dir)
        )

    def test_save_both_subtitles_fail(self):
        create_test_directory(
            TestProcessArchive.test_dir_structure,
            parent_dir=TestProcessArchive.test_dir,
        )
        with open(path.join(assets_path, "archive.zip"), "rb") as f:
            data = f.read()
        process_archive = get_function(both=True)
        test_video = Video(path.join(TestProcessArchive.test_dir, "sub1.mkv"))
        err, subnames = process_archive(test_video, data, ".zip")
        self.assertTrue(
            "sub1.ass" in os.listdir(TestProcessArchive.test_dir)
            and "sub1.srt" not in os.listdir(TestProcessArchive.test_dir)
        )

    def test_delete_existed_subtitles(self):
        dir_structure = copy.copy(TestProcessArchive.test_dir_structure)
        dir_structure["sub1.ass"] = None
        dir_structure["sub1.srt"] = None
        create_test_directory(
            dir_structure, parent_dir=TestProcessArchive.test_dir,
        )
        with open(path.join(assets_path, "archive.zip"), "rb") as f:
            data = f.read()
        process_archive = get_function(both=True)
        test_video = Video(path.join(TestProcessArchive.test_dir, "sub1.mkv"))
        err, subnames = process_archive(test_video, data, ".zip")
        self.assertTrue(
            "sub1.ass" in os.listdir(TestProcessArchive.test_dir)
            and "sub1.srt" not in os.listdir(TestProcessArchive.test_dir)
        )

    def test_identifier(self):
        dir_structure = copy.copy(TestProcessArchive.test_dir_structure)
        dir_structure["sub1.ass"] = None
        dir_structure["sub1.zh.srt"] = None
        create_test_directory(
            dir_structure, parent_dir=TestProcessArchive.test_dir,
        )
        with open(path.join(assets_path, "archive.zip"), "rb") as f:
            data = f.read()
        process_archive = get_function()
        test_video = Video(
            path.join(TestProcessArchive.test_dir, "sub1.mkv"), identifier=".zh"
        )
        err, subnames = process_archive(test_video, data, ".zip")
        self.assertTrue(
            "sub1.ass" in os.listdir(TestProcessArchive.test_dir)
            and "sub1.zh.ass" in os.listdir(TestProcessArchive.test_dir)
            and "sub1.zh.srt" not in os.listdir(TestProcessArchive.test_dir)
        )