// // Downloader.swift // TournamentStats // // Created by Laurent Morvillier on 02/10/2021. // Copyright © 2021 Stax River. All rights reserved. // import Foundation import RealmSwift class Downloader { static func downloadWSOPResults() { downloadWSOPResults(tournamentId: "19728") downloadWSOPResults(tournamentId: "19730") // for i in 19728...19732 { // DispatchQueue.global(qos: .utility).async { // // downloadWSOPResults(tournamentId: "\(i)") // // } // } } fileprivate static func downloadWSOPResults(tournamentId: String) { print("Importing tournament \(tournamentId)") guard let url = URL(string: "https://www.wsop.com/tournaments/results/?aid=2&grid=4865&tid=\(tournamentId)&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) if let infos = string.slice(from: "
", to: "
") { try realm.write { let tournament = realm.create(Tournament.self) if let name = infos.slice(from: "

", to: "

") { tournament.name = name tournament.date = Date() if let strNumber = name.slice(from: "Event #", to: ":"), let number = nf.number(from: strNumber) { tournament.number = number.intValue } } else { print("no name = \(infos)") } if let stats = string.slice(from: "