-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexamples.py
59 lines (43 loc) · 1.78 KB
/
examples.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
import asyncio
import sys
import pyimgbox
# # Uncomment this to enable debugging messages
# import logging
# logging.basicConfig(level=logging.DEBUG)
# Using Gallery as an asynchronous context manager is the simplest usage.
async def example1(filepaths):
async with pyimgbox.Gallery(title="Hello, World!") as gallery:
async for submission in gallery.add(filepaths):
if not submission['success']:
print(f"{submission['filename']}: {submission['error']}")
else:
print(submission)
# Without the asynchronous context manager, close() must be called manually.
async def example2(filepaths):
gallery = pyimgbox.Gallery(title="Hello, World!")
try:
async for submission in gallery.add(filepaths):
print(submission)
finally:
await gallery.close()
# If you need the gallery's URL before the first upload,
# you can call create() manually.
async def example3(filepaths):
async with pyimgbox.Gallery(title="Hello, World!") as gallery:
try:
await gallery.create()
except ConnectionError as e:
print('Gallery creation failed:', str(e))
else:
print('Gallery URL:', gallery.url)
print(' Edit URL:', gallery.edit_url)
async for submission in gallery.add(filepaths):
print(submission)
# Use upload() instead of add() for more flexibility.
async def example4(filepaths):
async with pyimgbox.Gallery(title="Hello, World!") as gallery:
submission1 = await gallery.upload(filepaths[0])
submission2 = await gallery.upload(filepaths[1])
submission3 = await gallery.upload(filepaths[2])
print('Submissions:', submission1, submission2, submission3)
asyncio.run(example1(sys.argv[1:]))