about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-06-17 16:59:45 +0530
committerAndinus <andinus@nand.sh>2020-06-17 16:59:45 +0530
commit430e6c16eb07531df436a42e34a667477c3e469a (patch)
tree1fe42313e30ee46e5b73964c114f2ac8ef3b945c /lib
parent8dd48e97f4a3824105a1a21cedfef70a486f47a2 (diff)
downloadcrux-430e6c16eb07531df436a42e34a667477c3e469a.tar.gz
Add UnsplashSource.pm
Diffstat (limited to 'lib')
-rw-r--r--lib/UnsplashSource.pm81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/UnsplashSource.pm b/lib/UnsplashSource.pm
new file mode 100644
index 0000000..bce5c59
--- /dev/null
+++ b/lib/UnsplashSource.pm
@@ -0,0 +1,81 @@
+#!/usr/bin/perl
+
+package UnsplashSource;
+
+use strict;
+use warnings;
+
+use HTTP::Tiny;
+use Carp qw( croak carp );
+
+my $api = "https://source.unsplash.com";
+my $http = HTTP::Tiny->new(
+    agent => "crux - UnsplashSource.pm - HTTP::Tiny",
+    max_redirect => 0,
+    verify_SSL => 1,
+    timeout => 60,
+
+);
+
+sub get {
+    my ( %options ) = @_;
+
+    if ( $options{daily} or $options{weekly} ) {
+        return fixed( %options );
+    } elsif ( $options{user} ) {
+        return user_random( %options );
+    } elsif ( ( scalar @{$options{search}} > 0 )
+                  or $options{featured} ) {
+        return random_search( %options );
+    } elsif ( $options{collection_id} ) {
+        return collection( %options );
+    } else {
+        return get_random( %options );
+    }
+}
+
+sub get_random {
+    my ( %options ) = @_;
+    my $url = "$api/random/$options{resolution}";
+    return $http->head($url);
+}
+
+sub random_search {
+    my ( %options ) = @_;
+    my $url = "$api/";
+    $url .= "featured/" if $options{featured};
+    $url .= $options{resolution};
+    $url .= "?";
+    $url .= "$_," foreach ( @{$options{search}});
+    return $http->head($url);
+}
+
+sub user_random {
+    my ( %options ) = @_;
+    my $url = "$api/user/$options{user}/";
+    $url .= "likes/" if $options{user_likes};
+    $url .= $options{resolution};
+    return $http->head($url);
+}
+
+sub collection {
+    my ( %options ) = @_;
+    my $url = "$api/collection/$options{collection_id}/";
+    $url .= $options{resolution};
+    return $http->head($url);
+}
+
+sub fixed {
+    my ( %options ) = @_;
+    croak "Cannot use daily & weekly together"
+        if $options{daily} and $options{weekly};
+    my $url = "$api/";
+    $url .= "user/$options{user}/" if $options{user};
+    $url .= "daily/" if $options{daily};
+    $url .= "weekly/" if $options{weekly};
+    $url .= "?";
+    $url .= "$_," foreach ( @{$options{search}});
+    return $http->head($url);
+}
+
+1;