-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntuOne.py
63 lines (51 loc) · 2.42 KB
/
ubuntuOne.py
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
import oauth2
import requests
from oauth2 import Request as Requ, SignatureMethod_HMAC_SHA1
class BadRequest(Exception):
"""An invalid request was submitted."""
class Unauthorized(Exception):
"""The provided email address and password were incorrect."""
class UbuntuOne:
def __init__(self, email_address, password, description, otp=None):
"""Aquire an OAuth access token for the given user."""
# Issue a new access token for the user.
params = {
'email': email_address,
'password': password,
'token_name': description,
}
if otp is not None:
params['otp'] = otp
response = requests.post(
'https://login.ubuntu.com/api/v2/tokens/oauth',
data=json.dumps(params),
headers={'content-type': 'application/json',
'accept': 'application/json'})
if response.status_code == 400:
raise BadRequest(response.content)
elif not response.ok:
# Unauthorized
raise Unauthorized(response.json())
data = response.json()
self.consumer = oauth2.Consumer(data['consumer_key'], data['consumer_secret'])
self.token = oauth2.Token(data['token_key'], data['token_secret'])
self.client = oauth2.Client(self.consumer, self.token)
def get_list(self, content_path, folder):
request_token_url = "https://one.ubuntu.com/api/file_storage/v1/" + content_path.replace(' ','%20') + "/" + folder.replace(' ','%20') + "?include_children=true"
resp,content = self.client.request(request_token_url, "GET")
return json.loads(content)['children']
def get_file(self, content_path):
request_token_url = "https://files.one.ubuntu.com" + content_path.replace(' ','%20')
resp,content = self.client.request(request_token_url, "GET")
return content
def get_stream_file(self,content_path):
url = "https://files.one.ubuntu.com" + content_path.replace(' ','%20')
# Get Oauth1 signature
req = Requ.from_consumer_and_token(self.consumer,
token=self.token, http_method='GET', http_url=url,
parameters=None, body='')
req.sign_request(SignatureMethod_HMAC_SHA1(), self.consumer, self.token)
realm = "https://files.one.ubuntu.com"
header = req.to_header(realm=realm)
return requests.get(url, stream=True, headers=header)