-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutubeDownloader.py
121 lines (95 loc) · 3.79 KB
/
youtubeDownloader.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
import os
import argparse
import subprocess, shlex
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
DEVELOPER_KEY = os.environ['DEV_KEY']
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = 'v3'
YOUTUBE_DL_URL="https://www.youtube.com/watch?v="
### BEGIN - AUX FUNCTIONS ###
def getUserRegion():
"""
Returns your region, we use the value when searching videos on youtube.
Default value is ES
"""
someRegions = {
"Madrid" : "ES",
"Paris" : "FR",
"London" : "GB",
"Berlin" : "DE",
"Rome" : "IT",
"America" : "US"
}
zone = subprocess.getoutput('timedatectl show --va -p Timezone | cut -d\'/\' -f2')
try:
return someRegions[zone]
except KeyError:
return "ES"
def changeName():
"""
Allows to give a new name to the video you are about to download.
"""
ans = str(input("Do you want to rename the title? [Y/n]: "))
if(ans in ["", "Y", "y"]):
newName = str(input("New title: "))
newName += ".%(ext)s"
return newName
# Format for youtube-dl, the video/audio will have the original yt name
return "%(title)s-%(id)s.%(ext)s"
### END - AUX FUNCTIONS ###
def yt_search(args):
"""
Calls Youtube API and gets a list of videos.
"""
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
yt_response = youtube.search().list(
q=args.SEARCH,
part='id, snippet',
maxResults=args.max_results,
order="relevance",
regionCode=getUserRegion()
).execute()
videos = []
videosId = []
videosChannel = []
# We "sort" the info in 3 diff lists
for yt_result in yt_response.get('items', []):
if(yt_result['id']['kind'] == "youtube#video" and yt_result['snippet']['liveBroadcastContent'] != "live"):
videos.append('%s ' % (yt_result['snippet']['title'])) # To show the video title
videosId.append('%s ' % (yt_result['id']['videoId'])) # To know the video id you may want to download
videosChannel.append('%s ' % (yt_result['snippet']['channelTitle'])) # To know the channels name
video_selection(videos, videosId, videosChannel, args)
def video_selection(videos, videosId, videosChannel, args):
"""
Shows a list of videos and let's you choose one, change the name and download it.
"""
print("")
for i in range (len(videos)):
print("Video Nº " + "%d" % (i+1) + ": " + videosChannel[i] + " ==> " + videos[i])
print("")
print("\n")
videoNumber = int(input("Choose the video you like to download: ")) - 1
newName = changeName()
if(args.format == "mp3"):
command = "youtube-dl -x --audio-format " + args.format + " -o " + args.folder + newName + " " + YOUTUBE_DL_URL + videosId[videoNumber]
else:
command = "youtube-dl --merge-output-format " + args.format + " -o " + args.folder + newName + " " + YOUTUBE_DL_URL + videosId[videoNumber]
subprocess.call(shlex.split(command))
if __name__ == '__main__':
music_folder = subprocess.getoutput('xdg-user-dir MUSIC') # Get your music folder
parser = argparse.ArgumentParser()
parser.add_argument('--SEARCH', help="Search from term", default="Music")
parser.add_argument('--max-results', help="Max results output", default=7)
parser.add_argument('--folder', help="Full path to downloads folder", default=music_folder)
parser.add_argument('--format', help="mp3 or mp4", default="mp3")
args = parser.parse_args()
if(not args.folder.endswith("/")):
args.folder += "/"
try:
yt_search(args)
except HttpError as e:
print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))
except Exception as e:
print(e)