-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tagger.py
119 lines (91 loc) · 3.99 KB
/
Tagger.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
# Python system libraries
import sys
import os
import urllib.request
import pprint as pp
from shutil import move
# Music database API
import pygn
# Metadata read/write library
from mutagen.id3 import ID3, APIC, error, TRCK, TIT2, TPE1, TALB, TDRC, TCON
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
# API keys
clientID = '420737233-ABB8789B14C3A2BAE6730A0EEE59B3D6'
userID = '94671866235528590-B0AC2AB3FE6629CD0B956F996F3D926A'
def read_files_in_current_directory():
# Return list of MP3 Files in current directory
return [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(".mp3")]
def get_song_metadata(song_name, artist_name):
# Query database and get song metadata as a dict
return pygn.search(clientID=clientID, userID=userID, artist=artist_name, track=song_name)
def write_metadata_to_file(metadict, filename):
# Write tags to MP3 file
mp3file = MP3(filename, ID3=EasyID3)
mp3file['Title'] = metadict["track_title"]
mp3file['Artist']= metadict["album_artist_name"]
mp3file['Album']= metadict["album_title"]
mp3file['TrackNumber'] = metadict["track_number"]
mp3file['Genre'] = metadict['genre']['1']['TEXT'] # TODO: Write multiple genres
mp3file['Date'] = metadict["album_year"]
mp3file.save(filename, v2_version=3, v1=2)
# Grab Album Art Image from online
urllib.request.urlretrieve(metadict["album_art_url"], "cover.jpg")
imagedata = open("cover.jpg", 'rb').read()
pic = APIC(3, 'image/jpeg', 3, 'Front Cover', imagedata)
# Save the artwork to the file
audio = MP3(filename)
audio.tags.add(pic)
audio.save(filename, v2_version=3, v1=2)
# Remove the image from the directory
os.remove("cover.jpg")
def change_file_name(metadata, song):
os.rename(song, metadata['track_title'] + " - " + metadata['album_artist_name'] + ".mp3")
def generate_playlists(playlist_specifier):
# Read in MP3 files in current directory
songs = read_files_in_current_directory()
for filename in songs:
audio = MP3(filename, ID3=EasyID3)
# Read proper metadata
try:
specifier = audio[playlist_specifier][0] # audio is a dict of lists
except:
specifier = "Unknown"
# Create directory if it doesn't exist
if not os.path.exists(specifier):
os.makedirs(specifier)
# The folder exists in the current directory.
# Move the MP3 file into that folder.
source = os.path.abspath(filename)
destination = os.getcwd() + "/" + specifier + "/" + filename
move(source, destination)
def read_metadata_from_file(filename):
audio = MP3(filename, ID3=EasyID3)
pp.pprint(audio)
def main_run(song, track_name, artist_name):
song_metadata = get_song_metadata(track_name, artist_name)
write_metadata_to_file(song_metadata, song)
change_file_name(song_metadata, song)
# Main Functionality
# songs = read_files_in_current_directory()
# for song in songs:
# print("================================")
# print(" METADATA BEFORE ")
# print("================================")
# read_metadata_from_file(song)
# track_name = input(song + ": Name - ")
# artist_name = input(song + ": Artist - ")
# song_metadata = get_song_metadata(track_name, artist_name)
# pp.pprint(song_metadata)
# ok = input("Is this metadata ok? ")
# if (ok == 'Y'):
# write_metadata_to_file(song_metadata, song)
# print("================================")
# print(" METADATA AFTER ")
# print("================================")
# read_metadata_from_file(song)
# change_file_name(song_metadata, song)
# # Sort into playlists
# # Possible options: 'Genre', 'Artist', 'Album', 'Date'
# playlist_option = input("How would you like to sort your music into playlists?\nEnter Genre, Artist, Album, or Date: ")
# generate_playlists(playlist_option)