-
-
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 ="
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
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