Ejemplo n.º 1
0
    def fetch(self):
        for data in self.slack_client.rtm_read():
            if data.get('type') == 'message':
                if self.botid == data.get('user'):
                    # Do not reply on answers by the Bot
                    continue

                if not self._is_direct(data.get('text')):
                    # Skip the message if bot is not mentioned
                    continue

                request_data = {
                    'id': str(uuid4()),
                    'message': data.get('text')
                }
                logger.log_it(json.dumps(request_data))

                response_data = self._on_direct_message(request_data)

                logger.log_it(response_data)

                if response_data is not None:
                    message = response_data['response'].encode('ascii',
                                                               'ignore')
                    message = "{}".format(message)
                    self.slack_client.api_call('chat.postMessage',
                                               text=message,
                                               as_user="******",
                                               token=self.token,
                                               channel=data.get('channel'))
Ejemplo n.º 2
0
    def start(self):
        self.connect()
        while True:

            try:
                self.fetch()
            except:
                logger.log_it('OOPS', log_type='exception')

            self.ping()
            time.sleep(.1)
Ejemplo n.º 3
0
def produce_shifted_tonemap(rawfile, base_ISO, base_Ev, Ev_shift):
    # Produce a TIFF-format tonemap of RAWFILE at a given EV_SHIFT relative to
    # BASE_ISO. Return the name of the TIFF file so produced.

    log_it("INFO: creating, tagging, and testing a file for Ev_shift %d" % Ev_shift, 2)
    outfile = os.path.splitext(rawfile)[0] + ("+" if Ev_shift >= 0 else "") + str(Ev_shift) + ".jpg"
    command = 'dcraw  -c -v -w -W -b %s %s | cjpeg -quality 100 -dct float > %s' % (2 ** Ev_shift, rawfile, outfile)
    subprocess.call(command, shell=True)

    # OK, we've produced a file. Let's give it EXIF data, then adjust that data
    try:
        ISO = int(base_ISO) * (2 ** Ev_shift)
    except BaseException as e:
        ISO = 100 * (2 ** Ev_shift)                 # Pick a plausible value for the base
        log_it("WARNING: unable to calculate real ISO because %s; using dummy ISO value %d" % (e, ISO), 3)
    try:
        Ev = int(base_Ev) + Ev_shift
    except BaseException as e:
        Ev = 8 + Ev_shift                           # Pick a plausible value for the base
        log_it("WARNING: unable to calculate real Ev because %s; using dummy Ev value %d" % (e, Ev), 3)
    command = 'exiftool -overwrite_original -tagsfromfile %s %s' % (rawfile, outfile)
    subprocess.call(command, shell=True)
    command = 'exiftool -overwrite_original -ISO=%d -AutoISO=%d -BaseISO=%d -MeasuredEV=%d, -MeasuredEV2=%d "%s"'
    command = command % (ISO, ISO, ISO, Ev, Ev, outfile)
    subprocess.call(command, shell=True)
    return outfile
Ejemplo n.º 4
0
def hdr_script_generator(rawfile):
    # Create a series of EV-shifted versions of RAWFILE, then produce a script that
    # will tonemap them. Returns the filename of the script.

    log_it("INFO: creating an HDR tonemapping script for raw file '%s'" % rawfile)
    olddir = os.getcwd()
    try:
        head, tail = os.path.split(rawfile)
        if head:                                    # If we're passed in a full path to a file ...
            os.chdir(os.path.dirname(rawfile))
            rawfile = tail
        files_to_merge = [][:]
        selected_files, shift_mappings = {}.copy(), {}.copy()
        original_ISO = fu.get_value_from_any_tag(fu.find_alt_version(rawfile, fu.jpeg_extensions), ['ISO', 'AutoISO', 'BaseISO'])
        original_Ev = fu.get_value_from_any_tag(fu.find_alt_version(rawfile, fu.jpeg_extensions), ['MeasuredEV', 'MeasuredEV2'])
        for shift_factor in shifts:                 # Create individual ISO-shifted files
            outfile = produce_shifted_tonemap(rawfile, original_ISO, original_Ev, shift_factor)
            shift_mappings[shift_factor] = outfile

        # OK, let's trim the list to actually useful images
        # First, start at the top and move downwards, seeking the darkest useful image.
        current_shift, found_beginning, found_end = max(shifts), False, False
        while current_shift >= min(shifts):
            h = get_smoothed_image_histogram(shift_mappings[current_shift])
            if found_end:                               # If we've already found the bottom image ...
                os.unlink(shift_mappings[current_shift])# ... delete this image, which is past it ...
                del(shift_mappings[current_shift])      # ... and track that we don't have it.
            elif found_beginning:                       # Otherwise, check if this is the last image, i.e. 1st one w/o right-edge clipping.
                if is_left_edge_clipping(h):
                    found_end = True
                    os.unlink(shift_mappings[current_shift])
                    del(shift_mappings[current_shift])
            else:
                found_beginning = not is_right_edge_clipping(h)
            current_shift -= 1
        # Now, start at the bottom, and find the lightest useful image
        current_shift, found_beginning, found_end = min(shift_mappings.keys()), False, False
        while current_shift <= max(shifts):
            h = get_smoothed_image_histogram(shift_mappings[current_shift])
            if found_end:
                os.unlink(shift_mappings[current_shift])
                del(shift_mappings[current_shift])
            elif found_beginning:
                if is_right_edge_clipping(h):
                    found_end = True
                    os.unlink(shift_mappings[current_shift])
                    del(shift_mappings[current_shift])
            else:
                found_beginning = not is_left_edge_clipping(h)
            current_shift += 1

        selected_files = list(shift_mappings.values())
        selected_files = massage_file_list(selected_files)
        files_to_merge = sorted(selected_files)

        # Now move the non-EV-shifted file to the front of the list, because create_script_from_file_list assumes precisely that.
        try:    # If the unshifted image appears in the file list, use that for the base exposure
            files_to_merge.insert(0, files_to_merge.pop(files_to_merge.index(os.path.splitext(rawfile)[0] + "+0.jpg")))
        except ValueError:  # Otherwise, just sort the list, which does a fairly good job of picking a low value for the front.
            files_to_merge.sort()
        chs.create_script_from_file_list(files_to_merge, delete_originals=True, suppress_align=True)
        return os.path.abspath(os.path.splitext(files_to_merge[0])[0] + '_HDR.SH')
    finally:
        os.chdir(olddir)