-
Notifications
You must be signed in to change notification settings - Fork 3
/
songdown.py
executable file
·146 lines (117 loc) · 5.08 KB
/
songdown.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import os, sys, requests, json, subprocess, argparse
DEVELOPER_KEY = 'AIzaSyB3x0d4TgYSnKg128hcBQWoolhX0A9v9NU'
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
names=[]
ids=[]
views=[]
def youtube_search(search_string, RESULTS):
payload = {'q':search_string, 'part':'id,snippet', 'maxResults':str(RESULTS), 'key':DEVELOPER_KEY}
search_request = requests.get('https://www.googleapis.com/youtube/v3/search', params=payload)
search_response = json.loads(search_request.text)
for item in search_response["items"]:
ids.append(item["id"]["videoId"])
names.append(item["snippet"]["title"])
# Add each result to the appropriate list
search_videos = []
search_videos = ",".join(ids)
payload = {'id':search_videos, 'part':'statistics', 'key':DEVELOPER_KEY}
video_request = requests.get('https://www.googleapis.com/youtube/v3/videos', params=payload)
video_response = json.loads(video_request.text)
for item in video_response["items"]:
views.append(item["statistics"]["viewCount"])
for i in xrange(len(names)):
print i, ". ", names[i]
print "Views: ", views[i], "\n"
if __name__ == "__main__":
#Managing config file
filename = os.path.expanduser('~/.songdownrc')
if not os.path.isfile(filename):
with open(filename, 'w') as file:
file.write('VIDEO=false\nDOWN_DIR="~/Music/SongDown/"\nRESULTS=3')
with open(filename, 'r') as file:
for line in file:
keys = line.split('=')
keys[1] = keys[1].strip()
if keys[0] == 'VIDEO':
try:
VIDEO = keys[1] == 'true'
except:
print "Config file error"
elif keys[0] == 'DOWN_DIR':
try:
DOWN_DIR = keys[1].split('"')[1]
except:
print "Config file error"
elif keys[0] == 'RESULTS':
try:
RESULTS = int(keys[1])
except:
print "Config file error"
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--video", help="Download video")
parser.add_argument("-p", "--path", help="Specify download path")
parser.add_argument("-r", "--results", help="Specify number of search results to be shown")
parser.add_argument("-d", "--default", help="Set default path")
parser.add_argument("-c", "--continue", dest="cont", help="Continue failed search/download", action="store_true")
args = parser.parse_args()
print "\nLooking for youtube-dl..."
if not os.system("youtube-dl --version"):
print "\nyoutube-dl found!"
else:
print "\nyoutube-dl missing!"
print "To install youtube-dl you can follow the instructions at\nhttp://www.tecmint.com/install-youtube-dl-command-line-video-download-tool/"
print "\nExiting...\n"
sys.exit()
DOWN_DIR = os.path.expanduser(DOWN_DIR)
# Create download dir if necessary
if not os.path.exists(DOWN_DIR):
os.makedirs(DOWN_DIR)
skip_name = False
skip_select = False
if args.cont:
try:
failsafe = open('/tmp/songdown.fail','r')
for line in failsafe:
line = line.split(" = ")
if line[0] == "SEARCH":
search_string = line[1];
skip_name = True
print "Previous search found: " + search_string
elif line[0] == "VID":
ids = [line[1]]
index = 0
skip_name = True
skip_select = True
print "Previous selected video found: " + ids[index]
failsafe.close()
except:
print "Failure log not found. Continuing normally..."
failsafe = open('/tmp/songdown.fail','w')
if not skip_name:
name = raw_input("\nSongdown started.\n\nEnter name of song - ")
search_string = name.replace(' ','+')
failsafe.write('SEARCH = ' + search_string + '\n')
if not skip_select:
print "\nRetrieving results from youtube...\n"
youtube_search(search_string, RESULTS)
selected = False
while not selected:
index = raw_input("Enter selection number - ")
try:
index = int(index)
print "\nDownloading", names[index]
selected = True
except:
print "\nInvalid selection!\n"
failsafe.write('VID = ' + ids[index] + '\n')
title = '/%' + '(title)s.%' +'(ext)s'
path = DOWN_DIR + title
if VIDEO:
subprocess.call(['youtube-dl', '-o', path, '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio', '--merge-output-format', 'mp4', 'https://www.youtube.com/watch?v='+ids[index]])
else:
subprocess.call(['youtube-dl', '-o', path, '--extract-audio', '--audio-format', 'mp3', '--audio-quality', '0', 'https://www.youtube.com/watch?v='+ids[index]])
failsafe.close()
os.remove('/tmp/songdown.fail')
print "\nSongDown finished. Exiting...\n"