-
Notifications
You must be signed in to change notification settings - Fork 48
/
rdio-call
executable file
·128 lines (105 loc) · 4.74 KB
/
rdio-call
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
"""A command-line tool for accessing the Rdio web service API with OAuth.
Copyright (c) 2010-2011 Rdio Inc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import sys
import os
import json
import webbrowser
from optparse import OptionParser, OptionGroup
from rdioapi import Rdio
def main(options, args):
"""Make a call to Rdio."""
# load the persistent state
config_path = os.path.expanduser('~/.rdio-tool.json')
if os.path.exists(config_path):
config = json.load(open(config_path))
else:
config = {'auth_state': {}}
# set up the keys
if options.client_id is not None:
config['client_id'] = options.client_id
if options.client_secret is not None:
config['client_secret'] = options.client_secret
urls = {
'api_endpoint': options.api_endpoint,
'device_code_url': options.device_code_url,
'token_url': options.token_url
}
if 'client_id' not in config or 'client_secret' not in config:
sys.stderr.write('Both the client id and client secret must be specified')
sys.exit(1)
# forget auth state, if we're logging in or logging out
if options.forget_auth or options.authenticate:
config['auth_state'] = {}
# create the Rdio client object
rdio = Rdio(config['client_id'], config['client_secret'], config['auth_state'], urls)
# authenticate, if requested
if options.authenticate:
url, device_code = rdio.begin_authentication()
print 'Please enter device code: %s on %s' % (device_code, url)
webbrowser.open(url)
rdio.complete_authentication()
# save state
with open(config_path, 'w') as filehandle:
json.dump(config, filehandle, indent=True)
filehandle.write('\n')
# process the response
response, content = rdio.call_raw(method, **args)
if response.code == '200':
if options.indent:
json.dump(json.loads(content), sys.stdout, indent=True)
sys.stdout.write('\n')
else:
print content
else:
if options.verbose:
if 'www-authenticate' in response.headers:
print 'Authentication required, pass --authenticate'
else:
print content
sys.exit(response.code)
if __name__ == "__main__":
parser = OptionParser(usage='%prog [options] method arg1=value1 arg2=value2...', version='%prog 0.1')
parser.add_option('--client-id', dest='client_id',
help='the client id to use for this and future requests', metavar='CLIENT_ID')
parser.add_option('--client-secret', dest='client_secret',
help='the client secret to use for this and future requests', metavar='SECRET')
parser.add_option('--authenticate', dest='authenticate',
help='authenticate against Rdio before making the request', action='store_true', default=False)
parser.add_option('--forget-auth', dest='forget_auth',
help='discard previous authentication information', action='store_true', default=False)
parser.add_option('-v', '--verbose', dest='verbose', help='verbose output', action='store_true', default=False)
parser.add_option('-i', '--indent', dest='indent', help='indent the response', action='store_true', default=False)
advanced = OptionGroup(parser, 'Advanced Options', 'Probably only useful in development.')
advanced.add_option('--api-endpoint', dest='api_endpoint', help='API endpoint', metavar='URL')
advanced.add_option('--device-code-url', dest='device_code_url', help='', metavar='URL')
advanced.add_option('--token-url', dest='token_url', help='Access token endpoint', metavar='URL')
parser.add_option_group(advanced)
# parse the arguments
(options, args) = parser.parse_args()
if len(args) < 1:
parser.print_help()
sys.exit(1)
method = args.pop(0)
try:
args = dict([a.split('=', 1) for a in args])
except ValueError:
parser.print_help()
sys.exit(1)
main(options, args)