Skip to content

Commit

Permalink
Merge pull request #14 from wzyboy/feat/inject-user-dict
Browse files Browse the repository at this point in the history
feat: inject user dicts
  • Loading branch information
wzyboy committed Aug 9, 2023
2 parents 0067857 + 6a9796e commit bf9118c
Show file tree
Hide file tree
Showing 7 changed files with 789 additions and 5 deletions.
6 changes: 6 additions & 0 deletions ash.py
Expand Up @@ -481,6 +481,12 @@ def get_tweet(tweet_id, ext):
else:
pass

# User dict injection
if user_dicts := app.config.get('T_USER_DICTS'):
screen_name = tweet['user']['screen_name']
if user_dict := user_dicts.get(screen_name):
tweet['user'].update(user_dict)

# Render HTML
rendered = flask.render_template(
'tweet.html',
Expand Down
13 changes: 11 additions & 2 deletions config.sample.py
Expand Up @@ -11,8 +11,8 @@ class Config:
# filesystem: Media files are served from T_MEDIA_FS_PATH
T_MEDIA_FROM = 'direct'

# You can also use mirror domains in case your Twitter account no longer exists
# This is just simple string substitution
# You can also use mirror domains in case your Twitter account no longer
# exists. This is just simple string substitution
T_MEDIA_MIRRORS = {
'pbs.twimg.com': 'd1111111111.cloudfront.net/pbs.twimg.com',
'video.twimg.com': 'd1111111111.cloudfront.net/video.twimg.com',
Expand All @@ -32,3 +32,12 @@ class Config:

# Default user to show on index
#T_DEFAULT_USER = 'jack'

# For imported tweets that has minimal `user` dict, this can be used to
# inject additional keys.
T_USER_DICTS = {
'jack': {
'name': 'Jack Dorsey',
'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1661201415899951105/azNjKOSH_400x400.jpg',
}
}
1 change: 1 addition & 0 deletions tests/conftest.py
Expand Up @@ -34,6 +34,7 @@ def es_index(es_host: str) -> str:
tweet_files = [
here / 'fixtures/tweet_with_photo.json',
here / 'fixtures/tweet_with_video.json',
here / 'fixtures/tweet_with_bare_user_dict.json',
]
for tweet_file in tweet_files:
tweet = json.loads(tweet_file.read_text())
Expand Down
37 changes: 37 additions & 0 deletions tests/fixtures/tweet_with_bare_user_dict.json
@@ -0,0 +1,37 @@
{
"@timestamp": "2023-07-04T00:21:26+00:00",
"created_at": "Tue Jul 04 00:21:26 +0000 2023",
"display_text_range": [
"0",
"45"
],
"edit_info": {
"initial": {
"editTweetIds": [
"1676023376631197696"
],
"editableUntil": "2023-07-04T01:21:26.000Z",
"editsRemaining": "5",
"isEditEligible": true
}
},
"entities": {
"hashtags": [],
"symbols": [],
"urls": [],
"user_mentions": []
},
"favorite_count": "5",
"favorited": false,
"full_text": "想不到Twitter比Facebook先死... 🔜@alan_blake@dabr.ca",
"id": "1676023376631197696",
"id_str": "1676023376631197696",
"lang": "ja",
"retweet_count": "0",
"retweeted": false,
"source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
"truncated": false,
"user": {
"screen_name": "alan_blake"
}
}
186 changes: 185 additions & 1 deletion tests/fixtures/tweet_with_photo.json
@@ -1 +1,185 @@
{"@timestamp": "2023-01-17T19:06:46+00:00", "contributors": null, "coordinates": null, "created_at": "Tue Jan 17 19:06:46 +0000 2023", "display_text_range": [0, 75], "entities": {"hashtags": [], "media": [{"display_url": "pic.twitter.com/9dauLWrDZS", "expanded_url": "https://twitter.com/wzyboy/status/1615425412921987074/photo/1", "id": 1615425410095017984, "id_str": "1615425410095017984", "indices": [76, 99], "media_url": "http://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg", "media_url_https": "https://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg", "sizes": {"large": {"h": 2048, "resize": "fit", "w": 922}, "medium": {"h": 1200, "resize": "fit", "w": 540}, "small": {"h": 680, "resize": "fit", "w": 306}, "thumb": {"h": 150, "resize": "crop", "w": 150}}, "type": "photo", "url": "https://t.co/9dauLWrDZS"}], "symbols": [], "urls": [], "user_mentions": []}, "extended_entities": {"media": [{"display_url": "pic.twitter.com/9dauLWrDZS", "expanded_url": "https://twitter.com/wzyboy/status/1615425412921987074/photo/1", "id": 1615425410095017984, "id_str": "1615425410095017984", "indices": [76, 99], "media_url": "http://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg", "media_url_https": "https://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg", "sizes": {"large": {"h": 2048, "resize": "fit", "w": 922}, "medium": {"h": 1200, "resize": "fit", "w": 540}, "small": {"h": 680, "resize": "fit", "w": 306}, "thumb": {"h": 150, "resize": "crop", "w": 150}}, "type": "photo", "url": "https://t.co/9dauLWrDZS"}]}, "favorite_count": 0, "favorited": false, "full_text": "Todoist: please connect a keyboard to your phone and press F12 to continue. https://t.co/9dauLWrDZS", "geo": null, "id": 1615425412921987074, "id_str": "1615425412921987074", "in_reply_to_screen_name": null, "in_reply_to_status_id": null, "in_reply_to_status_id_str": null, "in_reply_to_user_id": null, "in_reply_to_user_id_str": null, "is_quote_status": false, "lang": "en", "place": null, "possibly_sensitive": false, "retweet_count": 0, "retweeted": false, "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", "truncated": false, "user": {"contributors_enabled": false, "created_at": "Fri Jun 26 05:13:44 +0000 2009", "default_profile": false, "default_profile_image": false, "description": "Das Leben ist zu kurz, um Deutsch zu lernen. 每天给 @Uucky_Lee 洗碗。欢迎在 Fediverse 里关注我。了解 Fediverse 联邦宇宙: https://t.co/WGCqN97JAz", "entities": {"description": {"urls": [{"display_url": "wzyboy.im/post/1486.html", "expanded_url": "https://wzyboy.im/post/1486.html", "indices": [101, 124], "url": "https://t.co/WGCqN97JAz"}]}, "url": {"urls": [{"display_url": "wzyboy.im", "expanded_url": "https://wzyboy.im/", "indices": [0, 23], "url": "https://t.co/btXCkHdabG"}]}}, "favourites_count": 1608, "follow_request_sent": false, "followers_count": 5072, "following": false, "friends_count": 429, "geo_enabled": true, "has_extended_profile": true, "id": 50932982, "id_str": "50932982", "is_translation_enabled": true, "is_translator": true, "lang": null, "listed_count": 115, "location": "Vancouver, BC", "name": "@wzyboy@dabr.ca", "notifications": false, "profile_background_color": "FFFFFF", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme16/bg.gif", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme16/bg.gif", "profile_background_tile": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/50932982/1573897758", "profile_image_url": "http://pbs.twimg.com/profile_images/1195639287456391168/IAxCxK39_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1195639287456391168/IAxCxK39_normal.jpg", "profile_link_color": "8CB2BD", "profile_sidebar_border_color": "FFFFFF", "profile_sidebar_fill_color": "424C55", "profile_text_color": "8C94A2", "profile_use_background_image": false, "protected": true, "screen_name": "wzyboy", "statuses_count": 52391, "time_zone": null, "translator_type": "badged", "url": "https://t.co/btXCkHdabG", "utc_offset": null, "verified": false, "withheld_in_countries": []}}
{
"@timestamp": "2023-01-17T19:06:46+00:00",
"contributors": null,
"coordinates": null,
"created_at": "Tue Jan 17 19:06:46 +0000 2023",
"display_text_range": [
0,
75
],
"entities": {
"hashtags": [],
"media": [
{
"display_url": "pic.twitter.com/9dauLWrDZS",
"expanded_url": "https://twitter.com/wzyboy/status/1615425412921987074/photo/1",
"id": 1615425410095017984,
"id_str": "1615425410095017984",
"indices": [
76,
99
],
"media_url": "http://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg",
"media_url_https": "https://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg",
"sizes": {
"large": {
"h": 2048,
"resize": "fit",
"w": 922
},
"medium": {
"h": 1200,
"resize": "fit",
"w": 540
},
"small": {
"h": 680,
"resize": "fit",
"w": 306
},
"thumb": {
"h": 150,
"resize": "crop",
"w": 150
}
},
"type": "photo",
"url": "https://t.co/9dauLWrDZS"
}
],
"symbols": [],
"urls": [],
"user_mentions": []
},
"extended_entities": {
"media": [
{
"display_url": "pic.twitter.com/9dauLWrDZS",
"expanded_url": "https://twitter.com/wzyboy/status/1615425412921987074/photo/1",
"id": 1615425410095017984,
"id_str": "1615425410095017984",
"indices": [
76,
99
],
"media_url": "http://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg",
"media_url_https": "https://pbs.twimg.com/media/Fmsk2gHacAAJGL0.jpg",
"sizes": {
"large": {
"h": 2048,
"resize": "fit",
"w": 922
},
"medium": {
"h": 1200,
"resize": "fit",
"w": 540
},
"small": {
"h": 680,
"resize": "fit",
"w": 306
},
"thumb": {
"h": 150,
"resize": "crop",
"w": 150
}
},
"type": "photo",
"url": "https://t.co/9dauLWrDZS"
}
]
},
"favorite_count": 0,
"favorited": false,
"full_text": "Todoist: please connect a keyboard to your phone and press F12 to continue. https://t.co/9dauLWrDZS",
"geo": null,
"id": 1615425412921987074,
"id_str": "1615425412921987074",
"in_reply_to_screen_name": null,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"is_quote_status": false,
"lang": "en",
"place": null,
"possibly_sensitive": false,
"retweet_count": 0,
"retweeted": false,
"source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
"truncated": false,
"user": {
"contributors_enabled": false,
"created_at": "Fri Jun 26 05:13:44 +0000 2009",
"default_profile": false,
"default_profile_image": false,
"description": "Das Leben ist zu kurz, um Deutsch zu lernen. 每天给 @Uucky_Lee 洗碗。欢迎在 Fediverse 里关注我。了解 Fediverse 联邦宇宙: https://t.co/WGCqN97JAz",
"entities": {
"description": {
"urls": [
{
"display_url": "wzyboy.im/post/1486.html",
"expanded_url": "https://wzyboy.im/post/1486.html",
"indices": [
101,
124
],
"url": "https://t.co/WGCqN97JAz"
}
]
},
"url": {
"urls": [
{
"display_url": "wzyboy.im",
"expanded_url": "https://wzyboy.im/",
"indices": [
0,
23
],
"url": "https://t.co/btXCkHdabG"
}
]
}
},
"favourites_count": 1608,
"follow_request_sent": false,
"followers_count": 5072,
"following": false,
"friends_count": 429,
"geo_enabled": true,
"has_extended_profile": true,
"id": 50932982,
"id_str": "50932982",
"is_translation_enabled": true,
"is_translator": true,
"lang": null,
"listed_count": 115,
"location": "Vancouver, BC",
"name": "@wzyboy@dabr.ca",
"notifications": false,
"profile_background_color": "FFFFFF",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme16/bg.gif",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme16/bg.gif",
"profile_background_tile": false,
"profile_banner_url": "https://pbs.twimg.com/profile_banners/50932982/1573897758",
"profile_image_url": "http://pbs.twimg.com/profile_images/1195639287456391168/IAxCxK39_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1195639287456391168/IAxCxK39_normal.jpg",
"profile_link_color": "8CB2BD",
"profile_sidebar_border_color": "FFFFFF",
"profile_sidebar_fill_color": "424C55",
"profile_text_color": "8C94A2",
"profile_use_background_image": false,
"protected": true,
"screen_name": "wzyboy",
"statuses_count": 52391,
"time_zone": null,
"translator_type": "badged",
"url": "https://t.co/btXCkHdabG",
"utc_offset": null,
"verified": false,
"withheld_in_countries": []
}
}

0 comments on commit bf9118c

Please sign in to comment.