From e725ca2640f83685cb09646cf5179bc3272b9f85 Mon Sep 17 00:00:00 2001 From: Alexander Graf <17130992+aandergr@users.noreply.github.com> Date: Wed, 25 Mar 2020 17:13:46 +0100 Subject: [PATCH] Post properties is_sponsored and sponsor_users This allows to access the Paid Partnership With metadata of a Post. Closes #295. --- instaloader/structures.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/instaloader/structures.py b/instaloader/structures.py index 8160751..0348b84 100644 --- a/instaloader/structures.py +++ b/instaloader/structures.py @@ -413,6 +413,30 @@ class Post: lambda d: d['data']['shortcode_media']['edge_liked_by'], self._rhx_gis)) + @property + def is_sponsored(self) -> bool: + """ + Whether Post is a sponsored post, equivalent to non-empty :meth:`Post.sponsor_users`. + + .. versionadded:: 4.4 + """ + try: + sponsor_edges = self._field('edge_media_to_sponsor_user', 'edges') + except KeyError: + return False + return bool(sponsor_edges) + + @property + def sponsor_users(self) -> List['Profile']: + """ + The Post's sponsors. + + .. versionadded:: 4.4 + """ + return ([] if not self.is_sponsored else + [Profile(self._context, edge['node']['sponsor']) for edge in + self._field('edge_media_to_sponsor_user', 'edges')]) + @property def location(self) -> Optional[PostLocation]: """