Show Mastodon posts of the main account on the main page.

This commit is contained in:
BlenderDumbass 2024-12-09 22:51:48 +02:00
parent 3008659ce0
commit a434a3ba1f
3 changed files with 105 additions and 0 deletions

3
Tests.py Normal file
View file

@ -0,0 +1,3 @@
from modules import API
print(API.Mastodon("@blenderdumbass@mastodon.online"))

View file

@ -15,6 +15,8 @@ from datetime import datetime
from modules import Set from modules import Set
from modules.Common import * from modules.Common import *
API_cache = {}
def Get(url, data=None, headers={}): def Get(url, data=None, headers={}):
if data: if data:
@ -48,3 +50,40 @@ def Petition(article):
f = Set.Folder() f = Set.Folder()
with open(f+"/tabs"+article.get("url", "")+"/metadata.json", "w") as save: with open(f+"/tabs"+article.get("url", "")+"/metadata.json", "w") as save:
json.dump(article, save, indent=4) json.dump(article, save, indent=4)
def Mastodon(mastoname, posts=4):
# This function will get few latest posts
# and return them back to whatever needs
# them.
# First we want to get the mastodon ID of the
# user in question ( mastodon API is strange ).
if mastoname.startswith("@"):
mastoname = mastoname[1:]
username, host, *rest = mastoname.split("@")
if not "mastoid" in API_cache:
url = "https://"+host+"/api/v1/accounts/lookup?acct="+username
ID = Value(url, ["id"])
API_cache["mastoid"] = ID
ID = API_cache["mastoid"]
# Now that we have the ID we can request the actuall
# list that we came here for.
if time.time()-300 > API_cache.get("mastotime", 0):
url = "https://"+host+"/api/v1/accounts/"+str(ID)+"/statuses?inned=false&limit="+str(posts)
data = Get(url)
API_cache["mastotime"] = time.time()
API_cache["mastodata"] = data
return API_cache["mastodata"]

View file

@ -437,6 +437,10 @@ def MainPage(server):
html = html + "</center>" html = html + "</center>"
# Trending articles # Trending articles
Accounts = accounts()
owner = config.get("main_account", "")
mastodon = Accounts[owner].get("mastodon")
mastoposts = API.Mastodon(mastohead(mastodon), 8).copy()
html = html + '<div class="flexity">' html = html + '<div class="flexity">'
@ -445,6 +449,9 @@ def MainPage(server):
for n, article in enumerate(trends): for n, article in enumerate(trends):
if n >= Buffer: break if n >= Buffer: break
if n and n % 2 == 0 and mastoposts:
html = html + Mastoview(mastoposts.pop(0), owner)
article = trends[article] article = trends[article]
html = html + ArticlePreview(article, Tabs, server.cookie) html = html + ArticlePreview(article, Tabs, server.cookie)
@ -1643,6 +1650,62 @@ def ArticlePreview(article, Tabs, cookie=""):
return html return html
def Mastoview(mastopost, user="", repost=False):
html = """
<div class="article_box">
"""
url = mastopost.get("url", "")
content = mastopost.get("content", "")
if content:
html = html + '<a href="'+url+'">'
if not repost:
html = html + '<h1><img alt="[icon mastodon]" src="/icon/mastodon" style="vertical-align: middle">'
html = html + ' On Mastodon</h1>'
else:
html = html + "<i>...repost</i><br><br>"
if content:
html = html + "</a>"
# repost = 🔁
try:
media = mastopost.get("media_attachments")[0]
except:
media = {}
if media.get("type", "") == "image":
mediaURL = media.get("preview_url", "")
if not mediaURL: media.get("url", "")
html = html + '<center><a href="'+url+'"><img alt="[thumbnail]" src="'+mediaURL+'"></a></center>'
if not repost:
html = html + '<br><br><center>'+User(user)+'</center><br><br>'
else:
html = html + '<br><br><center>'+user+'</center><br><br>'
if content:
likes = mastopost.get("favourites_count", 0)
comments = mastopost.get("replies_count", 0)
reposts = mastopost.get("reblogs_count", 0)
html = html + '<center>⭐ '+str(likes)+' 💬 '+str(comments)+' 🔁 '+str(reposts)+'</center><br><br>'
if not content and mastopost.get("reblog", ""):
content = Mastoview(mastopost.get("reblog", ""), mastopost.get("reblog", {}).get("account", {}).get("acct", ""), True)
html = html + content
html = html + '</div>\n'
return html
def Footer(server): def Footer(server):
html = """ html = """