-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpdfmted-thumbnailer
executable file
·51 lines (40 loc) · 1.45 KB
/
pdfmted-thumbnailer
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
#!/usr/bin/python3
# General purpose GNOME thumbnailer script
# Written by James Henstridge (http://askubuntu.com/a/201997)
import os
import sys
from gi.repository import Gio, GnomeDesktop
def make_thumbnail(factory, filename):
mtime = os.path.getmtime(filename)
# Use Gio to determine the URI and mime type
f = Gio.file_new_for_path(filename)
uri = f.get_uri()
info = f.query_info(
'standard::content-type', Gio.FileQueryInfoFlags.NONE, None)
mime_type = info.get_content_type()
if factory.lookup(uri, mtime) is not None:
print("FRESH %s" % uri)
return False
if not factory.can_thumbnail(uri, mime_type, mtime):
print("UNSUPPORTED %s" % uri)
return False
thumbnail = factory.generate_thumbnail(uri, mime_type)
if thumbnail is None:
print("ERROR %s" % uri)
return False
print("OK %s" % uri)
factory.save_thumbnail(thumbnail, uri, mtime)
return True
def thumbnail_folder(factory, folder):
for dirpath, dirnames, filenames in os.walk(folder):
for filename in filenames:
make_thumbnail(factory, os.path.join(dirpath, filename))
def main(argv):
factory = GnomeDesktop.DesktopThumbnailFactory()
for filename in argv[1:]:
if os.path.isdir(filename):
thumbnail_folder(factory, filename)
else:
make_thumbnail(factory, filename)
if __name__ == '__main__':
sys.exit(main(sys.argv))