Skip to content

Instantly share code, notes, and snippets.

@yhenon
Created April 10, 2017 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhenon/24a2f78d0f3fd5ed089ef67fc0f4a44f to your computer and use it in GitHub Desktop.
Save yhenon/24a2f78d0f3fd5ed089ef67fc0f4a44f to your computer and use it in GitHub Desktop.
import requests
from requests.auth import HTTPBasicAuth
import json
import time
username = 'xxxxxxx'
api_token = 'xxxxxxx'
mean_likes = {
'kissinfashion': 23845,
'instagood': 9656,
'beautifuldestinations': 172748,
'etdieucrea': 2542,
'josecabaco': 370 }
query_url = 'http://challenges.instagram.unpossib.ly/api/live'
submit_url = 'http://challenges.instagram.unpossib.ly/api/submissions'
my_auth = HTTPBasicAuth(username, api_token)
submitted_posts = set()
while True:
try:
get_resp = requests.get(url=query_url, auth=my_auth)
if get_resp.status_code != 200:
print('Warning {}'.format(get_resp.text))
time.sleep(10)
continue
data = json.loads(get_resp.text)
if not data['success']:
time.sleep(10)
continue
else:
acct_list = data['accounts']
for account in acct_list:
username = account['username']
num_likes = mean_likes[username]
if len(account['posts']) == 0:
print('No new posts for account {}'.format(username))
for post in account['posts']:
post_id = post['instagram']['id']
if post_id in submitted_posts:
continue
submission = {'post':post_id,'likes':num_likes}
post_resp = requests.post(url=submit_url, auth=my_auth, data=json.dumps(submission))
print('Submitted response: {} likes for post {}'.format(num_likes,post_id))
print('Response: {}'.format(post_resp.text))
if post_resp.status_code == 200:
submitted_posts.add(post_id)
time.sleep(10)
except Exception as e:
print(e)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment