-
Notifications
You must be signed in to change notification settings - Fork 17
/
categories.py
141 lines (133 loc) · 5.48 KB
/
categories.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
#!/usr/bin/env python
import re
import os
import mimetypes
categories = [
'video',
'audio',
'archive',
'document',
'software',
'image',
'other',
]
rar_part_re = re.compile("^\.r[0-9]+$")
def guess(filename):
typ = None
mime = mimetypes.guess_type(filename, strict=False)[0]
if mime:
typ = mime_to_category(mime)
if typ is None:
ext = os.path.splitext(filename)[1]
if ext:
typ = extension_to_category(ext)
return typ
def extension_to_category(ext):
ext = ext.lower()
if ext in {
'.m2ts', '.clpi', '.vob', '.rmvb', '.ifo', '.bup', '.cdg', '.m4v', '.bdmv', '.bik', '.f4v',
'.idx', '.vp6', '.ogm', '.divx', '.mpg', '.avi', '.m2v', '.tp', '.ratdvd', '.iva', '.m2t'
}:
return 'video'
elif ext in {
'.pak', '.arc', '.sub', '.ccd', '.accurip', '.img', '.vghd', '.psarc', '.wv', '.tta',
'.mds', '.gz', '.msu', '.pimx', '.mdf', '.pima', '.package', '.pck', '.cso', '.sfz',
'.wbfs', '.ova', '.xz', '.bz2', '.vpk', '.nrg'
}:
return 'archive'
elif ext in {
'.ncw', '.nki', '.ape', '.mka', '.ac3', '.m4b', '.sb', '.exs', '.tak', '.wem', '.m4r',
'.fsb', '.cue', '.rx2', '.shn', '.sdat', '.nkm', '.aac', '.nmsv', '.at3', '.pcm', '.nkc',
'.swa', '.nkx', '.m4p', '.dtshd', '.ksd', '.dts'
}:
return 'audio'
elif ext in {'.dds', '.tga', '.webp', '.raw', '.abr', '.max', '.wmf', '.xm', '.ipl', '.pam'}:
return 'image'
elif ext in {
'.nfo', '.epub', '.log', '.mobi', '.lit', '.azw3', '.prc', '.asd', '.vtx', '.fb2', '.cas',
'.md', '.ps3'
}:
return 'document'
elif ext in {
'.php', '.lua', '.cmd', '.opa', '.pyd', '.cs', '.strings', '.res', '.properties',
'.manifest', '.nib', '.mo', '.rpyc', '.rpy', '.x32', '.fda', '.mui', '.nds', '.fx', '.prg',
'.rsrc', '.scss', '.dmt', '.catalyst', '.pkg', '.bin', '.so', '.sql', '.man', '.mui',
'.nds', '.qm', '.3ds', '.chd', '.inf'
}:
return 'software'
elif ext in {
'.sldprt', '.url', '.mpls', '.ass', '.dat', '.ini', '.db', '.xrm-ms', '.xxx', '.upk',
'.mst', '.fxp', '.ans', '.opal', '.w3x', '.zdct', '.ff', '.gmp', '.fbl', '.map', '.md5',
'.dcp', '.reg', '.lrtemplate', '.lmk', '.bc!', '.assets', '.poi', '.gp3', '.gp4', '.3dl',
'.toc', '.diz', '.cfg', '.nka', '.smc', '.lim', '.nm2', '.lng', '.amt', '.big', '.paz',
'.h2p', '.ssa', '.szs', '.xnb', '.dwg', '.ide', '.sys', '.index', '.3dc', '.rlf', '.lst',
'.ftr', '.ozf2', '.sxt', '.ipa', '.nes', '.data', '.fxb', '.bndl', '.lyc', '.smarch',
'.bfdca', '.sims3pack', '.fuz', '.fpa', '.fsp', '.sdfdata', '.meta', '.bk2', '.unity3d',
'.nkp', '.dsf', '.loc', '.lnk', '.nksn', '.lzarc', '.mpq', '.plist', '.hdr', '.gmspr',
'.avs', '.rxdata', '.bnk', '.rvdata2', '.sabs', '.pz2', '.w3m', '.bsp', '.msp', '.sse',
'.aep', '.efd', '.ngrr', '.rpym', '.dff', '.obf', '.unr', '.sba', '.ffp', '.nm7', '.rpymc',
'.jcd', '.pkz', '.vdb', '.fxc', '.grir', '.dylib', '.gpx', '.dl_', '.pub', '.txd', '.sgdt',
'.s', '.afpk', '.cmp', '.atw', '.gp5', '.sabl', '.cci', '.smd', '.config', '.mcd', '.prp',
'.ifs', '.dmp', '.pxs', '.icc', '.icns', '.yrdm', '.prt_omn', '.sob', '.rwd', '.sgo',
'.torrent', '.key', '.ttf', '.sig', '.otf', '.m3u8', '.pac', '.npk', '.ph'
}:
return 'other'
elif rar_part_re.match(ext):
return 'archive'
else:
return None
def mime_to_category(mime):
typ, sub_typ = mime.split('/')
sub_typ = sub_typ.lower()
if typ == "video":
return 'video'
elif typ == "audio":
return "audio"
elif typ == "image":
return "image"
elif typ in {"model", "message", "chemical"}:
return 'document'
elif typ == "text":
if sub_typ in {
'vnd.dmclientscript', 'x-c++hdr', 'x-c++src', 'x-chdr', 'x-crontab',
'x-csh', 'x-csrc', 'x-java', 'x-makefile', 'x-moc', 'x-pascal', 'x-pcs-gcd',
'x-perl', 'x-python', 'x-sh', 'x-tcl', 'x-dsrc', 'x-haskell', 'x-literate-haskell',
}:
return 'software'
elif sub_typ in {"vnd.abc", "x-lilypond"}:
return "audio"
else:
return 'document'
elif typ == "application":
if sub_typ in {"dicom"}:
return "image"
elif sub_typ in {
"ecmascript", "java-archive", "javascript", "java-vm", "vnd.android.package-archive",
"x-debian-package", "x-msdos-program", "x-msi", "x-python-code",
"x-redhat-package-manager", "x-ruby", "x-shockwave-flash", "x-silverlight", 'x-cab',
'x-sql'
}:
return "software"
elif sub_typ in {
"gzip", "rar", "x-7z-compressed", "x-apple-diskimage",
"x-iso9660-image", "x-lha", "x-lzh", "x-gtar-compressed", "x-tar", "zip"
}:
return 'archive'
elif sub_typ in {
"json", "msword", "oebps-package+xml", "onenote", "pdf", "postscript", "rtf",
"smil+xml", "x-abiword", "x-hdf", "x-cbr", "x-cbz"
}:
return 'document'
elif any(
sub_typ.startswith(t) for t in [
"vnd.ms-", "vnd.oasis.opendocument", "vnd.openxmlformats-officedocument",
"vnd.stardivision", "vnd.sun.xml"
]
):
return 'document'
else:
return None
elif mime == "x-epoc/x-sisx-app":
return 'software'
else:
return None