forked from tmhlsky/Instagram-API-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_all_comments.py
More file actions
51 lines (45 loc) · 1.55 KB
/
Copy pathget_all_comments.py
File metadata and controls
51 lines (45 loc) · 1.55 KB
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
from InstagramAPI import InstagramAPI
import time
from datetime import datetime
username = ''
pwd = ''
media_id = '1477006830906870775_19343908'
#stop conditions, the script will end when first of them will be true
until_date = '2017-03-31'
count = 100
API = InstagramAPI(username,pwd)
API.login()
API.getUsernameInfo()
has_more_comments = True
max_id = ''
comments = []
while has_more_comments:
_ = API.getMediaComments(media_id,max_id=max_id)
#comments' page come from older to newer, lets preserve desc order in full list
for c in reversed(API.LastJson['comments']):
comments.append(c)
has_more_comments = API.LastJson.get('has_more_comments',False)
#evaluate stop conditions
if count and len(comments)>=count:
comments = comments[:count]
#stop loop
has_more_comments = False
print "stopped by count"
if until_date:
older_comment = comments[-1]
dt=datetime.utcfromtimestamp(older_comment.get('created_at_utc',0))
#only check all records if the last is older than stop condition
if dt.isoformat()<=until_date:
#keep comments after until_date
comments = [
c
for c in comments
if datetime.utcfromtimestamp(c.get('created_at_utc',0)) > until_date
]
#stop loop
has_more_comments = False
print "stopped by until_date"
#next page
if has_more_comments:
max_id = API.LastJson.get('next_max_id','')
time.sleep(2)