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). 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) # 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)) 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)
def startListening_(self, sender=None): """Start listening for changes to the screenshot dir.""" event_handler = ScreenshotHandler() self.observer = Observer() self.observer.schedule(event_handler, path=SCREENSHOT_DIR) self.observer.start() self.update_menu() log.debug('Listening for screen shots to be added to: %s' % (SCREENSHOT_DIR)) growl = Growler.alloc().init() growl.notify('UpShot started', 'and listening for screenshots!')
def stopListening_(self, sender=None): """Stop listening to changes ot the screenshot dir.""" if self.observer is not None: self.observer.stop() self.observer.join() self.observer = None log.debug('Stop listening for screenshots.') growl = Growler.alloc().init() growl.notify('UpShot paused', 'Not listening for screenshots for now!') self.update_menu()
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)
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). 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 utils.get_pref('retinascale') and utils.resampleRetinaImage(f, f) with open(f, "rb") as image_file: from lib.phabricator import Phabricator PHAB = Phabricator() # This will use your ~/.arcrc file import base64 encoded_string = base64.b64encode(image_file.read()) result = PHAB.file.upload(data_base64=encoded_string) phid = result.response result = PHAB.file.info(phid=phid) url = result['uri'] object_name = result['objectName'] if utils.get_pref('share_format') == 'long': share_url = url elif utils.get_pref('share_format') == 'short': share_url = object_name logging.debug('Copying to clipboard.') utils.pbcopy(share_url) # Notify user. growl = Growler.alloc().init() growl.setCallback(self.notify_callback) growl.notify( 'Screenshot shared!', '{%s}@%s\n' % (object_name, url), context=url )
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). 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 utils.get_pref('retinascale') and utils.resampleRetinaImage(f, f) with open(f, "rb") as image_file: from lib.phabricator import Phabricator PHAB = Phabricator() # This will use your ~/.arcrc file import base64 encoded_string = base64.b64encode(image_file.read()) result = PHAB.file.upload(data_base64=encoded_string) phid = result.response result = PHAB.file.info(phid=phid) url = result['uri'] object_name = result['objectName'] 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' 'PHID is: %s\n' 'FILE is: %s\n' 'Click here to view file.' % (url, phid, object_name), context=f)
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)