Skip to content

Commit ea8bad0

Browse files
committed
tus uploads integrated
1 parent 56e53d2 commit ea8bad0

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

qencode/tus_uploader.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
from tusclient import client
3+
from utils import get_tus_from_url
4+
5+
class UploadStatus(object):
6+
def __init__(self, error = None, url= None, status= None):
7+
self.url = url
8+
self.error = error
9+
self.status = status
10+
11+
12+
def upload(file_path=None, url=None, chunk_size=None, log_func=None):
13+
"""
14+
Returns upload status and url using tus protocol
15+
16+
:fileUrl:
17+
18+
Url address where to upload the file
19+
20+
:Args:
21+
see tusclient.uploader.Uploader for required and optional arguments.
22+
"""
23+
try:
24+
my_client = client.TusClient(url=url)
25+
uploader = my_client.uploader(file_path=file_path, chunk_size=chunk_size, log_func=log_func)
26+
uploader.upload()
27+
url_storage = uploader.url
28+
tus_url = get_tus_from_url(url_storage)
29+
return UploadStatus(url=tus_url, status='Ok', error='')
30+
except:
31+
print('Error uploading file to ' + url)
32+
raise

qencode/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,17 @@ def log(self, path=None, name=None, log_format=None):
6565
while 1:
6666
log.info('{0} | {1} | {2}'.format(self.status, self.percent, self.message))
6767
if self.task_completed:
68-
break
68+
break
69+
70+
def get_tus_from_url(url=''):
71+
try:
72+
if url.find('tus:') == 0:
73+
return url
74+
else:
75+
x = url.split('/')[-1]
76+
if x == url:
77+
return url
78+
else:
79+
return 'tus:' + x
80+
except:
81+
return url

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
json
22
urllib
33
urllib2
4-
urlparse
4+
urlparse
5+
tuspy
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import os.path
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
7+
import qencode
8+
import time
9+
import json
10+
from qencode import QencodeClientException, QencodeTaskException, tus_uploader
11+
12+
#replace with your API KEY (can be found in your Project settings on Qencode portal)
13+
API_KEY = 'your-api-qencode-key'
14+
15+
file_path = '/path/to/file/for/upload'
16+
17+
query = """
18+
{"query": {
19+
"source": "%s",
20+
"format": [
21+
{
22+
"output": "mp4",
23+
"size": "320x240",
24+
"video_codec": "libx264"
25+
}
26+
]
27+
}
28+
}
29+
"""
30+
31+
32+
def start_encode():
33+
34+
"""
35+
Create client object
36+
:param api_key: string. required
37+
:param api_url: string. not required
38+
:param api_version: int. not required. default 'v1'
39+
:return: task object
40+
"""
41+
42+
client = qencode.client(API_KEY)
43+
if client.error:
44+
raise QencodeClientException(client.message)
45+
46+
print 'The client created. Expire date: %s' % client.expire
47+
48+
task = client.create_task()
49+
50+
if task.error:
51+
raise QencodeTaskException(task.message)
52+
53+
#get upload url from endpoint returned with /v1/create_task and task_token value
54+
uploadUrl = task.upload_url + '/' + task.task_token
55+
56+
#do upload and get uploaded file URI
57+
uploadedFile = tus_uploader.upload(file_path=file_path, url=uploadUrl, log_func=log_upload, chunk_size=2000000)
58+
59+
params = query % uploadedFile.url
60+
task.custom_start(params)
61+
62+
if task.error:
63+
raise QencodeTaskException(task.message)
64+
65+
print 'Start encode. Task: %s' % task.task_token
66+
67+
while True:
68+
status = task.status()
69+
# print status
70+
print json.dumps(status, indent=2, sort_keys=True)
71+
if status['error'] or status['status'] == 'completed':
72+
break
73+
time.sleep(5)
74+
75+
def log_upload(msg):
76+
print(msg)
77+
78+
if __name__ == '__main__':
79+
start_encode()

0 commit comments

Comments
 (0)