package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"strings"
)

func main() {
	url := "https://publicsuffix.org/list/public_suffix_list.dat"
	err := downloadAndParseTLDs(url, "tld.json")
	if err != nil {
		fmt.Println("Error:", err)
	}
}

func downloadAndParseTLDs(url, outputFilename string) error {
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	tlds := make(map[string]bool)

	scanner := bufio.NewScanner(resp.Body)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if strings.HasPrefix(line, "//") || line == "" {
			continue
		}
		tlds[line] = true
	}

	if err := scanner.Err(); err != nil {
		return err
	}

	jsonData, err := json.MarshalIndent(tlds, "", "    ")
	if err != nil {
		return err
	}

	outputFile, err := os.Create(outputFilename)
	if err != nil {
		return err
	}
	defer outputFile.Close()

	_, err = outputFile.Write(jsonData)
	if err != nil {
		return err
	}

	fmt.Println("TLDs successfully downloaded and saved to", outputFilename)
	return nil
}
