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 + '