-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_links.py
More file actions
98 lines (83 loc) · 2.43 KB
/
image_links.py
File metadata and controls
98 lines (83 loc) · 2.43 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
92
93
94
95
96
97
98
import os
import os.path
import time
from datetime import datetime, timezone
def print_header(fh):
header = """
<!doctype html>
<html>
<header>
<style>
body {
background-color: #BBCAFC;
}
div {
margin: 3px;
padding: 6px;
border: 2px solid;
font-size: 22px;
color: black;
background-color: yellow;
}
</style>
</header>
<body>
"""
print(header, file=fh)
def print_footer(fh):
footer = """
</body>
</html>
"""
print(footer, file=fh)
def mk_link(root, node):
return root + os.path.sep + node
def find_files(home, suffix = '.pdf', cap=None):
results = dict()
for root, dirs, nodes in os.walk(home):
for node in nodes:
if node.lower().endswith(suffix):
file = mk_link(root, node)
results[file] = os.stat(file).st_mtime
if cap and len(results) > cap:
break
return results
def gen_report(suffix, home = os.getcwd()):
results = find_files(home, suffix)
if not results:
print("None found.")
quit()
lres = sorted(results, key=lambda a: results[a], reverse=True)
report = f"{suffix}_links.html"
with open(report, "w") as fh:
print_header(fh)
print(f"<h1>{suffix.upper()} Links</h1>", file=fh)
print(f"<p>Reporting root is '{home}'</p>", file=fh)
print(f"<p>Report generated @ {time.asctime()}</p>", file=fh)
print("<hr>", file=fh)
print(f"<div>", file=fh)
for ss, file in enumerate(lres, 1):
mtime = str(datetime.fromtimestamp(results[file])).split(':')[0:-1]
mtime = mtime[0] + ':' + mtime[1]
cols = file.split(os.path.sep)
parent = cols[-2]
dparent = 'file://'
for z in cols[0:-1]:
dparent += z + os.path.sep
node = cols[-1]
if len(node) > 45:
node = node[:40] + f'... {suffix}'
link = 'file://' + file
print(f"\
<a href='{dparent}'>\
<img src='{file}' \
style='width:200px;height:300px;'>\
</a>", file=fh)
print(f"</div>", file=fh)
print_footer(fh)
return report
if __name__ == '__main__':
types = ".png", ".bmp", ".jpg"
for suffix in types:
report = gen_report(suffix)
os.popen(report)