Skip to content

Instantly share code, notes, and snippets.

@jkishner
Forked from brettkelly/BufferClipboardUrl.py
Last active August 29, 2015 14:03
Show Gist options
  • Save jkishner/b768709edd816891d8f1 to your computer and use it in GitHub Desktop.
Save jkishner/b768709edd816891d8f1 to your computer and use it in GitHub Desktop.
Instead of sending "{title} :: {url}" to bufferapp, this fork sends {title}={url}" to Drafts for iOS, and then triggers a Dropbox action. If you don't want the action, just remove "action=spotify&" from the line starting with "out ="
#!/usr/bin/env python
# Minor modifications by Jeffrey Kishner to send title and URL to Drafts instead of Buffer
# @kishner
# http://blog.jeffreykishner.com
# Coded poorly by Brett Kelly
# http://nerdgap.com
# @inkedmn
# -*- coding: utf-8 -*-
import urllib2
import urllib
import re
import clipboard
import urlparse
import notification
import webbrowser
# Sent to Buffer between title and url
post_sep = "="
def makeBufferUrl(url, title):
"""Cobble together the Buffer URL containing our datums"""
# em dashes avoid escaping in the quote() call, so
# maybe we just get rid of them.
if '—' in title:
title = title.replace('—', '--')
title += post_sep
out = "drafts:///create?action=spotify&text="
# UTF-8 encode and escape the page title
t = urllib.quote(title.encode('utf-8'), safe='')
out += "%s" % t
# Escape the URL
u = urllib.quote(url, safe='')
out += "%s" % u
return out
# Grab url (or whatever) from the clipboard
u = clipboard.get()
# this won't throw an exception ever, apparently:
# http://docs.python.org/2/library/urlparse.html
parts = urlparse.urlparse(u)
if parts.scheme not in ['http', 'https']:
# Not the droid we're looking for
# Notify the user and exit.
notification.schedule("Clipboard is not a URL")
print u
print
raise SystemExit
else:
# Yes, yes, I know. Parsing HTML with regexes is stupid.
# It's ok because I'm not a real programmer.
p = re.compile("<title>(.*)</title>", re.IGNORECASE|re.DOTALL)
try:
# open and read the web page
r = urllib2.urlopen(clipboard.get())
h = r.read()
# find and extract the title
t = p.search(h).group(1)
# construct the buffer URL
burl = makeBufferUrl(u, t.strip())
# open the Buffer URL (opens the Buffer app)
webbrowser.open(burl)
except Exception, e:
# Something pooped the bed opening or reading the URL
# This could either print this generic message
# or just spit out whatever was on the clipboard.
print u
# print '[Title Unavailable]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment