<commit_before>//
//  DuckDuckGoSearchController.swift
//  SwiftRest
//
//  Created by <NAME> on 2015-03-08.
//  Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//

/*
Duck Duck Go has an Instant Answers API that can be accessed like:
http://api.duckduckgo.com/?q=%22yoda%27s%20species%22&format=json&pretty=1
(&pretty=1 is optional but nice if you're calling it in a web browser)
Documentation is at https://api.duckduckgo.com/api

The results look like the following and includes an image:
{
  "DefinitionSource" : "",
  "Heading" : "Yoda's species (Star Wars)",
  "ImageWidth" : 109,
  "RelatedTopics" : [
    {
      "Result" : "<a href=\"https://duckduckgo.com/2/c/Members_of_Yoda's_species\">Members of Yoda's species</a>",
      "Icon" : {
      "URL" : "",
      "Height" : "",
      "Width" : ""
    },
    "FirstURL" : "https://duckduckgo.com/2/c/Members_of_Yoda's_species",
    "Text" : "Members of Yoda's species"
    },
    {
      "Result" : "<a href=\"https://duckduckgo.com/2/c/Unidentified_sentient_species\">Unidentified sentient species</a>",
      "Icon" : {
      "URL" : "",
      "Height" : "",
      "Width" : ""
    },
    "FirstURL" : "https://duckduckgo.com/2/c/Unidentified_sentient_species",
    "Text" : "Unidentified sentient species"
    }
  ],
  "Entity" : "",
  "Type" : "A",
  "Redirect" : "",
  "DefinitionURL" : "",
  "AbstractURL" : "http://starwars.wikia.com/wiki/Yoda's_species",
  "Definition" : "",
  "AbstractSource" : "Wookieepedia",
  "Infobox" : "",
  "Image" : "https://duckduckgo.com/i/fd2dabcf.jpg",
  "ImageIsLogo" : 0,
  "Abstract" : "The Jedi Master Yoda was the best-known member of a species whose true name is not recorded. Known in some sources simply as Yoda's species, this species of small carnivorous humanoids produced several well-known members of the Jedi Order during the time of the Galactic Republic.",
  "AbstractText" : "The Jedi Master Yoda was the best-known member of a species whose true name is not recorded. Known in some sources simply as Yoda's species, this species of small carnivorous humanoids produced several well-known members of the Jedi Order during the time of the Galactic Republic.",
  "AnswerType" : "",
  "ImageHeight" : 200,
  "Results" : [],
  "Answer" : ""
}
*/


import Foundation
import Alamofire
import SwiftyJSONextension Alamofire.Request {
import SwiftyJSON


class duckDuckGoSearchController
{

}

let IMAGE_KEY = "Image"<commit_msg>ImageSearchResult class & search results controller<commit_after>//
//  DuckDuckGoSearchController.swift
//  SwiftRest
//
//  Created by <NAME> on 2015-03-08.
//  Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//

/*
Duck Duck Go has an Instant Answers API that can be accessed like:
http://api.duckduckgo.com/?q=%22yoda%27s%20species%22&format=json&pretty=1
(&pretty=1 is optional but nice if you're calling it in a web browser)
Documentation is at https://api.duckduckgo.com/api

The results look like the following and includes an image:
{
  "DefinitionSource" : "",
  "Heading" : "Yoda's species (Star Wars)",
  "ImageWidth" : 109,
  "RelatedTopics" : [
    {
      "Result" : "<a href=\"https://duckduckgo.com/2/c/Members_of_Yoda's_species\">Members of Yoda's species</a>",
      "Icon" : {
      "URL" : "",
      "Height" : "",
      "Width" : ""
    },
    "FirstURL" : "https://duckduckgo.com/2/c/Members_of_Yoda's_species",
    "Text" : "Members of Yoda's species"
    },
    {
      "Result" : "<a href=\"https://duckduckgo.com/2/c/Unidentified_sentient_species\">Unidentified sentient species</a>",
      "Icon" : {
      "URL" : "",
      "Height" : "",
      "Width" : ""
    },
    "FirstURL" : "https://duckduckgo.com/2/c/Unidentified_sentient_species",
    "Text" : "Unidentified sentient species"
    }
  ],
  "Entity" : "",
  "Type" : "A",
  "Redirect" : "",
  "DefinitionURL" : "",
  "AbstractURL" : "http://starwars.wikia.com/wiki/Yoda's_species",
  "Definition" : "",
  "AbstractSource" : "Wookieepedia",
  "Infobox" : "",
  "Image" : "https://duckduckgo.com/i/fd2dabcf.jpg",
  "ImageIsLogo" : 0,
  "Abstract" : "The Jedi Master Yoda was the best-known member of a species whose true name is not recorded. Known in some sources simply as Yoda's species, this species of small carnivorous humanoids produced several well-known members of the Jedi Order during the time of the Galactic Republic.",
  "AbstractText" : "The Jedi Master Yoda was the best-known member of a species whose true name is not recorded. Known in some sources simply as Yoda's species, this species of small carnivorous humanoids produced several well-known members of the Jedi Order during the time of the Galactic Republic.",
  "AnswerType" : "",
  "ImageHeight" : 200,
  "Results" : [],
  "Answer" : ""
}
*/


import Foundation
import Alamofire
import SwiftyJSON

class ImageSearchResult
{
  let imageURL:String?
  let source:String?
  let attributionURL:String?
  var image:UIImage?
  
  required init(anImageURL: String?, aSource: String?, anAttributionURL: String?) {
    imageURL = anImageURL
    source = aSource
    attributionURL = anAttributionURL
  }
  
  func fullAttribution() -> String {
    var result:String = ""
    if attributionURL != nil && attributionURL!.isEmpty == false {
      result += "Image from \(attributionURL!)"
    }
    if source != nil && source!.isEmpty == false  {
      if result.isEmpty
      {
        result += "Image from "
      }
      result += " \(source!)"
    }
    return result
  }
}

class duckDuckGoSearchController
{
  private class func endpointForSearchString(searchString: String) -> String {
    // URL encode it, e.g., "Yoda's Species" -> "Yoda%27s%20Species"
    // and add star wars to the search string so that we don't get random pictures of the Hutt valley or Droid phones
    let encoded = "\(searchString) star wars".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    // create the search string
    // append &t=grokswift so DuckDuckGo knows who's using their services
    return "http://api.duckduckgo.com/?q=\(encoded!)&format=json&t=grokswift"
  }
  
  class func imageFromSearchString(searchString: String, completionHandler: (ImageSearchResult?, NSError?) -> Void) {
    let searchURLString = endpointForSearchString(searchString)
    Alamofire.request(.GET, searchURLString)
      .responseDuckDuckGoImageURL { (request, response, imageURLResult, error) in
        if let anError = error
        {
          completionHandler(imageURLResult, error)
          return
        }
        if imageURLResult == nil || imageURLResult?.imageURL?.isEmpty == true
        {
          completionHandler(imageURLResult, nil)
          return
        }
        // got the URL, now to load it
        Alamofire.request(.GET, imageURLResult!.imageURL!)
          .response { (request, response, data, error) in
            if data == nil
            {
              completionHandler(imageURLResult, nil)
              return
            }
            imageURLResult?.image = UIImage(data: data! as NSData)
            completionHandler(imageURLResult, nil)
        }
    }
  }

}

let IMAGE_KEY = "Image"
{'fraction_non_alphanumeric': 0.12926315789473683, 'fraction_numerical': 0.01024561403508772, 'mean_word_length': 3.1942319011183047, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 12, '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': '2877813', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2240, 'n_words': 622}