From 2f15edf548ec131d2eb97bd5338a1adef768acea Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 18 Mar 2020 18:00:10 +0530 Subject: Restructure for Cetus v0.5 This version aims for a cleaner code, better documentation & to adopt better practices. --- README.org | 149 +++----------------------------- build/ci/gitlab-ci.yml | 36 +++----- cmd/cetus-bing/cetus-bing.go | 127 --------------------------- cmd/cetus-nasa/cetus-nasa.go | 133 ----------------------------- cmd/cetus-wallhaven/cetus-wallhaven.go | 152 --------------------------------- pkg/cetus/cetus.go | 15 +--- pkg/wallhaven/wallhaven.go | 20 ----- 7 files changed, 25 insertions(+), 607 deletions(-) delete mode 100644 cmd/cetus-bing/cetus-bing.go delete mode 100644 cmd/cetus-nasa/cetus-nasa.go delete mode 100644 cmd/cetus-wallhaven/cetus-wallhaven.go delete mode 100644 pkg/wallhaven/wallhaven.go diff --git a/README.org b/README.org index f559a97..6f2bbcc 100644 --- a/README.org +++ b/README.org @@ -11,154 +11,27 @@ sources for fetching the background. | GitHub Mirror | [[https://github.com/andinus/cetus][Cetus - GitHub]] | *Note*: Cetus is a work in-progress & many features are yet to be implemented. +This whole project was changed multiple times, previous versions have had +support for services like Unsplash Source & Wallhaven. I'm working on this +because I want my desktop to have a new wallpaper everyday. -*Dependency*: [[https://feh.finalrewind.org/][feh]] +Cetus v0.5 will support NASA Astronomy Picture of the Day & Bing Photo of the +Day, I may add support for other services later. Both sources are very easy to +work with, I personally like NASA APOD & fallback to BPOD only when it's not an +image (somtimes they choose a video for APOD). -* Bing Photo of the Day -cetus-bing fetches Bing Photo of the Day. +Currently the only dependency is [[https://feh.finalrewind.org/][feh]], it is used to set the background. It +doesn't support some desktop environments but should work fine many others. I +plan to add support for unsupported DEs too provided they're not hard to +implement. -** Features -- set BPOD as background -- fetch information on BPOD -- choose random photo -** Examples -#+BEGIN_SRC sh -# set current BPOD as background -cetus-bing - -# set photo randomly -cetus-bing -random - -# change api endpoint -cetus-bing -api https://www.bing.com/HPImageArchive.aspx - -# don't set background, just fetch information -cetus-bing -fetch-only - -# don't set background, just fetch & print only the path (useful in -# scripts) -cetus-bing -fetch-only -path-only - -# don't output anything -cetus-bing -quiet - -# dump received response -cetus-bing -dump - -# don't set background, just fetch & don't output anything -cetus-bing -quiet -fetch-only # why would anyone do this? - -#+END_SRC -* NASA Astronomy Picture of the Day -cetus-nasa uses NASA's API to get the Astronomy Picture of the Day. - -*Note*: If the returned media type is not image then cetus-nasa will only print -details & not try to set background. - -** Features -- set APOD as background -- fetch information on APOD -- choose custom date -- choose date randomly -** Examples -#+BEGIN_SRC sh -# set currently APOD as background -cetus-nasa - -# set date randomly -cetus-nasa -random - -# set 2020-01-05 APOD as background -cetus-nasa -date 2020-01-05 - -# 2020-03-15 APOD is a video, cetus will only print details -cetus-nasa -date 2020-03-15 - -# change api endpoint & api key -cetus-nasa -api https://api.nasa.gov/planetary/apod \ - -api-key DEMO_KEY - -# don't set background, just fetch information -cetus-nasa -fetch-only - -# don't set background, just fetch & print only the path (useful in -# scripts) -cetus-nasa -fetch-only -path-only - -# don't output anything -cetus-nasa -quiet - -# dump received response -cetus-nasa -dump - -# don't set background, just fetch & don't output anything -cetus-nasa -quiet -fetch-only # why would anyone do this? -#+END_SRC -* Wallhaven -cetus-wallhaven uses Wallhaven's API to get random background. - -** Features -- set random image as background -- fetch information -** Examples -#+BEGIN_SRC sh -# set a random image as background -cetus-wallhaven - -# same as above -cetus-wallhaven -random - -# change api endpoint & api key -cetus-wallhaven -api https://wallhaven.cc/api/v1/search \ - -api-key - -# don't set background, just fetch information -cetus-wallhaven -fetch-only - -# don't set background, just fetch & print only the path (useful in -# scripts) -cetus-wallhaven -fetch-only -path-only - -# don't output anything -cetus-wallhaven -quiet - -# dump received response -cetus-wallhaven -dump - -# don't set background, just fetch & don't output anything -cetus-wallhaven -quiet -fetch-only # why would anyone do this? -#+END_SRC * Demo I just run some cetus commands on my computer, nothing fancy. I'll make better demo videos someday. -*Note*: Cetus was restructured multiple times & these demos may not work on the -latest release. - | Version | Video | |---------+----------------------------------------------------------------------| | v0.4.6 | https://diode.zone/videos/watch/965bb7a4-029a-4584-a40d-241959d1aced | | v0.4.1 | https://diode.zone/videos/watch/a52cc728-93e5-4bba-9375-752e1dba306d | | v0.3.1 | https://diode.zone/videos/watch/0808c512-315a-4dab-9526-4a537e8c3257 | | v0.2.0 | https://diode.zone/videos/watch/12db31e1-3517-4888-ad06-55f3859447a1 | -* Installation -** Binary -framagit.org compiles cetus for OpenBSD & GNU/Linux amd64 on every release, to -get the binary goto [[https://framagit.org/andinus/cetus/pipelines?scope=tags&page=1][Pipelines - tags]]. - -To get the latest binary goto [[https://framagit.org/andinus/cetus/pipelines][Pipelines]]. - -** From Source -#+BEGIN_SRC sh -# get master branch archive -curl -o cetus-master.tar.gz \ - https://framagit.org/andinus/cetus/-/archive/master/cetus-master.tar.gz - -# extract the archive -tar -xzf cetus-master.tar.gz - -# install cetus -cd cetus-master && \ - go install ./cmd/cetus-nasa && \ - go install ./cmd/cetus-bing -#+END_SRC diff --git a/build/ci/gitlab-ci.yml b/build/ci/gitlab-ci.yml index 93a8327..967029c 100644 --- a/build/ci/gitlab-ci.yml +++ b/build/ci/gitlab-ci.yml @@ -14,28 +14,18 @@ stages: compile: stage: build - only: - refs: - - web - - tags - - master - - schedules - - merge_requests + # only: + # refs: + # - web + # - tags + # - master + # - schedules + # - merge_requests script: - - cd $GOPATH/src/$REPO_NAME/cmd/cetus-nasa - - GOOS=openbsd GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-nasa-openbsd-amd64 - - GOOS=linux GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-nasa-linux-amd64 - - cd $GOPATH/src/$REPO_NAME/cmd/cetus-bing - - GOOS=openbsd GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-bing-openbsd-amd64 - - GOOS=linux GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-bing-linux-amd64 - - cd $GOPATH/src/$REPO_NAME/cmd/cetus-wallhaven - - GOOS=openbsd GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-wallhaven-openbsd-amd64 - - GOOS=linux GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-wallhaven-linux-amd64 - artifacts: + - cd $GOPATH/src/$REPO_NAME/cmd/cetus + - GOOS=openbsd GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-openbsd-amd64 + - GOOS=linux GOARCH=amd64 go build -o $CI_PROJECT_DIR/cetus-linux-amd64 + artifacts: paths: - - cetus-nasa-openbsd-amd64 - - cetus-nasa-linux-amd64 - - cetus-bing-openbsd-amd64 - - cetus-bing-linux-amd64 - - cetus-wallhaven-openbsd-amd64 - - cetus-wallhaven-linux-amd64 + - cetus-openbsd-amd64 + - cetus-linux-amd64 diff --git a/cmd/cetus-bing/cetus-bing.go b/cmd/cetus-bing/cetus-bing.go deleted file mode 100644 index 29ef6f4..0000000 --- a/cmd/cetus-bing/cetus-bing.go +++ /dev/null @@ -1,127 +0,0 @@ -package main - -import ( - "encoding/json" - "flag" - "fmt" - "math/rand" - "time" - - "framagit.org/andinus/cetus/pkg/background" - "framagit.org/andinus/cetus/pkg/bing" - "framagit.org/andinus/cetus/pkg/cetus" -) - -type photo struct { - StartDate string `json:"startdate"` - FullStartDate string `json:"fullstartdate"` - EndDate string `json:"enddate"` - Url string `json:"url"` - UrlBase string `json:"urlbase"` - Copyright string `json:"copyright"` - CopyrightLink string `json:"copyrightlink"` - Title string `json:"title"` - Hsh string `json:"hsh"` -} - -type bpod struct { - Photos []photo `json:"images"` -} - -var ( - t time.Duration - api string - dump bool - quiet bool - random bool - version bool - pathOnly bool - fetchOnly bool -) - -func main() { - parseFlags() - - if version { - cetus.Version() - return - } - rand.Seed(time.Now().Unix()) - - // Convert timeout to seconds - t = t * time.Second - - body, err := bpodBody() - if dump { - fmt.Println(body) - return - } - - bpod := bpod{} - err = json.Unmarshal([]byte(body), &bpod) - cetus.ErrChk("body unmarshal failed", err) - - // if random was set then bpodRes holds list of multiple - // responses, choose a random response from the list - var i int = rand.Intn(len(bpod.Photos)) - bpodPhoto := bpod.Photos[i] - - // correct image path - bpodPhoto.Url = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.Url) - - // correct date format - dt, err := time.Parse("20060102", bpodPhoto.StartDate) - cetus.ErrChk("bpodPhoto.startDate parse failed", err) - bpodPhoto.StartDate = dt.Format("2006-01-02") - - printDetails(bpodPhoto) - - // if fetchOnly is true then don't set background - if fetchOnly { - return - } - - err = background.Set(bpodPhoto.Url) - cetus.ErrChk("setting background failed", err) -} - -func parseFlags() { - flag.BoolVar(&quiet, "quiet", false, "No output") - flag.BoolVar(&version, "version", false, "Cetus version") - flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info") - flag.BoolVar(&dump, "dump", false, "Only dump received response") - flag.BoolVar(&random, "random", false, "Choose a random image (from 7 images)") - flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image") - - flag.StringVar(&api, "api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL") - - flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds") - flag.Parse() - -} - -func printDetails(bpodPhoto photo) { - if quiet { - return - } - if pathOnly { - cetus.PrintPath(bpodPhoto.Url) - return - } - fmt.Printf("Title: %s\n\n", bpodPhoto.Title) - fmt.Printf("Copyright: %s\n", bpodPhoto.Copyright) - fmt.Printf("Copyright Link: %s\n", bpodPhoto.CopyrightLink) - fmt.Printf("Date: %s\n\n", bpodPhoto.StartDate) - fmt.Printf("URL: %s\n", bpodPhoto.Url) -} - -func bpodBody() (string, error) { - reqInfo := make(map[string]string) - reqInfo["api"] = api - if random { - reqInfo["random"] = "true" - } - - body, err := bing.GetBpodJson(reqInfo, t) - return body, err -} diff --git a/cmd/cetus-nasa/cetus-nasa.go b/cmd/cetus-nasa/cetus-nasa.go deleted file mode 100644 index d71c41d..0000000 --- a/cmd/cetus-nasa/cetus-nasa.go +++ /dev/null @@ -1,133 +0,0 @@ -package main - -import ( - "encoding/json" - "flag" - "fmt" - "log" - "time" - - "framagit.org/andinus/cetus/pkg/background" - "framagit.org/andinus/cetus/pkg/cetus" - "framagit.org/andinus/cetus/pkg/nasa" -) - -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"` -} - -var ( - t time.Duration - api string - date string - dump bool - quiet bool - random bool - apiKey string - version bool - fetchOnly bool - pathOnly bool -) - -func main() { - parseFlags() - - if version { - cetus.Version() - return - } - - // Convert timeout to seconds - t = t * time.Second - - if random { - date = nasa.RandDate() - } - - body, err := apodBody() - if dump { - fmt.Println(body) - return - } - - apod := apod{} - err = json.Unmarshal([]byte(body), &apod) - cetus.ErrChk("body unmarshal failed", err) - if len(apod.Msg) != 0 { - log.Println("Message: ", apod.Msg) - } - - printDetails(apod) - - // if fetchOnly is true then don't set background - if fetchOnly { - return - } - - // if media type is an image then set background - if apod.MediaType == "image" { - err = background.Set(apod.HdURL) - cetus.ErrChk("setting background failed", err) - } -} - -func parseFlags() { - flag.BoolVar(&quiet, "quiet", false, "No output") - flag.BoolVar(&version, "version", false, "Cetus version") - flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info") - flag.BoolVar(&dump, "dump", false, "Only dump received response") - dateHelp := fmt.Sprintf("Choose a random date between 1995-06-16 & %s", - time.Now().UTC().Format("2006-01-02")) - flag.BoolVar(&random, "random", false, dateHelp) - flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image") - - flag.StringVar(&api, "api", "https://api.nasa.gov/planetary/apod", "APOD API URL") - flag.StringVar(&apiKey, "api-key", "DEMO_KEY", "api.nasa.gov key for expanded usage") - - dateDefault := time.Now().UTC().Format("2006-01-02") - flag.StringVar(&date, "date", dateDefault, "Date of the APOD image to retrieve") - - flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds") - flag.Parse() - -} - -func printDetails(apod apod) { - if quiet { - return - } - if pathOnly { - cetus.PrintPath(apod.HdURL) - return - } - fmt.Printf("Title: %s\n\n", apod.Title) - fmt.Printf("Copyright: %s\n", apod.Copyright) - fmt.Printf("Date: %s\n\n", apod.Date) - fmt.Printf("Media Type: %s\n", apod.MediaType) - if apod.MediaType == "image" { - fmt.Printf("URL: %s\n\n", apod.HdURL) - } else { - fmt.Printf("URL: %s\n\n", apod.Url) - } - fmt.Printf("Explanation: %s\n", apod.Explanation) -} - -func apodBody() (string, error) { - reqInfo := make(map[string]string) - reqInfo["api"] = api - reqInfo["apiKey"] = apiKey - reqInfo["date"] = date - - body, err := nasa.GetApodJson(reqInfo, t) - return body, err -} diff --git a/cmd/cetus-wallhaven/cetus-wallhaven.go b/cmd/cetus-wallhaven/cetus-wallhaven.go deleted file mode 100644 index 8c6c928..0000000 --- a/cmd/cetus-wallhaven/cetus-wallhaven.go +++ /dev/null @@ -1,152 +0,0 @@ -package main - -import ( - "encoding/json" - "flag" - "fmt" - "math/rand" - "time" - - "framagit.org/andinus/cetus/pkg/background" - "framagit.org/andinus/cetus/pkg/cetus" - "framagit.org/andinus/cetus/pkg/wallhaven" -) - -type wh struct { - Photos []photo `json:"data"` - // MetaI []meta `json:"meta"` -} - -// type meta struct { -// CurrentPage int `json:"current_page"` -// LastPage int `json:"last_page"` -// PerPage int `json:"per_page"` -// Total int `json:"total"` -// Query string `json:"query"` -// Seed string `json:"seed"` -// } - -type photo struct { - Id string `json:"id"` - Url string `json:"url"` - ShortUrl string `json:"short_url"` - Views int `json:"views"` - Favorites int `json:"favorites"` - Source string `json:"source"` - Purity string `json:"purity"` - Category string `json:"category"` - DimensionX int `json:"dimension_x"` - DimensionY int `json:"dimension_y"` - Resolution string `json:"resolution"` - Ratio string `json:"ratio"` - FileSize int `json:"file_size"` - FileType string `json:"file_type"` - CreatedAt string `json:"created_at"` - Path string `json:"path"` - Colors []string `json:"colors"` - // Thumbs []thumb `json:"thumbs"` -} - -// type thumb struct { -// Small string `json:"small"` -// Original string `json:"original"` -// Large string `json:"large"` -// } - -var ( - t time.Duration - api string - apiKey string - dump bool - quiet bool - random bool - version bool - pathOnly bool - fetchOnly bool -) - -func main() { - parseFlags() - - if version { - cetus.Version() - return - } - rand.Seed(time.Now().Unix()) - - // Convert timeout to seconds - t = t * time.Second - - body, err := whBody() - if dump { - fmt.Println(body) - return - } - - wh := wh{} - err = json.Unmarshal([]byte(body), &wh) - cetus.ErrChk("body unmarshal failed", err) - - // if random was set then wh holds list of multiple responses, - // choose a random response from the list - i := rand.Intn(len(wh.Photos)) - whPhoto := wh.Photos[i] - - printDetails(whPhoto) - - // if fetchOnly is true then don't set background - if fetchOnly { - return - } - - err = background.Set(whPhoto.Path) - cetus.ErrChk("setting background failed", err) -} - -func parseFlags() { - flag.BoolVar(&quiet, "quiet", false, "No output") - flag.BoolVar(&version, "version", false, "Cetus version") - flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info") - flag.BoolVar(&dump, "dump", false, "Only dump received response") - flag.BoolVar(&random, "random", true, "Choose a random image") - flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image") - - flag.StringVar(&api, "api", "https://wallhaven.cc/api/v1/search", "Wallhaven Search API URL") - flag.StringVar(&apiKey, "api-key", "", "Wallhaven API Key") - - flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds") - flag.Parse() - -} - -func printDetails(whPhoto photo) { - if quiet { - return - } - if pathOnly { - cetus.PrintPath(whPhoto.Path) - return - } - fmt.Printf("Id: %s\n", whPhoto.Id) - fmt.Printf("URL: %s\n", whPhoto.Url) - fmt.Printf("Short URL: %s\n", whPhoto.ShortUrl) - fmt.Printf("Source: %s\n", whPhoto.Source) - fmt.Printf("Date: %s\n\n", whPhoto.CreatedAt) - fmt.Printf("Resolution: %s\n", whPhoto.Resolution) - fmt.Printf("Ratio: %s\n", whPhoto.Ratio) - fmt.Printf("Views: %d\n", whPhoto.Views) - fmt.Printf("Favorites: %d\n", whPhoto.Favorites) - fmt.Printf("File Size: %d KiB\n", whPhoto.FileSize/1024) - fmt.Printf("Category: %s\n", whPhoto.Category) -} - -func whBody() (string, error) { - reqInfo := make(map[string]string) - reqInfo["api"] = api - if random { - reqInfo["random"] = "true" - } - - body, err := wallhaven.GetWhJson(reqInfo, t) - return body, err -} diff --git a/pkg/cetus/cetus.go b/pkg/cetus/cetus.go index c3f86cb..17fd906 100644 --- a/pkg/cetus/cetus.go +++ b/pkg/cetus/cetus.go @@ -5,22 +5,9 @@ import ( "log" ) -var version string = "v0.4.11" +var version string = "v0.5.0" // Version prints cetus version func Version() { fmt.Printf("Cetus %s\n", version) } - -// PrintPath prints the path passed -func PrintPath(path string) { - fmt.Println(path) -} - -// ErrChk logs the context & error -func ErrChk(ctx string, err error) { - if err != nil { - log.Println(ctx) - log.Fatal(err) - } -} diff --git a/pkg/wallhaven/wallhaven.go b/pkg/wallhaven/wallhaven.go deleted file mode 100644 index 25af77c..0000000 --- a/pkg/wallhaven/wallhaven.go +++ /dev/null @@ -1,20 +0,0 @@ -package wallhaven - -import ( - "time" - - "framagit.org/andinus/cetus/pkg/cetus" -) - -// GetWhJson returns json response received from the api -func GetWhJson(reqInfo map[string]string, t time.Duration) (string, error) { - params := make(map[string]string) - params["apikey"] = reqInfo["apiKey"] - if reqInfo["random"] == "true" { - params["sorting"] = "random" - - } - - body, err := cetus.GetRes(reqInfo["api"], params, t) - return string(body), err -} -- cgit 1.4.1-2-gfad0