diff --git a/Tests.py b/Tests.py new file mode 100644 index 0000000..8b22914 --- /dev/null +++ b/Tests.py @@ -0,0 +1,3 @@ +from modules import API + +print(API.Mastodon("@blenderdumbass@mastodon.online")) diff --git a/modules/API.py b/modules/API.py index e6163c0..2e61baa 100644 --- a/modules/API.py +++ b/modules/API.py @@ -15,6 +15,8 @@ from datetime import datetime from modules import Set from modules.Common import * +API_cache = {} + def Get(url, data=None, headers={}): if data: @@ -48,3 +50,40 @@ def Petition(article): f = Set.Folder() with open(f+"/tabs"+article.get("url", "")+"/metadata.json", "w") as save: 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"] + + diff --git a/modules/Render.py b/modules/Render.py index a084129..641c75c 100644 --- a/modules/Render.py +++ b/modules/Render.py @@ -437,6 +437,10 @@ def MainPage(server): html = html + "" # Trending articles + Accounts = accounts() + owner = config.get("main_account", "") + mastodon = Accounts[owner].get("mastodon") + mastoposts = API.Mastodon(mastohead(mastodon), 8).copy() html = html + '
' @@ -445,6 +449,9 @@ def MainPage(server): for n, article in enumerate(trends): if n >= Buffer: break + + if n and n % 2 == 0 and mastoposts: + html = html + Mastoview(mastoposts.pop(0), owner) article = trends[article] html = html + ArticlePreview(article, Tabs, server.cookie) @@ -1643,6 +1650,62 @@ def ArticlePreview(article, Tabs, cookie=""): return html +def Mastoview(mastopost, user="", repost=False): + + html = """ + +
+ + """ + + url = mastopost.get("url", "") + content = mastopost.get("content", "") + + if content: + html = html + '' + if not repost: + html = html + '

[icon mastodon]' + html = html + ' On Mastodon

' + else: + html = html + "...repost

" + + if content: + html = html + "
" + + # 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 + '
[thumbnail]
' + + if not repost: + html = html + '

'+User(user)+'


' + else: + html = html + '

'+user+'


' + + if content: + likes = mastopost.get("favourites_count", 0) + comments = mastopost.get("replies_count", 0) + reposts = mastopost.get("reblogs_count", 0) + + html = html + '
⭐ '+str(likes)+' 💬 '+str(comments)+' 🔁 '+str(reposts)+'


' + + + 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 + '
\n' + + return html + def Footer(server): html = """