-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
64 lines (56 loc) · 2.3 KB
/
common.py
File metadata and controls
64 lines (56 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
from rauth import OAuth1Service
import sys
from urllib.parse import urlsplit, urlunsplit, parse_qsl, urlencode
OAUTH_ORIGIN = 'https://secure.smugmug.com'
REQUEST_TOKEN_URL = OAUTH_ORIGIN + '/services/oauth/1.0a/getRequestToken'
ACCESS_TOKEN_URL = OAUTH_ORIGIN + '/services/oauth/1.0a/getAccessToken'
AUTHORIZE_URL = OAUTH_ORIGIN + '/services/oauth/1.0a/authorize'
API_ORIGIN = 'https://api.smugmug.com'
SERVICE = None
def get_service():
global SERVICE
if SERVICE is None:
try:
with open('config.json', 'r') as fh:
config = json.load(fh)
except IOError as e:
print('====================================================')
print('Failed to open config.json! Did you create it?')
print('The expected format is demonstrated in example.json.')
print('====================================================')
sys.exit(1)
if type(config) is not dict \
or 'key' not in config \
or 'secret' not in config\
or type(config['key']) is not str \
or type(config['secret']) is not str:
print('====================================================')
print('Invalid config.json!')
print('The expected format is demonstrated in example.json.')
print('====================================================')
sys.exit(1)
SERVICE = OAuth1Service(
name='smugmug-oauth-web-demo',
consumer_key=config['key'],
consumer_secret=config['secret'],
request_token_url=REQUEST_TOKEN_URL,
access_token_url=ACCESS_TOKEN_URL,
authorize_url=AUTHORIZE_URL,
base_url=API_ORIGIN + '/api/v2')
return SERVICE
def add_auth_params(auth_url, access=None, permissions=None):
if access is None and permissions is None:
return auth_url
parts = urlsplit(auth_url)
query = parse_qsl(parts.query, True)
if access is not None:
query.append(('Access', access))
if permissions is not None:
query.append(('Permissions', permissions))
return urlunsplit((
parts.scheme,
parts.netloc,
parts.path,
urlencode(query, True),
parts.fragment))