-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvk_video_downloader.py
executable file
·51 lines (44 loc) · 1.43 KB
/
vk_video_downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import vk_api
import sys
import os.path
import getopt
import youtube_dl
def main():
vk_token = ''
group_id = ''
argv = sys.argv[1:]
opts = []
try:
opts, args = getopt.getopt(argv, "t:g:")
except:
print("Error")
for opt, arg in opts:
if opt in ['-t']:
vk_token = arg
elif opt in ['-g']:
group_id = arg
vk_session = vk_api.VkApi(token=vk_token)
vk = vk_session.get_api()
videos_dir = str(group_id).lstrip('-')
albums = vk.video.getAlbums(owner_id=group_id, need_system=1)
for album in albums['items']:
videos = vk.video.get(owner_id=group_id, album_id=album['id'], count=100)
for video in videos['items']:
if 'player' in video:
player_url = video['player']
print(f"Player URL: {player_url}")
video_file_name = f"{videos_dir}-videos/{video['title']}.mp4"
if os.path.exists(video_file_name):
print(f"Video already downloaded: {video_file_name}")
continue
ydl_opts = {
'outtmpl': video_file_name,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([player_url])
print(f"Video downloaded: {video['title']}.mp4")
if __name__ == '__main__':
main()