{"id":214,"date":"2010-03-13T12:56:01","date_gmt":"2010-03-13T20:56:01","guid":{"rendered":"http:\/\/35.225.155.113\/blog\/index.php\/2010\/03\/13\/viddler_api_via_perl\/"},"modified":"2019-10-13T13:03:36","modified_gmt":"2019-10-13T20:03:36","slug":"viddler-api-via-perl","status":"publish","type":"post","link":"https:\/\/www.weinstein.org\/blog\/index.php\/2010\/03\/viddler-api-via-perl.html","title":{"rendered":"Viddler API via Perl"},"content":{"rendered":"<p>Recently, while doing some consulting work, I started working with an new online video platform called <a href=\"http:\/\/www.viddler.com\/\">Viddler<\/a>. Much like YouTube, Viddler is a web application, built around videos, that allows one to upload and share on the web.<\/p>\n<p>However, unlike YouTube, Viddler also provides a great deal of features for customization, from the skinning of the video player to the integration of the Viddler platform into customized web applications. The obvious advantage here for a business or organization is the ability to provide video content wrapped within their own branding or application without the expense of building and managing the huge computing infrastructure required for bandwidth and data storage.<\/p>\n<p>For example, checkout the <a href=\"http:\/\/techtv.mit.edu\/\">MIT Tech TV<\/a>, a video-sharing site for the MIT community built using Viddler.<\/p>\n<p>Alas, while there is <a href=\"http:\/\/developers.viddler.com\/projects\/api-wrappers\/phpviddler\/\">plenty of support for the Viddler Application Programming Interface via PHP<\/a>, which is what my consulting work is based in, the <a href=\"http:\/\/developers.viddler.com\/projects\/api-wrappers\/perlviddlerupload\/\">support for Perl is quite anemic<\/a>.<\/p>\n<p>To help rectify this dire situation, in my free time over the last week or so, I&#8217;ve been working on an Perl module that wraps around Viddler&#8217;s API. The goal here is not only to provide a basic how-to, but a quick method for integrating Perl-based applications with Viddler. As such, I plan on having something more formal to submit, not only to <a href=\"http:\/\/developers.viddler.com\/projects\/\">Viddler&#8217;s Project Directory<\/a>, but to <a href=\"http:\/\/www.cpan.org\/\">CPAN<\/a> as well, in the near future.<\/p>\n<p>With that in mind, here&#8217;s the basic layout with a few implemented methods for guidance and testing:<\/p>\n<div style=\"margin 5px; padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\n<pre>package Viddler;\nuse strict;\nuse warnings;\nuse LWP::Simple;\nuse XML::Simple;\nour $VERSION = \"0.01\";\n### To Do\n#\n# Complete support of all API methods\n# Add SSL option for methods such as users_auth\n# Validation\/Error Handling of parameters\/results\n#\n####\n=head1 NAME\nViddler - An encapsulation of the Viddler video platform in Perl\n=head1 SYNOPSIS\nuse Viddler;\nmy $videos = new Viddler( apiKey =&gt; $apiKey,\nusername =&gt; $username,\npassword =&gt; $passwd,\n);\nprint \"API Version: \" .$videos-&gt;api_getInfo(). \"\\n\";\n=head1 DESCRIPTION\nThis is an object-oriented library which focuses on pro diving Perl\nspecific methods for accessing the Viddler video service via their\nAPI, as documented at:\nhttp:\/\/developers.viddler.com\/documentation\/api\/\n=head2 Methods\n=head3 new\nmy $video = Viddler-&gt;new( apikey =&gt; $key,\nusername =&gt; $username,\npassword =&gt; $passwd );\nInstantiates an object which established the basic connection to\nthe API, including requesting and setting session id.\n=cut\n# The constructor of an object is called new() by convention.\nsub new {\nmy ( $class, %args ) = @_;\nmy $new = bless {\n_apiURL =&gt; 'http:\/\/api.viddler.com\/rest\/v1\/',\n_sessionID =&gt; undef,\n_record_token =&gt; undef,\n%args\n}, $class;\n# Get a sessionid\n$new-&gt;users_auth;\nreturn $new;\n}\n=head3 users_auth\nGets and sets a sessionid for an authenticated Viddler account.\nReturned sessionid is valid for 5 minutes (may change in the\nfuture). Every method request which contains valid sessionid,\nrenews its validity time.\n$video-&gt;users_auth;\nNo required parameters. Will use username and password defined\nat object's creation\nAdditional options parameters include:\n* get_record_token: If set to response will also include\nrecordToken\nReturns 0 ( false ) if unsucessful and 1 ( true ) if sucessful\n=cut\nsub users_auth {\nmy ( $self, $get_record_token ) = @_;\nmy $xml = new XML::Simple;\nmy $content = get $self-&gt;{_apiURL}.\n\"?method=viddler.users.auth&amp;api_key=\"\n.$self-&gt;{apiKey}. \"&amp;user=\" .$self-&gt;{username}.\n\"&amp;password=\" .$self-&gt;{password}.\n\"get_record_token=\" .$get_record_token;\nmy $results = $xml-&gt;XMLin( $content );\n$self-&gt;{_sessionID} = $results-&gt;{'sessionid'};\nif ( defined $results-&gt;{'get_record_token'} ) {\n$self-&gt;{_recordToken} = $results-&gt;{'record_toaken'};\n}\nif ( defined ( $self-&gt;{_sessionID} )) {\nreturn 1;\n} else {\nreturn 0;\n}\n}\n=head3 api_getInfo\nGets and returns the current version of the Viddler API.\n$video-&gt;api_getInfo;\nReturns current API version as a string\n=cut\nsub api_getInfo {\nmy ( $self ) = @_;\nmy $xml = new XML::Simple;\nmy $content = get $self-&gt;{_apiURL}.\n\"?method=viddler.api.getInfo&amp;api_key=\"\n.$self-&gt;{apiKey};\nmy $results = $xml-&gt;XMLin( $content );\nreturn $results-&gt;{'version'};\n}\n=head3 videos_search\nGets and returns results of a search of Viddler videos and people.\n$video-&gt;videos_search( $type, $query, $page, $per_page );\nRequires the following parameters:\n* type: The type of search (e.g. \"myvideos\",\n\"friendsvideos\", \"allvideos\", \"relevant\", \"recent\", \"popular\",\n\"timedtags\", \"globaltags\". (The \"timedtags\" and \"globetags\"\nsorting argument should be used in conjunction with an actual\ntag being given for the query argument.))\n* query: What to search for (e.g. \"iPhone\", \"Pennsylvania\", or\n\"Windows XP\")\nAdditional options parameters include:\n* page: The \"page number\" of results to retrieve (e.g. 1, 2, 3).\n* per_page: The number of results to retrieve per page (maximum\n100). If not specified, the default value equals 20.\nReturns a hash of an array of search results\n=cut\nsub videos_search( $ ) {\nmy ( $self, $type, $query, $page, $per_page ) = @_;\nmy $xml = new XML::Simple;\nmy $content = get $self-&gt;{_apiURL}.\n\"?method=viddler.videos.search&amp;api_key=\"\n.$self-&gt;{apiKey}. \"&amp;type=\" .$type.\n\"&amp;query=\" .$query. \"&amp;page=\" .$page.\n\"&amp;per_age=\" .$per_page. \"&amp;sessionid=\"\n.$self-&gt;{_sessionID};\nmy $results = $xml-&gt;XMLin( $content );\nreturn $results;\n}\n=head3 videos_getByUser\nGets and returns a lists of all videos that were uploaded by the\nspecified user.\n$video-&gt;videos_getByUser( $user, page, $per_page, $tags, $sort );\nRequires the following parameters:\n* user: The chosen Viddler user name. You can provide multiple\ncoma separated viddler usernames\nAdditional options parameters include:\n* page: The of results to retrieve (e.g. 1, 2, 3).\n* per_page: The number of results to retrieve per page (maximum\n100). If not specified, the default value equals 20.\n* tags: The tags you would like to filter your query by.\n* sort: How you would like to sort your query (views-asc,\nviews-desc, uploaded-asc, uploaded-desc)\nReturns a hash of an array of search results\n=cut\nsub videos_getByUser( $ ) {\nmy ( $self, $user, $per_page, $page, $tags, $sort ) = @_;\nmy $xml = new XML::Simple;\nmy $content = get $self-&gt;{_apiURL}.\n\"?method=viddler.videos.getByUser&amp;api_key=\"\n.$self-&gt;{apiKey}. \"&amp;sessionid=\"\n.$self-&gt;{_sessionID}. \"&amp;user=\" .$user.\n\"&amp;page=\" .$page. \"&amp;per_age=\" .$per_page.\n\"&amp;tags=\" .$tags. \"&amp;sort=\" .$sort;\nmy $results = $xml-&gt;XMLin( $content );\nreturn $results;\n}\n=head1 AUTHOR\nPaul Weinstein pdw [at] weinstein [dot] org\n=cut\n1;\n__END__\n<\/pre>\n<\/div>\n<p>And here&#8217;s a little code to test the demo package:<\/p>\n<div style=\"padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\n<pre>#!\/usr\/bin\/perl -T\nuse strict;\nuse warnings;\nuse Data::Dumper;\nuse Viddler;\nmy $videos = new Viddler( apiKey =&gt; '1234567890abcdefghij',\nusername =&gt; 'username',\npassword =&gt; 'password',\n);\nprint \"API Version: \" .$videos-&gt;api_getInfo(). \"\\n\";\nmy $searchResults = $videos-&gt;videos_getByUser( \"username\",\n\"\", \"\",\n\"test\", \"\" );\nprint Dumper( $searchResults );\n<\/pre>\n<\/div>\n<p>Comments, suggestions or corrections are quite welcomed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, while doing some consulting work, I started working with an new online video platform called Viddler. Much like YouTube, Viddler is a web application, built around videos, that allows one to upload and share on the web. However, unlike YouTube, Viddler also provides a great deal of features for customization, from the skinning of [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[7,44,43,281,4],"_links":{"self":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/214"}],"collection":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":2,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}