<commit_before>//
//  Track.swift
//  Plug
//
//  Created by <NAME> on 7/16/14.
//  Copyright (c) 2014 Plug. All rights reserved.
//

import Cocoa

public final class Track: NSObject, ResponseObjectSerializable, ResponseCollectionSerializable {
    public let id: String
    public let artist: String
    public let title: String
    public let lovedCount: Int
    public let lovedCountNum: NSNumber
    public let thumbURLSmall: NSURL?
    public let thumbURLMedium: NSURL?
    public let thumbURLLarge: NSURL?
    public let rank: Int?
    public let lovedBy: String?
    public let postedBy: String
    public let postedById: Int
    public let postedCount: Int
    public let postedByDescription: String
    public let datePosted: NSDate
    public let audioUnavailable: Bool
    public let tags: [Tag]

    public var loved: Bool
    
    public var postURL: NSURL! = nil
    public var iTunesURL: NSURL! = nil
    public var playlist: Playlist? = nil
    
    override public var description: String {
        return "<Track - artist: \(artist), title: \(title)>"
    }
    
    public required init?(response: NSHTTPURLResponse, representation: AnyObject) {
        self.id = representation.valueForKeyPath("itemid") as! String
        
        let artistJSON = representation.valueForKeyPath("artist") as? String
        if artistJSON == nil || artistJSON == "" {
            self.artist = "Unknown artist"
        } else {
            self.artist = artistJSON!
        }
        
        let titleJSON = representation.valueForKeyPath("title") as? String
        if titleJSON == nil || titleJSON == "" {
            self.title = "Unknown track"
        } else {
            self.title = titleJSON!
        }

        if representation.valueForKeyPath("ts_loved_me") is Int {
            self.loved = true
        } else {
            self.loved = false
        }
        
        self.lovedCount = representation.valueForKeyPath("loved_count") as! Int
        self.lovedCountNum = NSNumber(integer: lovedCount)
        
        if let thumb_url = representation.valueForKeyPath("thumb_url") as? String {
            self.thumbURLSmall = NSURL(string: thumb_url)
        } else {
            self.thumbURLSmall = nil
        }
        
        if let thumb_url_medium = representation.valueForKeyPath("thumb_url_medium") as? String {
            self.thumbURLMedium = NSURL(string: thumb_url_medium)
        } else {
            self.thumbURLMedium = nil
        }
        
        if let thumb_url_large = representation.valueForKeyPath("thumb_url_large") as? String {
            self.thumbURLLarge = NSURL(string: thumb_url_large)
        } else {
            self.thumbURLLarge = nil
        }
        
        self.rank = representation.valueForKeyPath("rank") as? Int
        self.lovedBy = representation.valueForKeyPath("via_user") as? String
        self.postedBy = representation.valueForKeyPath("sitename") as! String
        self.postedById = representation.valueForKeyPath("siteid") as! Int
        self.postedCount = representation.valueForKeyPath("posted_count") as! Int
        self.postedByDescription = (representation.valueForKeyPath("description") as? String) ?? "No description available"
        self.datePosted = NSDate(timeIntervalSince1970: representation.valueForKeyPath("dateposted") as! NSTimeInterval)
        if let pub_audio_unavail = representation.valueForKeyPath("pub_audio_unavail") as? Bool {
            self.audioUnavailable = pub_audio_unavail
        } else {
            self.audioUnavailable = false
        }
        
        let response = NSHTTPURLResponse()
        self.tags = [
            Tag(response: response, representation: ["tag_name": "electronic"])!,
            Tag(response: response, representation: ["tag_name": "indie"])!,
            Tag(response: response, representation: ["tag_name": "new rave"])!,
            Tag(response: response, representation: ["tag_name": "experimental"])!,
            Tag(response: response, representation: ["tag_name": "pop"])!,
            Tag(response: response, representation: ["tag_name": "british"])!,
            Tag(response: response, representation: ["tag_name": "2k11 mix"])!,
            Tag(response: response, representation: ["tag_name": "2011"])!,
        ]
        
        super.init()
        
        self.postURL = NSURL(string: cleanURLString(representation.valueForKeyPath("posturl") as! String))
        self.iTunesURL = NSURL(string: cleanURLString(representation.valueForKeyPath("itunes_link") as! String))
    }<commit_msg>Changed Track.lovedBy to viaUser to match the API Added viaQuery to Track<commit_after>//
//  Track.swift
//  Plug
//
//  Created by <NAME> on 7/16/14.
//  Copyright (c) 2014 Plug. All rights reserved.
//

import Cocoa

public final class Track: NSObject, ResponseObjectSerializable, ResponseCollectionSerializable {
    public let id: String
    public let artist: String
    public let title: String
    public let lovedCount: Int
    public let lovedCountNum: NSNumber
    public let thumbURLSmall: NSURL?
    public let thumbURLMedium: NSURL?
    public let thumbURLLarge: NSURL?
    public let rank: Int?
    public let viaUser: String?
    public let viaQuery: String?
    public let postedBy: String
    public let postedById: Int
    public let postedCount: Int
    public let postedByDescription: String
    public let datePosted: NSDate
    public let audioUnavailable: Bool
    public let tags: [Tag]

    public var loved: Bool
    
    public var postURL: NSURL! = nil
    public var iTunesURL: NSURL! = nil
    public var playlist: Playlist? = nil
    
    override public var description: String {
        return "<Track - artist: \(artist), title: \(title)>"
    }
    
    public required init?(response: NSHTTPURLResponse, representation: AnyObject) {
        self.id = representation.valueForKeyPath("itemid") as! String
        
        let artistJSON = representation.valueForKeyPath("artist") as? String
        if artistJSON == nil || artistJSON == "" {
            self.artist = "Unknown artist"
        } else {
            self.artist = artistJSON!
        }
        
        let titleJSON = representation.valueForKeyPath("title") as? String
        if titleJSON == nil || titleJSON == "" {
            self.title = "Unknown track"
        } else {
            self.title = titleJSON!
        }

        if representation.valueForKeyPath("ts_loved_me") is Int {
            self.loved = true
        } else {
            self.loved = false
        }
        
        self.lovedCount = representation.valueForKeyPath("loved_count") as! Int
        self.lovedCountNum = NSNumber(integer: lovedCount)
        
        if let thumb_url = representation.valueForKeyPath("thumb_url") as? String {
            self.thumbURLSmall = NSURL(string: thumb_url)
        } else {
            self.thumbURLSmall = nil
        }
        
        if let thumb_url_medium = representation.valueForKeyPath("thumb_url_medium") as? String {
            self.thumbURLMedium = NSURL(string: thumb_url_medium)
        } else {
            self.thumbURLMedium = nil
        }
        
        if let thumb_url_large = representation.valueForKeyPath("thumb_url_large") as? String {
            self.thumbURLLarge = NSURL(string: thumb_url_large)
        } else {
            self.thumbURLLarge = nil
        }
        
        self.rank = representation.valueForKeyPath("rank") as? Int
        self.viaUser = representation.valueForKeyPath("via_user") as? String
        self.viaQuery = representation.valueForKeyPath("via_query") as? String
        self.postedBy = representation.valueForKeyPath("sitename") as! String
        self.postedById = representation.valueForKeyPath("siteid") as! Int
        self.postedCount = representation.valueForKeyPath("posted_count") as! Int
        self.postedByDescription = (representation.valueForKeyPath("description") as? String) ?? "No description available"
        self.datePosted = NSDate(timeIntervalSince1970: representation.valueForKeyPath("dateposted") as! NSTimeInterval)
        if let pub_audio_unavail = representation.valueForKeyPath("pub_audio_unavail") as? Bool {
            self.audioUnavailable = pub_audio_unavail
        } else {
            self.audioUnavailable = false
        }
        
        let response = NSHTTPURLResponse()
        self.tags = [
            Tag(response: response, representation: ["tag_name": "electronic"])!,
            Tag(response: response, representation: ["tag_name": "indie"])!,
            Tag(response: response, representation: ["tag_name": "new rave"])!,
            Tag(response: response, representation: ["tag_name": "experimental"])!,
            Tag(response: response, representation: ["tag_name": "pop"])!,
            Tag(response: response, representation: ["tag_name": "british"])!,
            Tag(response: response, representation: ["tag_name": "2k11 mix"])!,
            Tag(response: response, representation: ["tag_name": "2011"])!,
        ]
        
        super.init()
        
        self.postURL = NSURL(string: cleanURLString(representation.valueForKeyPath("posturl") as! String))
        self.iTunesURL = NSURL(string: cleanURLString(representation.valueForKeyPath("itunes_link") as! String))
    }
{'fraction_non_alphanumeric': 0.09991377452037077, 'fraction_numerical': 0.004311273981461522, 'mean_word_length': 2.6105058365758755, 'pattern_counts': {'":': 16, '<': 7, '<?xml version=': 0, '>': 7, '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': '4074070', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2529, 'n_words': 647}