From e79f0b1bee20b3664d297e94ea58bca2bdd1f052 Mon Sep 17 00:00:00 2001 From: deounix Date: Mon, 17 Sep 2018 00:44:31 +0300 Subject: [PATCH 1/3] Add user tags to post_photo function --- instagram_web_api/client.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/instagram_web_api/client.py b/instagram_web_api/client.py index 2719258d..ac4046df 100644 --- a/instagram_web_api/client.py +++ b/instagram_web_api/client.py @@ -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) @@ -871,9 +872,20 @@ 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": []} + if isinstance(user_tags, list): + for user_id in user_tags: + user_tags_["in"].append({"user_id": user_id, "position": [random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)]}) + elif isinstance(user_tags, str): + user_tags_["in"].append({"user_id": user_tags, "position": [random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)]}) + 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 From 40b4f33dfb5b7ee79b1d5b0e592d593800295ff3 Mon Sep 17 00:00:00 2001 From: Mohammad Almoghrabi Date: Mon, 17 Sep 2018 00:56:53 +0300 Subject: [PATCH 2/3] update client.py Update --- instagram_web_api/client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/instagram_web_api/client.py b/instagram_web_api/client.py index ac4046df..29ef592f 100644 --- a/instagram_web_api/client.py +++ b/instagram_web_api/client.py @@ -876,10 +876,14 @@ def post_photo(self, photo_data, caption='', user_tags=None): if user_tags: user_tags_ = {"in": []} 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": [random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)]}) elif isinstance(user_tags, str): user_tags_["in"].append({"user_id": user_tags, "position": [random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)]}) + else: + raise ValueError('user_tags must be list of users id or string with one user id') res = self._make_request( endpoint, headers=headers, From 40e63214e1a64a5dc85c1b97d97119a514629a4b Mon Sep 17 00:00:00 2001 From: Mohammad Almoghrabi Date: Mon, 17 Sep 2018 17:05:38 +0300 Subject: [PATCH 3/3] Update client.py for Codacy warning Add random number that is cryptographically secure in python. --- instagram_web_api/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/instagram_web_api/client.py b/instagram_web_api/client.py index 29ef592f..f33de36b 100644 --- a/instagram_web_api/client.py +++ b/instagram_web_api/client.py @@ -875,13 +875,14 @@ def post_photo(self, photo_data, caption='', user_tags=None): 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": [random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)]}) + user_tags_["in"].append({"user_id": user_id, "position": [cryptogen.random(), cryptogen.random()]}) elif isinstance(user_tags, str): - user_tags_["in"].append({"user_id": user_tags, "position": [random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)]}) + 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')