import datetime import requests import io """ Interface The bridge between web application(niming) and igapi. An interface should have a get() function. """ HOST="http://172.16.20.145:3000" def get(index:int, media:bool=True) -> dict | None: """ Every interface should have this function. Backend calls this function to get data of a post. Args: index (int): ID of the post media (bool): Send media or not Returns: dict: Data of a post. Formatted as shown in Note 1 (an_example_of_context) None: An error occurred, and nothing was returned. """ # get data ## fake server in localhost ### where is the api of web application? res = requests.get(f"{HOST}/api/post?cursor={index}") if res.status_code != 200: return None rj = res.json()[0] media_arr = [] # get media if media and rj["enclosure"] is not None: for m in rj["enclosure"]: _m = requests.get(f"{HOST}/static/{m}") if _m.status_code == 200: media_arr.append(io.BytesIO(_m.content)) # return result = { "id": rj["id"], "metadata": { "create_time": datetime.datetime.strptime(rj["post_at"], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=datetime.timezone.utc), "author": rj["signing"], "tags": [], "category": "", # ext "parent": None #rj["parent"] # parent id }, "content": { "text": rj["content"], "media": media_arr } } return result # Note 1 an_example_of_context = { "id": int, "metadata": { "create_time": datetime.datetime, "author": str, "tags": list[str], "category": str, # ext "parent": int | None # parent id }, "content": { "text": str, "media": [ io.BytesIO ] } }