📜 ⬆️ ⬇️

What to do if Instagram did not give access to the API? Addition

Hello again! I read it and it seemed to me that it could be continued.

image

It's no secret that the most popular and profitable platform for advertising, business and other things is Instagram. Why they became just a service, in which at first it was possible to upload only pictures of a certain size (the aspect ratio means) and there was absolutely nothing that was on the social networks of that time - it is completely incomprehensible, but fact is a fact. Because of this, everyone is trying to get on the Instagram site and grab the largest number of audience from there, and they do it, of course, not manually. And it follows that Instagram rigidly blocks access for bots, spammers and so on, so that the network remains clean.

  1. The most useful functions (posting and deleting posts) are available only from the Instagram mobile application, the emulation of requests is complicated, as you need to pull out the key from the application, which is updated with each new version.
  2. The web version is cut off, but I am glad that it has the ability to like, comment and delete comments.
  3. There is an API, but the procedure for obtaining it is depressingly long and for spammers and bots this way just does not shine. Plus, there were many moments when API agreements changed, which is not always convenient.

Although I contacted Instagram not to make another spam bot that can subscribe and like, but I really didn’t want to bother with getting the Instagram API, so I had to write my own library for interacting with Instagram.
')
I would like to say that working with the Instagram web version is very pleasant for two reasons:

1. On any page you can get brief information if you send a GET request of the form:

https://instagram.com/zuck/?__a=1 

And the answer is JSON with available information, the first 10 posts of the page and so on. Very nice.

2. If there is not enough brief information, then there is one more good news. You can upload photos, subscriptions, comments using a specific query of the form

 https://www.instagram.com/graphql/query/?query_id=17888483320059182&variables=... 

where variables are passed variables for processing in JSON format. The answer is also JSON. And in general, it is obvious that this all works in GraphQL, so you can even google it in order to understand how requests are processed.

Based on knowledge data and built the entire library. I will briefly describe how it can be used, maybe someone will come in handy. By the way, I indicated the BSD 3 license in the repository. Tell me, maybe I should change it so that there are no difficulties?

Installation


Install it is not necessary. More precisely, I was too lazy to prescribe all sorts of setup.py or package when the library consists of just one file. Therefore, there is just an instagram.py file, which is connected as follows:

 import instaparser 

How to use it?


Interaction with Instagram is possible either with authorization or without. Without authorization, there are no functions for viewing subscriptions and subscribers, and it is obviously impossible to like something, comment something, and so on. The only restrictions with authorization: it is impossible to lay out posts and delete them.

I will give an example of interaction without authorization:

 from instaparser.agents import Agent from instaparser.entities import Account agent=Agent() account=Account("zuck") agent.update(account) media=agent.get_media(account, count=100) for m in media: print(m) 

As you understand, this script downloads information about Mark Zuckerberg’s page, loads the last 100 posts from his page and displays them on the screen.

I want to say that if I had not written

 agent.update(account) 

then it would not work to download posts, since no information about the Zuckerberg page was known.

Here is an example with authorization:

 from instaparser.agents import AgentAccount from instaparser.entities import Account agent=AgentAccount("oleg_yurchik", "imasuperpassword") agent.update() account=Account("zuck") agent.update(account) # and etc. 

This is the so-called Hello, world! . Or a quick start.

And now I will tell you more:

Instagram, in fact, has only 6 entities:

  1. Account
  2. Fast
  3. Geolocation
  4. Comment
  5. Hashtag
  6. Storys

Everything else is just lists of these entities, such as likes, subscriptions, subscribers, and more. And for each entity there is a class. For accounts - Account, posts - Media, geolocations - Location, comments - Comment, hashtags - Tag, stories - Story. And each of them (except comments) need to be updated before you work with it. That is, if you want to download all your posts, go through them and get a list of geolocations, then you need to do the following:

 from instaparser.agents import AgentAccoun agent=AgentAccount('oleg.yurchik', 'anothersuperpassword') agent.update() media=agent.get_media(count=agent.media_count) locations=[] for m in media: agent.like(m) agent.update(m) if m.location: locations.append(m.location) 

And if you later need to get the last 10 posts on a specific geolocation, then you will need to do the following:

 agent.update(location) media=agent.get_media(location, count=10) 

I had to take out the function of updating the account from initialization, because if you need to get all the subscribers, for example, the program would update each of the accounts, but this is not good.

The library is based on the requests library, and I consider that one of the tokens is that you can also pass additional parameters for requests to the methods. This idea came to me when I first got a 429 error from Instagram. It was necessary to use a proxy.

For example, you can do this:

 media=agent.get_media(count=agent.media_count, settings={'proxies': {'https': '127.0.0.1:80'}}) 

where 127.0.0.1:80 - you can specify your proxy

Also, another chip, I think, may be intercepting errors.

In the Agent and AgentAccount classes (the ones that communicate with Instagram) there is a dictionary organized as a tree, it is called exception_actions . It stores exception classes in the form of keys, and functions in the form of values. If any error occurs, it is intercepted and the function is executed from the dictionary. This function is passed an exception object and parameters with which the request was executed. It can perform some action and return the changed (or not) query parameters. The request will be repeated again. And it will be repeated as many times as indicated in the parameter Agent.repeats. The default is 1.

And you can not worry about memory overflow.

The class of each entity has a dictionary in which all objects of this class are stored (or even objects of the subclass). Thus, if you accidentally create, for example, an account that has already been created, the designer will return a link to the previously created account to you.

If you accidentally missed the link to the repository in the text, then here it is again .

And finally, I will say that due to some decisions some problems have appeared:

  1. For example, the problem when re-creating the object. If suddenly you want to use the account as a worker and interact through it, and it has already been created as a normal account, then create it again does not work. I don’t know how to solve it yet.
  2. Error trapping sometimes behaves very strange and is not fully tested.

I really hope that maybe this solution will be useful to someone, I hope for any useful comments and help in finishing this thing. In the article I mentioned, I gave an example of such a script in PHP, but it was only collecting information and, in my opinion, it worked only with the old version of the Instagram web interface.

Thanks for attention.

Source: https://habr.com/ru/post/339620/


All Articles