forked from Julian-O/Instagram-API-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstagram_api.py
More file actions
107 lines (81 loc) · 3.74 KB
/
Copy pathinstagram_api.py
File metadata and controls
107 lines (81 loc) · 3.74 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from .base import AuthenticationError
from .endpoints import InstagramAPIEndPoints
class InstagramAPI(InstagramAPIEndPoints):
""" Most of the available calls are defined in the InstagramAPIEndPoints.
This subclass adds additional helper functions to make common operations easier.
Clients should use this class.
"""
# Make visible to clients for ease of reference.
AuthenticationError = AuthenticationError
def __init__(self, username, password, two_factor_callback=None):
InstagramAPIEndPoints.__init__(self, username, password, two_factor_callback)
# Helper functions to gather complete lists/deal with pagination.
def followers_iter(self, username=None, delay_between_calls=0):
"""
Yields a series of dictionaries describing each user that follows this user.
"""
username = username or self._loggedinuserid
for item in self._iterator_template(
lambda max_id: self.get_user_followers(username, max_id),
field="users",
delay_between_calls=delay_between_calls):
yield item
def followings_iter(self, username=None, delay_between_calls=0):
"""
Yields a series of dictionaries describing each user that this user follows
If username is None, use logged in user.
"""
username = username or self._loggedinuserid
for item in self._iterator_template(
lambda max_id: self.get_user_followings(username, max_id),
field="users",
delay_between_calls=delay_between_calls):
yield item
def userfeed_iter(self, username=None, min_timestamp=None, delay_between_calls=0):
"""
Yields a series of dictionaries describing this user's feed.
If username is None, use logged in user.
"""
username = username or self._loggedinuserid
for item in self._iterator_template(
lambda max_id: self.get_user_feed(username, max_id, min_timestamp),
field="items",
delay_between_calls=delay_between_calls):
yield item
def likedmedia_iter(self, delay_between_calls=0):
"""
Yields a series of dictionaries describing liked media.
Note: Never ends.
"""
for item in self._iterator_template(
self.get_liked_media,
field="items",
delay_between_calls=delay_between_calls):
yield item
def media_comments_iter(self, media_id, delay_between_calls=0):
"""
Yields a series of dictionaries describing media comments.
"""
for item in self._iterator_template(
lambda max_id: self.get_media_comments(media_id, max_id),
field="comments",
delay_between_calls=delay_between_calls):
yield item
# Helper functions to find out information about the logged in user.
#
# Consider replacing these with None defaults for userid.
def self_geo_media(self):
return self.get_geo_media(self._loggedinuserid)
def self_user_feed(self, max_id='', min_timestamp=None):
return self.get_user_feed(self._loggedinuserid, max_id, min_timestamp)
def self_user_followings(self):
return self.get_user_followings(self._loggedinuserid)
def self_user_followers(self, max_id=''):
return self.get_user_followers(self._loggedinuserid, max_id=max_id)
def self_user_info(self):
return self.get_username_info(self._loggedinuserid)
def self_user_tag(self):
return self.get_user_tags(self._loggedinuserid)