mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-27 17:03:44 +03:00
Damir Modyarov
21c02df7f0
* Implement short link redirect service * Silence linter where imports needs to be below setup * Improve unknown link error * Provide session to worker to convert it to user id * Remove main entrypoint in favor of uvicorn command * Add base path for relative URLs * Add docs on launching redirect service to README.md * Fix wrong task argument name * Lint service code
19 lines
523 B
Python
19 lines
523 B
Python
import string
|
|
|
|
from redirect.settings import settings
|
|
|
|
URL_CHARACTERS = list(string.ascii_letters + string.digits + ";,:@&-_.!~*'()#")
|
|
SLUG_CUTOFF = settings.redirect_slug_cutoff
|
|
|
|
|
|
def slug_to_link_id(slug: str) -> int:
|
|
"""Converts given slug to an id of a link."""
|
|
link_id = 0
|
|
try:
|
|
for i, ch in enumerate(slug[:SLUG_CUTOFF - 1:-1]): # fmt: skip
|
|
value = URL_CHARACTERS.index(ch)
|
|
link_id += value * len(URL_CHARACTERS) ** i
|
|
except ValueError:
|
|
pass
|
|
return link_id
|