diff --git a/tests/scraper_test.py b/tests/scraper_test.py index 6183081..5ba6c98 100644 --- a/tests/scraper_test.py +++ b/tests/scraper_test.py @@ -150,3 +150,21 @@ def test_scrape_video_only2(): vks.download_media(res, tempdir) found_files = set(os.listdir(tempdir)) assert "video-17546758_456239898_0.mp4" in found_files + + +def test_scrape_private_video(): + """ + > Some videos are kept private and cannot be accessed without a passkey . In this case, send the ID in {owner_id}_{video_id}_{access_key}. + From https://dev.vk.com/ru/method/video.get + """ + res = vks.scrape("https://vk.com/wall-127774884_178565") + + with tempfile.TemporaryDirectory(dir="./") as tempdir: + vks.download_media(res, tempdir) + expect_files = { + "wall-127774884_178565_0.mp4", + "wall-127774884_178565_1.mp4", + "wall-127774884_178565_2.mp4", + } + found_files = set(os.listdir(tempdir)) + assert len(expect_files) == len(expect_files & found_files) diff --git a/vk_url_scraper/__main__.py b/vk_url_scraper/__main__.py index a2a7738..7b721de 100644 --- a/vk_url_scraper/__main__.py +++ b/vk_url_scraper/__main__.py @@ -19,7 +19,7 @@ def get_argument_parser(): action="store", dest="username", required=True, - help="username for a valid vk.com account", + help="username for a valid vk.com account (pass empty if using --token)", ) parser.add_argument( "-p", @@ -27,7 +27,7 @@ def get_argument_parser(): action="store", dest="password", required=True, - help="password for the valid vk.com account", + help="password for the valid vk.com account (pass empty if using --token)", ) parser.add_argument( "-t", diff --git a/vk_url_scraper/scraper.py b/vk_url_scraper/scraper.py index 2b5183b..5360e6a 100644 --- a/vk_url_scraper/scraper.py +++ b/vk_url_scraper/scraper.py @@ -37,7 +37,7 @@ class VkScraper: WALL_PATTERN = re.compile(r"(wall.{0,1}\d+_\d+)") PHOTO_PATTERN = re.compile(r"(photo.{0,1}\d+_\d+)") - VIDEO_PATTERN = re.compile(r"(video.{0,1}\d+_\d+)") + VIDEO_PATTERN = re.compile(r"(video.{0,1}\d+_\d+(?:_\w+)?)") def __init__( self, @@ -144,10 +144,11 @@ class VkScraper: first_type = a["type"] attachment = a[first_type] if first_type == "video": + video_path = f'video{attachment["owner_id"]}_{attachment["id"]}' + if "access_key" in attachment: + video_path += f"_{attachment['access_key']}" attachments["video"].extend( - self.scrape_videos(f'video{attachment["owner_id"]}_{attachment["id"]}')[ - 0 - ] + self.scrape_videos(video_path)[0] .get("attachments", {}) .get("video", [""]) ) @@ -352,9 +353,10 @@ class VkScraper: info = ydl.extract_info(url, download=True) filename = ydl.prepare_filename(info) if "unknown_video" in filename: + old_filename = filename filename = shutil.copy( - filename, filename.replace("unknown_video", "mkv") + filename, filename.replace("unknown_video", "mp4") ) - os.remove(filename) + os.remove(old_filename) downloaded.append(filename) return downloaded