From a33409aa6db660a076f318d506b1433985dbafd9 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 4 Mar 2012 17:08:26 +0100 Subject: [PATCH] Generate user token and assign it to recorded asciicast --- bin/rec.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/bin/rec.py b/bin/rec.py index 9af635c..3be0d28 100755 --- a/bin/rec.py +++ b/bin/rec.py @@ -18,6 +18,7 @@ import socket import glob import bz2 import ConfigParser +import uuid SCRIPT_NAME = os.path.basename(sys.argv[0]) @@ -26,8 +27,9 @@ class AsciiCast(object): BASE_DIR = os.path.expanduser("~/.ascii.io") QUEUE_DIR = BASE_DIR + "/queue" - def __init__(self, api_url, command, title, record_input): + def __init__(self, api_url, user_token, command, title, record_input): self.api_url = api_url + self.user_token = user_token self.path = AsciiCast.QUEUE_DIR + "/%i" % int(time.time()) self.command = command self.title = title @@ -61,6 +63,7 @@ class AsciiCast(object): columns = int(self._get_cmd_output(['tput', 'cols'])) data = { + 'user_token' : self.user_token, 'duration' : self.duration, 'recorded_at': recorded_at, 'title' : self.title, @@ -355,6 +358,10 @@ def main(): action = args[0] config = ConfigParser.RawConfigParser() + config.add_section('user') + config.add_section('api') + config.add_section('record') + cfg_file = os.path.expanduser('~/.ascii.io/config') try: config.read(cfg_file) @@ -362,16 +369,25 @@ def main(): print('Config file %s contains syntax errors' % cfg_file) sys.exit(2) + try: + user_token = config.get('user', 'token') + except ConfigParser.NoOptionError: + user_token = str(uuid.uuid1()) + config.set('user', 'token', user_token) + try: record_input = config.getboolean('record', 'input') - except ConfigParser.NoSectionError: + except ConfigParser.NoOptionError: record_input = False try: api_url = config.get('api', 'url') - except ConfigParser.NoSectionError: + except ConfigParser.NoOptionError: api_url = 'http://ascii.io/api' + with open(cfg_file, 'wb') as configfile: + config.write(configfile) + api_url = os.environ.get('ASCII_IO_API_URL', api_url) command = None @@ -393,7 +409,7 @@ def main(): if action == 'rec': check_pending() - if not AsciiCast(api_url, command, title, record_input).create(): + if not AsciiCast(api_url, user_token, command, title, record_input).create(): sys.exit(1) elif action == 'upload': upload_pending()