<commit_before>//
//  Utils.swift
//  beauties
//
//  Created by <NAME> on 15/7/4.
//  Copyright (c) 2015年 <NAME>. All rights reserved.
//

import Foundation
import Alamofire

class BeautyDateUtil {
    
    static let PAGE_SIZE = 20
    static let API_FORMAT = "yyyy/MM/dd"
    static let MAX_PAGE = 5
    
    class func generateHistoryDateString(page: Int) -> [String] {
        return self.generateHistoryDateString(format: self.API_FORMAT, historyCount: self.PAGE_SIZE, page: page)
    }
    
    class func generateHistoryDateString(#format: String, historyCount: Int, page: Int) -> [String] {
        
        let today = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let formatter = NSDateFormatter()
        formatter.dateFormat = format
        
        let unit = ((page - 1) * self.PAGE_SIZE)...(page * self.PAGE_SIZE - 1)
        return unit.map({calendar.dateByAddingUnit(.CalendarUnitDay, value: -$0, toDate: today, options: nil)}).filter({$0 != nil}).map({formatter.stringFromDate($0!)})
    }
    
    class func todayString() -> String {
        let today = NSDate()
        let formatter = NSDateFormatter()
        formatter.dateFormat = self.API_FORMAT
        return formatter.stringFromDate(today)
    }
}

class NetworkUtil {
    static let API = "http://gank.io/"
    
    static let patternString = "<img\\s+alt=\".*\"\\s+src=\"(.+)\"\\s+style=\"(.*)\"\\s*/>"
    static let regex = NSRegularExpression(pattern: patternString, options: .CaseInsensitive, error: nil)!
    
    static let heightPatternString = "height\\s*:\\s*(\\d+)px"
    static let heightRegex = NSRegularExpression(pattern: heightPatternString, options: .CaseInsensitive, error: nil)!
    static let widthPatterString = "width\\s*:\\s*(\\d+)px"
    static let widthRegex = NSRegularExpression(pattern: widthPatterString, options: .CaseInsensitive, error: nil)!
    
    class func getImageByDateSync(date: String) -> BeautyImageEntity? {
        
        if let entity = DataUtil.findBeautyForDate(date) {
            println("Hit Cache for date: \(date)!")
            return entity
        }
        
        let url = NSURL(string: API + date)!
        var request = NSURLRequest(URL: url)
        var response: AutoreleasingUnsafeMutablePointer<NSURLResponse? >= nil
        var error: NSErrorPointer = nil
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: error)
        
//        if error.memory != nil {
//            return nil
//        }
        
        if let htmlContentData = data {
            let htmlContent = NSString(data: htmlContentData, encoding: NSUTF8StringEncoding) as! String<commit_msg>print the error message<commit_after>//
//  Utils.swift
//  beauties
//
//  Created by <NAME> on 15/7/4.
//  Copyright (c) 2015年 <NAME>. All rights reserved.
//

import Foundation
import Alamofire

class BeautyDateUtil {
    
    static let PAGE_SIZE = 20
    static let API_FORMAT = "yyyy/MM/dd"
    static let MAX_PAGE = 5
    
    class func generateHistoryDateString(page: Int) -> [String] {
        return self.generateHistoryDateString(format: self.API_FORMAT, historyCount: self.PAGE_SIZE, page: page)
    }
    
    class func generateHistoryDateString(#format: String, historyCount: Int, page: Int) -> [String] {
        
        let today = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let formatter = NSDateFormatter()
        formatter.dateFormat = format
        
        let unit = ((page - 1) * self.PAGE_SIZE)...(page * self.PAGE_SIZE - 1)
        return unit.map({calendar.dateByAddingUnit(.CalendarUnitDay, value: -$0, toDate: today, options: nil)}).filter({$0 != nil}).map({formatter.stringFromDate($0!)})
    }
    
    class func todayString() -> String {
        let today = NSDate()
        let formatter = NSDateFormatter()
        formatter.dateFormat = self.API_FORMAT
        return formatter.stringFromDate(today)
    }
}

class NetworkUtil {
    static let API = "http://gank.io/"
    
    static let patternString = "<img\\s+alt=\".*\"\\s+src=\"(.+)\"\\s+style=\"(.*)\"\\s*/>"
    static let regex = NSRegularExpression(pattern: patternString, options: .CaseInsensitive, error: nil)!
    
    static let heightPatternString = "height\\s*:\\s*(\\d+)px"
    static let heightRegex = NSRegularExpression(pattern: heightPatternString, options: .CaseInsensitive, error: nil)!
    static let widthPatterString = "width\\s*:\\s*(\\d+)px"
    static let widthRegex = NSRegularExpression(pattern: widthPatterString, options: .CaseInsensitive, error: nil)!
    
    class func getImageByDateSync(date: String) -> BeautyImageEntity? {
        
        if let entity = DataUtil.findBeautyForDate(date) {
            println("Hit Cache for date: \(date)!")
            return entity
        }
        
        let url = NSURL(string: API + date)!
        var request = NSURLRequest(URL: url)
        var response: AutoreleasingUnsafeMutablePointer<NSURLResponse? >= nil
        var error: NSErrorPointer = nil
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: error)
        
        if error != nil && error.memory != nil {
            println("ERROR: \(error.memory)")
            return nil
        }
        
        if let htmlContentData = data {
            let htmlContent = NSString(data: htmlContentData, encoding: NSUTF8StringEncoding) as! String
{'fraction_non_alphanumeric': 0.12306843267108168, 'fraction_numerical': 0.006254598969830758, 'mean_word_length': 2.6836043360433606, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '1416352', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1576, 'n_words': 400}