<commit_before>package handler

import (
	yoBackHandler "github.com/yoed/yoed-handler-yo-back/handler"
	httpInterface "github.com/yoed/yoed-http-interface"

	"net/http"
	"log"
	"io/ioutil"
	"encoding/json"
	"strings"
	"time"
	"bytes"
	"math"
)

type Handler struct {
	yoBackHandler.Handler
	Config *Config
}

const Fmt = "15:04"
type configTime time.Time
func (ct *configTime) UnmarshalJSON(data []byte) error {
        b := bytes.NewBuffer(data)
        dec := json.NewDecoder(b)
        var s string
        if err := dec.Decode(&s); err != nil {
                return err
        }
        t, err := time.Parse(Fmt, s)
        if err != nil {
                return err
        }
        *ct = (configTime)(t)
        return nil
}

type configDelta time.Duration
func (cd *configDelta) UnmarshalJSON(data []byte) error {
        b := bytes.NewBuffer(data)
        dec := json.NewDecoder(b)
        var s string
        if err := dec.Decode(&s); err != nil {
                return err
        }
        t, err := time.ParseDuration(s)
        if err != nil {
                return err
        }
        *cd = (configDelta)(t)
        return nil
}

type Config struct {
	httpInterface.Config
	FromStation string
	ToStation string
	Hour configTime
	Delta configDelta
}

type TransilienApiResponse struct {
    Data []*struct{
        TrainHour string
    }
}

func (c *Handler) Handle(username string) {
	url := "http://transilien.ods.ocito.com/ods/transilien/iphone"
   	jsonDataIn := `[{"target":"/transilien/getNextTrains","map":{"codeArrivee":"`+c.Config.ToStation+`","codeDepart":"`+c.Config.FromStation+`"},"serial":"4"}]`
   	log.Printf(jsonDataIn)
   	b := strings.NewReader(jsonDataIn)
	resp, err := http.Post(url, "application/json", b)

	log.Print("Requesting Transilien API from station %s to station %s", c.Config.FromStation, c.Config.ToStation)

	if err != nil {
		log.Printf("Transilien API error: %s", err)
		return
	}

	defer resp.Body.Close()

	log.Printf("Transilien API response status: %s", resp.Status)

	if body, err := ioutil.ReadAll(resp.Body); err != nil {
		log.Printf("Transilien API response body error: %s", err)
	} else {
		log.Printf("Transilien API response body: %s", string(body))
		var v []*TransilienApiResponse
        json.Unmarshal(body, &v) 
        log.Printf("Transilien unmarshalled data %v", v)
        if c.trainIsOnTime(v[0]) {
        	c.Handler.Handle(username)
        }
	}
}

func (c *Handler) trainIsOnTime(data *TransilienApiResponse) bool {
	log.Printf("Parse data %v", data)

	hourPlusDelta := time.Time(c.Config.Hour).Add(time.Duration(c.Config.Delta))
	for _, aData := range data.Data {
		log.Printf("Data: %v", aData.TrainHour)
		p, err := time.Parse("02/01/2006 15:04", string(aData.TrainHour))
		if err != nil {
			log.Printf("Error parsing train time: %s", err)
			continue
		}

		trainHourParsed, err := time.Parse(Fmt, p.Format(Fmt))
		if err != nil {
			log.Printf("Error parsing train hour: %s", err)
			continue
		}
		delta := trainHourParsed.Sub(hourPlusDelta)
		if time.Time(c.Config.Hour).Equal(p) || math.Abs(float64(delta)) <= math.Abs(float64(c.Config.Delta)) {
			log.Printf("On time!")
			return true
		}
	}

	return false
}

func New() *Handler {

	c := &Handler{}

	if err := httpInterface.LoadConfig("./config.json", &c.Config); err != nil {
		log.Fatalf("failed loading config: %s", err)
	}

	return c
}<commit_msg>Using interval instead of fixed hour<commit_after>package handler

import (
	yoBackHandler "github.com/yoed/yoed-handler-yo-back/handler"
	httpInterface "github.com/yoed/yoed-http-interface"

	"net/http"
	"log"
	"io/ioutil"
	"encoding/json"
	"strings"
	"time"
	"bytes"
	"math"
)

type Handler struct {
	yoBackHandler.Handler
	Config *Config
}

const Fmt = "15:04"
type configInterval time.Duration
func (ct *configInterval) UnmarshalJSON(data []byte) error {
    b := bytes.NewBuffer(data)
    dec := json.NewDecoder(b)
    var s string
    if err := dec.Decode(&s); err != nil {
            return err
    }
    t, err := time.ParseDuration(s)
    if err != nil {
       return err
    }
    *ct = (configInterval)(t)
    return nil
}

type configDelta time.Duration
func (cd *configDelta) UnmarshalJSON(data []byte) error {
    b := bytes.NewBuffer(data)
    dec := json.NewDecoder(b)
    var s string
    if err := dec.Decode(&s); err != nil {
            return err
    }
    t, err := time.ParseDuration(s)
    if err != nil {
            return err
    }
    *cd = (configDelta)(t)
    return nil
}

type Config struct {
	httpInterface.Config
	FromStation string
	ToStation string
	Interval configInterval
	Delta configDelta
}

type TransilienApiResponse struct {
    Data []*struct{
        TrainHour string
    }
}

func (c *Handler) Handle(username string) {
	url := "http://transilien.ods.ocito.com/ods/transilien/iphone"
   	jsonDataIn := `[{"target":"/transilien/getNextTrains","map":{"codeArrivee":"`+c.Config.ToStation+`","codeDepart":"`+c.Config.FromStation+`"},"serial":"4"}]`
   	b := strings.NewReader(jsonDataIn)
	resp, err := http.Post(url, "application/json", b)

	log.Print("Requesting Transilien API from station %s to station %s", c.Config.FromStation, c.Config.ToStation)

	if err != nil {
		log.Printf("Transilien API error: %s", err)
		return
	}

	defer resp.Body.Close()

	log.Printf("Transilien API response status: %s", resp.Status)

	if body, err := ioutil.ReadAll(resp.Body); err != nil {
		log.Printf("Transilien API response body error: %s", err)
	} else {
		log.Printf("Transilien API response body: %s", string(body))
		var v []*TransilienApiResponse
        json.Unmarshal(body, &v) 
        log.Printf("Transilien unmarshalled data %v", v)
        if c.trainIsOnTime(v[0]) {
        	c.Handler.Handle(username)
        }
	}
}

func (c *Handler) trainIsOnTime(data *TransilienApiResponse) bool {
	log.Printf("Parse data %v", data)

	hourPlusInterval := time.Now().Add(time.Duration(c.Config.Interval))
	hourPlusDelta := time.Now().Add(time.Duration(c.Config.Delta))
	for _, aData := range data.Data {
		log.Printf("Data: %v", aData.TrainHour)
		p, err := time.Parse("02/01/2006 15:04", string(aData.TrainHour))
		if err != nil {
			log.Printf("Error parsing train time: %s", err)
			continue
		}

		trainHourParsed, err := time.Parse(Fmt, p.Format(Fmt))
		if err != nil {
			log.Printf("Error parsing train hour: %s", err)
			continue
		}
		delta := trainHourParsed.Sub(hourPlusDelta)
		if hourPlusInterval.Equal(p) || math.Abs(float64(delta)) <= math.Abs(float64(c.Config.Delta)) {
			log.Printf("On time!")
			return true
		}
	}

	return false
}

func New() *Handler {

	c := &Handler{}

	if err := httpInterface.LoadConfig("./config.json", &c.Config); err != nil {
		log.Fatalf("failed loading config: %s", err)
	}

	return c
}
{'fraction_non_alphanumeric': 0.14961561206386753, 'fraction_numerical': 0.006505026611472502, 'mean_word_length': 3.228125, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2051358', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2371, 'n_words': 545}