Skip to content

Instantly share code, notes, and snippets.

@mplewis
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mplewis/4c3cc8b45d678e01ece0 to your computer and use it in GitHub Desktop.
Save mplewis/4c3cc8b45d678e01ece0 to your computer and use it in GitHub Desktop.
Like CloudApp, but for imgur, and free. License: MIT. Don't forget: pip3 install appdirs imgurpython watchdog pync
#!/usr/bin/env python3
import appdirs
from imgurpython import ImgurClient
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from pync import Notifier
import os
import json
import webbrowser
import time
import subprocess
class NewScreenshotEventHandler(FileSystemEventHandler):
def on_created(self, event):
handle_path(event.src_path)
def on_moved(self, event):
handle_path(event.dest_path)
def ding():
subprocess.call(['afplay', 'teleport.mp3'])
def to_clipboard(text):
os.system('echo %s | tr -d "\n" | pbcopy' % text)
def handle_path(filepath):
filename = os.path.basename(filepath)
is_screenshot = (filename.startswith('Screen Shot ') and
filename.endswith('.png'))
if not is_screenshot:
return
try:
resp = client.upload_from_path(filepath, anon=False)
except Exception as e:
print(e)
return
link = resp['link']
print(link)
to_clipboard(link)
Notifier.notify(link, title='Upload Complete', open=link)
ding()
client_id = '3144101f767d911'
client_secret = 'f4c34e78086ebdd4a433c89460c30d3681a9c4e5'
client = ImgurClient(client_id, client_secret)
app_name = 'macshots'
app_author = 'mplewis'
creds_file = 'credentials.json'
app_dir = appdirs.user_data_dir(app_name, app_author)
creds_path = os.path.join(app_dir, creds_file)
desktop_dir = os.path.expanduser('~/Desktop')
os.makedirs(app_dir, exist_ok=True)
try:
with open(creds_path) as f:
credentials = json.load(f)
except OSError:
authorization_url = client.get_auth_url('pin')
webbrowser.open(authorization_url)
print()
print('Go here to authorize this app:')
print(authorization_url)
print()
pin = input('Paste your Imgur PIN here: ')
credentials = client.authorize(pin, 'pin')
with open(creds_path, 'w+') as f:
json.dump(credentials, f)
access_token = credentials['access_token']
refresh_token = credentials['refresh_token']
client.set_user_auth(access_token, refresh_token)
event_handler = NewScreenshotEventHandler()
observer = Observer()
observer.schedule(event_handler, desktop_dir)
observer.start()
print('macshots is ready!')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment