Skip to content

Commit

Permalink
Merge pull request #8 from johnste/resolve-short-urls
Browse files Browse the repository at this point in the history
Short url resolver
  • Loading branch information
johnste committed Jul 3, 2015
2 parents b14ab1c + e760b28 commit 8ab91cd
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Finicky/Finicky.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
54899CF31B20D5D100647101 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54899CF21B20D5D100647101 /* Cocoa.framework */; };
548F11CC1B2A39F800FDDC3C /* finicky.icns in Resources */ = {isa = PBXBuildFile; fileRef = 548F11CB1B2A39F800FDDC3C /* finicky.icns */; };
548F11CE1B2A43F700FDDC3C /* FNAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 548F11CD1B2A43F700FDDC3C /* FNAPI.swift */; };
54B0E7031B4678CE003F8AEE /* FNShortUrlResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */; };
54E33FD71B24E27000998E13 /* FNConfigLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54E33FD61B24E27000998E13 /* FNConfigLoader.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -45,6 +46,7 @@
54899CF21B20D5D100647101 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
548F11CB1B2A39F800FDDC3C /* finicky.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = finicky.icns; sourceTree = "<group>"; };
548F11CD1B2A43F700FDDC3C /* FNAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNAPI.swift; sourceTree = "<group>"; };
54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNShortUrlResolver.swift; sourceTree = "<group>"; };
54E33FD61B24E27000998E13 /* FNConfigLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNConfigLoader.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -99,6 +101,7 @@
54899CD31B20D5BC00647101 /* Supporting Files */,
54E33FD61B24E27000998E13 /* FNConfigLoader.swift */,
548F11CD1B2A43F700FDDC3C /* FNAPI.swift */,
54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */,
);
path = Finicky;
sourceTree = "<group>";
Expand Down Expand Up @@ -241,6 +244,7 @@
files = (
54E33FD71B24E27000998E13 /* FNConfigLoader.swift in Sources */,
54899CD61B20D5BC00647101 /* AppDelegate.swift in Sources */,
54B0E7031B4678CE003F8AEE /* FNShortUrlResolver.swift in Sources */,
548F11CE1B2A43F700FDDC3C /* FNAPI.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
35 changes: 23 additions & 12 deletions Finicky/Finicky/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

var statusItem: NSStatusItem!
var configLoader: FNConfigLoader!
var shortUrlResolver: FNShortUrlResolver!
var urlsToLoad = Array<String>()

static var defaultBrowser: String! = "com.google.Chrome"
Expand Down Expand Up @@ -59,36 +60,45 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func handleGetURLEvent(event: NSAppleEventDescriptor?, withReplyEvent: NSAppleEventDescriptor?) {
var url = event!.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue
var url : NSURL = NSURL(string: event!.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue!)!
let pid = event!.attributeDescriptorForKeyword(AEKeyword(keySenderPIDAttr))!.int32Value
let sourceBundleIdentifier = NSRunningApplication(processIdentifier: pid)?.bundleIdentifier

let callback = callUrlHandlers(sourceBundleIdentifier!)

if shortUrlResolver.isShortUrl(url) {
shortUrlResolver.resolveUrl(url, callback: callback)
} else {
callback(url: url)
}
}

func callUrlHandlers(sourceBundleIdentifier: String)(url: NSURL) {
let flags = getFlags()
var bundleIdentifier : String! = AppDelegate.defaultBrowser

let strategy = FinickyAPI.callUrlHandlers(url!, sourceBundleIdentifier: sourceBundleIdentifier!, flags: flags)
var newUrl : NSURL = url

let strategy = FinickyAPI.callUrlHandlers(newUrl, sourceBundleIdentifier: sourceBundleIdentifier, flags: flags)
if strategy["url"] != nil {
url = strategy["url"]
newUrl = NSURL(string: strategy["url"]!)!

let bundleId : String! = strategy["bundleIdentifier"] as String!

if bundleId != nil && !bundleId.isEmpty {
bundleIdentifier = strategy["bundleIdentifier"]!
bundleIdentifier = strategy["bundleIdentifier"]!
}

if bundleIdentifier != nil && !bundleIdentifier.isEmpty {
openUrlWithBrowser(url!, bundleIdentifier:bundleIdentifier)
openUrlWithBrowser(newUrl, bundleIdentifier:bundleIdentifier)
}
}
}

func openUrlWithBrowser(url: String, bundleIdentifier: String) {
func openUrlWithBrowser(url: NSURL, bundleIdentifier: String) {
var eventDescriptor: NSAppleEventDescriptor? = NSAppleEventDescriptor()

var errorInfo : NSDictionary? = nil

var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()

var urls = [NSURL(string: url)!]
var urls = [url]
NSWorkspace.sharedWorkspace().openURLs(urls, withAppBundleIdentifier: bundleIdentifier, options: NSWorkspaceLaunchOptions.Default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
}

Expand All @@ -104,6 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(aNotification: NSNotification) {
configLoader = FNConfigLoader()
configLoader.reload()
shortUrlResolver = FNShortUrlResolver()

var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
Expand Down
4 changes: 2 additions & 2 deletions Finicky/Finicky/FNAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ import JavaScriptCore
the new url and bundle identifier to spawn
*/

class func callUrlHandlers(originalUrl: String, sourceBundleIdentifier: String, flags : Dictionary<String, Bool>) -> Dictionary<String, String> {
class func callUrlHandlers(originalUrl: NSURL, sourceBundleIdentifier: String, flags : Dictionary<String, Bool>) -> Dictionary<String, String> {
var strategy : Dictionary<String, String> = [
"url": originalUrl,
"url": originalUrl.absoluteString!,
"bundleIdentifier": ""
]

Expand Down
85 changes: 85 additions & 0 deletions Finicky/Finicky/FNShortUrlResolver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// ShortUrlResolver.swift
// Finicky
//
// Created by John Sterling on 03/07/15.
// Copyright (c) 2015 John sterling. All rights reserved.
//

import Foundation

class ResolveShortUrls: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {

private var shortUrlResolver : FNShortUrlResolver? = nil

init(shortUrlResolver: FNShortUrlResolver) {
self.shortUrlResolver = shortUrlResolver
super.init()
}

func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
var newRequest : NSURLRequest? = request

if contains([301, 302, 309], response.statusCode) {
if let newUrl = NSURL(string: (response.allHeaderFields["Location"] as? String)!) {
if !shortUrlResolver!.isShortUrl(newUrl) {
newRequest = nil
}
}
}

completionHandler(newRequest)

}
}

class FNShortUrlResolver {

private var shortUrlProviders = [
"bit.ly",
"goo.gl",
"owl.ly",
"deck.ly",
"t.co",
"su.pr",
"spoti.fi",
"fur.ly",
"tinyurl.com"
]

init() {
}

func isShortUrl(url: NSURL) -> Bool {
return contains(shortUrlProviders, url.host!)
}

func resolveUrl(url: NSURL, callback: ((NSURL) -> Void)) -> Void {
if !self.isShortUrl(url) {
callback(url)
return
}

var response: NSURLResponse?
var error: NSError?

var request = NSURLRequest(URL: url)
let myDelegate = ResolveShortUrls(shortUrlResolver: self)

let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: myDelegate, delegateQueue: nil)

let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if let responsy : NSHTTPURLResponse = response as? NSHTTPURLResponse {
let newUrl = NSURL(string: (responsy.allHeaderFields["Location"] as? String)!)!
callback(newUrl)
} else {
callback(url)
}

})


task.resume()
return
}
}

0 comments on commit 8ab91cd

Please sign in to comment.