2023-02-23 00:11:49 +01:00
|
|
|
""" ================================== |
|
|
|
|
| This file is part of |
|
|
|
|
| staticoso |
|
|
|
|
| Just a simple Static Site Generator |
|
|
|
|
| |
|
|
|
|
| Licensed under the AGPLv3 license |
|
|
|
|
| Copyright (C) 2022-2023, OctoSpacc |
|
|
|
|
| ================================== """
|
2022-06-30 18:50:18 +02:00
|
|
|
|
2022-07-18 18:32:25 +02:00
|
|
|
import time
|
|
|
|
from Libs.dateutil.parser import parse as date_parse
|
2022-07-01 01:04:20 +02:00
|
|
|
from Libs.mastodon import Mastodon
|
2022-10-31 22:05:16 +01:00
|
|
|
from Modules.HTML import *
|
2022-06-30 18:50:18 +02:00
|
|
|
from Modules.Utils import *
|
|
|
|
|
2022-07-18 18:43:15 +02:00
|
|
|
def MastodonGetSession(InstanceURL, Token):
|
2022-06-30 18:50:18 +02:00
|
|
|
return Mastodon(
|
2022-07-18 18:43:15 +02:00
|
|
|
api_base_url=InstanceURL,
|
|
|
|
access_token=Token)
|
2022-06-30 18:50:18 +02:00
|
|
|
|
|
|
|
def MastodonGetMyID(Session):
|
|
|
|
return Session.me()['id']
|
|
|
|
|
|
|
|
def MastodonGetPostsFromUserID(Session, UserID):
|
|
|
|
return Session.account_statuses(
|
|
|
|
UserID,
|
|
|
|
exclude_replies=True)
|
|
|
|
|
2022-06-30 23:29:44 +02:00
|
|
|
def MastodonDoPost(Session, Text, Lang=None, Visibility='public'):
|
|
|
|
if Text:
|
2022-07-01 16:39:54 +02:00
|
|
|
return Session.status_post(
|
2022-06-30 23:29:44 +02:00
|
|
|
Text,
|
|
|
|
language=Lang,
|
|
|
|
visibility=Visibility)
|
|
|
|
|
2022-07-01 16:39:54 +02:00
|
|
|
def MastodonGetLinkFromPost(Post, Domain=None):
|
2022-10-31 22:05:16 +01:00
|
|
|
Parse = MkSoup(Post['content'])
|
2022-07-01 16:39:54 +02:00
|
|
|
if Parse.a:
|
|
|
|
Link = Parse.find_all('a')[-1]['href']
|
|
|
|
if not Domain or (Domain and Link.startswith(Domain)):
|
|
|
|
return {
|
|
|
|
'Post': Post['uri'],
|
|
|
|
'Link': Link}
|
|
|
|
return None
|
|
|
|
|
|
|
|
def MastodonGetAllLinkPosts(Session, Domain=None):
|
2022-07-01 14:00:25 +02:00
|
|
|
Posts = []
|
2022-07-01 16:39:54 +02:00
|
|
|
for p in MastodonGetPostsFromUserID(Session, MastodonGetMyID(Session)):
|
|
|
|
Post = MastodonGetLinkFromPost(p, Domain)
|
|
|
|
if Post:
|
|
|
|
Posts += [Post]
|
2022-07-01 14:00:25 +02:00
|
|
|
return Posts
|
|
|
|
|
2022-08-30 16:31:33 +02:00
|
|
|
def MastodonShare(Flags, Pages, Locale):
|
2022-07-19 18:26:46 +02:00
|
|
|
SaidPosting = False
|
2022-08-30 16:31:33 +02:00
|
|
|
SiteDomain, SiteLang = Flags['SiteDomain'], Flags['SiteLang']
|
|
|
|
InstanceURL, Token = Flags['MastodonURL'], Flags['MastodonToken']
|
|
|
|
TypeFilter, HoursLimit, CategoryFilter = Flags['ActivityPubTypeFilter'], Flags['ActivityPubHoursLimit'], Flags['FeedCategoryFilter']
|
2022-07-18 18:43:15 +02:00
|
|
|
Session = MastodonGetSession(InstanceURL, Token)
|
2022-07-01 16:39:54 +02:00
|
|
|
Posts = MastodonGetAllLinkPosts(Session, SiteDomain)
|
2022-06-30 23:29:44 +02:00
|
|
|
Pages.sort()
|
2022-07-01 17:29:37 +02:00
|
|
|
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages:
|
2022-07-18 18:32:25 +02:00
|
|
|
if (not TypeFilter or (TypeFilter and (Meta['Type'] == TypeFilter or TypeFilter == '*'))) and (not CategoryFilter or (CategoryFilter and (CategoryFilter in Meta['Categories'] or CategoryFilter == '*'))):
|
2022-08-23 17:25:05 +02:00
|
|
|
URL = f"{SiteDomain}/{StripExt(File)}.html"
|
2022-06-30 23:29:44 +02:00
|
|
|
DoPost = True
|
|
|
|
for p in Posts:
|
2022-10-19 13:29:06 +02:00
|
|
|
if p['Link'] in [URL]+Meta['URLs']:
|
2022-06-30 23:29:44 +02:00
|
|
|
DoPost = False
|
|
|
|
break
|
2022-10-18 16:43:21 +02:00
|
|
|
|
2022-08-13 20:30:44 +02:00
|
|
|
if DoPost and Meta['Feed'] == 'True' and (not HoursLimit or (Meta['CreatedOn'] and time.time() - time.mktime(date_parse(Meta['CreatedOn']).timetuple()) < 60*60*HoursLimit)):
|
2022-10-18 16:43:21 +02:00
|
|
|
Desc = ''
|
|
|
|
Paragraphs = MkSoup(ContentHTML).p.get_text().split('\n')
|
|
|
|
Read = '...' + Locale['ReadFullPost'] + ':\n'
|
|
|
|
for p in Paragraphs:
|
|
|
|
if p and len(Read+Desc+p)+25 < 500:
|
|
|
|
Desc += p + '\n\n'
|
|
|
|
else:
|
|
|
|
if Desc:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
Desc = p[:500-25-5-len(Read)] + '...'
|
|
|
|
|
2022-07-19 18:26:46 +02:00
|
|
|
if not SaidPosting:
|
|
|
|
print("[I] Posting to Mastodon")
|
|
|
|
SaidPosting = True
|
2022-10-18 16:43:21 +02:00
|
|
|
|
2022-08-13 16:20:26 +02:00
|
|
|
time.sleep(5) # Prevent flooding
|
2022-07-01 16:39:54 +02:00
|
|
|
Post = MastodonGetLinkFromPost(
|
|
|
|
Post=MastodonDoPost(
|
|
|
|
Session,
|
|
|
|
Text=Desc+Read+URL,
|
|
|
|
Lang=SiteLang),
|
|
|
|
Domain=SiteDomain)
|
|
|
|
if Post:
|
|
|
|
Posts += [Post]
|
2022-10-18 16:43:21 +02:00
|
|
|
|
2022-07-01 14:00:25 +02:00
|
|
|
return Posts
|