// // 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] = [ 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"), TS(id: "19733", date: "05/10/2021"), TS(id: "19731", date: "06/10/2021"), TS(id: "19734", date: "06/10/2021"), TS(id: "19926", date: "06/10/2021"), TS(id: "19927", date: "06/10/2021"), TS(id: "19737", date: "06/10/2021"), TS(id: "19735", date: "07/10/2021"), TS(id: "19736", date: "08/10/2021"), TS(id: "19738", date: "08/10/2021"), TS(id: "19739", date: "08/10/2021"), TS(id: "19740", date: "09/10/2021"), TS(id: "19741", date: "09/10/2021"), TS(id: "19742", date: "11/10/2021"), TS(id: "19743", date: "11/10/2021"), TS(id: "19745", date: "12/10/2021"), TS(id: "19747", date: "12/10/2021"), TS(id: "19748", date: "12/10/2021"), TS(id: "19749", date: "13/10/2021"), TS(id: "19744", date: "14/10/2021"), TS(id: "19751", date: "14/10/2021"), TS(id: "19752", date: "14/10/2021"), TS(id: "19750", date: "15/10/2021"), TS(id: "19754", date: "15/10/2021"), TS(id: "19753", date: "16/10/2021"), TS(id: "19755", date: "16/10/2021"), TS(id: "19756", date: "17/10/2021"), TS(id: "19757", date: "17/10/2021"), TS(id: "19759", date: "17/10/2021"), TS(id: "19761", date: "19/10/2021"), TS(id: "19762", date: "19/10/2021"), TS(id: "19758", date: "20/10/2021"), TS(id: "19763", date: "20/10/2021"), TS(id: "19764", date: "20/10/2021"), TS(id: "19765", date: "21/10/2021"), TS(id: "19766", date: "21/10/2021"), ] for info in infos { sleep(1) 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() do { try realm.write { self.downloadWSOPResults(url: url, date: tournamentInfo.date) } } catch { print("error = \(error)") } } fileprivate static func downloadWSOPResults(url: URL, date: String, currentTournament: Tournament? = nil) { 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: "
", to: "
") ?? string.slice(from: "
", to: "eventinfo-progress") ?? string.slice(from: "
", to: "eventstats") if let infos = infos { var tournament: Tournament? = currentTournament if tournament == nil { let tour = realm.create(Tournament.self) tournament = tour if let name = infos.slice(from: "

", to: "

") { tour.name = name tour.date = Formatter.basicDate.date(from: date) ?? Date() if let strNumber = name.slice(from: "Event #", to: ":"), let number = nf.number(from: strNumber) { tour.number = number.intValue } if name.contains("6-Handed") { tour.tableSize = 6 } else if name.contains("8-Handed") { tour.tableSize = 8 } else if name.contains("Heads-Up") { tour.tableSize = 2 } } else { print("no name = \(infos)") } if let stats = string.slice(from: "
    ", to: "
    ") { if let strBuyin = stats.slice(from: "Buy-in: $", to: ""), let buyin = nf.number(from: strBuyin) { tour.buyin = buyin.intValue } else { print("no buyin = \(stats)") } if let strprizepool = stats.slice(from: "Prizepool: $", to: " "), let pp = nf.number(from: strprizepool) { tour.prizepool = pp.doubleValue } else { print("no prizepool = \(stats)") } if let strentries = stats.slice(from: "Entries: ", to: " "), let entries = nf.number(from: strentries) { tour.entries = entries.intValue } else { print("no entries = \(stats)") } } } if let strresultsblock = string.slice(from: "Country", to: "
    ").last { name = nameraw.replacingOccurrences(of: "", with: "") } if let earningsraw = cells[2].components(separatedBy: "$").last { let strearnings = earningsraw.replacingOccurrences(of: "", with: "") earnings = nf.number(from: strearnings)?.doubleValue } if hasPOY, let poyraw = cells[3].components(separatedBy: "\'>").last { let strpoy = poyraw.replacingOccurrences(of: "", with: "") poy = nf.number(from: strpoy)?.doubleValue } let cityIndex = hasPOY ? 4 : 3 if let cityraw = cells[cityIndex].components(separatedBy: "\'>").last { city = cityraw.replacingOccurrences(of: "", with: "") } let stateIndex = hasPOY ? 5 : 4 if let stateraw = cells[stateIndex].components(separatedBy: "\'>").last { state = stateraw.replacingOccurrences(of: "", with: "") } let countryIndex = hasPOY ? 6 : 5 if let countryraw = cells[countryIndex].components(separatedBy: "\'>").last { country = String(countryraw.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) } if currentTournament == nil, let paging = string.slice(from: "
    ") { let pages = paging.components(separatedBy: "