Skip to content

Commit

Permalink
feat: return local link if tweet in db
Browse files Browse the repository at this point in the history
  • Loading branch information
wzyboy committed Aug 10, 2023
1 parent 0a707d9 commit 0b296cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions ash.py
Expand Up @@ -12,7 +12,6 @@
from functools import lru_cache
from urllib.parse import urlsplit
from collections.abc import Mapping

from collections.abc import Iterator

import flask
Expand Down Expand Up @@ -258,12 +257,18 @@ def get_tdb() -> TweetsDatabase:
return flask.g.tdb


@lru_cache(maxsize=1024)
@app.template_global('get_tweet_link')
def get_tweet_link(screen_name: str, tweet_id: str | int, original_link: bool = False) -> str:
if original_link:
return f'https://twitter.com/{screen_name}/status/{tweet_id}'
else:
def get_tweet_link(tweet_id: int | str, use_original_link: bool = False) -> str:
original_link = f'https://twitter.com/_/status/{tweet_id}'
if use_original_link:
return original_link

tdb = flask.g.tdb
if tdb.get(tweet_id):
return flask.url_for('get_tweet', tweet_id=tweet_id, ext='html')
else:
return original_link


@app.template_filter('format_tweet_text')
Expand Down Expand Up @@ -317,7 +322,7 @@ def format_tweet_text(tweet: dict) -> str:
# Twitter Archive always has "retweeted" set to false (identical to a
# "traditional" RT.
if retweeted_status := tweet.get('retweeted_status'):
link = get_tweet_link('status', retweeted_status['id'])
link = get_tweet_link(retweeted_status['id'])
a = f'<a href="{link}">RT</a>'
tweet_text = tweet_text.replace('RT', a, 1)

Expand Down Expand Up @@ -354,7 +359,7 @@ def in_reply_to_link(tweet: dict) -> str:
else:
return tweet['url']
else: # Twitter
return get_tweet_link('status', tweet['in_reply_to_status_id'])
return get_tweet_link(tweet['in_reply_to_status_id'])


def replace_media_url(url: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion templates/tweet.html
Expand Up @@ -79,7 +79,7 @@
</div>
{%- else %}
<div class="view-on-twitter">
<a class="twitter-link" href="{{ get_tweet_link(tweet.user.screen_name, tweet.id, True) }}">
<a class="twitter-link" href="{{ get_tweet_link(tweet.id, True) }}">
<span class="link-text">View on Twitter</span>
</a>
</div>
Expand Down

0 comments on commit 0b296cb

Please sign in to comment.