def apply_short_links(context, click_url, click_random=30, backup_arg=False):
shortened_link = []
extra = {}
for k, v in context.items():
# TODO deal with unsubscribe links properly
if k != 'unsubscribe_link' and looks_like_link(v):
r = secrets.token_urlsafe(click_random)[:click_random]
new_url = click_url + r
if backup_arg:
new_url += '?u=' + urlsafe_b64encode(v.encode()).decode()
extra[k] = new_url
extra[f'{k}_original'] = v
shortened_link.append((v, r))
context.update(extra)
return shortened_link
评论列表
文章目录