<commit_before>#!/usr/bin/env python

import json
import awsiot
import logging
import sys
import time
import platform
import datetime

try:
    import picamera
except ImportError:
    logging.error("Unable to import picamera")
    pass

RECOGNIZE = 'recognize'


def snapshot(filename):
    try:
        logging.info("snapshot: {}".format(filename))
        camera.capture(filename)
        return True
    except Exception as e:
        logging.error("snapshot failed {}".format(e.message))
        return False


def callback(client, user_data, message):
    try:
        msg = json.loads(message.payload)
    except ValueError:
        msg = None
    logging.debug("received {} {}".format(message.topic, msg))
    commands = filter(None, message.topic.replace(args.topic, '').split('/'))
    now = datetime.datetime.now()
    if len(commands) > 0:
        cmd = commands.pop(0)
        tags = {'created': awsiot.timestamp_string(now), 'source': args.source}
        if cmd == 'workspace':
            logging.debug("command: {}".format(cmd))
            filename = "{}-{}.jpg".format(args.source, awsiot.file_timestamp_string(now))
            if snapshot(filename) and args.bucket is not None:
                awsiot.mv_to_s3(filename, args.bucket, tags)
        elif cmd == 'snapshot':
            logging.debug("command: {}".format(cmd))
            filename = "{}.jpg".format(args.source)
            if snapshot(filename) and args.web_bucket is not None:
                awsiot.mv_to_s3(filename, args.web_bucket, tags)
        elif cmd == 'recording':
            logging.debug("command: {}".format(cmd))
        elif cmd == RECOGNIZE:
            logging.debug("command: {}".format(cmd))
            filename = "{}-{}.jpg".format(args.source, awsiot.file_timestamp_string(now))
            if snapshot(filename) and args.bucket is not None:
                awsiot.mv_to_s3(filename, args.bucket)
                result = awsiot.recognize(filename, args.bucket)
                logging.info("{}: {}".format(RECOGNIZE, result))
                if "Labels" in result:
                    tags[RECOGNIZE] = awsiot.tagify(result['Labels'], 'Name')
                    awsiot.s3_tag(filename, args.bucket, tags)
        else:
            logging.warning('Unrecognized command: {}'.format(cmd))
    else:
        logging.warning("No commands")


if __name__ == "__main__":
    parser = awsiot.iot_arg_parser()
    parser.add_argument("-x", "--width", help="camera resolution width", type=int, default=1920)
    parser.add_argument("-y", "--height", help="camera resolution height", type=int, default=1080)
    parser.add_argument("-z", "--rotation", help="camera rotation", type=int, default=0)
    parser.add_argument("-s", "--source", help="source name", default=platform.node().split('.')[0])
    parser.add_argument("-b", "--bucket", help="S3 bucket")
    parser.add_argument("-w", "--web_bucket", help="S3 bucket for web storage")
    args = parser.parse_args()

    logging.basicConfig(filename=awsiot.LOG_FILE, level=args.log_level, format=awsiot.LOG_FORMAT)
<commit_msg>make video recordings<commit_after>#!/usr/bin/env python

import json
import awsiot
import logging
import sys
import time
import platform
import datetime

try:
    import picamera
except ImportError:
    logging.error("Unable to import picamera")
    pass

RECOGNIZE = 'recognize'


def snapshot(filename):
    try:
        logging.info("snapshot: {}".format(filename))
        camera.capture(filename)
        return True
    except Exception as e:
        logging.error("snapshot failed {}".format(e.message))
        return False


def recording(filename, max_length=60, width=640, height=480, quality=23):
    try:
        logging.info("recording: {}".format(filename))
        camera.resolution = (width, height)
        camera.start_recording(filename, format='h264', quality=quality)
        camera.wait_recording(max_length)
        camera.stop_recording()
        return True
    except Exception as e:
        logging.error("snapshot failed {}".format(e.message))
        return False


def callback(client, user_data, message):
    try:
        msg = json.loads(message.payload)
    except ValueError:
        msg = None
    logging.debug("received {} {}".format(message.topic, msg))
    commands = filter(None, message.topic.replace(args.topic, '').split('/'))
    now = datetime.datetime.now()
    if len(commands) > 0:
        cmd = commands.pop(0)
        tags = {'created': awsiot.timestamp_string(now), 'source': args.source}
        if cmd == 'workspace':
            logging.debug("command: {}".format(cmd))
            filename = "{}-{}.jpg".format(args.source, awsiot.file_timestamp_string(now))
            if snapshot(filename) and args.bucket is not None:
                awsiot.mv_to_s3(filename, args.bucket, tags)
        elif cmd == 'snapshot':
            logging.debug("command: {}".format(cmd))
            filename = "{}.jpg".format(args.source)
            if snapshot(filename) and args.web_bucket is not None:
                awsiot.mv_to_s3(filename, args.web_bucket, tags)
        elif cmd == 'recording':
            logging.debug("command: {}".format(cmd))
            filename = "{}-{}.h264".format(args.source, awsiot.file_timestamp_string(now))
            if recording(filename) and args.archive_bucket is not None:
                awsiot.mv_to_s3(filename, args.bucket, tags)
        elif cmd == RECOGNIZE:
            logging.debug("command: {}".format(cmd))
            filename = "{}-{}.jpg".format(args.source, awsiot.file_timestamp_string(now))
            if snapshot(filename) and args.bucket is not None:
                awsiot.mv_to_s3(filename, args.bucket)
                result = awsiot.recognize(filename, args.bucket)
                logging.info("{}: {}".format(RECOGNIZE, result))
                if "Labels" in result:
                    tags[RECOGNIZE] = awsiot.tagify(result['Labels'], 'Name')
                    awsiot.s3_tag(filename, args.bucket, tags)
        else:
            logging.warning('Unrecognized command: {}'.format(cmd))
    else:
        logging.warning("No commands")


if __name__ == "__main__":
    parser = awsiot.iot_arg_parser()
    parser.add_argument("-x", "--width", help="camera resolution width", type=int, default=1920)
    parser.add_argument("-y", "--height", help="camera resolution height", type=int, default=1080)
    parser.add_argument("-z", "--rotation", help="camera rotation", type=int, default=0)
    parser.add_argument("-s", "--source", help="source name", default=platform.node().split('.')[0])
    parser.add_argument("-b", "--bucket", help="S3 bucket")
    parser.add_argument("-w", "--web_bucket", help="S3 bucket for web storage")
    parser.add_argument("-a", "--archive_bucket", help="S3 bucket for archive")
    args = parser.parse_args()

    logging.basicConfig(filename=awsiot.LOG_FILE, level=args.log_level, format=awsiot.LOG_FORMAT)

{'fraction_non_alphanumeric': 0.1448961937716263, 'fraction_numerical': 0.007785467128027681, 'mean_word_length': 2.864623955431755, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2792626', 'n_tokens_mistral': 2086, 'n_tokens_neox': 2033, 'n_words': 451}