· 5 years ago · Jul 09, 2020, 07:22 PM
1#!/usr/bin/python3.6
2'''
3 Download an entire channel in best possibly quality.
4 disclaimer: Youtube will probably ban you if you try to download pewdiepie.
5
6'''
7import youtube_dl
8from pyyoutube import Api
9import sys
10import json
11import os
12class Download():
13 def __init__(self):
14 ''' Some configuration '''
15 self.ydl_opts = {'format': 'bestvideo+bestaudio/best',
16 'outtmpl': '%(title)s.%(ext)s',
17 'quiet': True,
18 'forcetitle': True}
19 self.videourls = []
20 self.already = []
21 self.yt = Api(api_key='<GOOGLE CLOUD SERVICE API KEY>')
22 try:
23 self.videoID = sys.argv[1]
24 except:
25 self.videoID = None
26 pass
27
28 def run(self, videoID=None):
29 ''' get a list of all the videos of the channel this video belongs to '''
30 if videoID is None and self.videoID is None:
31 self.videoID = input('Paste a video from the channel: ')
32
33 self.videoID = self.videoID.split('=')[1]
34
35 try:
36 video = self.yt.get_video_by_id(video_id=self.videoID)
37 except:
38 print('Unknown format. url needs to be like https://www.youtube.com/watch?v=blahehalh')
39 sys.exit(0)
40 channelID = video.items[0].to_dict()['snippet']['channelId']
41 channel = self.yt.get_channel_info(channel_id=channelID)
42 for videos in channel.items:
43 item = videos.contentDetails
44 uploads = item.to_dict()['relatedPlaylists']['uploads']
45 _list = self.yt.get_playlist_by_id(playlist_id=uploads)
46 playlist_data = _list.to_dict()
47 channel_name = playlist_data['items'][0]['snippet']['title']
48 channel_videos = playlist_data['items'][0]['contentDetails']['itemCount']
49 if channel_videos > 50:
50 print('{} have {} videos.'.format(channel_name, channel_videos))
51 if channel_videos >= 100:
52 print('WARNING[>100]: This channel contains over 100 videos.\nEven though the download itself doesnt use the API, continuing this will most likely get your IP banned from Youtube.')
53 q = input('\nContinue? Abort? (y/a): ').lower()
54 if q == 'y' or q == 'c' or q == 'yes':
55 pass
56 elif q == 'a' or q == 'n' or q == 'no':
57 print('Aborting.')
58 return
59
60 print('Collecting information about videos..')
61 vid = self.yt.get_playlist_items(playlist_id=uploads, count=None)
62 for _video in list(vid.items):
63 _json = _video.to_dict()
64 name = _json['snippet']['title']
65 if name in self.already:
66 break
67 self.already.append(name)
68 url = 'https://www.youtube.com/watch?v={}'.format(_json['snippet']['resourceId']['videoId'])
69 self.videourls.append(url)
70 self.download(self.videourls)
71
72 def download(self, _list):
73 '''Downloads the set of videos in the best possible quality'''
74 print('Downloading files in progress..')
75 for i in self.videourls:
76 with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
77 ydl.download([i])
78 print('unable to download {}'.format(i))
79
80 def __del__(self):
81 '''clean up partially downloaded files'''
82 os.system('rm -f $(echo $PWD/*.part)') # Remove partial downloads.
83
84if __name__ == "__main__":
85 x = Download()
86 try:
87 x.run()
88 except KeyboardInterrupt:
89 print('Aborting.')
90 except:
91 print('Something went wrong! Wrong api key?')
92 pass