-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
173 lines (160 loc) · 4.98 KB
/
misc.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from change import *
from common import *
from covers import *
from audible import *
from pull import *
from images import *
from organize import *
from splitter import *
def remove_copyright(text):
if '©' in text:
text = text.split('©')[0].rstrip(" ,/")
if '©' in text:
text = text.split('©')[0].rstrip(" ,/")
if '(c)' in text:
text = text.split('(c)')[0].rstrip(" ,/")
return text
def remove_all_copyright_symbols(dirs):
for directory in dirs:
allmp3s = get_mp3_files(directory)
for mp3file in allmp3s:
tags = mutagen.File(mp3file, easy=True)
changed = False
if 'artist' in tags.keys():
tags['artist'] = remove_copyright(tags['artist'][0])
changed = True
if 'composer' in tags.keys():
tags['composer'] = remove_copyright(tags['composer'][0])
changed = True
if changed:
tags.save()
print(f"- Updated {directory}")
def check_if_books_exists(olddir=MAIN, newdir=NEW):
books = get_leaf_dirs(newdir)
duplicates = []
for book in books[:]:
relpath = os.path.relpath(book, start=newdir)
checkpath = os.path.join(olddir, relpath)
print(relpath)
if os.path.exists(checkpath):
print(" - duplicate")
books.remove(book)
duplicates.append(relpath)
else:
print(" - new")
return {'books': books, 'duplicates': duplicates}
def remove_after_parens(dirs):
for directory in dirs:
allmp3s = get_mp3_files(directory)
for mp3file in allmp3s:
tags = mutagen.File(mp3file, easy=True)
changed = False
title = tags['album'][0]
if '(' in title:
tags['album'] = title.split('(')[0]
tags.save()
print(f"- Updated {directory}")
def set_initial_tags(dirs):
for bookdir in dirs:
mp3files = [os.path.join(bookdir, i)
for i in os.listdir(bookdir) if i[-4:]=='.mp3']
for mp3file in mp3files:
set_tags(os.path.join(bookdir,mp3file))
def find_dateless(dirs=MAIN):
dateless = []
books = get_leaf_dirs(MAIN)
for book in books:
try:
mp3 = get_single_mp3(book)
tags = mutagen.File(mp3)
if 'TDRC' not in tags.keys():
dateless.append(book)
except TypeError as e:
print(e)
print(book)
print(mp3)
return dateless
def find_shortdates(dirs=MAIN):
bad_dates = []
books = get_leaf_dirs(MAIN)
for book in books:
try:
mp3 = get_single_mp3(book)
tags = mutagen.File(mp3, easy=True)
date = tags['date'][0]
if len(date) < 8:
bad_dates.append({'dir':book, 'date':date})
except TypeError as e:
print(e)
print(book)
print(mp3)
return bad_dates
def fix_shortdates(dirs=MAIN):
dateless = []
books = get_leaf_dirs(MAIN)
for book in books:
try:
mp3s = get_mp3_files(book)
for mp3 in mp3s:
tags = mutagen.File(mp3, easy=True)
try:
date = tags['date'][0]
except KeyError:
dateless.append(mp3)
continue
if len(date) == 6:
tags['date'] = date + '01'
tags.save()
elif len(date) == 4:
tags['date'] = date + '0101'
tags.save()
except:
print(mp3)
def fix_dateless(dirs=MAIN,dateless=None):
nomatch = []
nodate =[]
if dateless is None:
dateless = find_dateless(dirs=dirs)
for d in dateless:
mp3 = get_mp3_files(d)[0]
tags = mutagen.File(mp3, easy=True)
try:
result = match(goodreads_client,tags)
book = result['result']
except:
nomatch.append(d)
continue
pubdate = get_pubdate(book)
if pubdate and pubdate != (None,None,None):
tags['date'] = pubdate
tags.save()
else:
nodate.append(d)
return {'no date': nodate,
'no match': nomatch}
def get_pubdate(book):
try:
year = book.work['original_publication_year']['#text']
except KeyError:
year = None
try:
month = book.work['original_publication_month']['#text']
except KeyError:
month = None
try:
day = book.work['original_publication_day']['#text']
except KeyError:
day = None
if year and month and day:
pubdate = f'{year}{month:0>2}{day:0>2}'
elif year and month:
pubdate = f'{year}{month:0>2}01'
else:
pubdate = year
if pubdate is None:
month, day, year = book.publication_date
try:
pubdate = f'{year}{month:0>2}{day:0>2}'
except:
return
return pubdate