A simple API to access Reddit

I had always wanted to perform some analysis on all the posts that Reddit gets. I searched CPAN for a module that does something similar, and found nothing. So, I decided to write my own. This is still a work in progress, and currently only allows you to fetch and parse the data in XML.

package Reddit;

use strict;
use warnings;
use Carp;

use LWP::Simple;
use XML::Simple;

sub new {
	my $class=shift;
	my $self={
		version=>"2.0",
		format=>"xml"
	};
	bless $self, $class;
	return $self;
}

sub format{
	my $self=shift;
	my $data=shift;
	if (defined $data) {
		$self->{format}=$data if $data eq "xml" or $data eq "json" or $data eq "rss";
	}
	return $self->{format};
}

sub version{
	return $_[0]->{version};
}

sub search{
	my $self=shift;
	my $data=shift;
	croak "REDDIT: Search must be called by an instance only" if not defined ref $self;
	croak "REDDIT: No inputs specified" if not defined $data;
	my $url="http://www.reddit.com/search.".$self->{format}."?q=";
	$url.=$data->{query} if defined $data->{query};
	$url.=' author:'.$data->{author} if defined $data->{author};
	$url.=' over18:'.$data->{over18} if defined $data->{over18};
	$url.=' site:'.$data->{site} if defined $data->{site};
	$url.='&sort='.$data->{orderby} if defined $data->{orderby};
	carp q(REDDIT: over18 must be yes|no) if defined $data->{over18} and $data->{over18} ne "yes" and $data->{over18} ne "no";
	my $contents=get($url);
	if($self->{format} eq "xml") {
		my $xml=XMLin($contents);
		return $xml;
	}else{
		return $contents;
	}
}

sub front_page{
	my $self=shift;
	my $data=shift;
	my $subreddit="";
	$subreddit="r/".$data->{subreddit} if defined $data->{subreddit};
	if($data->{type}){
		$subreddit.="/".$data->{type}."/";
		carp "REDDIT: Type must be new/top/controversial only" 
			if $data->{type} ne "new" and $data->{type} ne "top" and $data->{type} ne "controversial";
	}
	my $url="http://www.reddit.com/".$subreddit.".".$self->{format};
	my $contents=get($url);
	if($self->{format} eq "xml") {
		my $xml=XMLin($contents);
		return $xml;
	}else{
		return $contents;
	}
}

sub get_links_from_xml{
	my $self=shift;
	my $xml=shift;
	croak "REDDIT: Provide an XML hash reference" if not defined ref $xml;
	my $contents=$xml->{channel}->{item};
	croak "REDDIT: No data in XML provided" if (not defined $contents);
	my @return_value=();
	if (ref $contents eq "ARRAY") {
		foreach(@$contents) {
			my $current_value={
				link=>$_->{link},
				title=>$_->{title}
			};
		push @return_value,$current_value;
		}
	}else{
		my $current_value={
			link=>$xml->{channel}->{item}->{link},
			title=>$xml->{channel}->{item}->{title}
		};
		push @return_value,$current_value;
	}
	return @return_value;
}

1;

Here is the usage:

#!/usr/bin/perl
use lib ".";

use Reddit;
use Data::Dumper;
my $reddit=Reddit->new;
my @links=$reddit->get_links_from_xml(
                        ($reddit->search(
                                {
                                 site=>"imgur.com",
                                 over18=>"no",
                                 query=>"cat"
                                }
                        ))
                   );
foreach(@links){
        print $_->{title},"\n";
}

Easy! Isn't it? If you want the fully commented source code, you can find it at Github.

2 Comments

Leave a comment

About Hathibelagal

user-pic I am a web applications, and enterprise systems developer who is always on the lookout for better programming paradigms and coding styles.