Don_Mag icon

Untitled

Don_Mag | PRO | 11/18/19 05:49:45 PM UTC | 0 ⭐ | 1145 👁️ | Never ⏰ | []
Swift |

4.24 KB

|

None

|

0 👍

/

0 👎

//
    func downloadTourneyListHTML() {
        
        // url string to URL
        guard let url = URL(string: "https://www.pickleballtournaments.com/pbt_tlisting.pl?when=F&selstate=AL&selsanctioning=&openregonly=false") else {
            // an error occurred
            print("Error: doesn't seem to be a valid URL")
            return
        }
        
        let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
        
        URLSession.shared.dataTask(with: request) { (data, response, error) in
            if let error = error as NSError? {
                NSLog("task transport error %@ / %d", error.domain, error.code)
                return
            }
            
            let response = response as! HTTPURLResponse
            let data = data!
            NSLog("task finished with status %d, bytes %d", response.statusCode, data.count)
            
            // blank line
            print()
            
            // content of url
            if let html = String(data: data, encoding: .isoLatin1) {
                // parse it into a Document
                do {
                    print("parse start - takes a few seconds")
                    let document = try SwiftSoup.parse(html)
                    print("parse done")
                    
                    // blank line
                    print()
                    
                    do {
                        
                        // get the "div row" elements for document section div class "tab-pane active tourneylist"
                        let tabPaneRows = try document.select("div.tab-pane.active.tourneylist").select("div.row")
                        
                        // for each row
                        for rw in tabPaneRows {
 
                            var tourneyName = ""
                            var tourneyID = ""
                            var tourneyLocation = ""
                            var tourneyDate = ""
                            
                            var tourneyStatus = ""
                            
                            var regEndDate = ""
                            var regStartDate = ""
                            var regAdvOnly = ""
                            
                            // get the "div infocenter" element
                            let info = try rw.select("div.infocenter")
                            
                            // get the "h3" element
                            let h3 = try info.select("h3")
                            
                            // get the tourneyName
                            tourneyName = try h3.text()
                            
                            // get the tourney link
                            let tLink = try h3.select("a").attr("href")
 
                            // get the text range between "tid=" and end of line
                            if let match = tLink.range(of: "(?<=tid=)([^\n]+)$", options: .regularExpression) {
                                // clip the "ID" from the link
                                tourneyID = String(tLink[match])
                            }
                            
                            // get the text from the first "p" tag from the info section
                            tourneyLocation = try info.select("p").first()?.text() ?? ""
                            
                            // get the text from the "p tourney-date" tag
                            tourneyDate = try rw.select("p.tourney-date").text()
                            
                            // look for "registration opennow" tag
                            var tState = try rw.select("div.registration.opennow")
                            if tState.count > 0 {
                                // found a registration opennow div
                                tourneyStatus = "OPEN"
                                // get the registernow text
                                regEndDate = try rw.select("p.registernow").text()
                            } else {
                                // did not find opennow div
                                tourneyStatus = "CLOSED"
                                // get the
                                tState = try rw.select("div.registration.closednow")
                                if tState.count > 0 {
                                    let adOnly = try rw.select("p.adonly")
                                    if adOnly.count > 0 {
                                        regAdvOnly = "Advertisment Only"
                                    }
                                } else {
                                    // did not find opennow or closednow, so look for soon-date
                                    let soon = try rw.select("p.soon-date")
                                    if soon.count > 1 {
                                        regStartDate = try soon.text()
                                    }
                                }
                            }
                            
                            print("tourneyName:", tourneyName)
                            print("tourneyID:", tourneyID)
                            print("tourneyLocation:", tourneyLocation)
                            print("tourneyDate:", tourneyDate)
                            
                            print("tourneyStatus:", tourneyStatus)
                            
                            print("regEndDate:", regEndDate)
                            print("regStartDate:", regStartDate)
                            print("regAdvOnly:", regAdvOnly)
                            
                            // here you would create a data object and add it to an array
                            
                            print()
                            
                        }
                        
                        // after populating your data array
                        DispatchQueue.main.async {
                            // call a UI func, such as
                            // self.tableView.reloadData()
                        }
                        
                    } catch Exception.Error(let type, let message){
                        print(message)
                    } catch {
                        print("error")
                    }
                } catch let error {
                    // an error occurred
                    print("Error: \(error)")
                }
            }
            
        }.resume()
        
    }

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎