about summary refs log tree commit diff
path: root/funcs.go
diff options
context:
space:
mode:
Diffstat (limited to 'funcs.go')
-rw-r--r--funcs.go76
1 files changed, 54 insertions, 22 deletions
diff --git a/funcs.go b/funcs.go
index afd2d06..256fcda 100644
--- a/funcs.go
+++ b/funcs.go
@@ -7,37 +7,69 @@ import (
 	"net/http"
 	"os"
 	"strings"
+	"time"
 )
 
 // 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)
+	timeNow := time.Now().Unix()
 
-	// 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()
+	// path where the category is stored
+	categoryPath := fmt.Sprintf("data/%s.txt", categoryName)
 
-	// 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)
-	}
+	// conditions used to define if a new category should be downloaded or not
+	var nonexistant bool
+	var tooOld = false
 
-	// 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)
+	// find out if the category all ready exists
+	if _, err := os.Stat(categoryPath); os.IsNotExist(err) {
+		nonexistant = true
+	} else {
+
+		// find out how old it is
+		categoryFileStat, _ := os.Stat(categoryPath)
+		categoryFileModTime := categoryFileStat.ModTime()
+		categoryFileModTimeUnix := categoryFileModTime.Unix()
+		if int(timeNow-categoryFileModTimeUnix) > categoryAgeThreshold {
+			tooOld = true
+		}
 	}
-	_, 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)
+
+	// if the local cache is too old or nonexistant, redownload
+	if tooOld || nonexistant {
+
+		log.Println("[c] cache to old, redownloading...")
+
+		// 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)
+		}
+
+		log.Println("[c] Done.")
 	}
 }