<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>del.icio.us</title>
  <link rel="alternate" type="text/html" href="http://glyphy.com/category/code/delicious"/>
  <link rel="self" type="application/atom+xml" href="http://glyphy.com/taxonomy/term/9/atom/feed"/>
  <id>http://glyphy.com/taxonomy/term/9/atom/feed</id>
  <updated>2008-01-11T07:51:59-08:00</updated>
  <entry>
    <title>Del.icio.us LiveJournal post</title>
    <link rel="alternate" type="text/html" href="http://glyphy.com/delicious-livejournal-post" />
    <id>http://glyphy.com/delicious-livejournal-post</id>
    <published>2008-01-10T21:15:15-08:00</published>
    <updated>2008-01-11T07:51:59-08:00</updated>
    <author>
      <name>dv</name>
    </author>
    <category term="del.icio.us" />
    <category term="LiveJournal" />
    <summary type="html"><![CDATA[<p><strong>Originally written on 2005-06-04</strong>.</p>

<p>I thought it would be cool to have link digests posted in my journal once in a while automatically. It would leave a record of places I&#8217;ve been and things I&#8217;ve been interested in, but haven&#8217;t had the time/itch to write extensively about.</p>

<p>There are quite a few requirements:</p>

<ol>
<li>must be called periodically (ie. via cron)</li>
<li>a functioning mail setup must be present</li>
<li>paid <a href="http://www.livejournal.com/">LJ</a> account for email posting</li>
<li>a bunch of Perl modules that your host may not have (mine didn&#8217;t have XML::Simple). I&#8217;m lazy, though, and just wanted to use the tools at my disposal.</li>
</ol>

<p>Note: If you&#8217;re going to use this, you may need to append &#8220;&amp;count=n&#8221; to the API url (where n is an int between 1 and 100) to get the appropriate number of recent posts. I don&#8217;t use del.icio.us extensively, so the default 15 is good for me.</p>

<p>I hope I got the LWP stuff right. The script will back-off if yelled at, but it&#8217;s quite persistent otherwise (which may not be a good thing). Maybe I&#8217;ll tune down the number of repeated requests before giving up.</p>

<p>So, now, if I post any new links with a <em>ljpost</em> tag, it should automatically update every Friday and lj-cut the entry if there are too many links.</p>

<p>Source: <a href="http://glyphy.com/files/ljdelpost.pl_.txt">Perl file</a></p>

<div class="geshifilter"><pre class="perl geshifilter-perl"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Copyright Dmitri Vassilenko (dv [at] glyphy [dot] com)</span>
<span style="color: #666666; font-style: italic;"># Personal use is OK. Keep the ownership info.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Needs to be called from cron (or whatever you have) once in a while.</span>
<span style="color: #666666; font-style: italic;"># Upon each successive call with no arguments, it will query del.icio.us</span>
<span style="color: #666666; font-style: italic;"># for new posts under a certain tag and store the fetched data on disk along</span>
<span style="color: #666666; font-style: italic;"># with the last post time. Naturally, you shouldn't run this in a tight loop.</span>
<span style="color: #666666; font-style: italic;"># I run this a couple of times a day. You may get throttled by del.icio.us if</span>
<span style="color: #666666; font-style: italic;"># you poll too often.</span>
<span style="color: #666666; font-style: italic;"># To force a post, use the -u cmd-line option.</span>
<span style="color: #666666; font-style: italic;"># If you post a lot, you may wish to append &quot;&amp;count=n&quot; to $DEL_API, where n</span>
<span style="color: #666666; font-style: italic;"># is the number of posts you'd like to fetch. The default is 15 and max is 100.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### Requirements</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Std</span>;
<span style="color: #000000; font-weight: bold;">use</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">Dumper</span>;
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span>;
<span style="color: #000000; font-weight: bold;">use</span> XML<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span>;
<span style="color: #000000; font-weight: bold;">use</span> Storable;
<span style="color: #000000; font-weight: bold;">use</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">W3CDTF</span>;
<span style="color: #000000; font-weight: bold;">use</span> Mail<span style="color: #339933;">::</span><span style="color: #006600;">Send</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">########## Define constants ##########</span>
<span style="color: #000000; font-weight: bold;">use</span> constant LJ_USERNAME  <span style="color: #339933;">=&gt;</span> <span style="">'YourLJUsername'</span>;
<span style="color: #000000; font-weight: bold;">use</span> constant LJ_PIN       <span style="color: #339933;">=&gt;</span> <span style="">'YourLJEmailPostingPin'</span>;
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_USERNAME</span>          <span style="color: #339933;">=</span> <span style="">'YourDeliciousUsername'</span>;
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_PASSWORD</span>          <span style="color: #339933;">=</span> <span style="">'YourDeliciousPassword'</span>;
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_TAG</span>               <span style="color: #339933;">=</span> <span style="">'postingTag'</span>;
&nbsp;
<span style="color: #666666; font-style: italic;"># changing the following is optional</span>
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_API</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;https://api.del.icio.us/v1/posts/recent?tag=$DEL_TAG&quot;</span>;
<span style="color: #000000; font-weight: bold;">use</span> constant TIME_FILE    <span style="color: #339933;">=&gt;</span> <span style="">'/home/you/last'</span>; <span style="color: #666666; font-style: italic;"># stores last update time</span>
<span style="color: #000000; font-weight: bold;">use</span> constant POSTS_FILE   <span style="color: #339933;">=&gt;</span> <span style="">'/home/you/posts'</span>; <span style="color: #666666; font-style: italic;"># stores queued items for posting</span>
<span style="color: #000000; font-weight: bold;">use</span> constant MAX_DOC_SIZE <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">100000</span>; <span style="color: #666666; font-style: italic;"># in bytes</span>
<span style="color: #000000; font-weight: bold;">use</span> constant TIMEOUT      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span>;
<span style="color: #000000; font-weight: bold;">use</span> constant MAX_POSTS    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">7</span>; <span style="color: #666666; font-style: italic;"># after this # of new items, an lj-cut is used</span>
<span style="color: #000000; font-weight: bold;">use</span> constant MIN_POSTS    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>; <span style="color: #666666; font-style: italic;"># don't post less than this # of items</span>
<span style="color: #666666; font-style: italic;">######################################</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> usage <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Usage: ljdelpost [-u] [-s] [-t n]
Fetches the last few posts from del.icio.us and stores them on disk.
      -u updates and forces a post
      -s shows the latest posts currently stored on disk without posting them
      -t n trims the posts list to n last posts<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Parse commands</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cmds</span> <span style="color: #339933;">=</span> getopts<span style="color: #009900;">&#40;</span><span style="">'usht:'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">our</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$opt_h</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_s</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_u</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_t</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_h</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000066;">exit</span> 0 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">########## Fetch stored data from disk ##########</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>e TIME_FILE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$initial_time</span> <span style="color: #339933;">=</span> 0;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">$initial_time</span><span style="color: #339933;">,</span> TIME_FILE<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$last</span> <span style="color: #339933;">=</span> retrieve<span style="color: #009900;">&#40;</span>TIME_FILE<span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Last update time from epoch</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>e POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@initial_posts</span>;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">@initial_posts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sposts</span> <span style="color: #339933;">=</span> retrieve<span style="color: #009900;">&#40;</span>POSTS_FILE<span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Posts stored on disk</span>
<span style="color: #666666; font-style: italic;">#################################################</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">splice</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_t</span><span style="color: #009900;">&#41;</span>;
    store<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sposts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . POSTS_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #0000ff;">$opt_s</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_s</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Stored posts:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #000066;">print</span> Dumper<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #000066;">exit</span> 0;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ua</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content</span>;
&nbsp;
<span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">max_size</span><span style="color: #009900;">&#40;</span>MAX_DOC_SIZE<span style="color: #009900;">&#41;</span>;
<span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">timeout</span><span style="color: #009900;">&#40;</span>TIMEOUT<span style="color: #009900;">&#41;</span>;
<span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">agent</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;LJDelPost (http://www.livejournal.com/users/troworld/162341.html)&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$req</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HTTP<span style="color: #339933;">::</span><span style="color: #006600;">Request</span> GET <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$DEL_API</span>;
<span style="color: #0000ff;">$req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">authorization_basic</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$DEL_USERNAME</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$DEL_PASSWORD</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$res</span>;
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$req</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">is_success</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">content</span>;
    <span style="color: #b1b100;">last</span>;
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">warn</span> <span style="color: #ff0000;">&quot;Error: &quot;</span> . <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">code</span> . <span style="">' '</span> . <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">message</span>;
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">code</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">503</span> || <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">code</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">401</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Back off</span>
    <span style="color: #000066;">sleep</span> <span style="color: #cc66cc;">60</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$posts</span> <span style="color: #339933;">=</span> XMLin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$content</span><span style="color: #339933;">,</span> ForceArray <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>post<span style="color: #009900;">&#125;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$f</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">W3CDTF</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dt</span>; <span style="color: #666666; font-style: italic;"># Date of $i'th post</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@newposts</span>; <span style="color: #666666; font-style: italic;"># will be copied to disk when complete</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$newtime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$f</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$posts</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">time</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># date of last new post</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$posts</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$dt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$f</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">time</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">epoch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> $<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$last</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Posts are in descending order sorted</span>
                                         <span style="color: #666666; font-style: italic;"># by time</span>
  <span style="color: #000066;">push</span> <span style="color: #0000ff;">@newposts</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Copy new posts to disk</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#newposts <span style="color: #339933;">&gt;=</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">unshift</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@newposts</span>;
  store<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sposts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . POSTS_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">epoch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> TIME_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . TIME_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #666666; font-style: italic;"># update last post time</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_u</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> 0 || $#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> MIN_POSTS<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">warn</span> <span style="">'Not enough new posts. Not sending any messages.'</span>;
    <span style="color: #000066;">exit</span> 0;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mail<span style="color: #339933;">::</span><span style="color: #006600;">Send</span>;
  <span style="color: #0000ff;">$msg</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">to</span><span style="color: #009900;">&#40;</span>LJ_USERNAME . <span style="">'+'</span> . LJ_PIN . <span style="">'@post.livejournal.com'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #666666; font-style: italic;">#$msg-&gt;cc('your@email.com'); # CC yourself</span>
  <span style="color: #0000ff;">$msg</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">subject</span><span style="color: #009900;">&#40;</span><span style="">'Links for '</span> . <span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">day</span> . <span style="">' '</span> . <span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">month_name</span> . <span style="">' '</span> . <span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">year</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$msg</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">open</span>;
  <span style="color: #666666; font-style: italic;">#open(my $mailer, '&gt;-'); # for debugging purposes</span>
&nbsp;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;lj-security: public<span style="color: #000099; font-weight: bold;">\n</span>lj-tags: links<span style="color: #000099; font-weight: bold;">\n</span>lj-userpic: default<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;lj-cut text=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> . <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span> . <span style="color: #ff0000;">&quot; links<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> MAX_POSTS<span style="color: #009900;">&#41;</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;lj-raw&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;ul class='delList'&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">reverse</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$posted_time</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$f</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">time</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'    &lt;li class=&quot;delPost&quot;&gt;
        &lt;a href=&quot;'</span> . <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>href<span style="color: #009900;">&#125;</span> .
    <span style="">'&quot;&gt;'</span> . <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>description<span style="color: #009900;">&#125;</span> . <span style="">'&lt;/a&gt; &lt;small&gt;(Posted on '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">day</span> . <span style="">' '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">month_name</span> . <span style="">' '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">year</span> . <span style="">' @ '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">hms</span> . <span style="">' '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">time_zone_short_name</span> . <span style="color: #ff0000;">&quot;)&lt;/small&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'        &lt;div class=&quot;delExt&quot;&gt;'</span> . <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>extended<span style="color: #009900;">&#125;</span> . <span style="color: #ff0000;">&quot;&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">exists</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>extended<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tagline</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>tag<span style="color: #009900;">&#125;</span>;
    <span style="color: #0000ff;">$tagline</span> <span style="color: #339933;">=~</span> <span style="color: #000066;">s</span>|<span style="color: #0000ff;">$DEL_TAG</span>||; <span style="color: #666666; font-style: italic;"># kill the lj tag</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span> <span style="color: #0000ff;">$tagline</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@tags</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tagline</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'        &lt;div class=&quot;delTags&quot;&gt;Tags: '</span>;
      <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@tags</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'&lt;a href=&quot;http://del.icio.us/'</span> . <span style="color: #0000ff;">$DEL_USERNAME</span> . <span style="color: #ff0000;">&quot;/$j<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;$j&lt;/a&gt; &quot;</span>;
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;    &lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;/ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'&lt;/lj-raw&gt;'</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;/lj-cut&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> MAX_POSTS<span style="color: #009900;">&#41;</span>;
  <span style="color: #0000ff;">$mailer</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">close</span>; <span style="color: #666666; font-style: italic;"># Send the message</span>
&nbsp;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@cleanposts</span>;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">@cleanposts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . POSTS_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></div>
    ]]></summary>
    <content type="html"><![CDATA[<p><strong>Originally written on 2005-06-04</strong>.</p>

<p>I thought it would be cool to have link digests posted in my journal once in a while automatically. It would leave a record of places I&#8217;ve been and things I&#8217;ve been interested in, but haven&#8217;t had the time/itch to write extensively about.</p>

<p>There are quite a few requirements:</p>

<ol>
<li>must be called periodically (ie. via cron)</li>
<li>a functioning mail setup must be present</li>
<li>paid <a href="http://www.livejournal.com/">LJ</a> account for email posting</li>
<li>a bunch of Perl modules that your host may not have (mine didn&#8217;t have XML::Simple). I&#8217;m lazy, though, and just wanted to use the tools at my disposal.</li>
</ol>

<p>Note: If you&#8217;re going to use this, you may need to append &#8220;&amp;count=n&#8221; to the API url (where n is an int between 1 and 100) to get the appropriate number of recent posts. I don&#8217;t use del.icio.us extensively, so the default 15 is good for me.</p>

<p>I hope I got the LWP stuff right. The script will back-off if yelled at, but it&#8217;s quite persistent otherwise (which may not be a good thing). Maybe I&#8217;ll tune down the number of repeated requests before giving up.</p>

<p>So, now, if I post any new links with a <em>ljpost</em> tag, it should automatically update every Friday and lj-cut the entry if there are too many links.</p>

<p>Source: <a href="http://glyphy.com/files/ljdelpost.pl_.txt">Perl file</a></p>

<div class="geshifilter"><pre class="perl geshifilter-perl"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Copyright Dmitri Vassilenko (dv [at] glyphy [dot] com)</span>
<span style="color: #666666; font-style: italic;"># Personal use is OK. Keep the ownership info.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Needs to be called from cron (or whatever you have) once in a while.</span>
<span style="color: #666666; font-style: italic;"># Upon each successive call with no arguments, it will query del.icio.us</span>
<span style="color: #666666; font-style: italic;"># for new posts under a certain tag and store the fetched data on disk along</span>
<span style="color: #666666; font-style: italic;"># with the last post time. Naturally, you shouldn't run this in a tight loop.</span>
<span style="color: #666666; font-style: italic;"># I run this a couple of times a day. You may get throttled by del.icio.us if</span>
<span style="color: #666666; font-style: italic;"># you poll too often.</span>
<span style="color: #666666; font-style: italic;"># To force a post, use the -u cmd-line option.</span>
<span style="color: #666666; font-style: italic;"># If you post a lot, you may wish to append &quot;&amp;count=n&quot; to $DEL_API, where n</span>
<span style="color: #666666; font-style: italic;"># is the number of posts you'd like to fetch. The default is 15 and max is 100.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### Requirements</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Std</span>;
<span style="color: #000000; font-weight: bold;">use</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">Dumper</span>;
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span>;
<span style="color: #000000; font-weight: bold;">use</span> XML<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span>;
<span style="color: #000000; font-weight: bold;">use</span> Storable;
<span style="color: #000000; font-weight: bold;">use</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">W3CDTF</span>;
<span style="color: #000000; font-weight: bold;">use</span> Mail<span style="color: #339933;">::</span><span style="color: #006600;">Send</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">########## Define constants ##########</span>
<span style="color: #000000; font-weight: bold;">use</span> constant LJ_USERNAME  <span style="color: #339933;">=&gt;</span> <span style="">'YourLJUsername'</span>;
<span style="color: #000000; font-weight: bold;">use</span> constant LJ_PIN       <span style="color: #339933;">=&gt;</span> <span style="">'YourLJEmailPostingPin'</span>;
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_USERNAME</span>          <span style="color: #339933;">=</span> <span style="">'YourDeliciousUsername'</span>;
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_PASSWORD</span>          <span style="color: #339933;">=</span> <span style="">'YourDeliciousPassword'</span>;
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_TAG</span>               <span style="color: #339933;">=</span> <span style="">'postingTag'</span>;
&nbsp;
<span style="color: #666666; font-style: italic;"># changing the following is optional</span>
<span style="color: #b1b100;">my</span>  <span style="color: #0000ff;">$DEL_API</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;https://api.del.icio.us/v1/posts/recent?tag=$DEL_TAG&quot;</span>;
<span style="color: #000000; font-weight: bold;">use</span> constant TIME_FILE    <span style="color: #339933;">=&gt;</span> <span style="">'/home/you/last'</span>; <span style="color: #666666; font-style: italic;"># stores last update time</span>
<span style="color: #000000; font-weight: bold;">use</span> constant POSTS_FILE   <span style="color: #339933;">=&gt;</span> <span style="">'/home/you/posts'</span>; <span style="color: #666666; font-style: italic;"># stores queued items for posting</span>
<span style="color: #000000; font-weight: bold;">use</span> constant MAX_DOC_SIZE <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">100000</span>; <span style="color: #666666; font-style: italic;"># in bytes</span>
<span style="color: #000000; font-weight: bold;">use</span> constant TIMEOUT      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span>;
<span style="color: #000000; font-weight: bold;">use</span> constant MAX_POSTS    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">7</span>; <span style="color: #666666; font-style: italic;"># after this # of new items, an lj-cut is used</span>
<span style="color: #000000; font-weight: bold;">use</span> constant MIN_POSTS    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>; <span style="color: #666666; font-style: italic;"># don't post less than this # of items</span>
<span style="color: #666666; font-style: italic;">######################################</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> usage <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Usage: ljdelpost [-u] [-s] [-t n]
Fetches the last few posts from del.icio.us and stores them on disk.
      -u updates and forces a post
      -s shows the latest posts currently stored on disk without posting them
      -t n trims the posts list to n last posts<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Parse commands</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cmds</span> <span style="color: #339933;">=</span> getopts<span style="color: #009900;">&#40;</span><span style="">'usht:'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">our</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$opt_h</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_s</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_u</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_t</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_h</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> usage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000066;">exit</span> 0 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">########## Fetch stored data from disk ##########</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>e TIME_FILE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$initial_time</span> <span style="color: #339933;">=</span> 0;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">$initial_time</span><span style="color: #339933;">,</span> TIME_FILE<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$last</span> <span style="color: #339933;">=</span> retrieve<span style="color: #009900;">&#40;</span>TIME_FILE<span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Last update time from epoch</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>e POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@initial_posts</span>;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">@initial_posts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sposts</span> <span style="color: #339933;">=</span> retrieve<span style="color: #009900;">&#40;</span>POSTS_FILE<span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Posts stored on disk</span>
<span style="color: #666666; font-style: italic;">#################################################</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">splice</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$opt_t</span><span style="color: #009900;">&#41;</span>;
    store<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sposts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . POSTS_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #0000ff;">$opt_s</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_s</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Stored posts:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #000066;">print</span> Dumper<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #000066;">exit</span> 0;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ua</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content</span>;
&nbsp;
<span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">max_size</span><span style="color: #009900;">&#40;</span>MAX_DOC_SIZE<span style="color: #009900;">&#41;</span>;
<span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">timeout</span><span style="color: #009900;">&#40;</span>TIMEOUT<span style="color: #009900;">&#41;</span>;
<span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">agent</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;LJDelPost (http://www.livejournal.com/users/troworld/162341.html)&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$req</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HTTP<span style="color: #339933;">::</span><span style="color: #006600;">Request</span> GET <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$DEL_API</span>;
<span style="color: #0000ff;">$req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">authorization_basic</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$DEL_USERNAME</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$DEL_PASSWORD</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$res</span>;
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$req</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">is_success</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">content</span>;
    <span style="color: #b1b100;">last</span>;
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">warn</span> <span style="color: #ff0000;">&quot;Error: &quot;</span> . <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">code</span> . <span style="">' '</span> . <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">message</span>;
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">code</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">503</span> || <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">code</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">401</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Back off</span>
    <span style="color: #000066;">sleep</span> <span style="color: #cc66cc;">60</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$posts</span> <span style="color: #339933;">=</span> XMLin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$content</span><span style="color: #339933;">,</span> ForceArray <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>post<span style="color: #009900;">&#125;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$f</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">W3CDTF</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dt</span>; <span style="color: #666666; font-style: italic;"># Date of $i'th post</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@newposts</span>; <span style="color: #666666; font-style: italic;"># will be copied to disk when complete</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$newtime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$f</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$posts</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">time</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># date of last new post</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$posts</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$dt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$f</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">time</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">epoch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> $<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$last</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;"># Posts are in descending order sorted</span>
                                         <span style="color: #666666; font-style: italic;"># by time</span>
  <span style="color: #000066;">push</span> <span style="color: #0000ff;">@newposts</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Copy new posts to disk</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#newposts <span style="color: #339933;">&gt;=</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">unshift</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@newposts</span>;
  store<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sposts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . POSTS_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">epoch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> TIME_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . TIME_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #666666; font-style: italic;"># update last post time</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$opt_u</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> 0 || $#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&lt;</span> MIN_POSTS<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">warn</span> <span style="">'Not enough new posts. Not sending any messages.'</span>;
    <span style="color: #000066;">exit</span> 0;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mail<span style="color: #339933;">::</span><span style="color: #006600;">Send</span>;
  <span style="color: #0000ff;">$msg</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">to</span><span style="color: #009900;">&#40;</span>LJ_USERNAME . <span style="">'+'</span> . LJ_PIN . <span style="">'@post.livejournal.com'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #666666; font-style: italic;">#$msg-&gt;cc('your@email.com'); # CC yourself</span>
  <span style="color: #0000ff;">$msg</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">subject</span><span style="color: #009900;">&#40;</span><span style="">'Links for '</span> . <span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">day</span> . <span style="">' '</span> . <span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">month_name</span> . <span style="">' '</span> . <span style="color: #0000ff;">$newtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">year</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$msg</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">open</span>;
  <span style="color: #666666; font-style: italic;">#open(my $mailer, '&gt;-'); # for debugging purposes</span>
&nbsp;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;lj-security: public<span style="color: #000099; font-weight: bold;">\n</span>lj-tags: links<span style="color: #000099; font-weight: bold;">\n</span>lj-userpic: default<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;lj-cut text=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> . <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span> . <span style="color: #ff0000;">&quot; links<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> MAX_POSTS<span style="color: #009900;">&#41;</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;lj-raw&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;ul class='delList'&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">reverse</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$posted_time</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$f</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">time</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'    &lt;li class=&quot;delPost&quot;&gt;
        &lt;a href=&quot;'</span> . <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>href<span style="color: #009900;">&#125;</span> .
    <span style="">'&quot;&gt;'</span> . <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>description<span style="color: #009900;">&#125;</span> . <span style="">'&lt;/a&gt; &lt;small&gt;(Posted on '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">day</span> . <span style="">' '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">month_name</span> . <span style="">' '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">year</span> . <span style="">' @ '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">hms</span> . <span style="">' '</span> . <span style="color: #0000ff;">$posted_time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">time_zone_short_name</span> . <span style="color: #ff0000;">&quot;)&lt;/small&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'        &lt;div class=&quot;delExt&quot;&gt;'</span> . <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>extended<span style="color: #009900;">&#125;</span> . <span style="color: #ff0000;">&quot;&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">exists</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>extended<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tagline</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>tag<span style="color: #009900;">&#125;</span>;
    <span style="color: #0000ff;">$tagline</span> <span style="color: #339933;">=~</span> <span style="color: #000066;">s</span>|<span style="color: #0000ff;">$DEL_TAG</span>||; <span style="color: #666666; font-style: italic;"># kill the lj tag</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span> <span style="color: #0000ff;">$tagline</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@tags</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tagline</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'        &lt;div class=&quot;delTags&quot;&gt;Tags: '</span>;
      <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@tags</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'&lt;a href=&quot;http://del.icio.us/'</span> . <span style="color: #0000ff;">$DEL_USERNAME</span> . <span style="color: #ff0000;">&quot;/$j<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;$j&lt;/a&gt; &quot;</span>;
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;    &lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;/ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="">'&lt;/lj-raw&gt;'</span>;
  <span style="color: #000066;">print</span> <span style="color: #0000ff;">$mailer</span> <span style="color: #ff0000;">&quot;&lt;/lj-cut&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>$#<span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sposts</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> MAX_POSTS<span style="color: #009900;">&#41;</span>;
  <span style="color: #0000ff;">$mailer</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">close</span>; <span style="color: #666666; font-style: italic;"># Send the message</span>
&nbsp;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@cleanposts</span>;
  store<span style="color: #009900;">&#40;</span>\<span style="color: #0000ff;">@cleanposts</span><span style="color: #339933;">,</span> POSTS_FILE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't store %a in &quot;</span> . POSTS_FILE . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></div>
    ]]></content>
  </entry>
</feed>
