|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +from instagram.client import InstagramAPI |
| 5 | + |
| 6 | +client_id = os.environ.get('IG_100D_CLIENT_ID') |
| 7 | +client_secret = os.environ.get('IG_100D_CLIENT_SECRET') |
| 8 | + |
| 9 | +IG_USER_ID = '4799' # bbelderbos |
| 10 | +SCOPE = '' |
| 11 | +REDIRECT_URL = 'http://127.0.0.1:5000' |
| 12 | + |
| 13 | +if not client_id or not client_secret: |
| 14 | + print('Please set the following env vars: ') |
| 15 | + print('IG_100D_CLIENT_ID') |
| 16 | + print('IG_100D_CLIENT_SECRET') |
| 17 | + sys.exit(1) |
| 18 | + |
| 19 | + |
| 20 | +def _get_access_token(): |
| 21 | + api = InstagramAPI(client_id=client_id, |
| 22 | + client_secret=client_secret, |
| 23 | + redirect_uri=REDIRECT_URL) |
| 24 | + redirect_uri = api.get_authorize_login_url(scope=SCOPE) |
| 25 | + |
| 26 | + print("Visit this page in browser and authorize access:\n", redirect_uri) |
| 27 | + code = input("Paste in code in query string after redirect: ").strip() |
| 28 | + |
| 29 | + access_token, user_info = api.exchange_code_for_access_token(code) |
| 30 | + |
| 31 | + return access_token |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == '__main__': |
| 35 | + # this script lets you use access_token from env or get it on the fly |
| 36 | + # you can also use: |
| 37 | + # https://github.com/facebookarchive/python-instagram/blob/master/get_access_token.py |
| 38 | + access_token = os.environ.get('IG_100D_ACCESS_TOKEN') |
| 39 | + if not access_token: |
| 40 | + _get_access_token() |
| 41 | + |
| 42 | + api = InstagramAPI(access_token=access_token) |
| 43 | + |
| 44 | + recent_media, next_ = api.user_recent_media(user_id=IG_USER_ID, count=50) |
| 45 | + |
| 46 | + for media in recent_media: |
| 47 | + print('thumb url: ', media.get_thumbnail_url()) # instagram/model.py |
| 48 | + print('\tstd res url: ', media.get_standard_resolution_url()) |
| 49 | + print() |
0 commit comments