From 9e821ef30bb8f45e70dd75ba857659baedb79091 Mon Sep 17 00:00:00 2001 From: Emile Date: Thu, 5 Sep 2019 20:52:19 +0200 Subject: split up the package into multiple files --- funcs.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 funcs.go (limited to 'funcs.go') diff --git a/funcs.go b/funcs.go new file mode 100644 index 0000000..5ab913c --- /dev/null +++ b/funcs.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + "os" +) + +// 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) + } +} -- cgit 1.4.1