-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeautify.py
More file actions
42 lines (38 loc) · 1.67 KB
/
Copy pathbeautify.py
File metadata and controls
42 lines (38 loc) · 1.67 KB
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
from PIL import Image
import requests
import ast
import subprocess
import glob
import eyed3
import StringIO
import os
# Spotify Web API Endpoints
search_endpoint = "https://api.spotify.com/v1/search/"
album_endpoint = "https://api.spotify.com/v1/albums/"
song_list = glob.glob("*.mp3")
for song in song_list:
command = ["echoprint-codegen", song, "10", "50"]
output,error = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if not error:
# Parse JSON output of echoprint (as string) to dictionary
output_json = ast.literal_eval(output)[0]
r = requests.get(search_endpoint, params={'q': output_json['metadata']['title'], 'type': 'track'})
album_art = r.json()
album = requests.get(album_endpoint+""+album_art['tracks']['items'][len(album_art['tracks']['items'])-1]['album']['id'])
r = requests.get(album_art['tracks']['items'][len(album_art['tracks']['items'])-1]['album']['images'][0]['url'])
buff = StringIO.StringIO()
buff.write(r.content)
buff.seek(0)
imagedata = buff.getvalue()
# Open and link mp3 file with eyed3
eyeTag = eyed3.load(song)
eyeTag.tag.genre = output_json['metadata']['genre'].decode('unicode-escape')
eyeTag.tag.artist = output_json['metadata']['artist'].decode('unicode-escape')
eyeTag.tag.album = album.json()['name'].decode('unicode-escape')
eyeTag.tag.title = output_json['metadata']['title'].decode('unicode-escape')
eyeTag.tag.images.set(3,imagedata,"image/jpeg",u'Description')
eyeTag.tag.save()
filename, extension = os.path.splitext(song)
os.rename(song, output_json['metadata']['title'] + extension)
else:
print(error)