Guest User

Untitled

a guest
Feb 28th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use utf8;
  6. use Net::Delicious;
  7.  
  8. # del.icio.us config
  9. my $user = "username";
  10. my $pass = "password";
  11.  
  12. my $delicious = Net::Delicious->new(
  13. {user => $user,
  14. pswd => $pass}
  15. );
  16.  
  17.  
  18. # read atom
  19. open(READ, "<dump.atom") || die "Can't open file\n";
  20. my @lines = <READ>;
  21. my $xml_data;
  22. for my $line ( @lines ) {
  23. $xml_data .= $line; # ファイルから全て読み込み
  24. }
  25. #print $xml_data;
  26. close(READ);
  27.  
  28. # parse atom
  29. my @entries;
  30. my @entries_xml = split(qq/<entry>/, $xml_data);
  31. for (my $i = 1; $i < @entries_xml; $i++){ # 0個目は無視
  32. my $s = $entries_xml[$i];
  33. my $entry; my @buf;
  34. # parse title
  35. @buf = split(/<title>/, $s);
  36. my @title = split(/<\/title>/, $buf[1]);
  37. $entry->{title} = $title[0];
  38.  
  39. # parse url
  40. @buf = split(/<link type=\"text\/html\" rel=\"related\" href=\"/, $s);
  41. my @url = split(/\"\/>/, $buf[1]);
  42. $entry->{url} = $url[0];
  43.  
  44. # parse extended
  45. @buf = split(/<summary>/, $s);
  46. my @extended = split(/<\/summary>/, $buf[1]);
  47. $entry->{extended} = $extended[0];
  48. if($entry->{extended} eq ""){
  49. $entry->{extended} = "";
  50. }
  51.  
  52. # parse tags
  53. @buf = split(/<dc:subject>/, $s);
  54. my $tags = "";
  55. for (my $j = 1; $j < @buf; $j++){ # 0個目は無視
  56. my @tag = split(/<\/dc:subject>/, $buf[$j]);
  57. if($tag[0] ne ""){
  58. $tags .= ' ' if($j > 1); # tagをカンマ区切り
  59. $tags .= $tag[0];
  60. }
  61. }
  62. $entry->{tags} = $tags;
  63.  
  64. # parse dt
  65. @buf = split(/<issued>/, $s);
  66. my @dt = split(/<\/issued>/, $buf[1]);
  67. $entry->{dt} = $dt[0];
  68.  
  69. push(@entries, $entry);
  70. }
  71.  
  72. print @entries . "entries loaded!!\n\n";
  73.  
  74. #print "dt:" . $entries[0]->{dt} . "\n";
  75. #exit;
  76.  
  77. # post & print debug
  78. my $err_count = 0;
  79. while(@entries){
  80. my $entry = pop(@entries);
  81. print $entry->{title}."\n";
  82. print $entry->{url}."\n";
  83. print $entry->{extended}."\n";
  84. print $entry->{tags}."\n";
  85. print $entry->{dt}."\n";
  86. print "posting the entry...\n";
  87. my $post = {
  88. url => $entry->{url},
  89. description => $entry->{title},
  90. extended => $entry->{extended},
  91. shared => 1,
  92. replace => 1,
  93. tags => $entry->{tags},
  94. dt => $entry->{dt},
  95. };
  96.  
  97. my $post_result = $delicious->add_post($post);
  98. if($post_result){
  99. print "success\n"
  100. }else{
  101. open(ERR, ">>error.log") || die "can't open error log\n";
  102. print "error!!(".++$err_count.")\n";
  103. print ERR $entry->{title}."\n";
  104. print ERR $entry->{url}."\n";
  105. print ERR $entry->{extended}."\n";
  106. print ERR $entry->{tags}."\n";
  107. print ERR $entry->{dt}."\n";
  108. print ERR "------\n";
  109. close(ERR);
  110. }
  111. print "--------\n";
  112. }
  113.  
  114. print "error: $err_count\n";
  115. open(ERR, ">>error.log") || die "can't open error log\n";
  116. print ERR "error: $err_count\n";
  117. close(ERR);
  118.  
  119.  
  120. print "\n---end---\n";
Add Comment
Please, Sign In to add comment