Update short.py to return list of URL substitutions
This commit is contained in:
@@ -36,16 +36,6 @@ def gen_short() -> Iterator[str]:
|
|||||||
length += 1
|
length += 1
|
||||||
|
|
||||||
|
|
||||||
def shorten(n: int) -> str:
|
|
||||||
"""
|
|
||||||
Get Nth short URL made from SDIGITS, where 0 is the first.
|
|
||||||
"""
|
|
||||||
iter_short = gen_short()
|
|
||||||
for _ in range(n+1):
|
|
||||||
short = next(iter_short)
|
|
||||||
return short
|
|
||||||
|
|
||||||
|
|
||||||
def gen_free_short(redirects: dict) -> Iterator[str]:
|
def gen_free_short(redirects: dict) -> Iterator[str]:
|
||||||
"""
|
"""
|
||||||
Generate next available short URL.
|
Generate next available short URL.
|
||||||
@@ -55,17 +45,23 @@ def gen_free_short(redirects: dict) -> Iterator[str]:
|
|||||||
yield short
|
yield short
|
||||||
|
|
||||||
|
|
||||||
def new_urls(urls: list[str], redirects: dict, targets: dict) -> None:
|
def shorten(urls: list[str], redirects: dict, targets: dict) -> list[tuple[str,str]]:
|
||||||
|
"""return (short, long) pairs, updating short.htaccess as needed""'
|
||||||
iter_short = gen_free_short(redirects)
|
iter_short = gen_free_short(redirects)
|
||||||
|
pairs = []
|
||||||
with open('short.htaccess', 'a') as fp:
|
with open('short.htaccess', 'a') as fp:
|
||||||
for url in urls:
|
for long in urls:
|
||||||
assert 'fpy.li' not in url, f"{url} is a fpy.li URL"
|
assert 'fpy.li' not in long, f"{long} is a fpy.li URL"
|
||||||
if url in targets:
|
if long in targets:
|
||||||
continue
|
short = targets[long]
|
||||||
|
else:
|
||||||
short = next(iter_short)
|
short = next(iter_short)
|
||||||
redirects[short] = url
|
redirects[short] = url
|
||||||
targets[url] = short
|
targets[url] = short
|
||||||
fp.write(f"RedirectTemp /{short} {url}\n")
|
fp.write(f"RedirectTemp /{short} {url}\n")
|
||||||
|
pairs.append((short, long))
|
||||||
|
|
||||||
|
return pairs
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -73,7 +69,8 @@ def main():
|
|||||||
urls = [f'https://example.com/{randrange(100000)}.html' for n in range(7)]
|
urls = [f'https://example.com/{randrange(100000)}.html' for n in range(7)]
|
||||||
|
|
||||||
redirects, targets = load_redirects()
|
redirects, targets = load_redirects()
|
||||||
new_urls(urls, redirects, targets)
|
for short, long in shorten(urls, redirects, targets):
|
||||||
|
print(f'fpy.li/{short}\t{long}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user