-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_links.py
More file actions
112 lines (97 loc) · 2.93 KB
/
page_links.py
File metadata and controls
112 lines (97 loc) · 2.93 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;
}
.even, div {
color: black;
background-color: white;
}
.odd {
color: black;
background-color: yellow;
}
.tiny {
width: 20%;
}
.short {
width: 30%;
}
</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("<table>", file=fh)
for ss, file in enumerate(lres, 1):
mtime = str(datetime.fromtimestamp(results[file])).split(':')[0:-1]
mtime = mtime[0] + ':' + mtime[1]
style = 'odd'
if ss % 2 == 0:
style = 'even'
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"<tr>\
<td><div class={style}>{ss:>04}.) <b>File: </b> {node} </div></td>\
<td class=tiny><div class={style}><b>Link: </b> <a href='{file}'>{mtime}</a></div></td>\
<td class=short><div class={style}><b>Folder: </b> <a href='{dparent}'>{parent}</a></div></td>\
</tr>", file=fh)
print("</table>", file=fh)
print_footer(fh)
return report
if __name__ == '__main__':
types = ".png", ".bmp", ".jpg", ".mp3", ".mp4", ".zip", ".pdf"
for suffix in types:
report = gen_report(suffix)
os.popen(report)