diff --git a/app/post/routes.py b/app/post/routes.py index 1473fa33..f033927e 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -15,7 +15,8 @@ from app.community.util import save_post, send_to_remote_instance from app.inoculation import inoculation from app.post.forms import NewReplyForm, ReportPostForm, MeaCulpaForm from app.community.forms import CreateLinkForm, CreateImageForm, CreateDiscussionForm, CreateVideoForm, CreatePollForm -from app.post.util import post_replies, get_comment_branch, post_reply_count, tags_to_string +from app.post.util import post_replies, get_comment_branch, post_reply_count, tags_to_string, url_has_paywall, \ + generate_paywall_bypass_link from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_LINK, \ POST_TYPE_IMAGE, \ POST_TYPE_ARTICLE, POST_TYPE_VIDEO, NOTIF_REPLY, NOTIF_POST, POST_TYPE_POLL @@ -299,6 +300,11 @@ def show_post(post_id: int): else: poll_form = True + # Bypass paywalls link + bypass_paywall_link = None + if post.type == POST_TYPE_LINK and 'https://archive.' not in post.body_html and url_has_paywall(post.url): + bypass_paywall_link = generate_paywall_bypass_link(post.url) + response = render_template('post/post.html', title=post.title, post=post, is_moderator=is_moderator, community=post.community, breadcrumbs=breadcrumbs, related_communities=related_communities, mods=mod_list, poll_form=poll_form, poll_results=poll_results, poll_data=poll_data, poll_choices=poll_choices, poll_total_votes=poll_total_votes, @@ -306,7 +312,7 @@ def show_post(post_id: int): description=description, og_image=og_image, POST_TYPE_IMAGE=constants.POST_TYPE_IMAGE, POST_TYPE_LINK=constants.POST_TYPE_LINK, POST_TYPE_ARTICLE=constants.POST_TYPE_ARTICLE, POST_TYPE_VIDEO=constants.POST_TYPE_VIDEO, POST_TYPE_POLL=constants.POST_TYPE_POLL, - autoplay=request.args.get('autoplay', False), + autoplay=request.args.get('autoplay', False), bypass_paywall_link=bypass_paywall_link, noindex=not post.author.indexable, preconnect=post.url if post.url else None, recently_upvoted=recently_upvoted, recently_downvoted=recently_downvoted, recently_upvoted_replies=recently_upvoted_replies, recently_downvoted_replies=recently_downvoted_replies, diff --git a/app/post/util.py b/app/post/util.py index 8aa2a2b2..296a109e 100644 --- a/app/post/util.py +++ b/app/post/util.py @@ -1,4 +1,5 @@ from typing import List +from urllib.parse import urlparse from flask_login import current_user from sqlalchemy import desc, text, or_ @@ -78,3 +79,22 @@ def post_reply_count(post_id) -> int: def tags_to_string(post: Post) -> str: if post.tags.count() > 0: return ', '.join([tag.name for tag in post.tags]) + + +def url_has_paywall(url) -> bool: + paywalled_sites = ['washingtonpost.com', 'wapo.st', 'nytimes.com', 'wsj.com', 'economist.com', 'ft.com', 'telegraph.co.uk', + 'bild.de', 'theatlantic.com', 'lemonde.fr'] + if url: + try: + parsed_url = urlparse(url.replace('www.', '')) + hostname = parsed_url.hostname.lower() + except: + return False + return hostname in paywalled_sites + else: + return False + + +def generate_paywall_bypass_link(url) -> bool: + url_without_protocol = url.replace('https://', '').replace('http://', '') + return 'https://archive.ph/' + url diff --git a/app/templates/post/_post_full.html b/app/templates/post/_post_full.html index d2e3baf3..c6ebc741 100644 --- a/app/templates/post/_post_full.html +++ b/app/templates/post/_post_full.html @@ -144,6 +144,9 @@ {% endif -%}
{{ post.body_html|community_links|safe if post.body_html else '' }} + {% if bypass_paywall_link -%} +

{{ _('Bypass paywall') }}

+ {% endif -%}
{% if post.type == POST_TYPE_POLL -%}