<commit_before>//
//  VideoPlayerView.swift
//  YouTubePlayer
//
//  Created by <NAME> on 12/21/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit

public enum YouTubePlayerState: String {
    case Unstarted = "-1"
    case Ended = "0"
    case Playing = "1"
    case Paused = "2"
    case Buffering = "3"
    case Queued = "4"
}

public enum YouTubePlayerEvents: String {
    case YouTubeIframeAPIReady = "onYouTubeIframeAPIReady"
    case Ready = "onReady"
    case StateChange = "onStateChange"
    case PlaybackQualityChange = "onPlaybackQualityChange"
}

public enum YouTubePlaybackQuality: String {
    case Small = "small"
    case Medium = "medium"
    case Large = "large"
    case HD720 = "hd720"
    case HD1080 = "hd1080"
    case HighResolution = "highres"
}

public protocol YouTubePlayerDelegate {
    func playerReady(videoPlayer: YouTubePlayer)
    func playerStateChanged(videoPlayer: YouTubePlayer, playerState: YouTubePlayerState)
    func playerQualityChanged(videoPlayer: YouTubePlayer, playbackQuality: YouTubePlaybackQuality)
}

private extension NSURL {
    func queryStringComponents() -> [String: AnyObject] {

        var dict = [String: AnyObject]()

        // Check for query string
        if let query = self.query {

            // Loop through pairings (separated by &)
            for pair in query.componentsSeparatedByString("&") {

                // Pull key, val from from pair parts (separated by =) and set dict[key] = value
                let components = pair.componentsSeparatedByString("=")
                dict[components[0]] = components[1]
            }

        }

        return dict
    }
}

public func videoIDFromYouTubeURL(videoURL: NSURL) -> String? {
    return videoURL.queryStringComponents()["v"] as String?
}

/** Embed and control YouTube videos */
public class YouTubePlayer: UIView, UIWebViewDelegate {

    typealias PlayerParameters = [String: AnyObject]

    var webView: UIWebView!

    // False until YouTubeIframeAPIReady
    public var ready = false

    /** The current state of the video player */
    public var playerState = YouTubePlayerState.Unstarted

    /** The current playback quality of the video player */
    public var playbackQuality = YouTubePlaybackQuality.Small

    public var delegate: YouTubePlayerDelegate?

    // MARK: Various methods for initialization

    override public init() {
        super.init()
        buildWebView(playerParameters())
    }

    override public init(frame: CGRect) {
        super.init(frame: frame)
        buildWebView(playerParameters())
    }

    required public init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        buildWebView(playerParameters())
    }

    override public func layoutSubviews() {
        super.layoutSubviews()

        // Remove web view in case it's within view hierarchy, reset frame, add as subview
        webView.removeFromSuperview()
        webView.frame = bounds
        addSubview(webView)
    }


    // MARK: Web view initialization

    private func buildWebView(parameters: [String: AnyObject]) {
        webView = UIWebView()
        webView.allowsInlineMediaPlayback = true
        webView.mediaPlaybackRequiresUserAction = false
        webView.delegate = self
    }

    // MARK: Player controls

    public func loadVideo(videoURL: NSURL) {
        if let videoID = videoIDFromYouTubeURL(videoURL) {
            loadVideo(videoID)
        }
    }

    public func loadVideo(videoID: String) {
        var playerParams = playerParameters()
        playerParams["videoId"] = videoID

        loadWebViewWithParameters(playerParams)
    }

    public func play() {
        evaluatePlayerCommand("playVideo()")
    }

    public func pause() {
        evaluatePlayerCommand("pauseVideo()")
    }

    public func stop() {
        evaluatePlayerCommand("stopVideo()")
    }

    public func clear() {
        evaluatePlayerCommand("clearVideo()")
    }

    public func seekTo(seconds: Float, seekAhead: Bool) {<commit_msg>clarify loadVideo funcs for obj-c support<commit_after>//
//  VideoPlayerView.swift
//  YouTubePlayer
//
//  Created by <NAME> on 12/21/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit

public enum YouTubePlayerState: String {
    case Unstarted = "-1"
    case Ended = "0"
    case Playing = "1"
    case Paused = "2"
    case Buffering = "3"
    case Queued = "4"
}

public enum YouTubePlayerEvents: String {
    case YouTubeIframeAPIReady = "onYouTubeIframeAPIReady"
    case Ready = "onReady"
    case StateChange = "onStateChange"
    case PlaybackQualityChange = "onPlaybackQualityChange"
}

public enum YouTubePlaybackQuality: String {
    case Small = "small"
    case Medium = "medium"
    case Large = "large"
    case HD720 = "hd720"
    case HD1080 = "hd1080"
    case HighResolution = "highres"
}

public protocol YouTubePlayerDelegate {
    func playerReady(videoPlayer: YouTubePlayer)
    func playerStateChanged(videoPlayer: YouTubePlayer, playerState: YouTubePlayerState)
    func playerQualityChanged(videoPlayer: YouTubePlayer, playbackQuality: YouTubePlaybackQuality)
}

private extension NSURL {
    func queryStringComponents() -> [String: AnyObject] {

        var dict = [String: AnyObject]()

        // Check for query string
        if let query = self.query {

            // Loop through pairings (separated by &)
            for pair in query.componentsSeparatedByString("&") {

                // Pull key, val from from pair parts (separated by =) and set dict[key] = value
                let components = pair.componentsSeparatedByString("=")
                dict[components[0]] = components[1]
            }

        }

        return dict
    }
}

public func videoIDFromYouTubeURL(videoURL: NSURL) -> String? {
    return videoURL.queryStringComponents()["v"] as String?
}

/** Embed and control YouTube videos */
public class YouTubePlayer: UIView, UIWebViewDelegate {

    typealias PlayerParameters = [String: AnyObject]

    var webView: UIWebView!

    // False until YouTubeIframeAPIReady
    public var ready = false

    /** The current state of the video player */
    public var playerState = YouTubePlayerState.Unstarted

    /** The current playback quality of the video player */
    public var playbackQuality = YouTubePlaybackQuality.Small

    public var delegate: YouTubePlayerDelegate?

    // MARK: Various methods for initialization

    override public init() {
        super.init()
        buildWebView(playerParameters())
    }

    override public init(frame: CGRect) {
        super.init(frame: frame)
        buildWebView(playerParameters())
    }

    required public init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        buildWebView(playerParameters())
    }

    override public func layoutSubviews() {
        super.layoutSubviews()

        // Remove web view in case it's within view hierarchy, reset frame, add as subview
        webView.removeFromSuperview()
        webView.frame = bounds
        addSubview(webView)
    }


    // MARK: Web view initialization

    private func buildWebView(parameters: [String: AnyObject]) {
        webView = UIWebView()
        webView.allowsInlineMediaPlayback = true
        webView.mediaPlaybackRequiresUserAction = false
        webView.delegate = self
    }

    // MARK: Player controls

    public func loadVideoURL(videoURL: NSURL) {
        if let videoID = videoIDFromYouTubeURL(videoURL) {
            loadVideoID(videoID)
        }
    }

    public func loadVideoID(videoID: String) {
        var playerParams = playerParameters()
        playerParams["videoId"] = videoID

        loadWebViewWithParameters(playerParams)
    }

    public func play() {
        evaluatePlayerCommand("playVideo()")
    }

    public func pause() {
        evaluatePlayerCommand("pauseVideo()")
    }

    public func stop() {
        evaluatePlayerCommand("stopVideo()")
    }

    public func clear() {
        evaluatePlayerCommand("clearVideo()")
    }

    public func seekTo(seconds: Float, seekAhead: Bool) {
{'fraction_non_alphanumeric': 0.09023577336131341, 'fraction_numerical': 0.007900259227255894, 'mean_word_length': 2.780681287914139, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '5697298', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2182, 'n_words': 625}