-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
91 lines (79 loc) · 2.47 KB
/
Copy pathindex.py
File metadata and controls
91 lines (79 loc) · 2.47 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
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
import pickle
import os
import time
from mutagen.easyid3 import EasyID3
from libpytunes import Library
import argparse
def update(song):
"""Update song name via it's ID3 tag."""
try:
audio = EasyID3('/' + song.location)
audio['title'] = song.name.replace(offence, replace_offence_with)
audio.save()
print('Saved as ' + audio['title'][0])
except Exception:
print('File not found. Try clearing your cache')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Searches your iTunes library and replaces an offending string'
)
parser.add_argument(
'-i',
metavar='iTunes Library',
type=str,
required=True,
help='Absolute path to iTunes Library XML file'
)
parser.add_argument(
'-s',
metavar='Search term',
type=str,
required=True,
help='The string you are searching for'
)
parser.add_argument(
'-r',
metavar='Replace with',
type=str,
required=True,
help='What you want to replace it with'
)
parser.add_argument(
'--nocache',
action='store_true',
help='Disable caching with pickle'
)
parser.add_argument(
'--noconfirm',
action='store_true',
help='Don\'t confirm changes, just apply to all results'
)
args = parser.parse_args()
itunes_library = os.path.realpath(args.i)
offence = args.s
replace_offence_with = args.r
confirm_message = 'Replace "' + offence + '" with ' + '"' + replace_offence_with + '" [y/n] ? '
found = False
lib_path = itunes_library
pickle_file = 'itl.p'
expiry = 0 if args.nocache else (60 * 60)
epoch_time = int(time.time())
if not os.path.isfile(pickle_file) or os.path.getmtime(pickle_file) + expiry < epoch_time:
itl_source = Library(lib_path)
pickle.dump(itl_source, open(pickle_file, 'wb'))
itl = pickle.load(open(pickle_file, 'rb'))
for id, song in itl.songs.items():
if song.name.find(offence) != -1:
found = True
print('/' + song.location)
if args.noconfirm:
update(song)
else:
confirmation = input(confirm_message)
if confirmation == 'y':
update(song)
else:
print('Song name not changed')
if found is False:
print('No results found')