From 6e5038e28218bfd618b0d306bfec1a1b4361f030 Mon Sep 17 00:00:00 2001 From: Kyle Mahan Date: Mon, 7 Dec 2015 08:52:31 -0800 Subject: [PATCH] embed quoted tweets --- woodwind/static/feed.js | 18 ++++++++++++------ woodwind/tasks.py | 15 +++++++++++---- woodwind/templates/base.jinja2 | 5 ++++- woodwind/views.py | 25 ++++++++++++++++++------- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/woodwind/static/feed.js b/woodwind/static/feed.js index 54b71f0..6babe04 100644 --- a/woodwind/static/feed.js +++ b/woodwind/static/feed.js @@ -11,8 +11,11 @@ $(function(){ function clickOlderLink(evt) { evt.preventDefault(); $.get(this.href, function(result) { - $(".pager").replaceWith( - $("article,.pager", $(result))); + var $newElements = $("article,.pager", $(result)); + $(".pager").replaceWith($newElements); + $newElements.each(function () { + twttr.widgets.load(this); + }); attachListeners(); }); } @@ -65,11 +68,11 @@ $(function(){ $("#older-link").off('click').click(clickOlderLink); $(".micropub-form button[type='submit']").off('click').click(submitMicropubForm); $(".reply-area.closed").hide(); - + $("article").off('click').click(function(evt) { var $target = $(evt.target); if ($target.closest("form, a, video, audio").length == 0) { - + $(".reply-area", this).toggleClass("closed"); $(".reply-area", this).slideToggle(200); } @@ -89,6 +92,9 @@ $(function(){ $('#unfold-link').text($('#fold>article:not(.reply-context)').length + " New Posts"); $('#unfold-link').off('click').click(clickUnfoldLink); $('#unfold-link').show(); + + // load twitter embeds + twttr.widgets.load($('#fold').get(0)); } // topic will be user:id or feed:id @@ -118,11 +124,11 @@ $(function(){ clickUnfoldLink(); } }); - + if (WS_TOPIC) { webSocketSubscribe(WS_TOPIC); } - + updateTimestamps(); window.setInterval(updateTimestamps, 60 * 1000); diff --git a/woodwind/tasks.py b/woodwind/tasks.py index 49e1a14..f8ef6c4 100644 --- a/woodwind/tasks.py +++ b/woodwind/tasks.py @@ -462,10 +462,17 @@ def hentry_to_entry(hentry, feed, backfill, now): published = hentry.get('published') updated = hentry.get('updated') - # make sure published timezone aware - if published and hasattr(published, 'tzinfo') and published.tzinfo: - published = published.astimezone(datetime.timezone.utc)\ - .replace(tzinfo=None) + if published: + # make sure published is in UTC and strip the timezone + if hasattr(published, 'tzinfo') and published.tzinfo: + published = published.astimezone(datetime.timezone.utc)\ + .replace(tzinfo=None) + # convert datetime.date to datetime.datetime + elif not hasattr(published, 'hour'): + published = datetime.datetime( + year=published.year, + month=published.month, + day=published.day) # retrieved time is now unless we're backfilling old posts retrieved = now diff --git a/woodwind/templates/base.jinja2 b/woodwind/templates/base.jinja2 index 106553c..60772f8 100644 --- a/woodwind/templates/base.jinja2 +++ b/woodwind/templates/base.jinja2 @@ -80,7 +80,6 @@ $("input[type='url']").blur(function() { } }); - $("input[type='url']").on("invalid", function() { if (this.value.trim() == '') { console.log('value is empty'); @@ -104,5 +103,9 @@ $("input[type='url']").on("invalid", function() { } }); + + + + diff --git a/woodwind/views.py b/woodwind/views.py index 6e24556..a8cb113 100644 --- a/woodwind/views.py +++ b/woodwind/views.py @@ -573,10 +573,11 @@ def add_preview(content): # don't add a preview to a post that already has one return content - instagram_regex = 'https?://instagram.com/p/[\w\-]+/?' - vimeo_regex = 'https?://vimeo.com/(\d+)/?' - youtube_regex = 'https?://(?:www.)youtube.com/watch\?v=([\w\-]+)' - youtube_short_regex = 'https://youtu.be/([\w\-]+)' + instagram_regex = r'https?://(?:www\.)?instagram.com/p/[\w\-]+/?' + vimeo_regex = r'https?://(?:www\.)?vimeo.com/(\d+)/?' + youtube_regex = r'https?://(?:www\.)?youtube.com/watch\?v=([\w\-]+)' + youtube_short_regex = r'https://youtu.be/([\w\-]+)' + twitter_regex = r'https?://(?:www\.)?twitter.com/(\w+)/status/(\d+)' m = re.search(instagram_regex, content) if m: @@ -607,9 +608,19 @@ def add_preview(content): 'allowfullscreen>' ).format(content, youtube_id) + # flatten links + flat = re.sub(r']+href="([^"]*)"[^>]*>[^<]*', r'\1', content) + m = re.search(twitter_regex + '$', flat) + if m: + tweet_url = m.group() + return content + ( + '' + ).format(tweet_url) + return content - + @views.app_template_filter() def proxy_image(url): proxy_url = flask.current_app.config.get('IMAGEPROXY_URL') @@ -635,8 +646,8 @@ def proxy_image(url): return (urllib.parse.urljoin(camo_url, digest) + '?url=' + urllib.parse.quote_plus(url)) return url - - + + @views.app_template_filter() def proxy_all(content): def repl(m):