-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicjourney.py
134 lines (99 loc) · 5.4 KB
/
musicjourney.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
# -*- coding: utf-8 -*-
################################################################################
# #
# Music Journey #
# #
# Author: Marcos Romero #
# Created on: 6th April 2019 #
# #
# This is a very simple python3 code to download CSV-Spotify-playlists. #
# You need eyed3 package and also youtube-dl. Just that!
# #
# To run this, simply launch your terminal, cd to this file folder and #
# then run:
# $ python3 magicjourney.py --list [your playlist csv] #
# #
################################################################################
################################################################################
# Loading packages and libraries ###############################################
import sys # default
import csv # default
import urllib # default
import urllib.request # default
import re # default
import subprocess # default
import os # default
import argparse # default
import eyed3 # pip3 install eyed3, it's needed to tag downloaded songs
FNULL = open(os.devnull, 'w') # in order to youtube-dl not printing shit
################################################################################
################################################################################
# Functions ####################################################################
def SearchYouTube(X): # --------------------------------------------------------
query = urllib.parse.urlencode({"search_query": X})
content = urllib.request.urlopen("http://www.youtube.com/results?" + query)
results = re.findall(r'href=\"\/watch\?v=(.{11})', content.read().decode())
Y = "http://www.youtube.com/watch?v=" + results[0]
return Y
def Download(Y): # -------------------------------------------------------------
# I do need youtube-dl installed to work
order = 'youtube-dl -x --audio-format "mp3" -o "newsong.%(ext)s" ' + Y
subprocess.call([order], shell=True, stdout=FNULL, stderr=subprocess.STDOUT)
def Tagger(Ti, Ar, Al, Nu): # --------------------------------------------------
file = eyed3.load('newsong.mp3')
file.tag.artist = Ar
file.tag.album = Al
file.tag.title = Ti
file.tag.track_num = Nu
# --> year when we know how to export it (exportify doesn't do this stuff)
file.tag.save()
filename = "{0} - {1}.mp3".format(file.tag.artist, file.tag.title)
path = os.path.join(os.getenv("HOME"),"MusicJourney")
#os.rename('newsong.mp3', filename)
if not os.path.isdir(path):
cmd = "mkdir -p " + path
print(" Creating directory:",path + ".")
subprocess.call([cmd], shell=True, stdout=FNULL, stderr=subprocess.STDOUT)
if os.path.exists(os.path.join(path,filename)):
print(" This song was downloaded before.")
else:
cmd = 'mv "newsong.mp3" ' + os.path.join(path,'"'+filename+'"')
subprocess.call([cmd], shell=True, stdout=FNULL, stderr=subprocess.STDOUT)
return filename
def DownLink(csvname): # -------------------------------------------------------
# Get file
csvname = csvname.split(".")[0]
csvfile = csv.reader(open(csvname + ".csv", "r"))
data = list(csvfile)
songs = len(data)
print('Playlist ' + csvname + ' correctly loaded.')
print('Start downloading...\n')
# Loop over songs
for l in range(1, songs, 1):
# Get info
ti = data[l][1]; ar = data[l][2]; al = data[l][3]; n = int(data[l][5])
# String to search at YouTube
string = data[l][1] + ' ' + data[l][2]
print(' # ' + ti +' - '+ar)
try:
# Search song's video
songurl = SearchYouTube(string)
# Download song
Download(songurl)
# Tag it!
filename = Tagger(ti, ar, al, n)
#print(' Success!')
except:
print(' An error occurred :( sorry.')
print('\nPlaylist ' + csvname + ' finished.')
print('Songs stored at:',os.path.join(os.getenv("HOME"),"MusicJourney"),'.')
################################################################################
################################################################################
# Command line ArgumentParser ##################################################
help = ["Magic Journey: a YouTube Spotify playlist downloader",
"CSV file with songs to download"]
parser = argparse.ArgumentParser(description=help[0])
parser.add_argument('--list', default='', type=str, help=help[1])
args = parser.parse_args()
DownLink(args.list)
################################################################################