<commit_before>//
//  URLSchemeHandler.swift
//  WWDC
//
//  Created by <NAME> on 29/05/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

private let _sharedInstance = URLSchemeHandler()

class URLSchemeHandler: NSObject {
    
    class func SharedHandler() -> URLSchemeHandler {
        return _sharedInstance
    }
    
    func register() {
        NSAppleEventManager.sharedAppleEventManager().setEventHandler(self, andSelector: "handleURLEvent:replyEvent:", forEventClass: UInt32(kInternetEventClass), andEventID: UInt32(kAEGetURL))
    }
    
    func handleURLEvent(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
        if let urlString = event.paramDescriptorForKeyword(UInt32(keyDirectObject))?.stringValue {
            if let url = NSURL(string: urlString) {
                if let host = url.host {
                    if let path = url.path {
                        findAndOpenSession(host,path.stringByReplacingOccurrencesOfString("/", withString: "", options: .CaseInsensitiveSearch, range: nil))
                    }
                }
            }
        }
    }
    
    private func findAndOpenSession(year: String, _ id: String) {
        if let sessions = DataStore.SharedStore.cachedSessions {
            print("Year: \(year) | id: \(id)")
            let foundSession = sessions.filter { session in
                if session.year == Int(year)! && session.id == Int(id)! {
                    return true
                } else {
                    return false
                }
            }
            
            if foundSession.count > 0 {
                let session = foundSession[0]

                if let url = session.hd_url {
                    if VideoStore.SharedStore().hasVideo(url) {
                        let url = VideoStore.SharedStore().localVideoAbsoluteURLString(url)
                        launchVideo(session, url: url)
                    } else {
                        launchVideo(session, url: url)
                    }
                } else {
                    launchVideo(session, url: session.url)
                }
            }
        }
    }
    
    private func launchVideo(session: Session, url: String) {
        let controller = VideoWindowController(session: session, videoURL: url)
        controller.showWindow(self)
    }
    
}<commit_msg>Updated URL scheme handler to new model<commit_after>//
//  URLSchemeHandler.swift
//  WWDC
//
//  Created by <NAME> on 29/05/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

private let _sharedInstance = URLSchemeHandler()

class URLSchemeHandler: NSObject {
    
    class func SharedHandler() -> URLSchemeHandler {
        return _sharedInstance
    }
    
    func register() {
        NSAppleEventManager.sharedAppleEventManager().setEventHandler(self, andSelector: "handleURLEvent:replyEvent:", forEventClass: UInt32(kInternetEventClass), andEventID: UInt32(kAEGetURL))
    }
    
    func handleURLEvent(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
        if let urlString = event.paramDescriptorForKeyword(UInt32(keyDirectObject))?.stringValue {
            if let url = NSURL(string: urlString) {
                if let host = url.host {
                    if let path = url.path {
                        findAndOpenSession(host,path.stringByReplacingOccurrencesOfString("/", withString: "", options: .CaseInsensitiveSearch, range: nil))
                    }
                }
            }
        }
    }
    
    private func findAndOpenSession(year: String, _ id: String) {
        let sessionKey = "#\(year)-\(id)"
        guard let session = WWDCDatabase.sharedDatabase.realm.objectForPrimaryKey(Session.self, key: sessionKey) else { return }
        
        // session has HD video
        if let url = session.hd_url {
            if VideoStore.SharedStore().hasVideo(url) {
                // HD video is available locally
                let url = VideoStore.SharedStore().localVideoAbsoluteURLString(url)
                launchVideo(session, url: url)
            } else {
                // HD video is not available locally
                launchVideo(session, url: url)
            }
        } else {
            // session has only SD video
            launchVideo(session, url: session.videoURL)
        }
    }
    
    private func launchVideo(session: Session, url: String) {
        let controller = VideoWindowController(session: session, videoURL: url)
        controller.showWindow(self)
    }
    
}
{'fraction_non_alphanumeric': 0.09260070052539404, 'fraction_numerical': 0.0074430823117338, 'mean_word_length': 1.908338637810312, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5256832', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1175, 'n_words': 278}