summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--categories.txt49
-rw-r--r--go.mod8
-rw-r--r--go.sum4
-rw-r--r--server.go126
5 files changed, 189 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 9a3a8d8..efceea2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,5 @@
 # Output of the go coverage tool, specifically when used with LiteIDE
 *.out
 
+# TLEs
+data/
diff --git a/categories.txt b/categories.txt
new file mode 100644
index 0000000..770e480
--- /dev/null
+++ b/categories.txt
@@ -0,0 +1,49 @@
+tations
+visual
+active
+analyst
+2019-006
+1999-025
+iridium-33-debris
+cosmos-2251-debris
+2012-044
+weather
+noaa
+goes
+resource
+sarsat
+dmc
+tdrss
+argos
+planet
+spire
+geo
+intelsat
+ses
+iridium
+iridium-NEXT
+starlink
+orbcomm
+globalstar
+amateur
+x-comm
+other-comm
+satnogs
+gorizont
+raduga
+molniya
+gps-ops
+glo-ops
+galileo
+beidou
+sbas
+nnss
+musson
+science
+geodetic
+engineering
+education
+military
+radar
+cubesat
+other
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..73f7e7e
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,8 @@
+module git.darknebu.la/Satellite/TLE2JSON
+
+go 1.12
+
+require (
+	git.darknebu.la/Satellite/TLE v0.0.0-20190829210835-ce5042e51ea2
+	github.com/gorilla/mux v1.7.3
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..e4cc836
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,4 @@
+git.darknebu.la/Satellite/TLE v0.0.0-20190829210835-ce5042e51ea2 h1:vNbVQb+qEzatcMlSD7nXnE5UepVHsjM2cbg3wik3/9g=
+git.darknebu.la/Satellite/TLE v0.0.0-20190829210835-ce5042e51ea2/go.mod h1:nm5ECKwvODjivHre4SKW/WydMHAxXCC+EG1ckQHHhqs=
+github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
+github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
diff --git a/server.go b/server.go
new file mode 100644
index 0000000..5524b0a
--- /dev/null
+++ b/server.go
@@ -0,0 +1,126 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"strings"
+
+	"net/http"
+
+	TLE "git.darknebu.la/Satellite/TLE"
+	"github.com/gorilla/mux"
+)
+
+var (
+	categories []string
+)
+
+func main() {
+	r := mux.NewRouter()
+
+	r.HandleFunc("/", indexHandler)
+	r.HandleFunc("/dumpall", dumpallHandler)
+	r.HandleFunc("/{station}/{name}", getHandler)
+
+	log.Fatal(http.ListenAndServe(":8080", r))
+}
+
+func indexHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "%s", "<h1>Hello World</h1>")
+}
+
+// dumpallHandler dumps all categories
+func dumpallHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("[ ] Dumping all TLEs")
+
+	// 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", "could not read categories.txt file!")
+	}
+	lines := strings.Split(string(content), "\n")
+
+	for _, line := range lines {
+		categories = append(categories, line)
+	}
+
+	// download all the categories to the data/<category name> directory
+	for _, category := range categories {
+		downloadCategory(category)
+	}
+
+	log.Println("[+] Done dumping all TLEs")
+}
+
+// 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)
+	}
+}
+
+// get Handler returns the specified TLE
+func getHandler(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
+
+	// read the TLEs from the categories file
+	//content, err := ioutil.ReadFile("categories.txt")
+	//if err != nil {
+	//	log.Println("Could note read categories.txt")
+	//}
+
+	// handle the response according to the query parameter "format"
+	// if ?format=json is given, return
+	if r.URL.Query().Get("format") == "json" {
+		w.Header().Set("Content-Type", "application/json")
+
+		RawTLE := `ISS (ZARYA)             
+1 25544U 98067A   19229.39083552  .00000228  00000-0  11917-4 0  9993
+2 25544  51.6447  57.6210 0007373 294.0868 138.8050 15.50381554184754`
+
+		JsonTLE, err := TLE.NewTLE(RawTLE)
+		if err != nil {
+			fmt.Println(err)
+		}
+
+		// convert the TLE to json
+		b, err := json.MarshalIndent(JsonTLE, "", " ")
+		if err != nil {
+			fmt.Println("error: ", err)
+		}
+		fmt.Fprintf(w, string(b))
+		//fmt.Fprintf(w, "{\"response\": \"Getting the TLE for %s/%s as json\"}", vars["station"], vars["name"])
+	} else {
+		fmt.Fprintf(w, "Getting the TLE for %s/%s raw", vars["station"], vars["name"])
+	}
+
+}