Skip to content

Instantly share code, notes, and snippets.

@beppu
Created March 21, 2009 04:49
Show Gist options
  • Select an option

  • Save beppu/82732 to your computer and use it in GitHub Desktop.

Select an option

Save beppu/82732 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Net::Delicious;
use XML::Feed;
use Storable;
use URI::Escape;
my %args = (
# feed to pull from
feed => 'http://weehours.net/rss.cgi',
# delicious account
user => '****',
password => '****',
# db of seen urls
db => "$ENV{HOME}/.feed-to-delicious.db",
# misc
verbose => 1,
);
sub url {
my $entry = shift;
my $uri = URI->new($entry->link);
$uri->fragment('');
my $url = $uri->as_string;
$url =~ s/#//;
$url;
}
sub title {
my $entry = shift;
my $title = $entry->title;
$title =~ s/#.*$//;
$title;
}
sub description {
my $entry = shift;
my $uri = URI->new($entry->link);
my $fragment = $uri->fragment;
if ($fragment) {
uri_unescape join(" ", map { ucfirst } split(/[-_]/, $fragment));
} else {
$entry->content->body;
}
}
sub person {
my $entry = shift;
my ($person) = map '@'.lc, ($entry->title =~ /^(.+?):/);
$person;
}
# Lazily create our storable database of already seen URLs.
if (not -f $args{db}) {
store({ }, $args{db});
}
my $seen = retrieve($args{db});
# __ _
# / _| ___ ___ __| |
# | |_ / _ \/ _ \/ _` |
# | _| __/ __/ (_| |
# |_| \___|\___|\__,_|
# 2 2
my $feed = XML::Feed->parse(URI->new($args{feed}))
or die XML::Feed->errstr;
# 2 2
# 2 2
# 2 2
# _ _ _ 2 _ 2
# __| | ___| | (_) ___(_) ___ _ _ ___
# / _` |/ _ \ | | |/ __| |/ _ \| | | / __|
# | (_| | __/ |_| | (__| | (_) | |_| \__ \
# \__,_|\___|_(_)_|\___|_|\___(_)__,_|___/
#
my $delicious = Net::Delicious->new({
user => $args{user},
pswd => $args{password},
});
# extra
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
for my $entry (reverse $feed->entries) {
my $url = url($entry);
my $tags = [
"weehours",
$year + 1900, sprintf('%d-%02d', $year + 1900, $mon + 1),
person($entry),
];
if (not $seen->{$url}) {
my $success = $delicious->add_post({
url => $url,
title => title($entry),
description => description($entry),
dt => $entry->issued->strftime('%FT%H:%M:%SZ'),
tags => join(' ', @$tags),
});
$seen->{$url} = 1 if ($success);
print "[+] $url\n" if ($success && $args{verbose});
print "[ ] $url\n" if (not($success) && $args{verbose});
} else {
print "[=] $url\n" if ($args{verbose});
}
}
store $seen, $args{db};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment