Skip to content

Instantly share code, notes, and snippets.

@linmic
Last active January 2, 2016 01:59
Show Gist options
  • Save linmic/8233958 to your computer and use it in GitHub Desktop.
Save linmic/8233958 to your computer and use it in GitHub Desktop.
import time
import threading
import pyperclip
import bitly
# fill you auth
bitlyAPI = bitly.Api(login='', apikey='')
def get_short_url(url):
return bitlyAPI.shorten(url, {'history':1})
def is_url_but_not_bitly(url):
if url.startswith("https://www.dropbox.com/s/") and not "bit.ly" in url:
return True
return False
def print_to_stdout(clipboard_content):
pyperclip.copy(get_short_url(clipboard_content))
# http://stackoverflow.com/questions/14685999/trigger-an-event-when-clipboard-content-changes
class ClipboardWatcher(threading.Thread):
def __init__(self, predicate, callback, pause=5.):
super(ClipboardWatcher, self).__init__()
self._predicate = predicate
self._callback = callback
self._pause = 5
self._stopping = False
def run(self):
recent_value = ""
while not self._stopping:
tmp_value = pyperclip.paste()
if tmp_value != recent_value:
recent_value = tmp_value
if self._predicate(recent_value):
self._callback(recent_value)
time.sleep(self._pause)
def stop(self):
self._stopping = True
def main():
watcher = ClipboardWatcher(is_url_but_not_bitly,
print_to_stdout,
5.) # up2u
watcher.start()
while True:
try:
time.sleep(10) # should be shorten
except KeyboardInterrupt:
watcher.stop()
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment