about summary refs log tree commit diff
path: root/funcs.go
blob: 256fcda2eb85b5684d74d2e97c780d72c056963a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
	"time"
)

// downloadCategory downloads the given category from celestrack
func downloadCategory(categoryName string) {

	timeNow := time.Now().Unix()

	// path where the category is stored
	categoryPath := fmt.Sprintf("data/%s.txt", categoryName)

	// conditions used to define if a new category should be downloaded or not
	var nonexistant bool
	var tooOld = false

	// 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
		}
	}

	// 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.")
	}
}

// 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/<category name> directory
	for _, category := range categories {
		log.Printf("downloading %s... ", category)
		downloadCategory(category)
	}
}