From 2c889c581f21ec841f9c2ce51178e788d3196fd2 Mon Sep 17 00:00:00 2001 From: octospacc Date: Mon, 18 Jul 2022 18:32:25 +0200 Subject: [PATCH] ActivityPub time limit and filtering --- Source/Build.py | 8 +++++++- Source/Modules/ActivityPub.py | 11 ++++++----- TODO | 1 - 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Source/Build.py b/Source/Build.py index 2ec70db..9f994f8 100755 --- a/Source/Build.py +++ b/Source/Build.py @@ -170,7 +170,10 @@ def Main(Args, FeedEntries): Pages, SiteDomain, SiteLang, - Locale) + Locale, + TypeFilter=Args.ActivityPubTypeFilter if Args.ActivityPubTypeFilter else 'Post', + CategoryFilter=Args.ActivityPubCategoryFilter if Args.ActivityPubCategoryFilter else 'Blog', + HoursLimit=Args.ActivityPubHoursLimit if Args.ActivityPubHoursLimit else 168) else: MastodonPosts = [] @@ -220,6 +223,9 @@ if __name__ == '__main__': Parser.add_argument('--MarkdownExts', type=str) Parser.add_argument('--MastodonURL', type=str) Parser.add_argument('--MastodonToken', type=str) + Parser.add_argument('--ActivityPubTypeFilter', type=str) + Parser.add_argument('--ActivityPubCategoryFilter', type=str) + Parser.add_argument('--ActivityPubHoursLimit', type=int) Parser.add_argument('--AutoCategories', type=str) Args = Parser.parse_args() diff --git a/Source/Modules/ActivityPub.py b/Source/Modules/ActivityPub.py index ecf24d9..1004a17 100644 --- a/Source/Modules/ActivityPub.py +++ b/Source/Modules/ActivityPub.py @@ -7,8 +7,9 @@ | Copyright (C) 2022, OctoSpacc | | ================================= """ -from time import sleep +import time from Libs.bs4 import BeautifulSoup +from Libs.dateutil.parser import parse as date_parse from Libs.mastodon import Mastodon from Modules.Utils import * @@ -51,12 +52,12 @@ def MastodonGetAllLinkPosts(Session, Domain=None): return Posts # TODO: Set a limit/cooldown on how many new posts at a time can be posted, or ignore posts older than date X.. otherwise if someone starts using this after having written 100 blog posts, bad things will happen -def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Locale): +def MastodonShare(MastodonURL, MastodonToken, TypeFilter, CategoryFilter, HoursLimit, Pages, SiteDomain, SiteLang, Locale): Session = MastodonGetSession(MastodonURL, MastodonToken) Posts = MastodonGetAllLinkPosts(Session, SiteDomain) Pages.sort() for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages: - if Meta['Type'] == 'Post': + if (not TypeFilter or (TypeFilter and (Meta['Type'] == TypeFilter or TypeFilter == '*'))) and (not CategoryFilter or (CategoryFilter and (CategoryFilter in Meta['Categories'] or CategoryFilter == '*'))): Desc = '' Parse = BeautifulSoup(ContentHTML, 'html.parser') Paragraphs = Parse.p.get_text().split('\n') @@ -75,8 +76,8 @@ def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Local if p['Link'] == URL: DoPost = False break - if DoPost: - sleep(3) + if DoPost and (not HoursLimit or (Meta['Created on'] and time.time() - time.mktime(date_parse(Meta['Created on']).timetuple()) < 60*60*HoursLimit)): + time.sleep(5) Post = MastodonGetLinkFromPost( Post=MastodonDoPost( Session, diff --git a/TODO b/TODO index 365f777..9f5f268 100644 --- a/TODO +++ b/TODO @@ -1,2 +1 @@ -- ActivityPub: Only post "Blog" posts and not older than some date - Fix arguments - some are only callable from CLI and not Site.ini