summary refs log tree commit diff stats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cetus/main.go186
1 files changed, 127 insertions, 59 deletions
diff --git a/cmd/cetus/main.go b/cmd/cetus/main.go
index 7d4ebc8..fa53e9a 100644
--- a/cmd/cetus/main.go
+++ b/cmd/cetus/main.go
@@ -3,6 +3,7 @@ package main
 import (
 	"flag"
 	"fmt"
+	"io/ioutil"
 	"math/rand"
 	"os"
 	"time"
@@ -140,12 +141,11 @@ func getCacheDir() string {
 }
 
 func execAPOD() {
-	// reqInfo holds all the parameters that needs to be
-	// sent with the request. GetJson() will pack apiKey &
-	// date in params map before sending it to another
-	// function. Adding params here will not change the
-	// behaviour of the function, changes have to be made
-	// in GetJson() too.
+	// reqInfo holds all the parameters that needs to be sent with
+	// the request. GetJson() will pack apiKey & date in params
+	// map before sending it to another function. Adding params
+	// here will not change the behaviour of the function, changes
+	// have to be made in GetJson() too.
 	reqInfo := make(map[string]string)
 	reqInfo["api"] = string(*apodAPI)
 	reqInfo["apiKey"] = string(*apodKey)
@@ -155,8 +155,28 @@ func execAPOD() {
 		reqInfo["date"] = apod.RandDate()
 	}
 
-	body, err := apod.GetJson(reqInfo)
-	chkErr(err)
+	cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "apod")
+	os.MkdirAll(cacheDir, os.ModePerm)
+
+	// Check if the file is available locally, if it is then don't
+	// download it again and get it from disk
+	var body string
+	file := fmt.Sprintf("%s/%s", cacheDir, reqInfo["date"])
+	if _, err := os.Stat(file); err == nil {
+		data, err := ioutil.ReadFile(file)
+		chkErr(err)
+		body = string(data)
+	} else if os.IsNotExist(err) {
+		body, err = apod.GetJson(reqInfo)
+		chkErr(err)
+
+		// Write body to the cache so that it can be read
+		// later
+		err = ioutil.WriteFile(file, []byte(body), 0644)
+		chkErr(err)
+	} else {
+		chkErr(err)
+	}
 
 	if *apodDump {
 		fmt.Printf(body)
@@ -164,34 +184,32 @@ func execAPOD() {
 	}
 
 	res := apod.Res{}
-	err = apod.UnmarshalJson(&res, body)
+	err := apod.UnmarshalJson(&res, body)
 	chkErr(err)
 
-	// res.Msg will be returned when there is error on
-	// user input or the api server.
+	// res.Msg will be returned when there is error on user input
+	// or the api server.
 	if len(res.Msg) != 0 {
 		fmt.Printf("Message: %s", res.Msg)
 		os.Exit(1)
 	}
 
-	// If path-only is passed then it will only print the
-	// path, even if quiet is passed. If the user wants
-	// the program to be quiet then path-only shouldn't be
-	// passed. If path-only is not passed & quiet is also
-	// not passed then print the response.
+	// If path-only is passed then it will only print the path,
+	// even if quiet is passed. If the user wants the program to
+	// be quiet then path-only shouldn't be passed. If path-only
+	// is not passed & quiet is also not passed then print the
+	// response.
 	//
-	// Path is only printed when the media type is an
-	// image because res.HDURL is empty on non image media
-	// type.
+	// Path is only printed when the media type is an image
+	// because res.HDURL is empty on non image media type.
 	if *apodPathOnly {
 		fmt.Println(res.HDURL)
 	} else if !*apodQuiet {
 		apod.Print(res)
 	}
 
-	// Proceed only if the command was set because if it
-	// was fetch then it's already finished & should exit
-	// now.
+	// Proceed only if the command was set because if it was fetch
+	// then it's already finished & should exit now.
 	if os.Args[1] == "fetch" {
 		os.Exit(0)
 	}
@@ -203,30 +221,29 @@ func execAPOD() {
 	if res.MediaType != "image" {
 		os.Exit(0)
 	}
-	cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "apod")
-	os.MkdirAll(cacheDir, os.ModePerm)
-	file := fmt.Sprintf("%s/%s", cacheDir, reqInfo["date"])
+	imgCacheDir := fmt.Sprintf("%s/%s", cacheDir, "background")
+	os.MkdirAll(imgCacheDir, os.ModePerm)
+	imgFile := fmt.Sprintf("%s/%s", imgCacheDir, reqInfo["date"])
 
 	// Check if the file is available locally, if it is
 	// then don't download it again and set it from disk
-	if _, err := os.Stat(file); os.IsNotExist(err) {
-		err = background.Download(file, res.HDURL)
+	if _, err := os.Stat(imgFile); os.IsNotExist(err) {
+		err = background.Download(imgFile, res.HDURL)
 		chkErr(err)
 	} else {
 		chkErr(err)
 	}
 
-	err = background.Set(file)
+	err = background.Set(imgFile)
 	chkErr(err)
 }
 
 func execBPOD() {
-	// reqInfo holds all the parameters that needs to be
-	// sent with the request. GetJson() will pack apiKey &
-	// date in params map before sending it to another
-	// function. Adding params here will not change the
-	// behaviour of the function, changes have to be made
-	// in GetJson() too.
+	// reqInfo holds all the parameters that needs to be sent with
+	// the request. GetJson() will pack apiKey & date in params
+	// map before sending it to another function. Adding params
+	// here will not change the behaviour of the function, changes
+	// have to be made in GetJson() too.
 	reqInfo := make(map[string]string)
 	reqInfo["api"] = string(*bpodAPI)
 
@@ -234,14 +251,54 @@ func execBPOD() {
 		reqInfo["random"] = "true"
 	}
 
-	body, err := bpod.GetJson(reqInfo)
-	chkErr(err)
+	cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "bpod")
+	os.MkdirAll(cacheDir, os.ModePerm)
 
-	if *bpodDump {
-		fmt.Printf(body)
-		os.Exit(0)
+	// Check if the file is available locally, if it is then don't
+	// download it again and get it from disk.
+	//
+	// We don't know the bpod date because that will be there in
+	// response & we can't read the response without requesting
+	// it. So this will assume the bpod date to be today's date if
+	// *bpodRand is not set true. If *bpodRand is set true then we
+	// can't assume the date. Also this way too it can cause error
+	// if our assumed date doesn't matches date at the server.
+	var body string
+	var file string
+	var err error
+
+	if !*bpodRand {
+		// If not *bpodRand and the file exists then read from
+		// disk, if the file doesn't exist then get it and
+		// save it to disk.
+		file = fmt.Sprintf("%s/%s", cacheDir, time.Now().UTC().Format("2006-01-02"))
+		if _, err := os.Stat(file); err == nil {
+			data, err := ioutil.ReadFile(file)
+			chkErr(err)
+			body = string(data)
+		} else if os.IsNotExist(err) {
+			body, err = bpod.GetJson(reqInfo)
+			chkErr(err)
+
+			// Write body to the cache so that it can be
+			// read later
+			err = ioutil.WriteFile(file, []byte(body), 0644)
+			chkErr(err)
+		} else {
+			chkErr(err)
+		}
+	} else {
+		// If *bpodRand then get the file and save it to disk
+		// after unmarshal because we don't know the file name
+		// yet
+		body, err = bpod.GetJson(reqInfo)
+		chkErr(err)
 	}
 
+	// Unmarshal before dump because otherwise if we come across
+	// the date for the first time then it would just dump and
+	// exit without saving it to cache. This way we first save it
+	// to cache if *bpodRand is true.
 	res, err := bpod.UnmarshalJson(body)
 	chkErr(err)
 
@@ -251,24 +308,35 @@ func execBPOD() {
 	chkErr(err)
 	res.StartDate = dt.Format("2006-01-02")
 
-	// If path-only is passed then it will only print the
-	// path, even if quiet is passed. If the user wants
-	// the program to be quiet then path-only shouldn't be
-	// passed. If path-only is not passed & quiet is also
-	// not passed then print the response.
+	file = fmt.Sprintf("%s/%s", cacheDir, res.StartDate)
+	if *bpodRand {
+		// Write body to the cache so that it can be read
+		// later
+		err = ioutil.WriteFile(file, []byte(body), 0644)
+		chkErr(err)
+	}
+
+	if *bpodDump {
+		fmt.Printf(body)
+		os.Exit(0)
+	}
+
+	// If path-only is passed then it will only print the path,
+	// even if quiet is passed. If the user wants the program to
+	// be quiet then path-only shouldn't be passed. If path-only
+	// is not passed & quiet is also not passed then print the
+	// response.
 	//
-	// Path is only printed when the media type is an
-	// image because res.HDURL is empty on non image media
-	// type.
+	// Path is only printed when the media type is an image
+	// because res.HDURL is empty on non image media type.
 	if *bpodPathOnly {
 		fmt.Println(res.Url)
 	} else if !*bpodQuiet {
 		bpod.Print(res)
 	}
 
-	// Proceed only if the command was set because if it
-	// was fetch then it's already finished & should exit
-	// now.
+	// Proceed only if the command was set because if it was fetch
+	// then it's already finished & should exit now.
 	if os.Args[1] == "fetch" {
 		os.Exit(0)
 	}
@@ -277,20 +345,20 @@ func execBPOD() {
 	// First it downloads the image to the cache directory and
 	// then tries to set it with feh. If the download fails then
 	// it exits with a non-zero exit code.
-	cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "bpod")
-	os.MkdirAll(cacheDir, os.ModePerm)
-	file := fmt.Sprintf("%s/%s", cacheDir, res.StartDate)
-
-	// Check if the file is available locally, if it is
-	// then don't download it again and set it from disk
-	if _, err := os.Stat(file); os.IsNotExist(err) {
-		err = background.Download(file, res.Url)
+	imgCacheDir := fmt.Sprintf("%s/%s", cacheDir, "background")
+	os.MkdirAll(imgCacheDir, os.ModePerm)
+	imgFile := fmt.Sprintf("%s/%s", imgCacheDir, res.StartDate)
+
+	// Check if the file is available locally, if it is then don't
+	// download it again and set it from disk
+	if _, err := os.Stat(imgFile); os.IsNotExist(err) {
+		err = background.Download(imgFile, res.Url)
 		chkErr(err)
 	} else {
 		chkErr(err)
 	}
 
-	err = background.Set(file)
+	err = background.Set(imgFile)
 	chkErr(err)
 }
 
> 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875