-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImaggaService_Backup.swift
191 lines (153 loc) · 8.27 KB
/
ImaggaService_Backup.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// ImaggaService.swift
// Slexie
//
// Created by Zafer Cavdar on 19/08/2016.
// Copyright © 2016 Zafer Cavdar. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
class ImaggaService: PhotoAnalyzeService {
private struct API{
static let key = "acc_d564dcda17e432d"
static let secret = "f7573c7958226776ab1e22c0542604ed"
static let endPoint = "https://api.imagga.com"
static let authenticationToken = "Basic YWNjX2Q1NjRkY2RhMTdlNDMyZDpmNzU3M2M3OTU4MjI2Nzc2YWIxZTIyYzA1NDI2MDRlZA=="
}
private struct ConsString {
static let contentType = "application/json"
}
private let threshold = 25.00
private let maxTagNumber = 10
// Object mapper, slackten bak
func findBackgroundColorWithContentID(contentID: String, completion: (color: String) -> Void) {
//let requestURL = "\(APIEndPoint)/v1/colors?url=\(imageURL)"
let requestURL = "\(API.endPoint)/v1/colors?content=\(contentID)"
var colorString = ""
Alamofire.request(.GET, requestURL).authenticate(user: API.key, password: API.secret)
.validate(contentType: [ConsString.contentType]).responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let color = json["results"][0]["info"]["background_colors"][0]["closest_palette_color_parent"]
colorString = String(color.description)
print("JSON Background Color: \(colorString)")
completion(color: colorString)
}
case .Failure(let error):
print("FAILED while finding background color \(error)")
}
}
}
func uploadPhotoGetContentID(imageData: NSData, completion: (id:String) -> Void){
let uploadURL = "\(API.endPoint)/v1/content"
Alamofire.upload(.POST, uploadURL,headers: ["Authorization": API.authenticationToken],
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "myImage.png", mimeType: "image/png")
},
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
if let value = response.result.value{
let json = JSON(value)
let statusString = String(json["status"].description)
if statusString == "success"{
let id = String(json["uploaded"][0]["id"].description)
completion(id: id)
}
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
}
func findRelatedTagsWith2(contentID contentID: String, completion: (tags: [String]) -> Void){
let requestURL = "\(API.endPoint)/v1/tagging?content=\(contentID)" // via content ID
var possibleTags: [String: Double] = [:]
var tagNames: [String] = []
Alamofire.request(.GET, requestURL).authenticate(user: API.key, password: API.secret)
.validate(contentType: [ConsString.contentType]).responseJSON { [weak self] response in
guard let strongSelf = self else { return }
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let tags = json["results"][0]["tags"]
for i in 0...tags.count{
let tag = String(tags[i]["tag"].description)
let confidence = Double(tags[i]["confidence"].description)
if confidence > strongSelf.threshold && tagNames.count < strongSelf.maxTagNumber {
possibleTags[tag] = confidence
tagNames.append(tag)
}
}
print("LOG: Tags found.\n")
completion(tags: tagNames)
}
case .Failure(let error):
print("FAILED while finding tags \(error)")
}
}
}
func findRelatedTagsWith(contentID contentID: String, completion: (tags: [String]) -> Void){
let requestURL = "\(API.endPoint)/v1/tagging?content=\(contentID)" // via content ID
var possibleTags: [String: Double] = [:]
var tagNames: [String] = []
Alamofire.request(.GET, requestURL).authenticate(user: API.key, password: API.secret)
.validate(contentType: [ConsString.contentType]).responseJSON { [weak self] response in
guard let strongSelf = self else { return }
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let tags = json["results"][0]["tags"]
for i in 0...tags.count{
let tag = String(tags[i]["tag"].description)
let confidence = Double(tags[i]["confidence"].description)
if confidence > strongSelf.threshold && tagNames.count < strongSelf.maxTagNumber {
possibleTags[tag] = confidence
tagNames.append(tag)
}
}
print("LOG: Tags found.\n")
completion(tags: tagNames)
}
case .Failure(let error):
print("FAILED while finding tags \(error)")
}
}
}
func findRelatedTagsWith(url url: String, completion: (tags: [String]) -> Void){
let requestURL = "\(API.endPoint)/v1/tagging?url=\(url)"
var possibleTags: [String: Double] = [:]
var tagNames: [String] = []
Alamofire.request(.GET, requestURL).authenticate(user: API.key, password: API.secret)
.validate(contentType: [ConsString.contentType]).responseJSON { [weak self] response in
guard let strongSelf = self else { return }
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let tags = json["results"][0]["tags"]
for i in 0...tags.count{
let tag = String(tags[i]["tag"].description)
let confidence = Double(tags[i]["confidence"].description)
if confidence > strongSelf.threshold && tagNames.count < strongSelf.maxTagNumber{
possibleTags[tag] = confidence
tagNames.append(tag)
}
}
print("LOG: Tags found.\n")
completion(tags: tagNames)
}
case .Failure(let error):
print("FAILED while finding tags \(error)")
}
}
}
}