<commit_before>//
//  DataController.swift
//  grokHTMLAndDownloads
//
//  Created by <NAME> on 2015-10-12.
//  Copyright © 2015 Teak Mobile Inc. All rights reserved.
//

import Foundation
import Alamofire
import HTMLReader

let URLString = "http://www.charts.noaa.gov/PDFs/PDFs.shtml"

class DataController {
  var charts: [Chart]?
  
  init() {
    fetchCharts()
  }
  
  func fetchCharts() {

  }
}
<commit_msg>Added function to fetch charts, doesn’t provide them to anything yet though<commit_after>//
//  DataController.swift
//  grokHTMLAndDownloads
//
//  Created by <NAME> on 2015-10-12.
//  Copyright © 2015 Teak Mobile Inc. All rights reserved.
//

import Foundation
import Alamofire
import HTMLReader

let URLString = "http://www.charts.noaa.gov/PDFs/PDFs.shtml"

class DataController {
  var charts: [Chart]?
  
  init() {
    fetchCharts()
  }
  
  func fetchCharts() {
    Alamofire.request(.GET, URLString)
      .responseString { responseString in
        if let htmlAsString = responseString.result.value {
          // TODO checks from Alamofire page
          let doc = HTMLDocument(string: htmlAsString)
          if let tableContents = doc.firstNodeMatchingSelector("tbody") {
            self.charts = []
            for row in tableContents.children {
              var url: String?
              var number: Int?
              var scale: Int?
              var title: String?
              if let rowElement = row as? HTMLElement { // TODO: should be able to combine this with loop above
                // first column: URL and number
                if let firstColumn = rowElement.children[0] as? HTMLElement {
                  // skip the first row, or any other where the first row doesn't contain a number
                  if let entry = firstColumn.childAtIndex(0) as? HTMLElement {
                    url = entry.objectForKeyedSubscript("href") as? String
                    if let contents = entry.childAtIndex(0) as? HTMLTextNode {
                      number = Int(contents.textContent)
                    }
                  }
                }
                // second column: Scale
                if (url == nil) {
                  continue // can't do anything without a URL, e.g., the header row
                }
                if let secondColumn = rowElement.children[1] as? HTMLElement {
                  if let entry = secondColumn.childAtIndex(0) as? HTMLTextNode {
                    scale = Int(entry.textContent)
                  }
                }
                // third column: Name
                if let thirdColumn = rowElement.children[2] as? HTMLElement {
                  if let entry = thirdColumn.childAtIndex(0) as? HTMLTextNode {
                    title = entry.textContent
                  }
                }

                if let title = title, url = url, number = number, scale = scale {
                  let newChart = Chart(title: title, url: url, number: number, scale: scale)
                  self.charts?.append(newChart)
                }
              }
            }
          }
        }
    }
  }
}

{'fraction_non_alphanumeric': 0.08876694641704326, 'fraction_numerical': 0.010006455777921239, 'mean_word_length': 1.6196111580726966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2966476', 'n_tokens_mistral': 881, 'n_tokens_neox': 820, 'n_words': 249}