summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-14 14:18:30 +0530
committerAndinus <andinus@inventati.org>2020-03-14 14:18:30 +0530
commitdde2d726d5b65e648b99e45538221014562f1a18 (patch)
tree1db38a43964b205a6dcbb0c160c66213fdffd94e
parent82a53c3c5b5ffd8af9c69c6184134f100f80b89a (diff)
downloadcetus-dde2d726d5b65e648b99e45538221014562f1a18.tar.gz
Add support for random photo as background v0.3.1
-rw-r--r--README.org12
-rw-r--r--cmd/cetus/cetus.go8
-rw-r--r--pkg/unsplash/unsplash.go43
3 files changed, 62 insertions, 1 deletions
diff --git a/README.org b/README.org
index 894c5e9..03891cc 100644
--- a/README.org
+++ b/README.org
@@ -6,7 +6,8 @@ wallpapers.
 *Note*: Cetus is a work in-progress & many features are yet to be implemented.
 
 * Features
-- set a specific photo as wallpaper
+- set a specific photo as background
+- set a random photo as background
 - customize wallpaper size
 * Demo
 I just run some cetus commands on my computer, nothing fancy. I'll make better
@@ -24,3 +25,12 @@ photo-id flag can be used to set a specific photo as background.
 cetus -width 1920 -height 1080 \
       -photo-id=FzrjgIId6NU
 #+END_SRC
+** random
+random flag can be used to set a random photo as background, this is the default
+behaviour.
+#+BEGIN_SRC sh
+cetus -width 1920 -height 1080 \
+      -random
+
+cetus -width 1920 -height 1080 # same as above
+#+END_SRC
diff --git a/cmd/cetus/cetus.go b/cmd/cetus/cetus.go
index 35afd01..e6799c8 100644
--- a/cmd/cetus/cetus.go
+++ b/cmd/cetus/cetus.go
@@ -24,6 +24,7 @@ import (
 func main() {
 	var (
 		photoID string
+		random  bool
 
 		width  int
 		height int
@@ -32,6 +33,7 @@ func main() {
 	)
 
 	flag.StringVar(&photoID, "photo-id", "", "Unsplash Photo ID to set as background")
+	flag.BoolVar(&random, "random", true, "Set a random photo as background")
 
 	flag.IntVar(&width, "width", 1920, "Width of the image")
 	flag.IntVar(&height, "height", 1080, "Height of the image")
@@ -43,6 +45,12 @@ func main() {
 		errChk(err)
 		return
 	}
+
+	if random {
+		err = unsplash.SetRandom(width, height)
+		errChk(err)
+		return
+	}
 }
 
 func errChk(err error) {
diff --git a/pkg/unsplash/unsplash.go b/pkg/unsplash/unsplash.go
index a284ebc..7cd408a 100644
--- a/pkg/unsplash/unsplash.go
+++ b/pkg/unsplash/unsplash.go
@@ -16,6 +16,8 @@ package unsplash
 
 import (
 	"fmt"
+	"net/http"
+	"time"
 
 	"framagit.org/andinus/cetus/pkg"
 )
@@ -31,12 +33,53 @@ func SetFromID(photoID string, width int, height int) error {
 	return err
 }
 
+// SetRandom sets a random photo as background
+func SetRandom(width int, height int) error {
+	var path string
+	var err error
+
+	path, err = getPathRandom(width, height)
+	if err != nil {
+		return err
+	}
+	err = background.Set(path)
+	return err
+}
+
 func getPathFromID(photoID string) string {
 	var path string
 	path = fmt.Sprintf("%s/%s", "https://source.unsplash.com", photoID)
 	return path
 }
 
+func getPathRandom(width int, height int) (string, error) {
+	var err error
+	var path string
+	var reqPath string
+
+	client := http.Client{
+		Timeout: time.Second * 64,
+	}
+
+	reqPath = "https://source.unsplash.com"
+	reqPath = appendSizeToPath(reqPath, width, height)
+
+	req, err := http.NewRequest(http.MethodGet, reqPath, nil)
+	if err != nil {
+		return "", err
+	}
+
+	res, err := client.Do(req)
+	if err != nil {
+		return "", err
+	}
+	defer res.Body.Close()
+
+	// Unsplash Source redirects to the photo
+	path = res.Request.URL.String()
+	return path, nil
+}
+
 func appendSizeToPath(path string, width int, height int) string {
 	var size string
 
'>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 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