forked from ping/instagram_private_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.py
63 lines (53 loc) · 2.22 KB
/
pagination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import os.path
import logging
import argparse
try:
from instagram_private_api import (
Client, __version__ as client_version)
except ImportError:
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from instagram_private_api import (
Client, __version__ as client_version)
if __name__ == '__main__':
logging.basicConfig()
logger = logging.getLogger('instagram_private_api')
logger.setLevel(logging.WARNING)
# Example command:
# python examples/savesettings_logincallback.py -u "yyy" -p "zzz" -settings "test_credentials.json"
parser = argparse.ArgumentParser(description='Pagination demo')
parser.add_argument('-u', '--username', dest='username', type=str, required=True)
parser.add_argument('-p', '--password', dest='password', type=str, required=True)
parser.add_argument('-debug', '--debug', action='store_true')
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
print('Client version: {0!s}'.format(client_version))
api = Client(args.username, args.password)
# ---------- Pagination with max_id ----------
user_id = '2958144170'
updates = []
results = api.user_feed(user_id)
updates.extend(results.get('items', []))
next_max_id = results.get('next_max_id')
while next_max_id:
results = api.user_feed(user_id, max_id=next_max_id)
updates.extend(results.get('items', []))
if len(updates) >= 30: # get only first 30 or so
break
next_max_id = results.get('next_max_id')
updates.sort(key=lambda x: x['pk'])
# print list of IDs
print(json.dumps([u['pk'] for u in updates], indent=2))
# ---------- Pagination with rank_token and exclusion list ----------
rank_token = Client.generate_uuid()
has_more = True
tag_results = []
while has_more and rank_token and len(tag_results) < 60:
results = api.tag_search(
'cats', rank_token, exclude_list=[t['id'] for t in tag_results])
tag_results.extend(results.get('results', []))
has_more = results.get('has_more')
rank_token = results.get('rank_token')
print(json.dumps([t['name'] for t in tag_results], indent=2))