-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (65 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
80 lines (65 loc) · 2.7 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
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
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, TIT2, TIT3, TALB, TPE1, TRCK, TYER
from shazamio import Shazam
import urllib.request
import mutagen.id3
import glob
import numpy as np
import os
import json
import asyncio
def removeSpecialChars(texto):
caracteres_especiales=['@','#','$','*','&',':','\n', '/', '\\','?','(',')','^','\"']
for caracter in caracteres_especiales:
if( caracter == '&'):
texto = texto.replace(caracter,",")
else:
texto=texto.replace(caracter,"")
return texto
def get_value_of(key, jsonObject, sanitize = True):
for attribute, value in jsonObject.items():
if( attribute == key):
if( sanitize ):
return removeSpecialChars( str.title(value) )
else:
return value
def getMetadata(key, jsonDocument):
for jsonObject in jsonDocument:
if( jsonObject["title"] == key):
return jsonObject["text"]
async def main():
shazam = Shazam()
directorio_de_musica = "C:\\Users\\bayro\\Music\\PENDIENTE"
for nombre_del_archivo in os.listdir(directorio_de_musica):
archivo = os.path.join(directorio_de_musica, nombre_del_archivo)
extension = os.path.splitext(nombre_del_archivo)[1]
if( os.path.isfile(archivo) ):
if( extension == ".mp3" ):
print("Reading ... " + archivo)
try:
jsonSongData = await shazam.recognize_song(archivo)
titulo_de_la_cancion = get_value_of("title", jsonSongData["track"])
artista_de_la_cancion = get_value_of("subtitle", jsonSongData["track"])
foto_del_album = get_value_of("coverart", jsonSongData["track"]["images"], False)
# sAlbum = getValue("Album", jsonSongData["track"]["sections"][0]["metadata"])
# sYear = getValue("Realeased", jsonSongData["track"]["sections"][0]["metadata"])
comentario = "www.itm-developers.com"
nuevo_nombre_del_archivo = artista_de_la_cancion +" - "+ titulo_de_la_cancion + ".mp3"
mp3file = MP3(archivo, ID3=EasyID3)
# mp3file['album'] = "album editado"
mp3file['artist'] = artista_de_la_cancion
# mp3file['year'] = 2000
mp3file['title'] = titulo_de_la_cancion
mp3file.save()
urllib.request.urlretrieve(foto_del_album, os.path.join(directorio_de_musica, artista_de_la_cancion +" - "+ titulo_de_la_cancion + os.path.splitext(foto_del_album)[1]))
os.rename(archivo, os.path.join(directorio_de_musica, nuevo_nombre_del_archivo))
except KeyError:
print("SHAZAM NO pudo reconocer el archivo: ", nombre_del_archivo)
except ConnectionResetError:
print("Ocurrio un Error de Conexion. Omitiendo Archivo: ", nombre_del_archivo)
continue
except FileExistsError:
os.remove(archivo)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())