-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorphaned_files
executable file
·140 lines (107 loc) · 3.59 KB
/
orphaned_files
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
#!/usr/bin/env python3
# Copyright 2019 Alex K (wtwf.com)
# PYTHON_ARGCOMPLETE_OK
"""Program to do the thing.
"""
__author__ = "wtwf.com (Alex K)"
import argparse
import collections
import html
import logging
import os
import urllib.parse
import argcomplete
def main():
"""Parse args and do the thing."""
logging.basicConfig()
parser = argparse.ArgumentParser(description="Program to do the thing.")
parser.add_argument(
"--music-folder", help="Location of iTunes media", default="/Volumes/d8/iTunes"
)
parser.add_argument("-v", "--verbose", help="Log verbosely", action="store_true")
parser.add_argument("-d", "--debug", help="Log debug messages", action="store_true")
parser.add_argument(
"--no-get-data",
action="store_false",
default=True,
dest="get_data",
help="Collect the data first",
)
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.info("Log")
if args.get_data:
get_data(args)
find_orphaned()
def get_data(args):
print("Getting Data from iTunes Music Library.xml")
library = os.path.expanduser("~/Music/iTunes/iTunes Music Library.xml")
os.system(
f"egrep -o 'file:///[^<]+' '{library}' "
"> /tmp/itunesmusiclibrary.locations.urlencoded.txt"
)
print("Finding Files")
os.system(f"(cd {args.music_folder}; find . -type f > /tmp/itunes_real_files.txt)")
def load_itdb_files():
items = []
prefix_length = len("file:///Volumes/D8/iTunes/")
with open("/tmp/itunesmusiclibrary.locations.urlencoded.txt") as infile:
for line in infile:
item = html.unescape(
urllib.parse.unquote(line[prefix_length:].strip())
).lower()
if item:
items.append("./" + item)
return items
def read_real_files():
items = []
with open("/tmp/itunes_real_files.txt") as infile:
for line in infile:
items.append(line.strip().lower())
return items
def find_orphaned():
real_files = read_real_files()
itdb_files = load_itdb_files()
real_files = [
fil
for fil in real_files
if fil[-10:] != "/.ds_store"
and fil[:39] != "./automatically add to itunes.localized"
and fil[:8] != "./tones/"
and fil[:22] != "./mobile applications/"
and fil[:27] != "./.itunes preferences.plist"
]
real_files_dict = dict.fromkeys(real_files, True)
missing = []
for fil in itdb_files:
if fil == "./":
print("FOUND", fil)
if fil in real_files_dict:
real_files_dict[fil] = False
else:
missing.append(fil)
print(f"Missing count: {len(missing)}")
orphans = [key for key, value in real_files_dict.items() if value]
orphan_dirs = collections.defaultdict(int)
for orphan in orphans:
orphan_dirs[os.path.dirname(orphan)] += 1
print(f"Orphans count: {len(orphans)}")
write_file("/tmp/orphans.txt", orphans)
write_file("/tmp/missing.txt", missing)
orphan_dirs = {
k: v for k, v in sorted(orphan_dirs.items(), key=lambda item: -1 * item[1])
}
for dirname, count in orphan_dirs.items():
if count > 3:
print(f"{count:4d} {dirname}")
def write_file(filename, items):
print(f"writing: {filename}")
with open(filename, "w") as outfile:
for item in sorted(items):
print(item, file=outfile)
if __name__ == "__main__":
main()