Created
February 19, 2014 04:48
-
-
Save brettkelly/9086201 to your computer and use it in GitHub Desktop.
Creates a Buffer-friendly "Title :: URL"-formatted post using the URL in the clipboard, then sends the whole mess to Buffer. This is meant for use with Pythonista on iOS
This file contains hidden or 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 | |
# 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 = "bufferapp://" | |
# UTF-8 encode and escape the page title | |
t = urllib.quote(title.encode('utf-8'), safe='') | |
out += "?t=%s" % t | |
# Escape the URL | |
u = urllib.quote(url, safe='') | |
out += "&u=%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