<commit_before>//
//  EpisodeViewController.swift
//  Tapas
//
//  Created by <NAME> on 16/06/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Cocoa
import AVKit
import AVFoundation
import WebKit

class EpisodeViewController: NSViewController, WebPolicyDelegate {

    @IBOutlet weak var playerView: AVPlayerView!
    @IBOutlet weak var webView: WebView!

    let avdiStyles = "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://avdi.org/stylesheets/code.css\" />"
    let ourStyles = "<style type=\"text/css\">body { font-family: sans-serif; font-size: 14px; line-height: 1.4 } p { max-width: 40em } pre { margin: 1.4em 0; border-left: 5px solid #f92661; padding-left: 10px }</style>"

    var episode: Episode? {
        didSet {
            if episode != oldValue {
                update()
            }
        }
    }

    var credentials: Credentials? {
        didSet {
            update()
        }
    }

    override func viewDidLoad() {
        update()
    }

    func update() {
        displayHTML()
        displayVideo()
    }

    func displayHTML() {
        if let html = self.episode?.description {
            let htmlWithCSS = avdiStyles + ourStyles + html
            webView.mainFrame.loadHTMLString(htmlWithCSS, baseURL: NSURL(string: "http://www.rubytapas.com/")!)
        }
    }

    func displayVideo() {
        if let url = self.episode?.url, let credentials = self.credentials {
            let token = Base64.encode("\(credentials.username):\(credentials.password)")
            let headers = ["Authorization": "Basic \(token!)"]
            let asset = AVURLAsset(URL: NSURL(string: url)!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
            let item = AVPlayerItem(asset: asset)
            let player = AVPlayer(playerItem: item)

            playerView.player = player

            player.play()
        }
    }

    override func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {
        let url = request.URL!

        if url.absoluteString.hasPrefix("https://rubytapas.dpdcart.com/subscriber/download") {
            NSWorkspace.sharedWorkspace().openURL(url)
        } else {
            listener.use()
        }
    }
}<commit_msg>Improve URL handling in EpisodeViewController<commit_after>//
//  EpisodeViewController.swift
//  Tapas
//
//  Created by <NAME> on 16/06/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Cocoa
import AVKit
import AVFoundation
import WebKit

class EpisodeViewController: NSViewController, WebPolicyDelegate {

    @IBOutlet weak var playerView: AVPlayerView!
    @IBOutlet weak var webView: WebView!

    let avdiStyles = "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://avdi.org/stylesheets/code.css\" />"
    let ourStyles = "<style type=\"text/css\">body { font-family: sans-serif; font-size: 14px; line-height: 1.4 } p { max-width: 40em } pre { margin: 1.4em 0; border-left: 5px solid #f92661; padding-left: 10px }</style>"

    var episode: Episode? {
        didSet {
            if episode != oldValue {
                update()
            }
        }
    }

    var credentials: Credentials? {
        didSet {
            update()
        }
    }

    override func viewDidLoad() {
        update()
    }

    func update() {
        displayHTML()
        displayVideo()
    }

    func displayHTML() {
        if let html = self.episode?.description {
            let htmlWithCSS = avdiStyles + ourStyles + html
            webView.mainFrame.loadHTMLString(htmlWithCSS, baseURL: NSURL(string: "http://www.rubytapas.com/")!)
        }
    }

    func displayVideo() {
        if let url = self.episode?.url, let credentials = self.credentials {
            let token = Base64.encode("\(credentials.username):\(credentials.password)")
            let headers = ["Authorization": "Basic \(token!)"]
            let asset = AVURLAsset(URL: NSURL(string: url)!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
            let item = AVPlayerItem(asset: asset)
            let player = AVPlayer(playerItem: item)

            playerView.player = player

            player.play()
        }
    }

    override func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {
        if let url = request.URL {
            let urlString = url.absoluteString

            if !blacklisted(urlString) && whitelisted(urlString) {
                listener.use()
            } else {
                NSWorkspace.sharedWorkspace().openURL(url)
            }
        }
    }


    private func blacklisted(str: String) -> Bool {
        return BLACKLIST.contains() { str.hasPrefix($0) }
    }

    private func whitelisted(str: String) -> Bool {
        return WHITELIST.contains() { str.hasPrefix($0) }
    }

    private let WHITELIST = [
        "https://rubytapas.dpdcart.com",
        "http://www.rubytapas.com/",
        "http://avdi.org"
    ]

    private let BLACKLIST = [
        "https://rubytapas.dpdcart.com/subscriber/download"
    ]
}
{'fraction_non_alphanumeric': 0.12287012495266944, 'fraction_numerical': 0.0121166224914805, 'mean_word_length': 2.335227272727273, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532658', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1531, 'n_words': 359}