mirror of
https://github.com/asciinema/asciinema.git
synced 2025-12-16 11:48:13 +01:00
Upload with curl
This commit is contained in:
51
bin/rec.py
51
bin/rec.py
@@ -32,7 +32,7 @@ class AsciiCast(object):
|
|||||||
|
|
||||||
def create(self):
|
def create(self):
|
||||||
self._record()
|
self._record()
|
||||||
self._upload()
|
return self._upload()
|
||||||
|
|
||||||
def _record(self):
|
def _record(self):
|
||||||
os.makedirs(self.path)
|
os.makedirs(self.path)
|
||||||
@@ -66,6 +66,9 @@ class AsciiCast(object):
|
|||||||
url = Uploader(self.path).upload()
|
url = Uploader(self.path).upload()
|
||||||
if url:
|
if url:
|
||||||
print url
|
print url
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class TimedFile(object):
|
class TimedFile(object):
|
||||||
@@ -232,41 +235,36 @@ class Uploader(object):
|
|||||||
Uploads recorded script to website using HTTP based API.
|
Uploads recorded script to website using HTTP based API.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
API_URL = os.environ.get('ASCII_IO_API_URL', 'http://ascii.io/api/asciicasts')
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self.api_host = os.environ.get('ASCIIIO_API_HOST', 'ascii.io')
|
|
||||||
self.api_path = '/scripts'
|
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
def upload(self):
|
def upload(self):
|
||||||
params = self._build_params()
|
files = {
|
||||||
url = self._make_request(params)
|
'meta': 'meta.json',
|
||||||
|
'stdout_data': 'stdout',
|
||||||
|
'stdout_timing': 'stdout.time'
|
||||||
|
}
|
||||||
|
|
||||||
return url
|
if os.path.exists(self.path + '/stdin'):
|
||||||
|
files['stdin_data'] = 'stdin'
|
||||||
|
files['stdin_timing'] = 'stdin.time'
|
||||||
|
|
||||||
def _build_params(self):
|
fields = ["-F %s=@%s/%s" % (f, self.path, files[f]) for f in files]
|
||||||
params = urllib.urlencode({
|
|
||||||
'metadata': 'lolza'
|
|
||||||
})
|
|
||||||
|
|
||||||
return params
|
cmd = "curl -sS -f -o - %s %s" % (' '.join(fields), Uploader.API_URL)
|
||||||
|
|
||||||
def _make_request(self, params):
|
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
|
||||||
headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" }
|
stderr=subprocess.PIPE)
|
||||||
conn = httplib.HTTPConnection(self.api_host)
|
stdout, stderr = process.communicate()
|
||||||
try:
|
|
||||||
conn.request("POST", self.api_path, params, headers)
|
|
||||||
except socket.error:
|
|
||||||
print "Oops, couldn't connect to ..."
|
|
||||||
return
|
|
||||||
|
|
||||||
response = conn.getresponse()
|
if stderr:
|
||||||
|
os.write(2, stderr)
|
||||||
|
|
||||||
if response.status == 201:
|
if stdout:
|
||||||
return response.read()
|
return stdout
|
||||||
else:
|
else:
|
||||||
# TODO stderr
|
|
||||||
print 'Oops, something is not right. (%d: %s)' % (response.status,
|
|
||||||
response.read())
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -351,7 +349,8 @@ def main():
|
|||||||
|
|
||||||
if action == 'rec':
|
if action == 'rec':
|
||||||
check_pending()
|
check_pending()
|
||||||
AsciiCast(command, title, record_input).create()
|
if not AsciiCast(command, title, record_input).create():
|
||||||
|
sys.exit(1)
|
||||||
elif action == 'upload':
|
elif action == 'upload':
|
||||||
upload_pending()
|
upload_pending()
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user