Skip to content

Commit b17e6ce

Browse files
author
ikronskun
committed
refactor(:recycle: change paged method.): now if use loop will use paged method. and add posts test.
1 parent a0daa0e commit b17e6ce

2 files changed

Lines changed: 318 additions & 48 deletions

File tree

pyfacebook/api.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -232,49 +232,14 @@ def get_page_info(self, page_id=None, username=None, return_json=False):
232232
else:
233233
return Page.new_from_json_dict(data)
234234

235-
def get_posts_paged(self,
236-
target=None,
237-
next_page=None,
238-
args=None):
239-
"""
240-
Fetch the paging posts data for given page.
241-
242-
Args:
243-
target:
244-
The ID or username for which page you want to retrieve data.
245-
next_page (str, optional):
246-
The paging next page url. for begin it is None.
247-
args:
248-
Relative params you want to retrieve data. Now is pointed.
249-
250-
Returns:
251-
next_page (str), previous_page (str), list of pyfacebook.Post instances.
252-
"""
253-
if next_page is None:
254-
path = '{0}/{1}/{2}'.format(self.version, target, 'posts')
255-
else:
256-
parse_path = urlparse(next_page)
257-
path = parse_path.path
258-
args = dict(parse_qsl(parse_path.query))
259-
resp = self._request(
260-
method='GET',
261-
path=path,
262-
args=args
263-
)
264-
next_page, previous_page = None, None
265-
data = self._parse_response(resp.content.decode('utf-8'))
266-
if 'paging' in data:
267-
next_page = data['paging'].get('next')
268-
previous_page = data['paging'].get('previous')
269-
result = [Post.new_from_json_dict(item) for item in data['data']]
270-
return next_page, previous_page, result
271-
272235
def get_posts(self,
273236
page_id=None,
274237
username=None,
275238
since_time=None,
276239
until_time=None,
277-
count=10):
240+
count=10,
241+
limit=10,
242+
return_json=False):
278243
"""
279244
Obtain give page's posts info.
280245
@@ -290,8 +255,12 @@ def get_posts(self,
290255
The posts retrieve until time.
291256
If neither since_time or until_time, it will by now time.
292257
count (int, optional)
293-
The count is each request get the result count. For posts it should no more than 100.
294-
258+
The count will retrieve posts.
259+
limit (int, optional)
260+
Each request retrieve posts count from api.
261+
For posts it should no more than 100.
262+
return_json (bool, optional):
263+
If True JSON data will be returned, instead of pyfacebook.Post, or return origin data by facebook.
295264
Returns:
296265
posts info list.
297266
"""
@@ -306,23 +275,28 @@ def get_posts(self,
306275
'fields': ','.join(set(constant.POST_BASIC_FIELDS + constant.POST_REACTIONS_FIELD)),
307276
'since': since_time,
308277
'until': until_time,
309-
'limit': count,
278+
'limit': limit,
310279
}
311280

312-
result = []
281+
posts = []
313282
next_page = None
314283

315284
while True:
316-
next_page, previous_page, posts = self.get_posts_paged(
285+
next_page, previous_page, data = self.paged_by_next(
286+
resource='posts',
317287
target=target,
318288
next_page=next_page,
319289
args=args
320290
)
321-
result += posts
291+
if return_json:
292+
posts += data.get('data', [])
293+
else:
294+
posts += [Post.new_from_json_dict(item) for item in data['data']]
322295
if next_page is None:
323296
break
324-
325-
return result
297+
if len(posts) >= count:
298+
break
299+
return posts[:count]
326300

327301
def get_post_info(self,
328302
post_id=None,
@@ -383,6 +357,9 @@ def paged_by_next(self,
383357
else:
384358
parse_path = urlparse(next_page)
385359
path = parse_path.path
360+
# now the path has begin with /
361+
if path.startswith('/'):
362+
path = path[1:]
386363
args = dict(parse_qsl(parse_path.query))
387364

388365
resp = self._request(
@@ -430,7 +407,7 @@ def get_comments(self,
430407
Each request retrieve comments count from api.
431408
For comments. Should not more than 100.
432409
return_json (bool, optional):
433-
If True JSON data will be returned, instead of pyfacebook.Post, or return origin data by facebook.
410+
If True JSON data will be returned, instead of pyfacebook.Comment, or return origin data by facebook.
434411
Returns:
435412
This will return tuple.
436413
(Comments set, CommentSummary's data)

0 commit comments

Comments
 (0)