// // 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 ids = ["21666", // 1 "21667", // 2 "21670", // 5 "21671", // 6 "21669", // 4 "21672", // 7 "21673", // 8 "21674", // 9 "21668", // 3 "21675", // 10 "21676", // 11 ] for id in ids { sleep(1) downloadWSOPResults(id: id) } } fileprivate static func downloadWSOPResults(id: String) { print("Importing tournament \(id)") guard let url = URL(string: "https://www.wsop.com/tournaments/results/?aid=2&grid=4943&tid=\(id)&rr=5") else { return } let realm = try! Realm() do { try realm.write { self.downloadWSOPResults(realm: realm, url: url) } } catch { print("error = \(error)") } } fileprivate static func downloadWSOPResults(realm: Realm, url: URL, currentTournament: Tournament? = nil) { 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: "
Sunday, July 17, 2022 to Sunday, July 17, 2022
if let name = infos.slice(from: "", to: "
") let fullDates = fullDatesString?.components(separatedBy: " to ") if let endString = fullDates?.last, let end = Formatter.longDate.date(from: endString) { tour.date = end } else { print("Using now for date") tour.date = Date() } if let strNumber = name.slice(from: "Event #", to: ":"), let number = nf.number(from: strNumber) { tour.number = number.intValue } print("#\(tour.number) \(name)") if name.contains("6-Handed") { tour.tableSize = 6 } else if name.contains("7-Handed") { tour.tableSize = 7 } else if name.contains("8-Handed") { tour.tableSize = 8 } else if name.contains("Heads-Up") || name.contains("Heads Up") { tour.tableSize = 2 } } else { print("no name = \(infos)") } if let stats = string.slice(from: "