Created
February 5, 2009 07:40
-
-
Save hokaccha/58602 to your computer and use it in GitHub Desktop.
hatebu2delicious.pl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Net::Delicious; | |
# del.icio.us config | |
my $user = "username"; | |
my $pass = "password"; | |
my $delicious = Net::Delicious->new( | |
{user => $user, | |
pswd => $pass} | |
); | |
# read atom | |
open(READ, "<dump.atom") || die "Can't open file\n"; | |
my @lines = <READ>; | |
my $xml_data; | |
for my $line ( @lines ) { | |
$xml_data .= $line; # ファイルから全て読み込み | |
} | |
#print $xml_data; | |
close(READ); | |
# parse atom | |
my @entries; | |
my @entries_xml = split(qq/<entry>/, $xml_data); | |
for (my $i = 1; $i < @entries_xml; $i++){ # 0個目は無視 | |
my $s = $entries_xml[$i]; | |
my $entry; my @buf; | |
# parse title | |
@buf = split(/<title>/, $s); | |
my @title = split(/<\/title>/, $buf[1]); | |
$entry->{title} = $title[0]; | |
# parse url | |
@buf = split(/<link type=\"text\/html\" rel=\"related\" href=\"/, $s); | |
my @url = split(/\"\/>/, $buf[1]); | |
$entry->{url} = $url[0]; | |
# parse extended | |
@buf = split(/<summary>/, $s); | |
my @extended = split(/<\/summary>/, $buf[1]); | |
$entry->{extended} = $extended[0]; | |
if($entry->{extended} eq ""){ | |
$entry->{extended} = ""; | |
} | |
# parse tags | |
@buf = split(/<dc:subject>/, $s); | |
my $tags = ""; | |
for (my $j = 1; $j < @buf; $j++){ # 0個目は無視 | |
my @tag = split(/<\/dc:subject>/, $buf[$j]); | |
if($tag[0] ne ""){ | |
$tags .= ' ' if($j > 1); # tagをカンマ区切り | |
$tags .= $tag[0]; | |
} | |
} | |
$entry->{tags} = $tags; | |
# parse dt | |
@buf = split(/<issued>/, $s); | |
my @dt = split(/<\/issued>/, $buf[1]); | |
$entry->{dt} = $dt[0]; | |
push(@entries, $entry); | |
} | |
print @entries . "entries loaded!!\n\n"; | |
#print "dt:" . $entries[0]->{dt} . "\n"; | |
#exit; | |
# post & print debug | |
my $err_count = 0; | |
while(@entries){ | |
my $entry = pop(@entries); | |
print $entry->{title}."\n"; | |
print $entry->{url}."\n"; | |
print $entry->{extended}."\n"; | |
print $entry->{tags}."\n"; | |
print $entry->{dt}."\n"; | |
print "posting the entry...\n"; | |
my $post = { | |
url => $entry->{url}, | |
description => $entry->{title}, | |
extended => $entry->{extended}, | |
shared => 1, | |
replace => 1, | |
tags => $entry->{tags}, | |
dt => $entry->{dt}, | |
}; | |
my $post_result = $delicious->add_post($post); | |
if($post_result){ | |
print "success\n" | |
}else{ | |
open(ERR, ">>error.log") || die "can't open error log\n"; | |
print "error!!(".++$err_count.")\n"; | |
print ERR $entry->{title}."\n"; | |
print ERR $entry->{url}."\n"; | |
print ERR $entry->{extended}."\n"; | |
print ERR $entry->{tags}."\n"; | |
print ERR $entry->{dt}."\n"; | |
print ERR "------\n"; | |
close(ERR); | |
} | |
print "--------\n"; | |
} | |
print "error: $err_count\n"; | |
open(ERR, ">>error.log") || die "can't open error log\n"; | |
print ERR "error: $err_count\n"; | |
close(ERR); | |
print "\n---end---\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment