0
\$\begingroup\$

I am writing a bunch on integrations with some social media platforms and have been hacking around with the Facebook API. I would like a review on the code below as i'll most likely rinse and repeat it for the other socials.

from __future__ import annotations

from typing import Any, Dict, Iterable, NamedTuple

import facebook


class FacebookRequest(NamedTuple):

    # https://facebook-sdk.readthedocs.io/en/latest/api.html

    access_token: str
    version: str = "3.1"

    def build_graph(self) -> facebook.GraphAPI:
        return facebook.GraphAPI(
            access_token=self.access_token,
            version=self.version
        )


class FacebookPost(NamedTuple):

    created_time: str
    message: str
    id: str

    @classmethod
    def _from_data(cls, data: Dict[str, Any]) -> FacebookPost:
        return cls(
            created_time=data["created_time"],
            message=data["message"],
            id=data["id"]
        )

    @classmethod
    def get_my_posts(cls, g: facebook.GraphAPI) -> Iterable[FacebookPost]:
        return (
            cls._from_data(data)
            for data in g.get_object("/me/posts")["data"]
        )


if __name__ == "__main__":
    facebook_req = FacebookRequest(access_token="***")
    graph = facebook_req.build_graph()
    posts = FacebookPost.get_my_posts(graph)
    print(list(posts))

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy