ActivityPub time limit and filtering

This commit is contained in:
octospacc 2022-07-18 18:32:25 +02:00
parent 7ac41b2884
commit 2c889c581f
3 changed files with 13 additions and 7 deletions

View File

@ -170,7 +170,10 @@ def Main(Args, FeedEntries):
Pages, Pages,
SiteDomain, SiteDomain,
SiteLang, 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: else:
MastodonPosts = [] MastodonPosts = []
@ -220,6 +223,9 @@ if __name__ == '__main__':
Parser.add_argument('--MarkdownExts', type=str) Parser.add_argument('--MarkdownExts', type=str)
Parser.add_argument('--MastodonURL', type=str) Parser.add_argument('--MastodonURL', type=str)
Parser.add_argument('--MastodonToken', 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) Parser.add_argument('--AutoCategories', type=str)
Args = Parser.parse_args() Args = Parser.parse_args()

View File

@ -7,8 +7,9 @@
| Copyright (C) 2022, OctoSpacc | | Copyright (C) 2022, OctoSpacc |
| ================================= """ | ================================= """
from time import sleep import time
from Libs.bs4 import BeautifulSoup from Libs.bs4 import BeautifulSoup
from Libs.dateutil.parser import parse as date_parse
from Libs.mastodon import Mastodon from Libs.mastodon import Mastodon
from Modules.Utils import * from Modules.Utils import *
@ -51,12 +52,12 @@ def MastodonGetAllLinkPosts(Session, Domain=None):
return Posts 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 # 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) Session = MastodonGetSession(MastodonURL, MastodonToken)
Posts = MastodonGetAllLinkPosts(Session, SiteDomain) Posts = MastodonGetAllLinkPosts(Session, SiteDomain)
Pages.sort() Pages.sort()
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages: 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 = '' Desc = ''
Parse = BeautifulSoup(ContentHTML, 'html.parser') Parse = BeautifulSoup(ContentHTML, 'html.parser')
Paragraphs = Parse.p.get_text().split('\n') Paragraphs = Parse.p.get_text().split('\n')
@ -75,8 +76,8 @@ def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Local
if p['Link'] == URL: if p['Link'] == URL:
DoPost = False DoPost = False
break break
if DoPost: if DoPost and (not HoursLimit or (Meta['Created on'] and time.time() - time.mktime(date_parse(Meta['Created on']).timetuple()) < 60*60*HoursLimit)):
sleep(3) time.sleep(5)
Post = MastodonGetLinkFromPost( Post = MastodonGetLinkFromPost(
Post=MastodonDoPost( Post=MastodonDoPost(
Session, Session,

1
TODO
View File

@ -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 - Fix arguments - some are only callable from CLI and not Site.ini