RaceNotifier/RaceNotifier.go

103 lines
2.4 KiB
Go
Raw Normal View History

2024-08-28 10:56:58 +07:00
package main
import (
2024-08-28 11:16:58 +07:00
"encoding/json"
"fmt"
2024-08-28 11:13:03 +07:00
"log"
2024-08-28 11:16:58 +07:00
"net"
2024-08-28 10:56:58 +07:00
"os"
)
type Race struct {
Id int `json:"id"`
Hash string `json:"hash"`
Name string `json:"name"`
Category string `json:"category"`
Content_Path string `json:"content_path"`
Root_Path string `json:"root_path"`
Save_Path string `json:"save_path"`
Size string `json:"size"`
Files string `json:"files"`
}
type PreRace struct {
Name string `json:"name"`
Indexer string `json:"indexer"`
}
func main() {
2024-08-28 11:15:14 +07:00
file, err := os.OpenFile("/opt/RaceNotifier/racer.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
2024-08-28 11:13:03 +07:00
if err != nil {
log.Fatal(err)
}
2024-08-28 11:10:06 +07:00
2024-08-28 11:13:03 +07:00
log.SetOutput(file)
defer file.Close()
2024-08-28 11:10:06 +07:00
2024-08-28 11:13:03 +07:00
log.Println("Starting Program")
2024-08-28 11:16:58 +07:00
if os.Args[1] == "PreRace" || os.Args[1] == "Prerace" || os.Args[1] == "prerace" {
// This is a pre race, need to find all the details of the release
log.Printf("[PreRace] Prerace launched with details %v, %v, %v", os.Args[1], os.Args[2], os.Args[3])
r := PreRace{Name: os.Args[2], Indexer: os.Args[3]}
tcpAddr, err := net.ResolveTCPAddr("tcp", "212.7.203.107:3000")
if err != nil {
log.Fatal("Could not parse IP address:", err)
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Fatal("Could not connect:", err)
}
defer conn.Close()
_, err = conn.Write([]byte(r.Indexer + " " + r.Name))
if err != nil {
log.Fatal("Could not send message: ", err)
}
log.Println("Message sent...")
os.Exit(0)
} else if os.Args[1] == "test" {
for n, args := range os.Args {
log.Println("Arg", n, "->", args)
}
} else {
// This is the race thus the torrent has finished downloading.
r := Race{Name: os.Args[1], Category: os.Args[2], Content_Path: os.Args[3], Root_Path: os.Args[4], Save_Path: os.Args[5], Hash: os.Args[6], Size: os.Args[7], Files: os.Args[8]}
data, err := json.Marshal(r)
if err != nil {
fmt.Println(err)
}
err = os.WriteFile("test.json", data, 0644)
if err != nil {
log.Fatal(err)
}
tcpAddr, err := net.ResolveTCPAddr("tcp", "212.7.203.107:3000")
if err != nil {
log.Fatal("Could not parse IP address:", err)
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Fatal("Could not connect:", err)
}
defer conn.Close()
_, err = conn.Write([]byte(data))
if err != nil {
log.Fatal("Could not send message: ", err)
}
os.Exit(0)
}
2024-08-28 10:56:58 +07:00
}