-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy patheditor.py
executable file
·117 lines (92 loc) · 3.07 KB
/
editor.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
#!/usr/bin/python3
import re, os
import shutil
import genius
#used for mp3 ID3 tagging
from mutagen.id3._frames import TIT2, TALB, TPE1, USLT
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
#used for web scraping
import urllib.request
class TagEditor(object):
@staticmethod
def getImageFromSpotify(url, name):
if len(url):
urllib.request.urlretrieve(url, name)
else:
cachepath = os.getcwd() + '/cache'
datapath = os.getcwd() + '/Data'
os.system(f'cp {datapath}/temp.png {name}')
@staticmethod
def getTags():
pass
@staticmethod
def setTags(data):
'''
Adding taggs to mp3 file
:param filename: name of file
:param data: dictionary with song data
structure of dictionary:
{
'uri' : 'str', # Song URI id
'name':'str', # Name of song
'artist':'tuple', # List of artists
'album':'str', # Name of album
'image':'str', # Url for image from Sporify
}
as example:
{ 'uri' : '4g5MorMCNI2aOwEBSov4RT',
'name': 'and then, it swallowed me',
'artist': ['Nohidea', 'killedmyself', 'Delta Sleep'],
'album': 'and then, it swallowed me',
'image': 'https://i.scdn.co/image/033879df...f2ddb66'
}
:return: boolean, in case of some errors - False, else True
'''
if data:
#download image
TagEditor.getImageFromSpotify(data['image'], f"cache/{data['uri']}/{data['uri']}.png")
audio = MP3(
f"cache/{data['uri']}/{data['uri']}.mp3",
ID3=ID3
)
#handle tag errors
try:
audio.add_tags()
except error:
pass
#add a picture
audio.tags.add(APIC(3,
'image/jpeg',
3,
'Front cover',
open(f"cache/{data['uri']}/{data['uri']}.png", 'rb').read())
)
#add song name
audio.tags.add(TIT2(
encoding=3,
text=(data['name']))
)
#add song album
audio.tags.add(TALB(
encoding=3,
text=(data['album']))
)
#add song artist
audio.tags.add(TPE1(
encoding=3,
text=(data['artist'][0]))
)
#add song artist
audio.tags.add(USLT(
encoding=3,
lang=u'eng',
desc=u'desc',
text=genius.getLyrics(data['artist'][0],data['name']))
)
#save result
audio.save()
ID3(f"cache/{data['uri']}/{data['uri']}.mp3").save(v2_version=3)
return True
else:
return False