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)
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)
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)