-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv3doc_collection.py
157 lines (118 loc) · 5.15 KB
/
v3doc_collection.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python3
"""Sample script that shows how to list models, and various operations with them (comments,
collections...) using the V3 api and the requests library."""
import json
# import the requests library
# http://docs.python-requests.org/en/latest
# pip install requests
import requests
from requests.exceptions import RequestException
SKETCHFAB_API_URL = 'https://api.sketchfab.com/v3'
API_TOKEN = 'YOUR API_TOKEN from https://sketchfab.com/settings/password'
def _get_request_payload(*, data=None, files=None, json_payload=False):
"""Helper method that returns the authentication token and proper content type depending on
whether or not we use JSON payload."""
data = data or {}
files = files or {}
headers = {'Authorization': 'Token {}'.format(API_TOKEN)}
if json_payload:
headers.update({'Content-Type': 'application/json'})
data = json.dumps(data)
return {'data': data, 'files': files, 'headers': headers}
def list_my_models():
my_models_endpoint = f'{SKETCHFAB_API_URL}/me/models'
payload = _get_request_payload()
try:
response = requests.get(my_models_endpoint, **payload)
except RequestException as exc:
print(f'An API error occured: {exc}')
else:
data = response.json()
if not len(data['results']) > 0:
print('You don\'t seem to have any model :(')
return data['results']
def get_collection():
my_collections_endpoint = f'{SKETCHFAB_API_URL}/me/collections'
payload = _get_request_payload()
try:
response = requests.get(my_collections_endpoint, **payload)
except RequestException as exc:
print(f'An API error occured: {exc}')
exit(1)
data = response.json()
if not data['results']:
print('You don\'t seem to have any collection, let\'s create one!')
return
else:
return data['results'][0]
def create_collection(model):
collections_endpoint = f'{SKETCHFAB_API_URL}/collections'
data = {'name': 'A Beautiful Collection', 'models': [model['uid']]}
payload = _get_request_payload(data=data, json_payload=True)
try:
response = requests.post(collections_endpoint, **payload)
except RequestException as exc:
print(f'An API error occured: {exc}')
else:
# We created our collection \o/
# Now retrieve the data
collection_url = response.headers['Location']
response = requests.get(collection_url)
return response.json()
def add_model_to_collection(model, collection):
collection_model_endpoint = f'{SKETCHFAB_API_URL}/collections/{collection["uid"]}/models'
payload = _get_request_payload(data={'models': [model['uid']]}, json_payload=True)
try:
response = requests.post(collection_model_endpoint, **payload)
except RequestException as exc:
print(f'An API error occured: {exc}')
else:
if response.status_code == requests.codes.created:
print(f'Model successfully added to collection {collection["uid"]}!')
else:
print('Model already in collection')
def remove_model_from_collection(model, collection):
collection_model_endpoint = f'{SKETCHFAB_API_URL}/collections/{collection["uid"]}/models'
payload = _get_request_payload(data={'models': [model['uid']]}, json_payload=True)
try:
response = requests.delete(collection_model_endpoint, **payload)
except RequestException as exc:
print(f'An API error occured: {exc}')
else:
if response.status_code == requests.codes.no_content:
print(f'Model successfully removed from collection {collection["uid"]}!')
else:
print(f'Model not in collection: {response.content}')
def comment_model(model, msg):
comment_endpoint = f'{SKETCHFAB_API_URL}/comments'
payload = _get_request_payload(data={'model': model['uid'], 'body': msg}, json_payload=True)
try:
response = requests.post(comment_endpoint, **payload)
except RequestException as exc:
print(f'An API error occured: {exc}')
else:
if response.status_code == requests.codes.created:
print('Comment successfully posted!')
else:
print(f'Failed to post the comment: {response.content}')
if __name__ == '__main__':
# This requires that your profile contains at least two models
print('Getting models from your profile...')
if models := list_my_models():
try:
model_1, model_2, *models = models
except ValueError:
print('You need at least two models in your profile to run this script')
exit(1)
# List your collections, create one if you don't have any
collection = get_collection()
if not collection:
collection = create_collection(model_1)
print(f'Now do some more stuff on model {model_2["uid"]}')
# Obviously it's not really logical but these are just examples
# of what you can achieve
add_model_to_collection(model_2, collection)
comment_model(model_2, u'Wao, this is... Wao Oo')
remove_model_from_collection(model_2, collection)
else:
print('You don\'t have any models !')