forked from JTM-rootstorm/Osu-Stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI Functions.swift
executable file
·149 lines (120 loc) · 4.99 KB
/
API Functions.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
//
// API Functions.swift
// Osu! Stats
//
// Created by erich on 7/8/15.
// Heavily Modified by Michael Justman on 3/7/16 - 3/9/16
// Copyright © 2016 Chancellor Porter. All rights reserved.
//
// Official API documentation https://github.com/ppy/osu-api/wiki
// Example of how the Access URL is used in combination with Variables
//https://osu.ppy.sh/api/get_user?k=API-KEY&u=lilchancep
import Foundation
import APIPod
import SwiftyJSON
class API_Functions{
private static var key = APIKey.getKey()
private static var user = User()
static var validUser:Bool = false
class func APICall(api: String) ->Bool{ //, completionHandler: (jData: JSON)->()
var output:String = ""
//build url to use in API call
var URL = NSString(string:"https://osu.ppy.sh/api/").stringByAppendingString(api).stringByAppendingString(self.key)
if(user.username.isEmpty){
output = readFromFile(output)
}
else{
if(!writeToFile()){
return false
}
//remove spaces in username, crashes elsewise; probably can just throw an error when a space
// is found
output = user.username.stringByReplacingOccurrencesOfString(" ", withString: "");
}
URL = URL + output
let session = NSURLSession.sharedSession()
let loginURL = NSURL(string: URL)!
let semaphore = dispatch_semaphore_create(0)
let task = session.dataTaskWithURL(loginURL){(data, response, error) -> Void in
let loginData = JSON(data: data!)
if(!loginData.isEmpty){
if let data = loginData[0].rawString(){
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
//async thread call to update the User data and spit it out to our JSON cache
dispatch_async(dispatch_get_global_queue(priority, 0)){
self.user = User(json: data)
userToJSONFile()
validUser = true
dispatch_semaphore_signal(semaphore)
}
}
}
else{
validUser = false
dispatch_semaphore_signal(semaphore)
}
}
task.resume()
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
return true
}
class func autoPopUsername() -> String{
var username:String = ""
username = readFromFile(username)
return username
}
/** read username from text file for autopopulation of username field on next launch */
private class func readFromFile(var output:String) -> String{
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let readPath = NSString(string: path).stringByAppendingPathComponent("Username.txt")
let readFile = (try? NSString(contentsOfFile: readPath, encoding: NSUTF8StringEncoding)) as? String
if let fileContents = readFile{
output = fileContents as String
}
else{
//print("Error reading file contents at " + readPath)
}
if (output.isEmpty){
print("File is empty")
}
return output
}
/** write username to text file for autopopulation of username field on next launch */
private class func writeToFile() -> Bool{
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let writePath = NSString(string: path).stringByAppendingPathComponent("Username.txt")
var error = NSError(domain: "somedomain", code: 123, userInfo: nil)
let written:Bool
do{
try user.username.writeToFile(writePath, atomically: true, encoding: NSUTF8StringEncoding)
written = true
}
catch let error1 as NSError{
error = error1
written = false
}
if (!written) {
print("There was a problem: \(error)")
return false
}
return true
}
/** output user data to JSON file as a cache so we don't have to call the API every time */
class func userToJSONFile() ->Bool{
let userFile = NSString(string: user.username).stringByAppendingString(".JSON")
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let writePath = NSString(string: path).stringByAppendingPathComponent(userFile)
do{
try user.toJsonString().writeToFile(writePath, atomically: true, encoding: NSUTF8StringEncoding)
print(writePath)
}
catch let error1 as NSError{
print("Error: \(error1)")
return false
}
return true
}
class func getUser() ->User{
return user
}
}