Last active
January 18, 2018 15:46
-
-
Save andreinechaev/58e6181d4b8e7d6e89280c9ade458d87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nuxeo.nuxeo import Nuxeo | |
from nuxeo.blob import BufferBlob | |
import requests | |
import random | |
from threading import Thread | |
rand_url = 'http://thecatapi.com/api/images/get?type=' | |
class ImagePublisher(Thread): | |
def __init__(self, auth, amount, name='Thread_' + str(random.randint(0, 1000))): | |
Thread.__init__(self) | |
self._nuxeo = Nuxeo(base_url='http://your.address:8080/nuxeo', auth=auth) | |
self._amount = amount | |
self.name = name | |
def run(self): | |
for i in range(0, self._amount): | |
rnd = i + random.randint(0, 999999) | |
docName = 'Uploaded_by_script_' + str(rnd) | |
print(self.name + ' Creating document with name: ' + docName) | |
operation = self._nuxeo.operation('Document.Create') | |
operation.params({ | |
'type': 'Picture', | |
'name': docName, | |
# 'properties': 'dc:title=' + docName + '\ndc:description=None' | |
}) | |
operation.input('/default-domain/workspaces/pictures') | |
doc = operation.execute() | |
ext = 'png' if rnd % 2 is 0 else 'jpg' | |
try: | |
res = requests.get(rand_url + ext) | |
blob = BufferBlob(res.content, 'img_'+ ext +'_' + str(rnd), 'image/png') | |
uploaded = self._nuxeo.batch_upload().upload(blob) | |
operation = self._nuxeo.operation('Blob.AttachOnDocument') | |
operation.params({'document': doc['path']}) | |
operation.input(uploaded) | |
operation.execute() | |
except Exception: | |
print(self.name + ' Couldn\'t load a pic') | |
admin = { | |
'username': 'Administrator', | |
'password': 'Administrator' | |
} | |
uploader = { | |
'username': 'Uploader', | |
'password': 'Uploader' | |
} | |
t1 = ImagePublisher(admin, 1000, name='0') | |
t1.daemon = True | |
t2 = ImagePublisher(admin, 1000, name='1') | |
t2.daemon = True | |
t1.start() | |
t2.start() | |
print('Threads started') | |
t1.join() | |
t2.join() | |
print('End of program') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment