<commit_before><?php

/*
* The MIT License (MIT)
* 
* Copyright (c) 2014 <NAME>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* 
*/

include "credentials.php";

class OCTAPI {

    var $dataset;
    var $nextTrips;
    var $busRoute;

    function curl($url, $fields, $fields_string) {
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, $fields);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);

        curl_close($ch);

        return $result;
    }

    function encodeFieldsToUrl($fields) {

        $fields_string = "";

        //url-ify the data for the POST
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');
        return $fields_string;
    }

    function queryOcTranspo($url, $appkey, $appid, $stopno, $routeno) {
        $fields = array(
            'appID' => urlencode($appid),
            'apiKey' => urlencode($appkey),
            'stopNo' => urlencode($stopno),
            'routeNo' => urlencode($routeno)
        );
        $fields_string = $this->encodeFieldsToUrl($fields);

        $result = $this->curl($url, count($fields), $fields_string);
        $this->pullSoapData($result);
        return $result;
    }

    function pullSoapData($soap) {

        $this->dataset = simplexml_load_string($soap);
        //$this->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        $this->dataset->registerXPathNamespace('soap', 'envelope.xml');
        $this->dataset = $this->dataset->xpath('/soap:Envelope/soap:Body');
        $this->dataset = $this->dataset[0];

        if ($this->dataset->GetNextTripsForStopResponse->getName() != "") {
            $this->nextTrips = $this->dataset->GetNextTripsForStopResponse->GetNextTripsForStopResult;

            $this->busRoute = $this->nextTrips->Route->RouteDirection;


            return $this->dataset;
        }
        else if ($this->dataset->GetRouteSummaryForStopResponse->getName() != "") {
            $this->nextTrips = $this->dataset->GetRouteSummaryForStopResponse->GetRouteSummaryForStopResult;

            $this->busRoute = $this->nextTrips->Routes->Route;

            //print_r($this->dataset);
            //exit();

            return $this->dataset;        <commit_msg>Added exception for the dreaded bad data from OC Transpo.<commit_after><?php

/*
* The MIT License (MIT)
* 
* Copyright (c) 2014 <NAME>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* 
*/

include "credentials.php";

class OCTAPI {

    var $dataset;
    var $nextTrips;
    var $busRoute;

    function curl($url, $fields, $fields_string) {
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, $fields);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);

        curl_close($ch);

        return $result;
    }

    function encodeFieldsToUrl($fields) {

        $fields_string = "";

        //url-ify the data for the POST
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');
        return $fields_string;
    }

    function queryOcTranspo($url, $appkey, $appid, $stopno, $routeno) {
        $fields = array(
            'appID' => urlencode($appid),
            'apiKey' => urlencode($appkey),
            'stopNo' => urlencode($stopno),
            'routeNo' => urlencode($routeno)
        );
        $fields_string = $this->encodeFieldsToUrl($fields);

        $result = $this->curl($url, count($fields), $fields_string);
        $this->pullSoapData($result);
        return $result;
    }

    function pullSoapData($soap) {

        try{
            $this->dataset = simplexml_load_string($soap);
            //$this->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            $this->dataset->registerXPathNamespace('soap', 'envelope.xml');
            $this->dataset = $this->dataset->xpath('/soap:Envelope/soap:Body');
        }
        catch(Exception $e){
            echo 'Caught exception: ',  $e->getMessage(), "\n";
            ob_end_clean();
            exit(0);
        }
        $this->dataset = $this->dataset[0];

        if ($this->dataset->GetNextTripsForStopResponse->getName() != "") {
            $this->nextTrips = $this->dataset->GetNextTripsForStopResponse->GetNextTripsForStopResult;

            $this->busRoute = $this->nextTrips->Route->RouteDirection;


            return $this->dataset;
        }
        else if ($this->dataset->GetRouteSummaryForStopResponse->getName() != "") {
            $this->nextTrips = $this->dataset->GetRouteSummaryForStopResponse->GetRouteSummaryForStopResult;

            $this->busRoute = $this->nextTrips->Routes->Route;

            //print_r($this->dataset);
            //exit();

            return $this->dataset;        
{'fraction_non_alphanumeric': 0.12570023227216834, 'fraction_numerical': 0.0015029375597759257, 'mean_word_length': 2.826450601150026, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7110427', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2040, 'n_words': 601}