Skip to content

Commit 11a05f1

Browse files
authored
Update shortener.py
1 parent 7c3363d commit 11a05f1

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

links/shortener.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ class ShortenResult(NamedTuple):
6969
def parse_htaccess(text: str) -> Iterator[tuple[str, str]]:
7070
for line in text.splitlines():
7171
fields = line.split()
72-
if len(fields) < 3 or fields[0] != 'RedirectTemp':
73-
continue
74-
path = fields[1]
75-
assert path[0] == '/', f'Missing /: {path!r}'
76-
path = path[1:] # Remove leading slash
77-
assert len(path) > 0, f'Root path in line {line!r}'
78-
yield (path, fields[2])
72+
if len(fields) >= 3 and fields[0] == 'RedirectTemp':
73+
path = fields[1]
74+
assert path[0] == '/', f'Missing /: {path!r}'
75+
path = path[1:] # Remove leading slash
76+
assert len(path) > 0, f'Root path in line {line!r}'
77+
yield (path, fields[2])
7978

8079

8180
def choose(a: str, b: str) -> str:
@@ -113,9 +112,14 @@ def shorten_one(target: str, path_gen: Iterator[str], redirects: dict, targets:
113112
return ShortenResult(target, path, True)
114113

115114

116-
def update_htaccess(new_targets: list[ShortenResult]):
117-
118-
pass
115+
def update_htaccess(f: file, srs: list[ShortenResult]) -> int:
116+
"""append new redirects, returns count of new redirects"""
117+
directives = [t for t in srs if t.new]
118+
if directives:
119+
# xxx write timestamp, then...
120+
for url, path, _new in directives:
121+
f.write(f'RedirectTemp /{path} {url}')
122+
return len(directives)
119123

120124

121125
SDIGITS = '23456789abcdefghjkmnpqrstvwxyz'

0 commit comments

Comments
 (0)