Last active
December 23, 2015 03:29
-
-
Save geduldig/6573973 to your computer and use it in GitHub Desktop.
Search instagram by user name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import json | |
| from datetime import datetime | |
| import os | |
| import time | |
| import urllib | |
| SEARCH_OLD = True | |
| USER = 'boxnumber03' | |
| PHOTO_DIR = './photos' | |
| COUNT = 10 | |
| params = {} | |
| #params['client_id'] = ... | |
| params['access_token'] = ... | |
| params['count'] = COUNT | |
| def get_userid(user): | |
| params['q'] = user | |
| r = requests.request('GET', 'https://api.instagram.com/v1/users/search', params=params) | |
| del params['q'] | |
| j = r.json() | |
| if 'data' in j and len(j['data']) > 0: | |
| return j['data'][0]['id'] | |
| else: | |
| return None | |
| def print_data(item, brief=True): | |
| print(item['id']) | |
| print('%s - %s' % (item['created_time'], datetime.fromtimestamp(int(item['created_time'])))) | |
| if not brief: | |
| user = item['user'] | |
| print(user['id']) | |
| print(user['username']) | |
| print(user['full_name']) | |
| print(user['profile_picture']) | |
| if item['location']: | |
| print(item['location']['latitude'], item['location']['longitude']) | |
| print(item['type']) | |
| if 'images' in item: | |
| print(item['images']['standard_resolution']['url']) | |
| if 'videos' in item: | |
| print(item['videos']['standard_resolution']['url']) | |
| if item['caption']: | |
| print(item['caption']['text']) | |
| print(item['tags']) | |
| print(item['likes']['count']) | |
| def get_photo(item): | |
| if item['type'] == 'image': | |
| photo_url = item['images']['standard_resolution']['url'] | |
| name = item['user']['username'] | |
| file_name = os.path.join(PHOTO_DIR, name) + '.' + photo_url.split('.')[-1] | |
| urllib.urlretrieve(photo_url, file_name) | |
| url = 'https://api.instagram.com/v1/users/%s/media/recent' % get_userid(USER) | |
| while True: | |
| print('-------------------------------------') | |
| r = requests.request('GET', url, params=params) | |
| print(r.status_code) | |
| j = r.json() | |
| if 'data' in j and len(j['data']) > 0: | |
| for item in j['data']: | |
| print_data(item) | |
| get_photo(item) | |
| if SEARCH_OLD: | |
| if 'next_max_id' in j['pagination']: | |
| params['max_id'] = j['pagination']['next_max_id'] | |
| else: | |
| break; | |
| else: | |
| if 'next_min_id' in j['pagination']: | |
| params['min_id'] = j['pagination']['next_min_id'] | |
| else: | |
| break; | |
| time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment