<commit_before>//
//  WeatherAPI.swift
//  WeatherTest
//
//  Created by <NAME> on 14/02/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation


class WeatherService
{

    var apiUrl : String
    var apiKey : String
    
    var connectionManager = AFHTTPRequestOperationManager()
    
    init(apiUrl:String, apiKey:String) {
        self.apiUrl = apiUrl
        self.apiKey = apiKey
        
        NSLog("init weather")
        
    }
    
    
    func iconNameByCode(code:Int, big: Bool = false) -> String {
        
        let postfix = big ? "_Big" : ""
        
        switch code {
        case 300...500,200  :   return "Lightning"+postfix
        case 200...299:         return "Wind"+postfix
        case 0...118:           return "Sun"+postfix
        default:                return "Cloudy"+postfix
        }
        
        
    }
    
    
    //MARK: - Weather API
    func fetchWeather(weather: Weather) {
        
        var params = [
            "key": apiKey,
            "q" : weather.latLng,
            "format": "json",
            "fx": "true", //forecast
            "num_of_days": 7,
            "tp": 24, //day average for forcast
            
        ]
        
        connectionManager.GET(apiUrl, parameters: params, success: { (request, response) -> Void in
            
            weather.weatherStartUpdate()
            if let root = response as? NSDictionary {
                if let data = root["data"] as? NSDictionary
                {
                    if let error = root["error"] as? NSArray {
                        //no match
                        weather.isFetching = false
                        return
                    }
                    
                    if  let current_conditions = data["current_condition"] as? [NSDictionary] {
                        if let current = current_conditions.first {
                            
                            weather.tempreatureC =  current["temp_C"] as String
                            weather.tempreatureF =  current["temp_F"] as String
                            weather.type = (current["weatherDesc"] as [NSDictionary]).first!["value"] as String
                            weather.iconCode = (current["weatherCode"] as String).toInt()!
                            weather.windDirection = current["winddir16Point"] as String
                            weather.windSpeedMetric = current["windspeedKmph"] as String
                            weather.windSpeedImperial = current["windspeedMiles"] as String
                            weather.rainAmmountMm = (current["precipMM"] as NSString).floatValue
                            weather.rainPercentage = (current["cloudcover"] as String) + "%"
                            
                        }
                    }
                    
                    if let forecasts = data["weather"] as? [NSDictionary] {
                        
                        var dateFormatter = NSDateFormatter()
                        dateFormatter.dateFormat = "yyyy-MM-dd"
                        var dayFormatter = NSDateFormatter()
                        dayFormatter.dateFormat = "EEEE"
                        
//                        weather.forecastList = [Forecast]()
                        
                        for forecast in forecasts {
                            
                            var forecastDay = Forecast()
                            forecastDay.date = dateFormatter.dateFromString(forecast["date"] as String)!<commit_msg>Multiple forecast item fix<commit_after>//
//  WeatherAPI.swift
//  WeatherTest
//
//  Created by <NAME> on 14/02/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation


class WeatherService
{

    var apiUrl : String
    var apiKey : String
    
    var connectionManager = AFHTTPRequestOperationManager()
    
    init(apiUrl:String, apiKey:String) {
        self.apiUrl = apiUrl
        self.apiKey = apiKey
        
        NSLog("init weather")
        
    }
    
    
    func iconNameByCode(code:Int, big: Bool = false) -> String {
        
        let postfix = big ? "_Big" : ""
        
        switch code {
        case 300...500,200  :   return "Lightning"+postfix
        case 200...299:         return "Wind"+postfix
        case 0...118:           return "Sun"+postfix
        default:                return "Cloudy"+postfix
        }
        
        
    }
    
    
    //MARK: - Weather API
    func fetchWeather(weather: Weather) {
        
        var params = [
            "key": apiKey,
            "q" : weather.latLng,
            "format": "json",
            "fx": "true", //forecast
            "num_of_days": 7,
            "tp": 24, //day average for forcast
            
        ]
        
        connectionManager.GET(apiUrl, parameters: params, success: { (request, response) -> Void in
            
            weather.weatherStartUpdate()
            if let root = response as? NSDictionary {
                if let data = root["data"] as? NSDictionary
                {
                    if let error = root["error"] as? NSArray {
                        //no match
                        weather.isFetching = false
                        return
                    }
                    
                    if  let current_conditions = data["current_condition"] as? [NSDictionary] {
                        if let current = current_conditions.first {
                            
                            weather.tempreatureC =  current["temp_C"] as String
                            weather.tempreatureF =  current["temp_F"] as String
                            weather.type = (current["weatherDesc"] as [NSDictionary]).first!["value"] as String
                            weather.iconCode = (current["weatherCode"] as String).toInt()!
                            weather.windDirection = current["winddir16Point"] as String
                            weather.windSpeedMetric = current["windspeedKmph"] as String
                            weather.windSpeedImperial = current["windspeedMiles"] as String
                            weather.rainAmmountMm = (current["precipMM"] as NSString).floatValue
                            weather.rainPercentage = (current["cloudcover"] as String) + "%"
                            
                        }
                    }
                    
                    if let forecasts = data["weather"] as? [NSDictionary] {
                        
                        var dateFormatter = NSDateFormatter()
                        dateFormatter.dateFormat = "yyyy-MM-dd"
                        var dayFormatter = NSDateFormatter()
                        dayFormatter.dateFormat = "EEEE"
                        
                        weather.forecastList = [Forecast]()
                        
                        for forecast in forecasts {
                            
                            var forecastDay = Forecast()
                            forecastDay.date = dateFormatter.dateFromString(forecast["date"] as String)!
{'fraction_non_alphanumeric': 0.08844587954513548, 'fraction_numerical': 0.00954653937947494, 'mean_word_length': 1.1561743341404358, 'pattern_counts': {'":': 10, '<': 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': '4931352', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1658, 'n_words': 396}