about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--go.mod8
-rw-r--r--go.sum6
-rw-r--r--http.go42
4 files changed, 51 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
index efceea2..c997945 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,4 +13,4 @@
 *.out
 
 # TLEs
-data/
+data/*
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..2f86a43
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,8 @@
+module git.darknebu.la/Satellite/tle2json
+
+go 1.13
+
+require (
+	git.darknebu.la/Satellite/tle v0.0.0-20190914205455-d4635457825b
+	github.com/gorilla/mux v1.7.3
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..0908764
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,6 @@
+git.darknebu.la/Satellite/tle v0.0.0-20190911184504-c329b3df3b08 h1:xfD/71DqBE2YtiXJLk4RGp/Sa7H2TxFQxoHM+T/+6ns=
+git.darknebu.la/Satellite/tle v0.0.0-20190911184504-c329b3df3b08/go.mod h1:B0DDMD4PQ/hFeNj9RCdFY8mXVqyzjyKdKeqdgX5jLO8=
+git.darknebu.la/Satellite/tle v0.0.0-20190914205455-d4635457825b h1:gTVUUG344JSI5RE9oA7MQeP0tXvUra8EG0Z9QzmQzyY=
+git.darknebu.la/Satellite/tle v0.0.0-20190914205455-d4635457825b/go.mod h1:B0DDMD4PQ/hFeNj9RCdFY8mXVqyzjyKdKeqdgX5jLO8=
+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/http.go b/http.go
index 7d551ae..84dc503 100644
--- a/http.go
+++ b/http.go
@@ -25,17 +25,22 @@ func dumpallHandler(w http.ResponseWriter, r *http.Request) {
 	// the result to the categories slice
 	content, err := ioutil.ReadFile("categories.txt")
 	if err != nil {
-		log.Printf("%s", "could not read categories.txt file!")
+		log.Printf("%s\n", "could not read categories.txt file!")
 	}
 	lines := strings.Split(string(content), "\n")
 
 	for _, line := range lines {
-		categories = append(categories, line)
+		if line != "" {
+			categories = append(categories, line)
+		}
 	}
 
 	// download all the categories to the data/<category name> directory
 	for _, category := range categories {
+		fmt.Fprintf(w, "downloading %s... ", category)
+		log.Printf("downloading %s... ", category)
 		downloadCategory(category)
+		fmt.Fprintf(w, "done.\n")
 	}
 
 	log.Println("[+] Done dumping all TLEs")
@@ -46,20 +51,45 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
 	vars := mux.Vars(r)
 
 	// read the TLEs from the categories file
+	// TODO: Handle caching
+	//			- dump all if not present
+	//			- if old, fetch from celestrakc
 	//content, err := ioutil.ReadFile("categories.txt")
 	//if err != nil {
 	//	log.Println("Could note read categories.txt")
 	//}
 
+	// open the file containing the data for the individual station
+	categoryPath := fmt.Sprintf("data/%s.txt", vars["station"])
+	content, err := ioutil.ReadFile(categoryPath)
+	if err != nil {
+		log.Printf("Could not open %s", categoryPath)
+	}
+
+	lines := strings.Split(string(content), "\n")
+
+	// iterate over all lines searching for the satellite
+	var j int
+	for i, line := range lines {
+		if strings.Contains(line, vars["name"]) {
+			j = i
+			break
+		}
+	}
+
+	RawTLE := strings.Join(lines[j:j+3], "\n")
+
+	/*
+			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`
+	*/
+
 	// 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)