make everything functional
This commit is contained in:
@@ -10,44 +10,38 @@ import Foundation
|
||||
import SwiftSoup
|
||||
|
||||
class MatchesAPI {
|
||||
let baseURL = "https://liquipedia.net/"
|
||||
let games = ["dota2", "starcraft", "starcraft2", "heroes", "counterstrike", "rocketleague", "rainbowsix", "overwatch"]
|
||||
let query = "/api.php?action=parse&page=Liquipedia:Upcoming_and_ongoing_matches&format=json&prop=text"
|
||||
var data = ""
|
||||
var matches = [Match]()
|
||||
|
||||
func fetch(for game: String) -> [Match] {
|
||||
fetchData(for: game)
|
||||
extractHTML()
|
||||
func fetchMatches(for game: String) -> [Match] {
|
||||
let games = ["dota2", "starcraft", "starcraft2", "heroes", "counterstrike", "rocketleague", "rainbowsix", "overwatch"]
|
||||
if !games.contains(game) { return [Match]() }
|
||||
let data = fetchData(for: game)
|
||||
let matches = extractMatches(from: data)
|
||||
return matches
|
||||
}
|
||||
|
||||
func constructURL(for game: String) -> String {
|
||||
return baseURL + game + query
|
||||
}
|
||||
|
||||
func fetchData(for game: String) {
|
||||
if !games.contains(game) { return }
|
||||
private func fetchData(for game: String) -> String {
|
||||
let url = constructURL(for: game)
|
||||
|
||||
// DispatchQueue.global().async { [unowned self] in
|
||||
if let url = URL(string: url) {
|
||||
if let data = try? String(contentsOf: url) {
|
||||
self.data = data
|
||||
return data
|
||||
}
|
||||
}
|
||||
// }
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractHTML() {
|
||||
self.data = data.replacingOccurrences(of: "\\n", with: "")
|
||||
self.data = data.replacingOccurrences(of: "\\", with: "")
|
||||
private func extractMatches(from data: String) -> [Match] {
|
||||
var tmp = data.replacingOccurrences(of: "\\n", with: "")
|
||||
tmp = tmp.replacingOccurrences(of: "\\", with: "")
|
||||
|
||||
do {
|
||||
let doc: Document = try SwiftSoup.parse(data)
|
||||
let doc: Document = try SwiftSoup.parse(tmp)
|
||||
let ongoing: Element = try doc.getElementById("infobox_matches")!
|
||||
let matches: Elements = try ongoing.getElementsByTag("table")
|
||||
for match in matches {
|
||||
let matchString: Elements = try ongoing.getElementsByTag("table")
|
||||
|
||||
var matches = [Match]()
|
||||
for match in matchString {
|
||||
let score = try match.getElementsByClass("versus").text()
|
||||
let split = score.split(separator: ":")
|
||||
let leftscore = String(split[0])
|
||||
@@ -62,15 +56,23 @@ class MatchesAPI {
|
||||
team2name: try match.getElementsByClass("team-right").text(),
|
||||
team2score: rightscore
|
||||
)
|
||||
|
||||
self.matches.append(newMatch)
|
||||
matches.append(newMatch)
|
||||
}
|
||||
return matches
|
||||
} catch Exception.Error(_, _) {
|
||||
print("")
|
||||
return [Match]()
|
||||
} catch {
|
||||
print("")
|
||||
return [Match]()
|
||||
}
|
||||
}
|
||||
|
||||
private func constructURL(for game: String) -> String {
|
||||
let baseURL = "https://liquipedia.net/"
|
||||
let query = "/api.php?action=parse&page=Liquipedia:Upcoming_and_ongoing_matches&format=json&prop=text"
|
||||
return baseURL + game + query
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user