-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundcloud-to-iTunes.py
143 lines (120 loc) · 4.65 KB
/
soundcloud-to-iTunes.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
try:
import requests
import eyed3
import os
import platform
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
except:
raise Exception("Some required packages are not installed. Use 'pip install -r requirements.txt' to install required packages, then run this pogram again")
def metaDataFromUrl(url):
arr = url.split('/')[3:]
obj = {'artist': '', 'title': '', 'album': ''}
title = arr[1]
title = title.split('?')[0]
title = title.split('-')[1:]
title = " ".join(title)
obj['artist'] = arr[0].replace('-', ' ').title()
obj['title'] = title.title()
try:
obj['album'] = arr[-1].replace('-', ' ').title()
except:
print('Could not find album')
return obj
def type(path, text, driver): # wait for element to load then type
try:
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, path)))
driver.find_element_by_xpath(path).send_keys(text)
except TimeoutException:
print("Failed to load", path)
def init(driver):
link = input("Paste link: ")
driver.get(link)
links = []
try:
list = driver.find_element_by_xpath(
'//*[@id="content"]/div/div[3]/div[1]/div/div[2]/div[2]/div/div[3]/div/ul').find_elements_by_tag_name('li')
for item in list:
items = item.find_elements_by_tag_name('a')
for i in items:
links.append(i.get_attribute('href'))
while("" in links):
links.remove("")
print("found " + str(len(links)) + " songs")
for i in links:
getSong(i)
except:
getSong(link, driver)
driver.quit()
moveSongs()
def getSong(link, driver):
print("Getting " + link)
driver.get('https://www.klickaud.co/')
type('//*[@id="header"]/div/div[1]/div[1]/form/input[1]', link, driver)
driver.find_element_by_xpath('//*[@id="btn"]').click()
print("Found song")
downloadLink = driver.find_element_by_xpath(
'//*[@id="header"]/div/div/div[1]/div/div[3]/table/tbody/tr/td[2]/a').get_attribute("href")
r = requests.get(downloadLink)
songData = metaDataFromUrl(link)
fileName = '%s.mp3' % (songData['title'])
open('songs/' + fileName, 'wb').write(r.content)
print("Downloaded song")
if not os.path.exists("artwork/art.jpg"):
artLink = driver.find_element_by_xpath(
'//*[@id="header"]/div/div/div[1]/div/table/tbody/tr/td[1]/img').get_attribute('src')
r = requests.get(artLink)
open('artwork/art.jpg', 'wb').write(r.content)
print('Downloaded art')
# apply meta data tags
mp3 = eyed3.load('songs/'+fileName)
if (mp3.tag == None):
mp3.initTag()
mp3.tag.title = songData['title']
mp3.tag.artist = songData['artist']
if songData != '':
mp3.tag.album = songData['album']
mp3.tag.images.set(3, open('artwork/art.jpg', 'rb').read(), 'image/jpeg')
mp3.tag.save(version=eyed3.id3.ID3_V2_3)
print('saved tags')
def moveSongs():
system = platform.system()
path = ""
endpoint = ""
if system == "Darwin":
path = os.path.expanduser("~") + '/Music'
if(os.path.isdir(path + '/Music')):
endpoint = path + '/Music/Media.localized/Automatically Add to Music.localized'
elif os.path.isdir(path + '/iTunes'):
endpoint = path + '/iTunes/iTunes Media/Automatically Add to iTunes.localized'
print("moving songs to " + endpoint)
elif system == "Windows":
path = os.path.expanduser("~")
endpoint = path + '\Music\iTunes\iTunes Media\Automatically Add to iTunes'
print(endpoint)
elif system == "Linux":
pass
else:
print('Could not find operating system')
return
songs = os.listdir("songs")
for song in songs:
if song != '.gitignore':
os.rename("songs/" + song, endpoint + "/" + song)
print("Done! All songs have been added to library")
if __name__ == '__main__':
pathToDriver = ""
if platform.system() == "Darwin":
pathToDriver = "drivers/chromedriver"
elif platform.system() == "Windows":
pathToDriver = "drivers/chromedriver.exe"
options = Options()
options.add_argument("--headless") # makes chrome window not show
options.add_argument("log-level=3") # surpresses warnings
driver = webdriver.Chrome(pathToDriver, options=options)
init(driver)