-
Notifications
You must be signed in to change notification settings - Fork 701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix for user info (user info gql) #1868
base: master
Are you sure you want to change the base?
Conversation
__a=1&__d=dis no longer work , replaced with web_profile_info
While you probably found the correct workaround, this doesn't seem to be compatible with the already existing methods and setup of instagrapi. @subzeroid (or @tajbowness), as this is a rather important problem, do you possibly have the time to look into fixing this, based on this PR? The solution itself looks pretty easy and just needs to be fit into the existing code base. |
What do you mean ? Did you try and got any error ? I tried it and worked for me without any problem, the native public request of Instagrapi uses __a=1 and i bring plane requests to this part to fix it, you can use "extract_gql..." or not, i comment that line, if you use commented line, the result is exactly the same as before, you can easily get user info without need of login |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reviewed your code to highlight the current problems.
Of course, I am sure that it works right now. It is just not fitting the project structure and thus creating new issues and maintainability problems if it's merged like this.
Let me know if you need more help with refactoring your code.
instagrapi/mixins/user.py
Outdated
@@ -141,7 +141,10 @@ def user_info_by_username_gql(self, username: str) -> User: | |||
An object of User type | |||
""" | |||
username = str(username).lower() | |||
return extract_user_gql(self.public_a1_request(f"/{username!s}/")["user"]) | |||
headers = {'Host': 'www.instagram.com','X-Requested-With': 'XMLHttpRequest','Sec-Ch-Prefers-Color-Scheme': 'dark','Sec-Ch-Ua-Platform': '"Linux"','X-Ig-App-Id': '936619743392459','Sec-Ch-Ua-Model': '""','Sec-Ch-Ua-Mobile': '?0','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.112 Safari/537.36','Accept': '*/*','X-Asbd-Id': '129477','Sec-Fetch-Site': 'same-origin','Sec-Fetch-Mode': 'cors','Sec-Fetch-Dest': 'empty','Referer': 'https://www.instagram.com/','Accept-Language': 'en-US,en;q=0.9','Priority': 'u=1, i'} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you set up new headers here? Please use the existing methods. Otherwise, the user cannot set its own headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new endpoint for user info need some new headers and user-agent and the old headers wont work , this is the reason i put headers directly , and instagrapi users wont need to change headers in gql requests like this request , so the better idea is to use headers directly in endpoints like this , understood ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mh. But just giving new headers hard coded is not a clean way to go in that case, either. This might also result in Instagram banning clients, if they see requests with different user agents from the same IP.
So, how do the new headers differ from the old expected ones? Can they somehow get translated into each other? Or be set explicitly somewhere, like the old headers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as i said this is a public request (gql , or non auth request) , and the headers are same everytime you send request , even the current model uses same headers everytime (for public request) ( monitor the instagrapi requests so you can see ) , so instagram will not ban you ,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, if I set a different user agent, this won't get used for this request. Also, and I repeat, hardcoding the header here is the wrong place.
I am happy to assist you with improving the code quality. But I hope, you can understand why this is not good coding style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check the instagrapi gql requests , the headers are same everytime , it uses its own user agent , not logged-in client user agent (mobile), not base_headers , if you use another user agent for this request , it will return you "mismatch user agent error etc"
remove commented line
the new endpoint for user info need some new headers and user-agent and the old headers wont work , this is the reason i put headers directly , and instagrapi users wont need to change headers in gql requests like this request , so the better idea is to use headers directly in endpoints like this , understood ? commented line has been removed. thank you ! |
I hope, you understand my points. Hardcoding values like this and not relying on existing functions is in general a bad practice (maintainability, generalizability, ...). I got it that the endpoint probably requires different requests, so in that case, probably the way to go is to generalize the existing functions for the new use cases. |
as i said this is a public request (gql , or non auth request) , and the headers are same everytime you send request , even the current model uses same headers everytime (for public request) ( monitor the instagrapi requests so you can see ) , so instagram will not ban you , rotating headers is for private request ( authenticated requests , my pr is for non authenticated request for user info ) , now do you understand ? |
Here, you can see the current setup: instagrapi/instagrapi/mixins/public.py Line 209 in b83a00b
instagrapi/instagrapi/mixins/public.py Line 77 in b83a00b
|
this is for __a=1 , they removed that |
check the instagrapi gql requests , the headers are same everytime , it uses its own user agent , not logged-in client user agent (mobile), not base_headers , if you use another user agent for this request , it will return you "mismatch user agent error etc" |
My main argument stands. Hard coding this like you did is not good practice. Also, there is no error handling for the request. The already existing functions provide that already. So, I hope, you can find a way how to rely on them (or refactoring them appropriately). I will be of help, if needed. |
I meant hard coding like you did. You hard coded this in the wrong place, where it can't be reused. There is no error handling, no reliance on good, already existing functions. |
you can see hard-coded by the author in the link below, no problem in public request to hard-code headers instagrapi/instagrapi/mixins/public.py Line 69 in b83a00b
thats why i told you to check instagrapi public requests in mitm thank you for your contribution |
(You can also edit GitHub comments, no need to delete and send a new one) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats not personalized , its app id
instagrapi/mixins/user.py
Outdated
@@ -141,7 +141,10 @@ def user_info_by_username_gql(self, username: str) -> User: | |||
An object of User type | |||
""" | |||
username = str(username).lower() | |||
return extract_user_gql(self.public_a1_request(f"/{username!s}/")["user"]) | |||
headers = {'Host': 'www.instagram.com','X-Requested-With': 'XMLHttpRequest','Sec-Ch-Prefers-Color-Scheme': 'dark','Sec-Ch-Ua-Platform': '"Linux"','X-Ig-App-Id': '936619743392459','Sec-Ch-Ua-Model': '""','Sec-Ch-Ua-Mobile': '?0','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.112 Safari/537.36','Accept': '*/*','X-Asbd-Id': '129477','Sec-Fetch-Site': 'same-origin','Sec-Fetch-Mode': 'cors','Sec-Fetch-Dest': 'empty','Referer': 'https://www.instagram.com/','Accept-Language': 'en-US,en;q=0.9','Priority': 'u=1, i'} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check the instagrapi gql requests , the headers are same everytime , it uses its own user agent , not logged-in client user agent (mobile), not base_headers , if you use another user agent for this request , it will return you "mismatch user agent error etc"
Here, you can see some part of the already existing request error handling code: So I hope this helps you understand, why it is important to rely on existing functions and why just using |
the current code which i wrote in my pr will work , however if you mean adding error handling , you can do it by this :
but the custom headers still needed |
added self.public_request for better error handling.
Already better. But still needs some more refactoring, from my perspective. One possible problem now is this line: Not sure, but probably we don't want to update the internal headers with the ones that are only used for that request? Additionally, probably some more refactoring is sensible (like moving the custom headers to a different place. Defining them here like you do is not really sensible, as it won't allow any other functions to make use of them.). |
Correct we don't need to use these headers on the other methods so make sense to pass them directly to the self.public_request instead of update the headers |
Try to have a look at my referenced line again. It shows that if headers are passed to |
yeah i got you ,
|
Your solution fixes the problem, but I think, there is a better way: Refactoring some functions in Why? Because I am rather confident that the headers will be needed for other calls as well in the future. So one possible suggestion would be adding another function similar to That function will have the headers (there, they can probably be hardcoded, then), and it will call I suggest though, to also refactor This was kind of what I had in mind all along, and I thought, it was rather obvious, but apparently, my suggestions weren't clear to you, before. What do you think? |
user info gql fix + implemented in public_request + avoid headers to be updated
user info gql fix + implemented in public_request + avoid headers to be updated
check the changes i made now , i added update_headers=False and i think everything is good now and enough for this step |
user info gql fix + implemented in public_request + avoid headers to be updated
Thanks, some more nitpicks, though. |
@subzeroid, can you give a review and possibly make some calls on discussed topics? |
Just add one thing, hardcoding user-agent can lead to auto-detection of the bot on the Instagram-side so I would recommend actually making it exactly the same globally for every request or at least being able to set it as a config variable. |
Hi, thx for your attention |
Forgotten comma on that line. also:
No Json imported for json.loads Also:
Looks like user.py:147 has a update_headers after () from public request |
fix missing comma
import json
hi, thanks for the mentioning |
@SaeidB Works now, thanks, also I see that there are some conflicts so you need to resolve it before anyone will merge it |
Will this fix be merged soon? |
hello, i dunno really, but it works and you can add this to your lib even if it didn't merge |
The prior pull request still has an issue but this will work: #2003 |
how would I add this to my lib? |
Can it still be used now? |
hi, yes |
The only actual additional header you need is edit: |
This will get it working, however I've glossed over 2 fields that may be needed by other things
|
__a=1&__d=dis no longer work , replaced with web_profile_info