· 5 years ago · Jul 09, 2020, 07:10 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='<YOUTUBE 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('This channel contains over 100 videos. This will most likely get you 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('Stopping.')
58 return
59 print('Collecting information about videos..')
60 vid = self.yt.get_playlist_items(playlist_id=uploads, count=None)
61 for _video in list(vid.items):
62 _json = _video.to_dict()
63 name = _json['snippet']['title']
64 if name in self.already:
65 break
66 self.already.append(name)
67 url = 'https://www.youtube.com/watch?v={}'.format(_json['snippet']['resourceId']['videoId'])
68 self.videourls.append(url)
69 self.download(self.videourls)
70
71
72
73 def download(self, _list):
74 '''Downloads the set of videos in the best possible quality'''
75 print('Downloading files in progress..')
76 for i in self.videourls:
77 with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
78 ydl.download([i])
79 print('unable to download {}'.format(i))
80
81 def __del__(self):
82 '''clean up partially downloaded files'''
83 os.system('rm -f $(echo $PWD/*.part)') # Remove partial downloads.
84
85if __name__ == "__main__":
86 x = Download()
87 try:
88 x.run()
89 except KeyboardInterrupt:
90 print('Aborting process. Cleaning up.')
91 except:
92 print('Something went wrong! Wrong api key?')
93 pass