{"id":513,"date":"2005-11-26T20:44:33","date_gmt":"2005-11-26T20:44:33","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2005\/11\/using_del_icio_us_bookmarks_with_perl-shtml\/"},"modified":"2005-11-26T20:44:33","modified_gmt":"2005-11-26T20:44:33","slug":"using_del_icio_us_bookmarks_with_perl-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/","title":{"rendered":"Using del.icio.us Bookmarks With Perl"},"content":{"rendered":"<p>\nEarlier this week I put live some <a href=\"http:\/\/www.perl.com\/\">Perl<\/a> code that took bookmarks I was posting to <a href=\"http:\/\/del.icio.us\/\">del.icio.us<\/a> and added them automatically to my blog.\n<\/p>\n<p>\nI&#8217;ve had a few people ask how I was able to do this, and it&#8217;s no big secret.\n<\/p>\n<p>\n<a href=\"http:\/\/del.icio.us\/help\/api\">del.icio.us expose an API<\/a> that anyone can use to interact with their service. I&#8217;m using this with some Perl glue to aggregate the previous days posts and put them up on my own site.\n<\/p>\n<p>\nThe function I&#8217;m using is GET, which can take a date as an optional parameter. I use this parameter to get all postings from yesterday. So if the date today is 25th November 2005, to get yesterdays posts from del.icio.us I call the URL <a href=\"http:\/\/del.icio.us\/api\/posts\/get?dt=2005-11-24\">http:\/\/del.icio.us\/api\/posts\/get?dt=2005-11-24<\/a>.\n<\/p>\n<p>\nAll API calls to del.icio.us use HTTP-Auth, these are your site login and password.\n<\/p>\n<p>\nTo access this data using perl, we can use the LWP modules from <a href=\"http:\/\/www.cpan.org\/\">CPAN<\/a>.\n<\/p>\n<div class=\"code\"><code>my $ua = LWP::UserAgent-&gt;new;<br \/>\n$ua-&gt;agent('robslinkbot\/0.1 (+http:\/\/www.robertprice.co.uk)');<br \/>\nmy $get_url = 'http:\/\/del.icio.us\/api\/posts\/get?dt=' . $yesterday;<br \/>\nmy $req = HTTP::Request-&gt;new(GET =&gt; $get_url);<br \/>\n$req-&gt;authorization_basic(USERNAME, PASSWORD);<br \/>\nmy $res = $ua-&gt;request($req);<br \/>\nif ($res-&gt;is_success) {<br \/>\nmy $xml = $res-&gt;content;<br \/>\n} else {<br \/>\nwarn \"unable to get content from del.icio.usn\";<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nIn this case <code>USERNAME<\/code> and <code>PASSWORD<\/code> are both two constant values with my username and password defined in. You&#8217;ll need to put your own details in here.\n<\/p>\n<p>\nYou&#8217;ll also notice that I&#8217;m setting the agent to be <code>robslinkbot\/0.1 (+http:\/\/www.robertprice.co.uk)<\/code>. del.icio.us specicially request that a user-agent is set as they tend to ban generic user-agents from time to time. If I didn&#8217;t set this, it would be set to something like <code>lwp-perl<\/code>.\n<\/p>\n<p>\nSo now we have this code, and if it&#8217;s worked correctly, I should have my posts from the previous day in the variable <var>$xml<\/var>, and if it&#8217;s not worked, I should have seen a warning informing me that the script was unable to get content from del.icio.us.\n<\/p>\n<p>\nWe can parse the XML provided very easily using one of Perl&#8217;s many <acronym title=\"eXtensible Markup Language\">XML<\/acronym> modules. In this case, I&#8217;m going to use <code>XML::XPath<\/code>.\n<\/p>\n<p>\nAs we&#8217;ll have multiple bookmarks (hopefully), we&#8217;ll create a list of hashrefs. The hashrefs will contain the information relating to the post.\n<\/p>\n<p>\nOk, so firstly we create a variable called <var>@posts<\/var> to store the hashrefs in.\n<\/p>\n<div class=\"code\"><code>my @posts;<br \/>\n<\/code><\/div>\n<p>\nNow we can create our XML::XPath object, and get it to parse the xml we&#8217;ve already downloaded from del.icio.us and have in the <var>$xml<\/var> variable.\n<\/p>\n<div class=\"code\"><code>my $xp = XML::XPath-&gt;new(xml =&gt; $xml);<br \/>\n<\/code><\/div>\n<p>\nWe need to see if the XPath \/posts\/post exists, if it does it means we have posts to parse.\n<\/p>\n<div class=\"code\"><code>if ($xp-&gt;exists('\/posts\/post')) {<br \/>\n<\/code><\/div>\n<p>\nNow we have to find al the posts and iterate over them\n<\/p>\n<div class=\"code\"><code>foreach my $posts ($xp-&gt;find('\/\/post')-&gt;get_nodelist) {<br \/>\n<\/code><\/div>\n<p>\nNow all we do is to extract the information we need from each post, store it in our hashref and finally store it in the <var>@posts<\/var> list.\n<\/p>\n<div class=\"code\"><code>my $post_hashref;<br \/>\n## we use .= to stringify the find value, must be a better way to do that.<br \/>\n$post_hashref-&gt;{'href'} .= $posts-&gt;find('@href');<br \/>\n$post_hashref-&gt;{'description'} .= $posts-&gt;find('@description');<br \/>\n$post_hashref-&gt;{'time'} .= $posts-&gt;find('@time');<br \/>\n$post_hashref-&gt;{'hash'} .= $posts-&gt;find('@hash');<br \/>\n$post_hashref-&gt;{'extended'} .= $posts-&gt;find('@extended');<br \/>\nmy @tags = split(\/ \/, $posts-&gt;find('@tag'));<br \/>\n$post_hashref-&gt;{'tags'} = @tags;<br \/>\npush @posts, $post_hashref;<br \/>\n<\/code><\/div>\n<p>\nYou may have noticed that we have split the tags and stored them as a list. I just find this easier to work with.\n<\/p>\n<p>\nAnd that is about it. You should have list you can iterate over, or pass to something like Template Toolkit for displaying. This is a process I use on robertprice.co.uk.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier this week I put live some Perl code that took bookmarks I was posting to del.icio.us and added them automatically to my blog. I&#8217;ve had a few people ask how I was able to do this, and it&#8217;s no big secret. del.icio.us expose an API that anyone can use to interact with their service. &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using del.icio.us Bookmarks With Perl&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[12,47,75,80],"class_list":["post-513","post","type-post","status-publish","format-standard","hentry","category-dev","tag-blog","tag-perl","tag-web-development","tag-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using del.icio.us Bookmarks With Perl - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using del.icio.us Bookmarks With Perl - Robert Price\" \/>\n<meta property=\"og:description\" content=\"Earlier this week I put live some Perl code that took bookmarks I was posting to del.icio.us and added them automatically to my blog. I&#8217;ve had a few people ask how I was able to do this, and it&#8217;s no big secret. del.icio.us expose an API that anyone can use to interact with their service. &hellip; Continue reading &quot;Using del.icio.us Bookmarks With Perl&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2005-11-26T20:44:33+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using del.icio.us Bookmarks With Perl\",\"datePublished\":\"2005-11-26T20:44:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/\"},\"wordCount\":505,\"keywords\":[\"Blog\",\"Perl\",\"Web Development\",\"XML\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/\",\"name\":\"Using del.icio.us Bookmarks With Perl - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2005-11-26T20:44:33+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_del_icio_us_bookmarks_with_perl-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using del.icio.us Bookmarks With Perl\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using del.icio.us Bookmarks With Perl - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Using del.icio.us Bookmarks With Perl - Robert Price","og_description":"Earlier this week I put live some Perl code that took bookmarks I was posting to del.icio.us and added them automatically to my blog. I&#8217;ve had a few people ask how I was able to do this, and it&#8217;s no big secret. del.icio.us expose an API that anyone can use to interact with their service. &hellip; Continue reading \"Using del.icio.us Bookmarks With Perl\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/","og_site_name":"Robert Price","article_published_time":"2005-11-26T20:44:33+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using del.icio.us Bookmarks With Perl","datePublished":"2005-11-26T20:44:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/"},"wordCount":505,"keywords":["Blog","Perl","Web Development","XML"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/","name":"Using del.icio.us Bookmarks With Perl - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2005-11-26T20:44:33+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_del_icio_us_bookmarks_with_perl-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using del.icio.us Bookmarks With Perl"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}