Skip to content
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

Add user tags to post_photo function #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions instagram_web_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,13 @@ def search(self, query_text):
return res

@login_required
def post_photo(self, photo_data, caption=''):
def post_photo(self, photo_data, caption='', user_tags=None):
"""
Post a photo

:param photo_data: byte string of the image
:param caption: caption text
:param user_tags: user's id
"""
warnings.warn('This endpoint has not been fully tested.', UserWarning)

Expand Down Expand Up @@ -871,9 +872,25 @@ def post_photo(self, photo_data, caption=''):
headers['Content-Type'] = 'application/x-www-form-urlencoded'
del headers['Content-Length']
endpoint = 'https://www.instagram.com/create/configure/'

if user_tags:
user_tags_ = {"in": []}
cryptogen = random.SystemRandom()
if isinstance(user_tags, list):
if len(user_tags) > 60:
warnings.warn('Adding more than 60 users will put your account at banned risk', UserWarning)
for user_id in user_tags:
user_tags_["in"].append({"user_id": user_id, "position": [cryptogen.random(), cryptogen.random()]})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forcibly putting user tags at random positions is not a good idea at all imo.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe adding an array with the x and y positions of each tag? i like the tagging idea, but i agree with you, adding it at random is very bad.

elif isinstance(user_tags, str):
user_tags_["in"].append({"user_id": user_tags, "position": [cryptogen.random(), cryptogen.random()]})
else:
raise ValueError('user_tags must be list of users id or string with one user id')

res = self._make_request(
endpoint, headers=headers,
params={'upload_id': upload_id, 'caption': caption},
params={'upload_id': upload_id, 'caption': caption} \
if not user_tags else \
{'upload_id': upload_id, 'caption': caption, 'usertags': user_tags_},
get_method=lambda: 'POST')
return res

Expand Down