/
submit.py
63 lines (46 loc) · 1.74 KB
/
submit.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
"""
Copyright (C) 2018-2020 Vanessa Sochat.
This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
from helpme.logger import bot
import os
import re
def upload_asciinema(filename):
"""a wrapper around generation of an asciinema.api.Api to call the
upload command given an already existing asciinema file.
Parameters
==========
filename: the asciinema file to upload, can be generated with
function record_asciinema in record.py
"""
if os.path.exists(filename):
try:
from asciinema.commands.upload import UploadCommand
import asciinema.config as aconfig
from asciinema.api import Api
except:
bot.exit(
"The asciinema module is required to submit "
"an asciinema recording. Try pip install helpme[asciinema]"
)
# Load the API class
cfg = aconfig.load()
api = Api(cfg.api_url, os.environ.get("USER"), cfg.install_id)
# Perform the upload, return the url
uploader = UploadCommand(api, filename)
try:
url, warn = uploader.api.upload_asciicast(filename)
if warn:
uploader.print_warning(warn)
# Extract just the url, if provided (always is https)
if url:
match = re.search("https://.+", url)
if match:
url = match.group()
return url
except:
bot.error("Problem with upload, skipping")
else:
bot.warning("Cannot find %s, skipping submission." % filename)