Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expansion of Substack redirects to link shortening parser. #158

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions unfurl/parsers/parse_shortlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def run(unfurl, node):
if not unfurl.remote_lookups:
return

preceding_domain = unfurl.find_preceding_domain(node)

# LinkedIn has another method of URL shortening that's different from how most others do it; I can
# refactor this in the future to be more flexible if I find more sites that operate this way, but for now
# this works.
if node.data_type == 'url.query.pair' and node.key == 'code':
if 'linkedin.com' in unfurl.find_preceding_domain(node):
if 'linkedin.com' in preceding_domain:
expanded_url = expand_url_via_redirect_header('https://www.linkedin.com/slink?code=', node.value)
if expanded_url:
unfurl.add_to_queue(
Expand All @@ -67,7 +69,19 @@ def run(unfurl, node):
parent_id=node.node_id, incoming_edge_config=shortlink_edge)
return

if not node.data_type == 'url.path':
# Substack inserts a redirect
if 'substack.com' == preceding_domain and node.key == 2 and \
unfurl.check_sibling_nodes(node, data_type='url.path.segment', key=1, value='redirect'):
expanded_url = expand_url_via_redirect_header('https://substack.com/redirect/', node.value)

if expanded_url:
unfurl.add_to_queue(
data_type='url', key=None, value=expanded_url,
label=f'Expanded URL: {expanded_url}',
hover=f'Expanded URL, retrieved from {preceding_domain} via "Location" header',
parent_id=node.node_id, incoming_edge_config=shortlink_edge)

if node.data_type != 'url.path':
return

bitly_domains = ['bit.ly', 'bitly.com', 'j.mp']
Expand Down Expand Up @@ -128,7 +142,6 @@ def run(unfurl, node):
{'domain': 'x.co', 'base_url': 'https://x.co/'},
]

preceding_domain = unfurl.find_preceding_domain(node)
for redirect_expand in redirect_expands:
if redirect_expand['domain'] == preceding_domain:
expanded_url = expand_url_via_redirect_header(redirect_expand['base_url'], node.value[1:])
Expand Down