Skip to content

Commit 5b2ec09

Browse files
committed
Merge pull request Imgur#33 from RandomInsano/breakout_code_examples
Break out code examples
2 parents 167cc00 + 24ac383 commit 5b2ec09

5 files changed

Lines changed: 114 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ config.json
55
dist
66
build
77
imgurpython.egg-info
8-
runner.py
8+
runner.py
9+
examples/auth.ini

examples/auth.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[credentials]
2+
client_id=YOUR ID HERE
3+
client_secret=YOUR SECRET HERE
4+
refresh_token=

examples/auth.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
Here's how you go about authenticating yourself! The important thing to
5+
note here is that this script will be used in the other examples so
6+
set up a test user with API credentials and set them up in auth.ini.
7+
'''
8+
9+
from imgurpython import ImgurClient
10+
from helpers import get_input, get_config
11+
12+
def authenticate():
13+
# Get client ID and secret from auth.ini
14+
config = get_config()
15+
config.read('auth.ini')
16+
client_id = config['credentials']['client_id']
17+
client_secret = config['credentials']['client_secret']
18+
19+
client = ImgurClient(client_id, client_secret)
20+
21+
# Authorization flow, pin example (see docs for other auth types)
22+
authorization_url = client.get_auth_url('pin')
23+
24+
print("Go to the following URL: {0}".format(authorization_url))
25+
26+
# Read in the pin, handle Python 2 or 3 here.
27+
pin = get_input("Enter pin code: ")
28+
29+
# ... redirect user to `authorization_url`, obtain pin (or code or token) ...
30+
credentials = client.authorize(pin, 'pin')
31+
client.set_user_auth(credentials['access_token'], credentials['refresh_token'])
32+
33+
print("Authentication successful! Here are the details:")
34+
print(" Access token: {0}".format(credentials['access_token']))
35+
print(" Refresh token: {0}".format(credentials['refresh_token']))
36+
37+
return client
38+
39+
# If you want to run this as a standalone script, so be it!
40+
if __name__ == "__main__":
41+
authenticate()

examples/helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
These functions have nothing to do with the API, they just help ease
3+
issues between Python 2 and 3
4+
'''
5+
6+
def get_input(string):
7+
''' Get input from console regardless of python 2 or 3 '''
8+
try:
9+
return raw_input(string)
10+
except:
11+
return input(string)
12+
13+
def get_config():
14+
''' Create a config parser for reading INI files '''
15+
try:
16+
import ConfigParser
17+
return ConfigParser.ConfigParser()
18+
except:
19+
import configparser
20+
return configparser.ConfigParser()

examples/upload.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
Here's how you upload an image. For this example, put the cutest picture
5+
of a kitten you can find in this script's folder and name it 'Kitten.jpg'
6+
7+
For more details about images and the API see here:
8+
https://api.imgur.com/endpoints/image
9+
'''
10+
11+
# Pull authentication from the auth example (see auth.py)
12+
from auth import authenticate
13+
14+
from datetime import datetime
15+
16+
album = None # You can also enter an album ID here
17+
image_path = 'Kitten.jpg'
18+
19+
def upload_kitten(client):
20+
'''
21+
Upload a picture of a kitten. We don't ship one, so get creative!
22+
'''
23+
24+
# Here's the metadata for the upload. All of these are optional, including
25+
# this config dict itself.
26+
config = {
27+
'album': album,
28+
'name': 'Catastrophe!',
29+
'title': 'Catastrophe!',
30+
'description': 'Cute kitten being cute on {0}'.format(datetime.now())
31+
}
32+
33+
print("Uploading image... ")
34+
image = client.upload_from_path(image_path, config=config, anon=False)
35+
print("Done")
36+
print()
37+
38+
return image
39+
40+
41+
# If you want to run this as a standalone script
42+
if __name__ == "__main__":
43+
client = authenticate()
44+
image = upload_kitten(client)
45+
46+
print("Image was posted! Go check your images you sexy beast!")
47+
print("You can find it here: {0}".format(image['link']))

0 commit comments

Comments
 (0)