package main import ( "fmt" "io/ioutil" "log" "net/http" "os" "strings" ) // downloadCategory downloads the given category from celestrack func downloadCategory(categoryName string) { // define where to download the TLEs from base := "https://celestrak.com/NORAD/elements/" downloadURL := fmt.Sprintf("%s%s.txt", base, categoryName) // define the HTTP GET request and make it resp, err := http.Get(downloadURL) if err != nil { log.Printf("Could not download %s", categoryName) } defer resp.Body.Close() // read the respose from the GET request body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Printf("Could not read the category %s", categoryName) } // write the response to the corresponding file filename := fmt.Sprintf("data/%s.txt", categoryName) f, err := os.Create(filename) // creating the file if err != nil { fmt.Printf("Could not create file data/%s.txt", categoryName) } _, err = f.WriteString(string(body)) // writing the TLE to the file if err != nil { fmt.Printf("Could not write to file data/%s.txt", categoryName) } } // dumpall dumps all the categories into the data folder func dumpall() { // read the name of the categories from the categories.txt file and write // the result to the categories slice content, err := ioutil.ReadFile("categories.txt") if err != nil { log.Printf("%s\n", "could not read categories.txt file!") } lines := strings.Split(string(content), "\n") for _, line := range lines { if line != "" { categories = append(categories, line) } } // download all the categories to the data/ directory for _, category := range categories { log.Printf("downloading %s... ", category) downloadCategory(category) } }