<commit_before>//
//  TRARoute.swift
//  little-daemonPackageDescription
//
//  Created by 江承諭 on 2017/10/18.
//

import Foundation
import LineBot

class TRARoute {
  static func queryTRARoute(message: String, lineBot: LineBot) throws {
    let message = message.replacingOccurrences(of: "台", with: "臺")

    // 如果一開始的字沒有包含車站名稱就跳離
    guard let fromStation = try TRAStation.makeQuery()
      .filter(raw: "$1 LIKE name || '%'", [message])
      .first() else {
        return
    }

    let firstIndex = message.index(message.startIndex, offsetBy: fromStation.name.characters.count)
    let truncated = message.substring(from: firstIndex)

    // 去掉開頭車站名後如果沒有車站名稱就跳離
    guard let toStation = try TRAStation.makeQuery()
      .filter(raw: "$1 LIKE '%' || name || '%'", [truncated])
      .first() else {
        return
    }

    try twtrafficAPI(fromStation: fromStation, toStation: toStation, lineBot: lineBot)
  }

  private static func twtrafficAPI(fromStation: TRAStation,
                                   toStation: TRAStation,
                                   lineBot: LineBot) throws {
    let now = Date()
    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 3600 * 8)
    dateFormatter.dateFormat = "yyyy/MM/dd"
    let dateString = dateFormatter.string(from: now)
    dateFormatter.dateFormat = "HHmm"
    let fromTime = dateFormatter.string(from: now)
    let toTime = dateFormatter.string(from: now.addingTimeInterval(3600 * 3))

    var url = "http://twtraffic.tra.gov.tw/twrail/mobile//TimeTableSearchResult.aspx"
      url += "?searchtype=1"
      url += "&searchdate=\(dateString)"
      url += "&trainclass=2"
      url += "&fromtime=\(fromTime)"
      url += "&totime=\(Int(fromTime)! >= 2100 ? String(Int(toTime)! + 2400) : toTime)"
      url += "&searchtext=\(fromStation.name.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "")"<commit_msg>修復搜尋“花壇新烏日”會回傳 花壇 >> 烏日的bug<commit_after>//
//  TRARoute.swift
//  little-daemonPackageDescription
//
//  Created by 江承諭 on 2017/10/18.
//

import Foundation
import LineBot

class TRARoute {
  static func queryTRARoute(message: String, lineBot: LineBot) throws {
    let message = message.replacingOccurrences(of: "台", with: "臺")

    // 如果一開始的字沒有包含車站名稱就跳離
    guard let fromStation = try TRAStation.makeQuery()
      .filter(raw: "$1 LIKE name || '%'", [message])
      .sort(raw: "length(name) DESC")
      .first() else {
        return
    }

    let firstIndex = message.index(message.startIndex, offsetBy: fromStation.name.characters.count)
    let truncated = message.substring(from: firstIndex)

    // 去掉開頭車站名後如果沒有車站名稱就跳離
    guard let toStation = try TRAStation.makeQuery()
      .filter(raw: "$1 LIKE '%' || name || '%'", [truncated])
      .sort(raw: "length(name) DESC")
      .first() else {
        return
    }

    try twtrafficAPI(fromStation: fromStation, toStation: toStation, lineBot: lineBot)
  }

  private static func twtrafficAPI(fromStation: TRAStation,
                                   toStation: TRAStation,
                                   lineBot: LineBot) throws {
    let now = Date()
    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 3600 * 8)
    dateFormatter.dateFormat = "yyyy/MM/dd"
    let dateString = dateFormatter.string(from: now)
    dateFormatter.dateFormat = "HHmm"
    let fromTime = dateFormatter.string(from: now)
    let toTime = dateFormatter.string(from: now.addingTimeInterval(3600 * 3))

    var url = "http://twtraffic.tra.gov.tw/twrail/mobile//TimeTableSearchResult.aspx"
      url += "?searchtype=1"
      url += "&searchdate=\(dateString)"
      url += "&trainclass=2"
      url += "&fromtime=\(fromTime)"
      url += "&totime=\(Int(fromTime)! >= 2100 ? String(Int(toTime)! + 2400) : toTime)"
      url += "&searchtext=\(fromStation.name.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "")"
{'fraction_non_alphanumeric': 0.13306246825799897, 'fraction_numerical': 0.015236160487557136, 'mean_word_length': 3.044147843942505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532651', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1339, 'n_words': 264}