Jake.codes

OpenIn, for Pythonista

Thursday, November 07, 2013

# -*- coding: utf-8 -*-
# Open In for Safari
# By Jake Bilbrey
# http://jakebilbrey.com
#
# Hate having multiple bookmarklets to
# open websites in their proper iOS app?
# Me too.
#
# javascript:location.href='pythonista://OpenIn?action=run&argv='+encodeURIComponent(location.href);
#
import sys
import re
import console
import webbrowser

browserURL = sys.argv[1]

if "gist.github.com" in browserURL:
    # Install Gist in Pythonista
    # See: http://jake.codes/installgist/
    browserURL = 'pythonista://InstallGist?action=run&argv=' + browserURL
elif "github.com" in browserURL:
    # Open in iOctocat
    browserURL = browserURL.replace('http://','ioc://')
    browserURL = browserURL.replace('https://','ioc://')
elif "twitter.com" in browserURL:
    # Open in Tweetbot
    browserURL = browserURL.replace('https://twitter.com/', 'tweetbot://')
    browserURL = browserURL.replace('statuses', 'status')
    browserURL = browserURL.replace('http://twitter.com/', 'tweetbot://')
    browserURL = browserURL.replace('http://mobile.twitter.com/', 'tweetbot://')
    browserURL = browserURL.replace('https://mobile.twitter.com/', 'tweetbot://')
elif ("wikia.com" in browserURL) or ("wikipedia.org" in browserURL) or ("wiki." in browserURL) or ("wiki/" in browserURL):
    # Open in Wikipanion
    browserURL = browserURL.replace('http://','wplink://')
    browserURL = browserURL.replace('https://','wplink://')
elif ("bing.com" in browserURL) or ("google.com" in browserURL) or ("yahoo.com" in browserURL):
    # Searches on Safari's Default Search Engines reroute' through DuckDuckGo 
    browserURL = 'safari-https://duckduckgo.com/' + re.search("\?(q|p)=.+?(?=&)", browserURL, re.I).group(0)
    browserURL = browserURL.replace('?p=', '?q=')
elif "imdb.com" in browserURL: 
    # Open in IMDB
    browserURL = re.sub("http(s)?://(www.|m.)?imdb.com/", "imdb:///", browserURL)
elif (".zip" in browserURL) or (".rar" in browserURL) or (".torrent" in browserURL): 
    # Open in Documents
    browserURL = 'r' + browserURL
elif ".tumblr.com" in browserURL: 
    # Open Tumblr blog or post in Tumblr
    tumblrUser = re.search("(?<=http://).+(?=.tumblr.com)", browserURL).group(0)
    tumblrPost = ''
    
    if re.search("/post/\d+", browserURL): 
        tumblrPost = re.search("(?<=/post/)\d+", browserURL).group(0)
    
    browserURL = 'tumblr://x-callback-url/blog?blogName=' + tumblrUser + '&postID=' + tumblrPost

console.clear()
print browserURL

webbrowser.open(browserURL)