-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimestuff.py
172 lines (154 loc) · 5.45 KB
/
mimestuff.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
import os
import magic
import errno
import shutil
import mutagen
from collections import defaultdict
from tagger import tagfile
from common import copy, JUMBLED_JUNK_DIR, NEW
MIME_EXT = {
'application/octet-stream': '.mp3',
'application/zip': '.zip',
'audio/mpeg': '.mp3',
'audio/x-m4a': '.m4a',
'audio/x-mp4a-latm': '.m4a',
'audio/x-wav': '.wav',
'video/3gpp': '.3gp',
'video/mp4': '.mp4',
'video/quicktime': '.mov',
"application/epub+zip" : '.epub',
'text/xml' : '.xml',
'text/vcard': '.vcard',
'text/x-python': '.py',
'text/html': '.html',
'text/x-asm': '.asm',
'text/plain': '.txt',
'image/png': '.png',
'image/x-ms-bmp': '.bmp',
'image/x-tga': '.tga',
'image/jpeg': '.jpeg',
'image/tiff': '.tiff',
'image/gif': '.gif',
'image/x-icon': '.ico',
'video/quicktime': '.mov',
'video/3gpp': '.3gp',
'video/mp4': '.mp4',
'inode/x-empty': '',
'application/x-tplink-bin': '.bin',
'application/vnd.ms-opentype': '.otf',
'application/x-wine-extension-ini': '.ini',
'application/octet-stream': '.a',
'application/json': '.json',
'application/x-dosexec': '.exec',
'application/epub+zip': '.epub',
'application/vnd.lotus-1-2-3': '.123',
'application/x-gzip': '.gzip',
'application/zip': '.zip',
'application/x-sqlite3': '.db',
'application/pdf': '.pdf',
'audio/x-mp4a-latm': '.m4a',
'audio/x-wav': '.wav',
'audio/amr': '.amr',
'audio/x-m4a': '.m4a',
'audio/mpeg': '.mp3'
}
MIME_EXT = defaultdict(lambda: "", MIME_EXT)
DESIRED_TYPES = [
# 'application/octet-stream',
'audio/mpeg',
]
VIDEO_TYPES = [
'video/3gpp',
'video/mp4',
'video/quicktime',
]
EBOOK_TYPES = [
'application/epub+zip',
'application/zip',
''
]
MIME_DEST = '/Volumes/mangomedia/audiobook_processing/by_mime/'
def list_mimetypes(startdir=JUMBLED_JUNK_DIR):
'''Returns a list of the mimetypes of the files in startdir '''
mimetypes = []
for root, dirs, files in os.walk(startdir, followlinks=True):
for name in files:
filepathandname = os.path.join(root, name)
mimetype = magic.from_file(filepathandname, mime=True)
if mimetype not in mimetypes:
mimetypes.append(mimetype)
return mimetypes
def pull_by_mimetype(startdir=JUMBLED_JUNK_DIR, destination=MIME_DEST,
move_without_copying=False):
"""Organize files in directory tree by mimetype"""
if move_without_copying:
move = os.renames
else:
move = copy
for root, dirs, files in os.walk(startdir, followlinks=True):
for name in files:
filepathandname = os.path.join(root, name)
mimetype = magic.from_file(filepathandname, mime=True)
newpathandname = os.path.join(destination, mimetype, name)
ext = MIME_EXT[mimetype]
move(filepathandname, newpathandname)
def copy_desired_filetypes(startdir=JUMBLED_JUNK_DIR, destination=MIME_DEST,
desired_types=DESIRED_TYPES, move_without_copying=False):
if move_without_copying:
move = os.renames
else:
move = copy
for root, dirs, files in os.walk(startdir, followlinks=True):
for name in files:
filepathandname = os.path.join(root, name)
mimetype = magic.from_file(filepathandname, mime=True)
if mimetype in desired_types:
ext = MIME_EXT[mimetype]
newname = name + ext
newpathandname = os.path.join(destination, mimetype, newname)
move(filepathandname, newpathandname)
def add_mp3_extension(maindir):
for root, dirs, files in os.walk(maindir):
for name in files:
filepathandname = os.path.join(root, name)
if os.path.splitext(name)[1] == '':
os.rename(filepathandname, filepathandname + '.mp3')
def add_extensions(maindir=MIME_DEST):
for root, dirs, files in os.walk(maindir):
relpath = os.path.relpath(root, start=maindir)
mimetype = relpath.strip('/')
print(repr(mimetype))
try:
ext = MIME_EXT[mimetype]
print(ext)
except:
continue
for name in files:
filepathandname = os.path.join(root, name)
if os.path.splitext(name)[1] == '':
os.rename(filepathandname, filepathandname + ext)
def get_ringtones(startdir=NEW, destination=MIME_DEST, copy=False, dryrun=False):
mp3files = []
if copy:
move = copy
else:
move = os.renames
for root, dirs, files in os.walk(startdir, followlinks=True):
for name in files:
icon = '.'
filepathandname = os.path.join(root, name)
tags = mutagen.File(filepathandname, easy=True)
if tags:
try:
if tags['genre'] in (['Ringtone'],['Other']):
newname = tags['title'][0] + '.mp3'
if dryrun:
break
newpathandname = os.path.join(destination, 'ringtones', newname)
mp3files.append(newname)
if not os.path.exists(newpathandname) and newname != '..mp3':
move(filepathandname, newpathandname)
except KeyError:
pass
print('.', end='')
return mp3files