You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
8.5 KiB
198 lines
8.5 KiB
//
|
|
// Downloader.swift
|
|
// TournamentStats
|
|
//
|
|
// Created by Laurent Morvillier on 02/10/2021.
|
|
// Copyright © 2021 Stax River. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import RealmSwift
|
|
|
|
class Downloader {
|
|
|
|
struct TS {
|
|
var id: String
|
|
var date: String
|
|
}
|
|
|
|
static func downloadWSOPResults() {
|
|
|
|
let infos = [
|
|
TS(id: "19728", date: "02/10/2021"),
|
|
TS(id: "19729", date: "03/10/2021"),
|
|
TS(id: "19730", date: "02/10/2021"),
|
|
TS(id: "19732", date: "04/10/2021")]
|
|
|
|
for info in infos {
|
|
downloadWSOPResults(tournamentInfo: info)
|
|
}
|
|
|
|
// for i in 19728...19732 {
|
|
// DispatchQueue.global(qos: .utility).async {
|
|
//
|
|
// downloadWSOPResults(tournamentId: "\(i)")
|
|
//
|
|
// }
|
|
// }
|
|
|
|
}
|
|
|
|
fileprivate static func downloadWSOPResults(tournamentInfo: TS) {
|
|
|
|
print("Importing tournament \(tournamentInfo.id)")
|
|
|
|
guard let url = URL(string: "https://www.wsop.com/tournaments/results/?aid=2&grid=4865&tid=\(tournamentInfo.id)&rr=5") else {
|
|
return
|
|
}
|
|
|
|
let realm = try! Realm()
|
|
|
|
let nf = NumberFormatter()
|
|
nf.numberStyle = .decimal
|
|
nf.locale = Locale.init(identifier: "en_US")
|
|
nf.groupingSeparator = ","
|
|
|
|
do {
|
|
let string = try String(contentsOf: url)
|
|
|
|
let infos = string.slice(from: "<div id=\"eventinfo\">", to: "<div class='eventinfo-reports'>")
|
|
?? string.slice(from: "<div id=\"eventinfo\">", to: "eventinfo-progress")
|
|
?? string.slice(from: "<div id=\"eventinfo\">", to: "eventstats")
|
|
|
|
if let infos = infos {
|
|
|
|
try realm.write {
|
|
|
|
let tournament = realm.create(Tournament.self)
|
|
|
|
if let name = infos.slice(from: "<h1>", to: "</h1>") {
|
|
tournament.name = name
|
|
tournament.date = Formatter.basicDate.date(from: tournamentInfo.date) ?? Date()
|
|
|
|
if let strNumber = name.slice(from: "Event #", to: ":"), let number = nf.number(from: strNumber) {
|
|
tournament.number = number.intValue
|
|
}
|
|
|
|
if name.contains("6-Handed") {
|
|
tournament.tableSize = 6
|
|
} else if name.contains("8-Handed") {
|
|
tournament.tableSize = 8
|
|
} else if name.contains("Heads-Up") {
|
|
tournament.tableSize = 2
|
|
}
|
|
|
|
} else {
|
|
print("no name = \(infos)")
|
|
}
|
|
|
|
if let stats = string.slice(from: "<ul id=\"eventstats\">", to: "<div id=\"nav-tabs\">") {
|
|
|
|
if let strBuyin = stats.slice(from: "Buy-in:</b> $", to: "</li>"), let buyin = nf.number(from: strBuyin) {
|
|
tournament.buyin = buyin.intValue
|
|
} else {
|
|
print("no buyin = \(stats)")
|
|
}
|
|
|
|
if let strprizepool = stats.slice(from: "Prizepool:</b> $", to: " </li>"), let pp = nf.number(from: strprizepool) {
|
|
tournament.prizepool = pp.doubleValue
|
|
} else {
|
|
print("no prizepool = \(stats)")
|
|
}
|
|
|
|
if let strentries = stats.slice(from: "Entries:</b> ", to: " </li>"), let entries = nf.number(from: strentries) {
|
|
tournament.entries = entries.intValue
|
|
} else {
|
|
print("no entries = \(stats)")
|
|
}
|
|
}
|
|
|
|
if let strresultsblock = string.slice(from: "Country</li>", to: "<div id=\"PagingNav\"") {
|
|
let strresults = strresultsblock.components(separatedBy: "<li class='place")
|
|
|
|
print("Results count = \(strresults.count)")
|
|
|
|
for strresult in strresults {
|
|
|
|
let cells = strresult.components(separatedBy: "<li")
|
|
if !(cells.count == 6 || cells.count == 7) {
|
|
print("cell count = \(cells.count)")
|
|
continue
|
|
}
|
|
|
|
let hasPOY = cells.count == 7
|
|
|
|
var rank: Int? = nil
|
|
var name: String? = nil
|
|
var earnings: Double? = nil
|
|
var city: String? = nil
|
|
var state: String? = nil
|
|
var country: String? = nil
|
|
var poy: Double? = nil
|
|
|
|
if let strrank = cells[0].slice(from: ">", to: "<") {
|
|
rank = nf.number(from: strrank)?.intValue
|
|
}
|
|
|
|
if let nameraw = cells[1].components(separatedBy: "'>").last {
|
|
name = nameraw.replacingOccurrences(of: "</a></li>", with: "")
|
|
}
|
|
|
|
if let earningsraw = cells[2].components(separatedBy: "$").last {
|
|
let strearnings = earningsraw.replacingOccurrences(of: "</li>", with: "")
|
|
earnings = nf.number(from: strearnings)?.doubleValue
|
|
}
|
|
|
|
if hasPOY, let poyraw = cells[3].components(separatedBy: "\'>").last {
|
|
let strpoy = poyraw.replacingOccurrences(of: "</li>", with: "")
|
|
poy = nf.number(from: strpoy)?.doubleValue
|
|
}
|
|
|
|
let cityIndex = hasPOY ? 4 : 3
|
|
if let cityraw = cells[cityIndex].components(separatedBy: "\'>").last {
|
|
city = cityraw.replacingOccurrences(of: "</li>", with: "")
|
|
}
|
|
|
|
let stateIndex = hasPOY ? 5 : 4
|
|
if let stateraw
|
|
= cells[stateIndex].components(separatedBy: "\'>").last {
|
|
state = stateraw.replacingOccurrences(of: "</li>", with: "")
|
|
}
|
|
let countryIndex = hasPOY ? 6 : 5
|
|
if let countryraw
|
|
= cells[countryIndex].components(separatedBy: "\'>").last {
|
|
country = String(countryraw.prefix(2))
|
|
// country = countrytmp.prefix(2)
|
|
}
|
|
|
|
guard let rank = rank, let name = name, let earnings = earnings else {
|
|
print("Missing data: R: \(rank), N: \(name), E: \(earnings)")
|
|
continue
|
|
}
|
|
|
|
let player = realm.getOrCreatePlayer(name: name, city: city ?? "", state: state ?? "", country: country ?? "")
|
|
|
|
let result = realm.create(Result.self)
|
|
result.player = player
|
|
result.earnings = earnings
|
|
result.rank = rank
|
|
|
|
tournament.results.append(result)
|
|
|
|
}
|
|
|
|
} else {
|
|
print("Bad page strresultsblock")
|
|
}
|
|
}
|
|
} else {
|
|
print("Missing name block")
|
|
}
|
|
|
|
} catch {
|
|
print(error)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|