8000 098 · stephen-codepython/100DaysOfCode@59802eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 59802eb

Browse files
committed
098
1 parent 63d653d commit 59802eb

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

098/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## IG API
2+
3+
Create an IG app:
4+
5+
![ig-api.png](ig-api)
6+
7+
pip install [python-instagram](https://github.com/facebookarchive/python-instagram)
8+
9+
Note this library is not actively maintained. [get_access_token.py](https://github.com/facebookarchive/python-instagram/blob/master/get_access_token.py) has a bug:
10+
11+
$ python get_access_token.py
12+
...
13+
instagram.oauth2.OAuth2AuthExchangeError: You must provide a client_id
14+
15+
Patch with [this fix](https://github.com/vgavro/python-instagram/commit/9dfc264571ad7c343af3899445d13afedf23e3aa) or make your own method, e.g. [`exchange_code_for_access_token`](https://stackoverflow.com/questions/38329960/instagram-api-keep-raise-you-must-provide-a-client-id-exception-when-i-use-pyt).
16+
17+
Another required fix: when querying my media I got:
18+
19+
for comment in entry['comments']['data']:
20+
KeyError: 'data'
21+
22+
Fix [here](https://github.com/facebookarchive/python-instagram/pull/235/files).
23+
24+
So probably need to fork this repo or make own wrapper using `requests` or something.

098/ig-api.png

49.3 KB
Loading

098/pull_photos.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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()

LOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@
9999
| 095 | Jul 02, 2017 | [Class to cache moviedb API responses #shelve #decorator #namedtuple](095) | Part of challenge 25 solution where we query themoviedb API to get upcoming/now-playing movies or airing tv series to send out mail alerts |
100100
| 096 | Jul 03, 2017 | [Script to measure which 100Days tweets were most successful (RTs / Favs)](096) | Using csv module to parse downloaded archive, tweepy to query status for each tweet, parsing out retweet_count and favorite_count. I add those up and sort the result descending on this combined number. Curious to see what our most popular tweets were ... |
101101
| 097 | Jul 04, 2017 | [Create a default #Flask App dir structure for new projects](097) | Unpythonic and simple. A rough script I threw together that creates a basic Flask App directory structure (templates and static folder) with required base files. Will refactor this later but for now it works! |
102-
| 098 | Jul 05, 2017 | [TITLE](098) | LEARNING |
102+
| 098 | Jul 05, 2017 | [Script to use the #Instagram #API to authenticate and pull your media](098) | Experimentation with the Instagram API - hit some bugs which I documented in README, considering making my own wrapper ... |
103103
| 099 | Jul 06, 2017 | [TITLE](099) | LEARNING |
104104
| 100 | Jul 07, 2017 | [TITLE](100) | LEARNING |

0 commit comments

Comments
 (0)
0