Exemplo n.º 1
0
    def handle_screenshot_candidate(self, f):
        """Given a candidate file, handle it if it's a screenshot."""
        # The file could be anything. Only act if it's a screenshot.
        if not utils.is_screenshot(f):
            log.debug('%s is missing or not a screenshot.' % f)
            return

        # Do not act on files that are too old (so we don't swallow old files
        # that are not new screenshots).
        now = time.time()
        filename_time = utils.timestamp_from_filename(
            f)  # Parse time out of filename.
        if (now - os.path.getctime(f) > TIME_THRESHOLD or not filename_time
                or now - filename_time > TIME_THRESHOLD):
            log.debug('Ignoring %s, too old.' % f)
            return

        # Create target dir if needed.
        if not os.path.isdir(SHARE_DIR):
            log.debug('Creating share dir %s' % SHARE_DIR)
            os.makedirs(SHARE_DIR)

        # Determine target filename in share directory.
        log.debug('Moving %s to %s' % (f, SHARE_DIR))
        if utils.get_pref('randomize'):  # Randomize file names?
            ext = os.path.splitext(f)[1]
            while True:
                shared_name = utils.randname() + ext
                target_file = os.path.join(SHARE_DIR, shared_name)
                if not os.path.exists(target_file):
                    log.debug('New file name is: %s' % shared_name)
                    break
        else:
            shared_name = os.path.basename(f)
            target_file = os.path.join(SHARE_DIR, shared_name)

        # Move/copy file there.
        if (utils.get_pref('retinascale')
                and utils.resampleRetinaImage(f, target_file)):
            if not utils.get_pref('copyonly'):
                os.unlink(f)
        else:
            if utils.get_pref('copyonly'):
                shutil.copy(f, target_file)
            else:
                shutil.move(f, target_file)

        # Create shared URL.
        url = utils.share_url(urllib.quote(shared_name))
        log.debug('Share URL is %s' % url)

        log.debug('Copying to clipboard.')
        utils.pbcopy(url)

        # Notify user.
        notify('Screenshot shared!',
               'Your URL is: %s\n\n'
               'Click here to view file.' % url,
               context=target_file,
               callback=self.notify_callback)
Exemplo n.º 2
0
    def handle_screenshot_candidate(self, f):
        """Given a candidate file, handle it if it's a screenshot."""
        # The file could be anything. Only act if it's a screenshot.
        if not utils.is_screenshot(f):
            log.debug('%s is missing or not a screenshot.' % f)
            return

        # Do not act on files that are too old (so we don't swallow old files
        # that are not new screenshots).
        now = time.time()
        filename_time = utils.timestamp_from_filename(f)  # Parse time out of filename.
        if (now - os.path.getctime(f) > TIME_THRESHOLD or
            not filename_time or
            now - filename_time > TIME_THRESHOLD):
            log.debug('Ignoring %s, too old.' % f)
            return

        # Create target dir if needed.
        if not os.path.isdir(SHARE_DIR):
            log.debug('Creating share dir %s' % SHARE_DIR)
            os.makedirs(SHARE_DIR)

        # Determine target filename in share directory.
        log.debug('Moving %s to %s' % (f, SHARE_DIR))
        if utils.get_pref('randomize'):  # Randomize file names?
            ext = os.path.splitext(f)[1]
            while True:
                shared_name = utils.randname() + ext
                target_file = os.path.join(SHARE_DIR, shared_name)
                if not os.path.exists(target_file):
                    log.debug('New file name is: %s' % shared_name)
                    break
        else:
            shared_name = os.path.basename(f)
            target_file = os.path.join(SHARE_DIR, shared_name)

        # Move/copy file there.
        if (utils.get_pref('retinascale') and
            utils.resampleRetinaImage(f, target_file)):
            if not utils.get_pref('copyonly'):
                os.unlink(f)
        else:
            if utils.get_pref('copyonly'):
                shutil.copy(f, target_file)
            else:
                shutil.move(f, target_file)

        # Create shared URL.
        url = utils.share_url(urllib.quote(shared_name))
        log.debug('Share URL is %s' % url)

        log.debug('Copying to clipboard.')
        utils.pbcopy(url)

        # Notify user.
        notify('Screenshot shared!',
               'Your URL is: %s\n\n'
               'Click here to view file.' % url,
               context=target_file, callback=self.notify_callback)
Exemplo n.º 3
0
    def on_moved(self, event):
        """
        Catch move event: OS X creates a temp file, then moves it to its
        final name.
        """
        f = event.dest_path

        # The moved file could be anything. Only act if it's a screenshot.
        if not utils.is_screenshot(f):
            return

        # Create target dir if needed.
        if not os.path.isdir(SHARE_DIR):
            log.debug('Creating share dir %s' % SHARE_DIR)
            os.makedirs(SHARE_DIR)

        # Move image file to target dir.
        log.debug('Moving %s to %s' % (f, SHARE_DIR))
        if utils.get_pref('randomize'):  # Randomize file names?
            ext = os.path.splitext(f)[1]
            while True:
                shared_name = utils.randname() + ext
                target_file = os.path.join(SHARE_DIR, shared_name)
                if not os.path.exists(target_file):
                    log.debug('New file name is: %s' % shared_name)
                    if utils.get_pref('copyonly'):
                        shutil.copy(f, target_file)
                    else:
                        shutil.move(f, target_file)
                    break
        else:
            shared_name = os.path.basename(f)
            shutil.move(f, SHARE_DIR)
            target_file = os.path.join(SHARE_DIR, shared_name)

        # Create shared URL
        url = urlparse.urljoin(
            SHARE_URL.format(dropboxid=utils.get_pref('dropboxid')),
            urllib.quote(shared_name))
        logging.debug('Share URL is %s' % url)

        logging.debug('Copying to clipboard.')
        utils.pbcopy(url)

        # Notify user.
        growl = Growler.alloc().init()
        growl.setCallback(self.notify_callback)
        growl.notify('Screenshot shared!',
                     'Your URL is: %s\n\n'
                     'Click here to view file.' % url,
                     context=target_file)
Exemplo n.º 4
0
    def on_moved(self, event):
        """
        Catch move event: OS X creates a temp file, then moves it to its
        final name.
        """
        f = event.dest_path

        # The moved file could be anything. Only act if it's a screenshot.
        if not utils.is_screenshot(f):
            return

        # Create target dir if needed.
        if not os.path.isdir(SHARE_DIR):
            log.debug('Creating share dir %s' % SHARE_DIR)
            os.makedirs(SHARE_DIR)

        # Move image file to target dir.
        log.debug('Moving %s to %s' % (f, SHARE_DIR))
        if utils.get_pref('randomize'):  # Randomize file names?
            ext = os.path.splitext(f)[1]
            while True:
                shared_name = utils.randname() + ext
                target_file = os.path.join(SHARE_DIR, shared_name)
                if not os.path.exists(target_file):
                    log.debug('New file name is: %s' % shared_name)
                    if utils.get_pref('copyonly'):
                        shutil.copy(f, target_file)
                    else:
                        shutil.move(f, target_file)
                    break
        else:
            shared_name = os.path.basename(f)
            shutil.move(f, SHARE_DIR)
            target_file = os.path.join(SHARE_DIR, shared_name)

        # Create shared URL
        url = urlparse.urljoin(
            SHARE_URL.format(dropboxid=utils.get_pref('dropboxid')),
            urllib.quote(shared_name))
        logging.debug('Share URL is %s' % url)

        logging.debug('Copying to clipboard.')
        utils.pbcopy(url)

        # Notify user.
        growl = Growler.alloc().init()
        growl.setCallback(self.notify_callback)
        growl.notify('Screenshot shared!',
                     'Your URL is: %s\n\n'
                     'Click here to view file.' % url,
                     context=target_file)
Exemplo n.º 5
0
    def handle_screenshot_candidate(self, f):
        """Given a candidate file, handle it if it's a screenshot."""
        # Do not act on files that are too old (so we don't swallow files
        # that are not new screenshots).
        if os.path.getctime(f) - time.time() > TIME_THRESHOLD:
            return

        # The file could be anything. Only act if it's a screenshot.
        if not utils.is_screenshot(f):
            return

        # Create target dir if needed.
        if not os.path.isdir(SHARE_DIR):
            log.debug('Creating share dir %s' % SHARE_DIR)
            os.makedirs(SHARE_DIR)

        # Move image file to target dir.
        log.debug('Moving %s to %s' % (f, SHARE_DIR))
        if utils.get_pref('randomize'):  # Randomize file names?
            ext = os.path.splitext(f)[1]
            while True:
                shared_name = utils.randname() + ext
                target_file = os.path.join(SHARE_DIR, shared_name)
                if not os.path.exists(target_file):
                    log.debug('New file name is: %s' % shared_name)
                    if utils.get_pref('copyonly'):
                        shutil.copy(f, target_file)
                    else:
                        shutil.move(f, target_file)
                    break
        else:
            shared_name = os.path.basename(f)
            shutil.move(f, SHARE_DIR)
            target_file = os.path.join(SHARE_DIR, shared_name)

        # Create shared URL
        url = utils.share_url(urllib.quote(shared_name))
        logging.debug('Share URL is %s' % url)

        logging.debug('Copying to clipboard.')
        utils.pbcopy(url)

        # Notify user.
        growl = Growler.alloc().init()
        growl.setCallback(self.notify_callback)
        growl.notify('Screenshot shared!',
                     'Your URL is: %s\n\n'
                     'Click here to view file.' % url,
                     context=target_file)
Exemplo n.º 6
0
    def handle_screenshot_candidate(self, f):
        """Given a candidate file, handle it if it's a screenshot."""
        # Do not act on files that are too old (so we don't swallow old files
        # that are not new screenshots).
        log.debug('Handling the screenshot candidate...')

        # sleep for a bit to let the file system catch up
        # else `is_screenshot` might be wrong
        time.sleep(0.5)

        if os.path.getctime(f) - time.time() > TIME_THRESHOLD:
            log.debug('Time threshhold exceeded.')
            return

        # The file could be anything. Only act if it's a screenshot.
        if not utils.is_screenshot(f):
            log.debug('Does not look like a screenshot.')
            return

        # Create target dir if needed.
        if not os.path.isdir(SHARE_DIR):
            log.debug('Creating share dir %s' % SHARE_DIR)
            os.makedirs(SHARE_DIR)

        # Determine target filename in share directory.
        log.debug('Moving %s to %s' % (f, SHARE_DIR))
        if utils.get_pref('randomize'):  # Randomize file names?
            ext = os.path.splitext(f)[1]
            while True:
                shared_name = utils.randname() + ext
                target_file = os.path.join(SHARE_DIR, shared_name)
                if not os.path.exists(target_file):
                    log.debug('New file name is: %s' % shared_name)
                    break
        else:
            shared_name = os.path.basename(f)
            target_file = os.path.join(SHARE_DIR, shared_name)

        # We repurpose the "copyonly" setting.
        # True: Move screenshot to SHARE_DIR and leave it there
        # False: Remove all screenshots from the filesystem after upload

        # Move/copy file there.
        if (utils.get_pref('retinascale') and
            utils.resampleRetinaImage(f, target_file)):
            os.unlink(f)
        else:
            shutil.move(f, target_file)

        try:
            # Actually upload the file to our custom url
            if not utils.get_pref('upload_url'):
                log.debug('No Upload URL defined: {0}'.format(utils.get_pref('upload_url')))
                raise Exception('No Upload URL defined.')

            content = base64.b64encode(open(target_file).read())
            payload = {'image': content}
            headers = {'Content-Type': 'application/json'}
            r = requests.post(utils.get_pref('upload_url'), data=json.dumps(payload), headers=headers)

            log.debug('Response raw content:')
            log.debug(r.content)

            url = r.json()['link']
            log.debug('Share URL is %s' % url)

            log.debug('Copying to clipboard.')
            utils.pbcopy(url)

            # Remove old file
            if not utils.get_pref('copyonly'):
                os.unlink(target_file)

            # Notify user of success
            if utils.get_pref('notification_growl'):
                growl = Growler.alloc().init()
                growl.setCallback(self.notify_callback)
                growl.notify('Success! Screenshot uploaded.',
                             '%s\n\n' % url,
                             context=target_file)

            if utils.get_pref('notification_sound'):
                return_code = subprocess.call(["afplay", NOTIFICATION_SOUND])
                log.debug('Playing notification sound: {0}'.format(return_code))

        except Exception, e:
            # Notify user of error
            log.debug('EXCEPTION %s' % str(e))
            message = str(e)
            growl = Growler.alloc().init()
            growl.setCallback(self.notify_callback)
            growl.notify('Error uploading screenshot!', message,
                         context=target_file)