summary refs log tree commit diff stats
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go120
1 files changed, 77 insertions, 43 deletions
diff --git a/main.go b/main.go
index 95e91fa..c8ad829 100644
--- a/main.go
+++ b/main.go
@@ -35,6 +35,9 @@ var (
 	bpodAPI     string
 	bpodNum     int
 	unsplashAPI string
+
+	width  int
+	height int
 )
 
 func main() {
@@ -57,10 +60,14 @@ func main() {
 	flag.StringVar(&src, "src", "random", "Source for the image")
 	flag.StringVar(&mode, "mode", "random", "Daily, Weekly or Random wallpaper")
 
+	flag.IntVar(&width, "width", 1920, "Width of the image")
+	flag.IntVar(&height, "height", 1080, "Height of the image")
+
 	flag.StringVar(&apodAPI, "apod-api", "https://api.nasa.gov/planetary/apod", "APOD API URL")
 	flag.StringVar(&apodAPIKey, "apod-api-key", "DEMO_KEY", "APOD API Key")
 	flag.StringVar(&bpodAPI, "bpod-api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL")
 	flag.IntVar(&bpodNum, "bpod-num", 16, "BPOD Number of images to fetch")
+	flag.StringVar(&unsplashAPI, "unsplash-api", "https://source.unsplash.com", "Unsplash Source API URL")
 	flag.DurationVar(&timeout, "timeout", 16, "Timeout for http client")
 	flag.Parse()
 
@@ -112,6 +119,53 @@ func parseSrcAndGetPath(src string, mode string) (string, error) {
 func getPathAPOD(mode string) (string, error) {
 	var err error
 	var imgPath string
+
+	switch mode {
+	case "daily", "random":
+		break
+	default:
+		return "", fmt.Errorf("Error: Unknown Mode")
+	}
+
+	type apodRes struct {
+		Copyright      string `json:"copyright"`
+		Date           string `json:"string"`
+		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"`
+	}
+
+	apodNow := apodRes{}
+
+	req, err := http.NewRequest(http.MethodGet, apodAPI, nil)
+	if err != nil {
+		return "", err
+	}
+	q := req.URL.Query()
+	q.Add("api_key", apodAPIKey)
+	req.URL.RawQuery = q.Encode()
+
+	res, err := getRes(req)
+	if err != nil {
+		fmt.Printf("Error: GET %s\n", apodAPI)
+		return "", err
+	}
+	defer res.Body.Close()
+
+	apiBody, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return "", err
+	}
+
+	err = json.Unmarshal([]byte(apiBody), &apodNow)
+	if err != nil {
+		return "", err
+	}
+
+	imgPath = apodNow.HDURL
 	return imgPath, err
 }
 
@@ -182,65 +236,45 @@ func getPathBPOD(mode string) (string, error) {
 func getPathUnsplash(mode string) (string, error) {
 	var err error
 	var imgPath string
-	return imgPath, err
-}
-
-// Calls feh to set the wallpaper
-func setWall(imgPath string) error {
-	feh, err := exec.LookPath("feh")
-	if err != nil {
-		fmt.Println("Error: feh is not in $PATH")
-		return err
-	}
-
-	fmt.Printf("Path to set as Wallpaper: %s\n", imgPath)
-
-	err = exec.Command(feh, "--bg-fill", imgPath).Run()
-	return err
-}
 
-// Get url of Astronomy Picture of the Day & pass it to setWall()
-func setWallFromAPOD(apodI map[string]string) error {
-	type apodRes struct {
-		Copyright      string `json:"copyright"`
-		Date           string `json:"string"`
-		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"`
+	switch mode {
+	case "daily", "weekly":
+		unsplashAPI = fmt.Sprintf("%s/%s",
+			unsplashAPI, mode)
+	case "random":
+		unsplashAPI = fmt.Sprintf("%s/%sx%s",
+			unsplashAPI, strconv.Itoa(width), strconv.Itoa(height))
+	default:
+		return "", fmt.Errorf("Error: Unknown Mode")
 	}
 
-	apodNow := apodRes{}
-
-	req, err := http.NewRequest(http.MethodGet, apodI["api"], nil)
+	req, err := http.NewRequest(http.MethodGet, unsplashAPI, nil)
 	if err != nil {
-		return err
+		return "", err
 	}
-	q := req.URL.Query()
-	q.Add("api_key", apodI["apiKey"])
-	req.URL.RawQuery = q.Encode()
 
 	res, err := getRes(req)
 	if err != nil {
-		fmt.Printf("Error: GET %s\n", apodI["api"])
-		return err
+		return "", err
 	}
 	defer res.Body.Close()
 
-	apiBody, err := ioutil.ReadAll(res.Body)
-	if err != nil {
-		return err
-	}
+	// Unsplash Source API will redirect to the image
+	imgPath = res.Request.URL.String()
+	return imgPath, err
+}
 
-	err = json.Unmarshal([]byte(apiBody), &apodNow)
+// Calls feh to set the wallpaper
+func setWall(imgPath string) error {
+	feh, err := exec.LookPath("feh")
 	if err != nil {
+		fmt.Println("Error: feh is not in $PATH")
 		return err
 	}
 
-	// Set Astronomy Picture of the Day as wallpaper
-	err = setWall(apodNow.HDURL)
+	fmt.Printf("Path to set as Wallpaper: %s\n", imgPath)
+
+	err = exec.Command(feh, "--bg-fill", imgPath).Run()
 	return err
 }
 
n340'>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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 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