-
Notifications
You must be signed in to change notification settings - Fork 11
/
example_list_paginated_items.py
68 lines (55 loc) · 2.27 KB
/
example_list_paginated_items.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
# (C) Catchoom Technologies S.L.
# Licensed under the MIT license.
# https://github.com/catchoom/craftar-python/blob/master/LICENSE
# All warranties and liabilities are disclaimed.
from optparse import OptionParser
import craftar
"""
Lists all your items. If needed, they can be limited to a specific collection.
- Requires an account and a collection with items at https://my.craftar.net
- The @api_key is your management api key. Get it at
https://my.craftar.net/accounts/apis/
- You can limit the result to a specific @collection.
- Don't set the @hostname, unless you are using a custom platform.
"""
def list_paginated_items(api_key, items_per_page=25, collection=None,
hostname=None):
"List all items, paginated by @items_per_page"
if hostname:
craftar.settings.MANAGEMENT_HOSTNAME = hostname
offset = 0
while True:
print("- Retrieving items %s to %s..." % (offset + 1,
offset + items_per_page))
item_list = craftar.get_item_list(
api_key=api_key,
limit=items_per_page,
offset=offset,
collection=collection,
)
for i in item_list:
print("%s: %s" % (i["uuid"], i["name"]))
offset += items_per_page
if len(item_list) < items_per_page:
print("No more items!")
return
if __name__ == '__main__':
usage = "usage: %prog -a API_KEY [-c COLLECTION -H HOSTNAME]"
parser = OptionParser(usage, version="%prog 1.0")
parser.add_option('-a', '--api_key',
dest='api_key',
help="Management API key.")
parser.add_option('-c', '--collection',
dest='collection',
default=False,
help='Collection UUID')
parser.add_option('-H', '--hostname',
dest='hostname',
default=False,
help='CraftAR recognition API hostname')
(options, args) = parser.parse_args()
if not options.api_key:
parser.error("Missing parameter: -a API_KEY")
list_paginated_items(api_key=options.api_key,
collection=options.collection,
hostname=options.hostname)