<commit_before>//
//  TPCNetworkUtil.swift
//  WKCC
//
//  Created by tripleCC on 15/11/18.
//  Copyright © 2015年 tripleCC. All rights reserved.
//

import Foundation
import SwiftyJSON
import Alamofire


public enum TPCConfigType: String {
    static let TPCWKCCNBaseURLString = "https://raw.githubusercontent.com/tripleCC/GanHuo/master/configuration/"
    case AboutMe = "AboutMe"
    case LaunchConfig = "LaunchConfig"
    
    func path() -> String {
        var pathComponent = String()
        switch self {
        case .AboutMe:
            pathComponent = "AboutMe.json"
        case .LaunchConfig:
            pathComponent = "LaunchConfig.json"
        }
        return TPCConfigType.TPCWKCCNBaseURLString + pathComponent
    }
}

public enum TPCTechnicalType {
    static let TPCGankBaseURLString = "http://gank.avosapps.com/api"
    case Data(String, Int, Int)
    case Day(Int, Int, Int)
    case Random(String, Int)
    
    func path() -> String {
        var pathComponent = String()
        switch self {
        case let .Data(type, count, page):
            pathComponent = "/data/\(type)/\(count)/\(page)"
        case let .Day(year, month, day):
            pathComponent = "/day/\(year)/\(month)/\(day)"
        case let .Random(type, count):
            pathComponent = "/data/\(type)/\(count)"
        }
        return TPCTechnicalType.TPCGankBaseURLString + pathComponent
    }
}

typealias TPCTechnicalDictionary = [String : [TPCTechnicalObject]]

class TPCNetworkUtil {
    static let shareInstance = TPCNetworkUtil()
    var requests = [Request]()
    func loadTechnicalByYear(year: Int, month: Int, day: Int, completion:((TPCTechnicalDictionary, [String]) -> ())?) {
        TPCNetworkUtil.shareInstance
        Alamofire.request(.GET, TPCTechnicalType.Day(year, month, day).path())
                 .response(completionHandler: { (request, response, data, ErrorType) -> Void in
                    // 移除已经下载好的请求
//                    dispatchGlobal() { self.removeRequest(request) }
                    dispatchGlobal({ () -> () in
                        // 这里有时候会出现顺序错位情况，但是如果定死单个子线程，解析速度会变慢
                        // 后面再优化
                        if let data = data {
                            let categories = JSON(data: data)["category"].arrayValue
                            var categoryArray = [String]()
                            var technicalDict = [String : [TPCTechnicalObject]]()
                            if categories.count > 0 {
                                for category in categories {
                                    categoryArray.append(category.stringValue)
                                }
                                
                                let results = JSON(data: data)["results"].dictionaryValue
                                for item in categories {
                                    if let itemArray = results[item.stringValue]?.arrayValue {
                                        var technicalArray = [TPCTechnicalObject]()
                                        for json in itemArray {
                                            if let dict = json.dictionary {
                                                var technical = TPCTechnicalObject(dict: dict)
                                                technical.desc = TPCTextParser.shareTextParser.parseOriginString(technical.desc!)
                                                technicalArray.append(technical)
                                            }
                                        }
                                        technicalDict[item.stringValue] = technicalArray
                                    }
                                }
                            }
                            if categoryArray.count > TPCConfiguration.allCategories.count && TPCVenusUtil.venusFlag {
                                if categoryArray.count > TPCStorageUtil.fetchAllCategories().count {
                                    TPCStorageUtil.saveAllCategories(categoryArray)
                                    TPCConfiguration.allCategories = categoryArray
                                }
                            }
                            dispatchMain() { completion?(technicalDict, categoryArray) }
                        }
                        
                    })
        })
    }
    
    func cancelAllRequests() {
        for request in requests {
            debugPrint(request.request?.URL?.absoluteString)
            request.cancel()
        }
        requests.removeAll()
    }
    
    func removeRequest(request: NSURLRequest?) {
        if let request = request {
            for i in 0..<requests.count {
                if requests[i].request == request {
                    requests.removeAtIndex(i)
                    break
                }
            }
        }
    }
    
    func loadAbountMe(completion: (aboutMe: TPCAboutMe) -> ()) {
        loadConfigByType(.AboutMe) { JSON in
            completion(aboutMe: TPCAboutMe(dict: JSON))
        }
    }
    
    func loadLaunchConfig(completion: (launchConfig: TPCLaunchConfig) -> ()) {
        loadConfigByType(.LaunchConfig) { JSON in
            completion(launchConfig: TPCLaunchConfig(dict: JSON))
        }
    }
    
    func loadConfigByType(type: TPCConfigType, completion: (response: JSON) -> ()) {
        debugPrint(type.path())
        Alamofire.request(.GET, type.path())
                 .response(completionHandler: { (request, response, data, ErrorType) -> Void in
                    if let data = data {
                        completion(response: JSON(data: data))
                    }
                 })
    }
}<commit_msg>优化api代码<commit_after>//
//  TPCNetworkUtil.swift
//  WKCC
//
//  Created by tripleCC on 15/11/18.
//  Copyright © 2015年 tripleCC. All rights reserved.
//

import Foundation
import SwiftyJSON
import Alamofire

public enum TPCGanHuoType {
    static let TPCGanHuoBaseURLString = "https://raw.githubusercontent.com/tripleCC/GanHuo/master/"
    
    public enum ImageTypeSubtype {
        static let ImageTypeSubtypeURLString = "images/"
        case VenusImage(Int)
        case SelfImage(Int, Int, Int)
        
        func path() -> String {
            var pathComponent = String()
            switch self {
            case let .VenusImage(dayInterval):
                pathComponent = "VenusImages/\(dayInterval).png"
            case let .SelfImage(year, month, day):
                pathComponent = "SelfImages/\(year)\(month)\(day).png"
            }
            return TPCGanHuoType.TPCGanHuoBaseURLString + ImageTypeSubtype.ImageTypeSubtypeURLString + pathComponent
        }
    }
    case ImageType(ImageTypeSubtype)
    
    public enum ConfigTypeSubtype: String {
        static let ConfigTypeSubtypeURLString = "configuration/"
        case AboutMe = "AboutMe"
        case LaunchConfig = "LaunchConfig"
        
        func path() -> String {
            var pathComponent = String()
            switch self {
            case .AboutMe:
                pathComponent = "AboutMe.json"
            case .LaunchConfig:
                pathComponent = "LaunchConfig.json"
            }
            return TPCGanHuoType.TPCGanHuoBaseURLString + ConfigTypeSubtype.ConfigTypeSubtypeURLString + pathComponent
        }
    }
    case ConfigType(ConfigTypeSubtype)
}



public enum TPCTechnicalType {
    static let TPCGankBaseURLString = "http://gank.avosapps.com/api"
    case Data(String, Int, Int)
    case Day(Int, Int, Int)
    case Random(String, Int)
    
    func path() -> String {
        var pathComponent = String()
        switch self {
        case let .Data(type, count, page):
            pathComponent = "/data/\(type)/\(count)/\(page)"
        case let .Day(year, month, day):
            pathComponent = "/day/\(year)/\(month)/\(day)"
        case let .Random(type, count):
            pathComponent = "/data/\(type)/\(count)"
        }
        return TPCTechnicalType.TPCGankBaseURLString + pathComponent
    }
}

typealias TPCTechnicalDictionary = [String : [TPCTechnicalObject]]

class TPCNetworkUtil {
    static let shareInstance = TPCNetworkUtil()
    var requests = [Request]()
    func loadTechnicalByYear(year: Int, month: Int, day: Int, completion:((TPCTechnicalDictionary, [String]) -> ())?) {
        TPCNetworkUtil.shareInstance
        Alamofire.request(.GET, TPCTechnicalType.Day(year, month, day).path())
                 .response(completionHandler: { (request, response, data, ErrorType) -> Void in
                    // 移除已经下载好的请求
//                    dispatchGlobal() { self.removeRequest(request) }
                    dispatchGlobal({ () -> () in
                        // 这里有时候会出现顺序错位情况，但是如果定死单个子线程，解析速度会变慢
                        // 后面再优化
                        if let data = data {
                            let categories = JSON(data: data)["category"].arrayValue
                            var categoryArray = [String]()
                            var technicalDict = [String : [TPCTechnicalObject]]()
                            if categories.count > 0 {
                                for category in categories {
                                    categoryArray.append(category.stringValue)
                                }
                                
                                let results = JSON(data: data)["results"].dictionaryValue
                                for item in categories {
                                    if let itemArray = results[item.stringValue]?.arrayValue {
                                        var technicalArray = [TPCTechnicalObject]()
                                        for json in itemArray {
                                            if let dict = json.dictionary {
                                                var technical = TPCTechnicalObject(dict: dict)
                                                technical.desc = TPCTextParser.shareTextParser.parseOriginString(technical.desc!)
//                                                if TPCVenusUtil.venusFlag {
//                                                    technical.url = 
//                                                }
                                                // 这里对图片url进行替换
                                                technicalArray.append(technical)
                                            }
                                        }
                                        // 这里对item进行判断，安卓过滤
                                        technicalDict[item.stringValue] = technicalArray
                                    }
                                }
                            }
                            if categoryArray.count > TPCConfiguration.allCategories.count && TPCVenusUtil.venusFlag {
                                if categoryArray.count > TPCStorageUtil.fetchAllCategories().count {
                                    TPCStorageUtil.saveAllCategories(categoryArray)
                                    TPCConfiguration.allCategories = categoryArray
                                }
                            }
                            dispatchMain() { completion?(technicalDict, categoryArray) }
                        }
                        
                    })
        })
    }
    
    func cancelAllRequests() {
        for request in requests {
            debugPrint(request.request?.URL?.absoluteString)
            request.cancel()
        }
        requests.removeAll()
    }
    
    func removeRequest(request: NSURLRequest?) {
        if let request = request {
            for i in 0..<requests.count {
                if requests[i].request == request {
                    requests.removeAtIndex(i)
                    break
                }
            }
        }
    }
    
    func loadAbountMe(completion: (aboutMe: TPCAboutMe) -> ()) {
        loadGanHuoByPath(TPCGanHuoType.ConfigTypeSubtype.AboutMe.path()) { (response) -> () in
            completion(aboutMe: TPCAboutMe(dict: response))
        }
    }
    
    func loadLaunchConfig(completion: (launchConfig: TPCLaunchConfig) -> ()) {
        loadGanHuoByPath(TPCGanHuoType.ConfigTypeSubtype.LaunchConfig.path()) { (response) -> () in
            completion(launchConfig: TPCLaunchConfig(dict: response))
        }
    }
    
    func loadGanHuoByPath(path: String, completion: (response: JSON) -> ()) {
        debugPrint(path)
        Alamofire.request(.GET, path)
                 .response(completionHandler: { (request, response, data, ErrorType) -> Void in
                    if let data = data {
                        completion(response: JSON(data: data))
                    }
                 })
    }
}
{'fraction_non_alphanumeric': 0.08829295067827178, 'fraction_numerical': 0.0018819101387908728, 'mean_word_length': 1.3059121316217681, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6252256', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3102, 'n_words': 614}