<commit_before>package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"
)

// Settings struct for config.json
type ConfigJSON struct {
	TVlogfile   string  `json:"TVlogfile"`
	ConnLogLine float64 `json:"LastConnLogLine"`
	LastRun     string  `json:"LastRun"`
}

// Function parses json file into Struct variables
func readConfig(settingFile string) ConfigJSON {
	raw, err := ioutil.ReadFile(settingFile)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	var s ConfigJSON
	json.Unmarshal(raw, &s)
	return s
}

// Function saves json file into Struct variables
func (s *ConfigJSON) saveConfig(settingFile string) error {
	// let's write it back
	out, err := json.Marshal(s)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(settingFile, out, 0600)
	if err != nil {
		return err
	}
	return nil
}

func verifyNewAlarm(s ConfigJSON, linematch float64) {
	// check if new alert is needed or not
	if s.ConnLogLine != linematch {
		fmt.Println("Launch alarm ", s.ConnLogLine, " & linematch", linematch)
		//only alarm is sent, update the struct to avoid double alarm
		s.ConnLogLine = linematch
	}
}

func main() {
	//s := readConfig("config.json")
	s := readConfig("test.json")
	fmt.Println("The output was... ", s)
	fmt.Printf("%T", s)

	fmt.Println("The path is... ", s.TVlogfile)
	file, err := os.Open("TeamViewer12_Logfile.log")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	// fun search
	var linematch float64 = 0
	var line float64 = 1
	// scanner goes line by line. add counter
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		// new connection check string
		if strings.Contains(scanner.Text(), "firefox.exe") {
			linematch = line
			verifyNewAlarm(s, linematch)
		}
		line++
	}
	fmt.Println("Found matching last line...", linematch)

	// update the time playtime
	s.LastRun = (time.Now()).String()
	s.saveConfig("test.json")
}<commit_msg>Properly loops, sends message only if new entries are found.<commit_after>package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"github.com/gregdel/pushover"
)

// Settings struct for config.json
type ConfigJSON struct {
	TVlogfile   string  `json:"TVlogfile"`
	ConnLogLine float64 `json:"LastConnLogLine"`
	DiscLogLine float64 `json:"LastDiscLogLine"`
	LastRun     string  `json:"LastRun"`
}

// Function parses json file into Struct variables
func readConfig(settingFile string) ConfigJSON {
	raw, err := ioutil.ReadFile(settingFile)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	var s ConfigJSON
	json.Unmarshal(raw, &s)
	return s
}

// Function saves json file into Struct variables
func (s *ConfigJSON) saveConfig(settingFile string) error {
	// let's write it back
	out, err := json.Marshal(s)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(settingFile, out, 0600)
	if err != nil {
		return err
	}
	return nil
}

func verifyNewAlarm(s ConfigJSON, linematch float64, kind string) ConfigJSON {

	switch kind {
	case "connect":
		if s.ConnLogLine < linematch {
			fmt.Println("Launch alarm ", s.ConnLogLine, " & linematch", linematch)
			pushoverNotification("New connection", "CONNECT title")
			//only alarm is sent, update the struct to avoid double alarm
			s.ConnLogLine = linematch
		}
	case "disconnect":
		if s.DiscLogLine < linematch {
			fmt.Println("Launch alarm ", s.DiscLogLine, " & linematch", linematch)
			pushoverNotification("New connection", "CONNECT title")
			//only alarm is sent, update the struct to avoid double alarm
			s.DiscLogLine = linematch
		}
	}
	return s
}

func pushoverNotification(messageString string, title string) {
	// Create a new pushover app with a token
	app := pushover.New("az6p3exoje7usacvfgov2i2o84ba37")

	// Create a new recipient
	recipient := pushover.NewRecipient("u8Uc4AH3Z3ixWCMrSZUSenrSHmj3Fp")

	// Create the message to send
	message := &pushover.Message{
		Message:   messageString,
		Title:     title,
		Priority:  pushover.PriorityNormal,
		Timestamp: time.Now().Unix(),
		Sound:     pushover.SoundCosmic,
	}

	// Send the message to the recipient
	response, err := app.SendMessage(message, recipient)
	if err != nil {
		log.Panic(err)
	}

	// Print the response if you want
	log.Println(response)
}

func main() {

	//s := readConfig("config.json")
	s := readConfig("test.json")
	fmt.Println("The output was... ", s)
	fmt.Printf("%T", s)

	fmt.Println("The path is... ", s.TVlogfile)
	file, err := os.Open("TeamViewer12_Logfile.log")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	// fun search

	var newConnLineMatch float64 = 0
	var newDiscLineMatch float64 = 0
	var line float64 = 1

	// scanner goes line by line. add counter
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		// new connection check string
		if strings.Contains(scanner.Text(), "Starting desktop process for") {
			newConnLineMatch = line
			s = verifyNewAlarm(s, newConnLineMatch, "connect")
		}
		// end session search string = "EndSession(): Session to"
		if strings.Contains(scanner.Text(), "EndSession(): Session to") {
			newDiscLineMatch = line
			s = verifyNewAlarm(s, newDiscLineMatch, "disconnect")
		}
		line++
	}

	// update the time playtime
	s.LastRun = (time.Now()).String()
	fmt.Println(s)
	s.saveConfig("test.json")
}
{'fraction_non_alphanumeric': 0.12788061956932376, 'fraction_numerical': 0.010011333585190782, 'mean_word_length': 3.8136363636363635, 'pattern_counts': {'":': 2, '<': 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': '5019035', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1852, 'n_words': 513}