88from pyfacebook .api .base_resource import BaseResource
99from pyfacebook .models .ig_business_models import (
1010 IgBusUser ,
11- IgBusMediaResponse ,
12- IgBusPublishLimit ,
11+ IgBusDiscoveryUserResponse ,
12+ IgBusDiscoveryUserMediaResponse ,
13+ IgBusPublishLimitResponse ,
14+ IgBusInsightsResponse ,
1315)
1416from pyfacebook .utils .params_utils import enf_comma_separated
1517
@@ -50,16 +52,16 @@ def discovery_user(
5052 username : str ,
5153 fields : Optional [Union [str , list , tuple ]] = None ,
5254 return_json : bool = False ,
53- ) -> Union [IgBusUser , dict ]:
55+ ) -> Union [IgBusDiscoveryUserResponse , dict ]:
5456 """
5557 Get other business account info by username.
5658
5759 :param username: Username to get data.
5860 :param fields: Comma-separated id string for data fields which you want.
5961 You can also pass this with an id list, tuple.
60- :param return_json: Set to false will return a dataclass for IgBusUser .
62+ :param return_json: Set to false will return a dataclass for IgBusDiscoveryUserResponse .
6163 Or return json data. Default is false.
62- :return: IG Business User information.
64+ :return: Business discovery user response information.
6365 """
6466
6567 if fields is None :
@@ -72,9 +74,9 @@ def discovery_user(
7274 fields = f"business_discovery.username({ username } ){{{ metric } }}" ,
7375 )
7476 if return_json :
75- return data [ "business_discovery" ]
77+ return data
7678 else :
77- return IgBusUser .new_from_json_dict (data = data [ "business_discovery" ] )
79+ return IgBusDiscoveryUserResponse .new_from_json_dict (data = data )
7880
7981 def discovery_user_medias (
8082 self ,
@@ -84,7 +86,7 @@ def discovery_user_medias(
8486 after : Optional [str ] = None ,
8587 before : Optional [str ] = None ,
8688 return_json : bool = False ,
87- ) -> Union [IgBusMediaResponse , dict ]:
89+ ) -> Union [IgBusDiscoveryUserMediaResponse , dict ]:
8890 """
8991 Get other business account's media by username.
9092
@@ -95,9 +97,9 @@ def discovery_user_medias(
9597 It should no more than 100. Default is None will use api default limit.
9698 :param after: The cursor that points to the end of the page of data that has been returned.
9799 :param before: The cursor that points to the start of the page of data that has been returned.
98- :param return_json: Set to false will return a dataclass for IgBusMediaResponse .
100+ :param return_json: Set to false will return a dataclass for IgBusDiscoveryUserMediaResponse .
99101 Or return json data. Default is false.
100- :return: Media response information.
102+ :return: Business discovery media response information.
101103 """
102104
103105 if fields is None :
@@ -119,25 +121,24 @@ def discovery_user_medias(
119121 fields = f"business_discovery.username({ username } ){{media{ after_str } { before_str } { limit_str } {{{ metric } }}}}" ,
120122 )
121123
122- media = data ["business_discovery" ]["media" ]
123124 if return_json :
124- return media
125+ return data
125126 else :
126- return IgBusMediaResponse .new_from_json_dict (media )
127+ return IgBusDiscoveryUserMediaResponse .new_from_json_dict (data )
127128
128129 def get_content_publishing_limit (
129130 self ,
130131 fields : Optional [Union [str , list , tuple ]] = None ,
131132 return_json : bool = False ,
132- ) -> Union [IgBusPublishLimit , dict ]:
133+ ) -> Union [IgBusPublishLimitResponse , dict ]:
133134 """
134135 Get user's current content publishing usage.
135136
136137 :param fields: Comma-separated id string for data fields which you want.
137138 You can also pass this with an id list, tuple.
138- :param return_json: Set to false will return a dataclass for PublishLimit .
139+ :param return_json: Set to false will return a dataclass for IgBusPublishLimitResponse .
139140 Or return json data. Default is false.
140- :return: IG Business content publish limit information.
141+ :return: Business content publish limit response information.
141142 """
142143
143144 if fields is None :
@@ -149,6 +150,55 @@ def get_content_publishing_limit(
149150 fields = enf_comma_separated (field = "fields" , value = fields ),
150151 )
151152 if return_json :
152- return data ["data" ][0 ]
153+ return data
154+ else :
155+ return IgBusPublishLimitResponse .new_from_json_dict (data )
156+
157+ def get_insights (
158+ self ,
159+ metric : Union [str , list , tuple ],
160+ period : str ,
161+ since : Optional [str ] = None ,
162+ until : Optional [str ] = None ,
163+ user_id : Optional [str ] = None ,
164+ access_token : Optional [str ] = None ,
165+ return_json : bool = False ,
166+ ) -> Union [IgBusInsightsResponse , dict ]:
167+ """
168+ Get social interaction metrics on an IG User.
169+
170+ :param metric: Comma-separated id string for insights metrics which you want.
171+ You can also pass this with an id list, tuple.
172+ :param period: Period to aggregation data.
173+ Accepted parameters: lifetime,day,week,days_28
174+ :param since: Lower bound of the time range to fetch data. Need Unix timestamps.
175+ :param until: Upper bound of the time range to fetch data. Need Unix timestamps.
176+ Notice: time range may not more than 30 days.
177+ :param user_id: ID for business user to get insights.
178+ :param access_token: Access token with permissions for user_id.
179+ :param return_json: Set to false will return a dataclass for Insights.
180+ Or return json data. Default is false.
181+ :return: Business user insights response information.
182+ """
183+
184+ if user_id is None :
185+ user_id = self .client .instagram_business_id
186+
187+ args = {
188+ "metric" : enf_comma_separated (field = "metric" , value = metric ),
189+ "period" : period ,
190+ "since" : since ,
191+ "until" : until ,
192+ }
193+ if access_token :
194+ args ["access_token" ] = access_token
195+
196+ data = self .client .get_connection (
197+ object_id = user_id ,
198+ connection = "insights" ,
199+ ** args ,
200+ )
201+ if return_json :
202+ return data
153203 else :
154- return IgBusPublishLimit .new_from_json_dict (data [ "data" ][ 0 ] )
204+ return IgBusInsightsResponse .new_from_json_dict (data )
0 commit comments