summary refs log tree commit diff stats
path: root/pkg
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-14 19:10:01 +0530
committerAndinus <andinus@inventati.org>2020-03-14 19:10:01 +0530
commit50871fcd3f7698b5d3b68518aea12d31533c5c20 (patch)
treec547b1932a3422e5372d4b4f422d4628000b6cbb /pkg
parenta7e66bc241524caf1accf02966c76f436fa19210 (diff)
downloadcetus-50871fcd3f7698b5d3b68518aea12d31533c5c20.tar.gz
Add cetus-nasa program v0.4.0
cetus-nasa uses NASA's API to get Astronomy Picture of the Day.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/nasa/apod.go109
1 files changed, 109 insertions, 0 deletions
diff --git a/pkg/nasa/apod.go b/pkg/nasa/apod.go
new file mode 100644
index 0000000..62eea96
--- /dev/null
+++ b/pkg/nasa/apod.go
@@ -0,0 +1,109 @@
+// Copyright (c) 2020, Andinus <andinus@inventati.org>
+
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+package nasa
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"math/rand"
+	"net/http"
+	"regexp"
+	"time"
+)
+
+// APOD holds responses
+type APOD struct {
+	Copyright      string `json:"copyright"`
+	Date           string `json:"date"`
+	Explanation    string `json:"explanation"`
+	HDURL          string `json:"hdurl"`
+	MediaType      string `json:"media_type"`
+	ServiceVersion string `json:"service_version"`
+	Title          string `json:"title"`
+	URL            string `json:"url"`
+
+	Code int    `json:"code"`
+	Msg  string `json:"msg"`
+}
+
+// RandDate returns a random date between 1995-06-16 & today
+func RandDate() string {
+	var (
+		min   int64
+		max   int64
+		sec   int64
+		delta int64
+		date  string
+	)
+	min = time.Date(1995, 6, 16, 0, 0, 0, 0, time.UTC).Unix()
+	max = time.Now().UTC().Unix()
+	delta = max - min
+
+	sec = rand.Int63n(delta) + min
+	date = time.Unix(sec, 0).Format("2006-01-02")
+
+	return date
+}
+
+// APODPath returns Astronomy Picture of the Day path
+func APODPath(apodInfo map[string]string, timeout time.Duration) (APOD, error) {
+	var err error
+	apodRes := APOD{}
+
+	// validate date
+	re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])")
+	if !re.MatchString(apodInfo["date"]) {
+		return apodRes, fmt.Errorf("%s does not match format 'YYYY-MM-DD'", apodInfo["date"])
+	}
+
+	client := http.Client{
+		Timeout: time.Second * timeout,
+	}
+
+	req, err := http.NewRequest(http.MethodGet, apodInfo["api"], nil)
+	if err != nil {
+		return apodRes, err
+	}
+	q := req.URL.Query()
+	q.Add("api_key", apodInfo["apiKey"])
+	q.Add("date", apodInfo["date"])
+	req.URL.RawQuery = q.Encode()
+
+	res, err := client.Do(req)
+
+	if err != nil {
+		fmt.Printf("Error: GET %s\n", apodInfo["api"])
+		return apodRes, err
+	}
+	defer res.Body.Close()
+
+	resBody, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return apodRes, err
+	}
+
+	err = json.Unmarshal([]byte(resBody), &apodRes)
+	if err != nil {
+		return apodRes, err
+	}
+
+	if res.StatusCode != 200 {
+		return apodRes, fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode, http.StatusText(res.StatusCode))
+	}
+
+	return apodRes, err
+}
ref='#n285'>285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554