onegram
A simplistic api-like instagram bot powered by requests.
Warnings!
- This isn't an official api. Use at your own risk.
- Make sure you have the latest version installed. To update:
pip install -U onegram
. - Default rate limits are not configured properly, so adjust them to not get banned!
Installation
pip install onegram
Dependencies
Python 3.6
Examples
Follow someone
from onegram import follow
follow('<someone>')
Like all someone posts
from onegram import like, posts
for post in posts('<someone>'):
like(post)
Who likes you most?
from collections import defaultdict
from operator import itemgetter
from onegram import posts, likes
rank = defaultdict(int)
for post in posts():
for like_info in likes(post):
username = like_info['username']
rank[username] += 1
rank = sorted(rank.items(), key=itemgetter(1), reverse=True)
print(rank[:10]) # TOP 10!
Explicit login (optional)
from onegram import Login, posts
with Login(username='user', password='pass'):
user_posts = list(posts())
other_posts = list(posts('other'))
Also possible
from onegram import *
login()
last_post = next(posts())
post_likes = list(likes(last_post))
logout()
Cheatsheet
The set of functions are divided into queries and actions. For some queries the user is optional, with default set to the logged user.
Query | Argument(s) | Action | Argument(s) | |
---|---|---|---|---|
user_info |
[user] |
follow |
user |
|
followers |
[user] |
unfollow |
user |
|
following |
[user] |
like |
post |
|
posts |
[user] |
unlike |
post |
|
post_info |
post |
comment |
comment[, post] |
|
likes |
post |
uncomment |
comment[, post] |
|
comments |
post |
save |
post |
|
feed |
unsave |
post |
||
explore |
[tag] |
Rate Limits
Settings can be overridden if you use one of the explicit login forms (defaults: onegram/settings.py
). It's possible to define a fixed User-Agent
, for example.
Overall, the most userful setting is RATE_LIMITS
, where you can set rate limits for:
- Each query or action
- All queries or all actions
- All queries and actions
Example
from onegram import login
minute = 60
hour = minute * 60
day = hour * 24
rate_limits = {
'queries': [(1, 1)], # One query per second
'actions': [(1, 3)], # One action per 3 seconds
'like': [
(5, minute), # A maximum of 5 likes in a minute
(100, day) # A maximum of 100 likes in a day
],
'comment': [(1, 5)], # One comment per 5 seconds
}
# You can use '*' to set limits for all queries and actions.
settings = {
'RATE_LIMITS': rate_limits,
'RATE_PERSIST_ENABLED': True, # enabled by default
'RATE_PERSIST_DIR': '.onegram/rates' # default directory
}
login(custom_settings=settings)
# ...
Notice that you can specify a greedy or patient behaviour, or both. This is possible because you can have different rates for different time intervals.
For example, both (10, 10)
and (1, 1)
will do 10 requests in 10 seconds, but the first
one is greedy. It will make 10 requests and wait 10 seconds to continue, whereas the second one will make one request at each second.
As general rule:
Greedy = (times, seconds)
Patient = (1, seconds/times)
Tips
- Export your credentials so you don't have to type it:
export INSTA_USERNAME=username export INSTA_PASSWORD=password