-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoMusicDownloader.py
113 lines (100 loc) · 4.06 KB
/
autoMusicDownloader.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
#!/usr/bin/env python
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# For calling instantMusic
from subprocess import call, check_call
from subprocess import Popen
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
def get_credentials():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
def open_downloaded_songs_list(filename):
"""
Returns the file object of the downloaded songs list.
If file doesn't exist, creates one.
"""
open(filename, "a").close()
fp = open(filename, "r")
return fp
def call_instant_music(song, destDir):
"""
Makes a system call to instantmusic to download the given song.
"""
print(50*"=")
print("Trying to download {}".format(song))
print(50*"=")
try:
retVal = call(["instantmusic", "-p", "-s", "{}".format(song)], cwd="./"+destDir)
except ValueError:
print("Invalid choice.")
return retVal
def main():
service = build('sheets', 'v4', credentials=get_credentials())
# Insert your spread sheet ID and the desired data range.
spreadsheetId = '<sheed_id_here>'
rangeName = 'Sheet1!A:N'
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheetId, range=rangeName).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
header = values[0]
for colIndex in range(9):
# Create a separate directory for each category
destDir = "{}".format(header[colIndex])
filename = "Downloaded Songs List.txt"
try:
check_call(["mkdir", destDir])
except:
print("Directory already exists.")
for row in values[1:]:
if (len(row) >= colIndex+1):
song = row[colIndex].strip()
if song:
fp = open_downloaded_songs_list(destDir+"/"+filename)
downloadedSongs = fp.readlines()
fp.close()
songExists = False
for dlSong in downloadedSongs:
dlSong = dlSong.strip()
# TODO: Need fuzzy search here.
if (dlSong.find(song) == 0):
songExists = True
break
if not songExists:
retVal = call_instant_music(song, destDir)
if (retVal==0):
try:
fp = open(destDir+"/"+filename, "a")
except:
print("Unable to open "+destDir+"/"+filename)
return
fp.write(song)
fp.close()
else:
print("Skipping. The song already exists locally.")
if __name__ == '__main__':
main()