Advertisement
Don_Mag

Untitled

Nov 18th, 2019
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.24 KB | None | 0 0
  1. //
  2.     func downloadTourneyListHTML() {
  3.        
  4.         // url string to URL
  5.         guard let url = URL(string: "https://www.pickleballtournaments.com/pbt_tlisting.pl?when=F&selstate=AL&selsanctioning=&openregonly=false") else {
  6.             // an error occurred
  7.             print("Error: doesn't seem to be a valid URL")
  8.             return
  9.         }
  10.        
  11.         let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
  12.        
  13.         URLSession.shared.dataTask(with: request) { (data, response, error) in
  14.             if let error = error as NSError? {
  15.                 NSLog("task transport error %@ / %d", error.domain, error.code)
  16.                 return
  17.             }
  18.            
  19.             let response = response as! HTTPURLResponse
  20.             let data = data!
  21.             NSLog("task finished with status %d, bytes %d", response.statusCode, data.count)
  22.            
  23.             // blank line
  24.             print()
  25.            
  26.             // content of url
  27.             if let html = String(data: data, encoding: .isoLatin1) {
  28.                 // parse it into a Document
  29.                 do {
  30.                     print("parse start - takes a few seconds")
  31.                     let document = try SwiftSoup.parse(html)
  32.                     print("parse done")
  33.                    
  34.                     // blank line
  35.                     print()
  36.                    
  37.                     do {
  38.                        
  39.                         // get the "div row" elements for document section div class "tab-pane active tourneylist"
  40.                         let tabPaneRows = try document.select("div.tab-pane.active.tourneylist").select("div.row")
  41.                        
  42.                         // for each row
  43.                         for rw in tabPaneRows {
  44.  
  45.                             var tourneyName = ""
  46.                             var tourneyID = ""
  47.                             var tourneyLocation = ""
  48.                             var tourneyDate = ""
  49.                            
  50.                             var tourneyStatus = ""
  51.                            
  52.                             var regEndDate = ""
  53.                             var regStartDate = ""
  54.                             var regAdvOnly = ""
  55.                            
  56.                             // get the "div infocenter" element
  57.                             let info = try rw.select("div.infocenter")
  58.                            
  59.                             // get the "h3" element
  60.                             let h3 = try info.select("h3")
  61.                            
  62.                             // get the tourneyName
  63.                             tourneyName = try h3.text()
  64.                            
  65.                             // get the tourney link
  66.                             let tLink = try h3.select("a").attr("href")
  67.  
  68.                             // get the text range between "tid=" and end of line
  69.                             if let match = tLink.range(of: "(?<=tid=)([^\n]+)$", options: .regularExpression) {
  70.                                 // clip the "ID" from the link
  71.                                 tourneyID = String(tLink[match])
  72.                             }
  73.                            
  74.                             // get the text from the first "p" tag from the info section
  75.                             tourneyLocation = try info.select("p").first()?.text() ?? ""
  76.                            
  77.                             // get the text from the "p tourney-date" tag
  78.                             tourneyDate = try rw.select("p.tourney-date").text()
  79.                            
  80.                             // look for "registration opennow" tag
  81.                             var tState = try rw.select("div.registration.opennow")
  82.                             if tState.count > 0 {
  83.                                 // found a registration opennow div
  84.                                 tourneyStatus = "OPEN"
  85.                                 // get the registernow text
  86.                                 regEndDate = try rw.select("p.registernow").text()
  87.                             } else {
  88.                                 // did not find opennow div
  89.                                 tourneyStatus = "CLOSED"
  90.                                 // get the
  91.                                 tState = try rw.select("div.registration.closednow")
  92.                                 if tState.count > 0 {
  93.                                     let adOnly = try rw.select("p.adonly")
  94.                                     if adOnly.count > 0 {
  95.                                         regAdvOnly = "Advertisment Only"
  96.                                     }
  97.                                 } else {
  98.                                     // did not find opennow or closednow, so look for soon-date
  99.                                     let soon = try rw.select("p.soon-date")
  100.                                     if soon.count > 1 {
  101.                                         regStartDate = try soon.text()
  102.                                     }
  103.                                 }
  104.                             }
  105.                            
  106.                             print("tourneyName:", tourneyName)
  107.                             print("tourneyID:", tourneyID)
  108.                             print("tourneyLocation:", tourneyLocation)
  109.                             print("tourneyDate:", tourneyDate)
  110.                            
  111.                             print("tourneyStatus:", tourneyStatus)
  112.                            
  113.                             print("regEndDate:", regEndDate)
  114.                             print("regStartDate:", regStartDate)
  115.                             print("regAdvOnly:", regAdvOnly)
  116.                            
  117.                             // here you would create a data object and add it to an array
  118.                            
  119.                             print()
  120.                            
  121.                         }
  122.                        
  123.                         // after populating your data array
  124.                         DispatchQueue.main.async {
  125.                             // call a UI func, such as
  126.                             // self.tableView.reloadData()
  127.                         }
  128.                        
  129.                     } catch Exception.Error(let type, let message){
  130.                         print(message)
  131.                     } catch {
  132.                         print("error")
  133.                     }
  134.                 } catch let error {
  135.                     // an error occurred
  136.                     print("Error: \(error)")
  137.                 }
  138.             }
  139.            
  140.         }.resume()
  141.        
  142.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement