パスワードを忘れた? アカウント作成
376977 journal

dsegの日記: 日記の自動投稿スクリプト試作版, v0.13

日記 by dseg
#!/usr/bin/perl -w
# --------------------------------------------------------------------
# Title:  slash_journal_poster.pl
# Author: dseg <ds26 at mail.goo.ne.jp>
# URL:    http://srad.jp/journal.pl?op=display&uid=14237&id=222312
# Summary:...later...
# Commentary: Yet another quick hack
#
# $Id: slash_journal_poster.pl,v 0.13 2004-05-28 00:56:14+09 dseg Exp $
# --------------------------------------------------------------------

use strict;
use File::Basename;
use Getopt::Std;

my $VERSION = (q$Revision: 0.13 $ =~ /((?:\d\.)+\d+)/)[0];
sub usage{die "slash_journal_poster.pl -u <username> -p <password>\n"};

my %opts;
getopt('u:p:', \%opts);
&usage unless $opts{u} && $opts{p};

my $browser = new SlashdotJapan(
  agent      => "journal-autoposter/$VERSION ",
  autocheck  => 1,  # Any errors found are errors, not warnings.
);

$browser->login($opts{u}, $opts{p});
die "Login failed. quit.\n"
  unless $browser->login_succeed($opts{u});

# Read the journal entry from the text file
my $filespec = dirname($0) . '/journal.txt';
open F, "< $filespec" or die "$!\n";

my %entry = (body => '');
# Parse headers
while(<F>) {
  last if /^\s*$/; # Read lines until the first blank line
  s/\s+$//g;
  $entry{lc $1} = $2 if /^(\w+):?\s*(.*)$/;
}
# Parse body
while(<F>) {
  $entry{body} .= $_; # Read the rest
}
die "Journal title required.\n" if $entry{title} eq '';
#$entry{body} =~ tr/\015//d; # Remove CRs

my $res = $browser->submit_entry(\%entry);
if($res->is_success) {
  print "Journal entry successfully added.\n";
} else {
  warn $res->status_line, "\n";
  exit 1;
}

# --------------------------------------------------------------------
package SlashdotJapan;

use base qw/WWW::Mechanize/;

sub login {
  my $self = shift;
  my ($login, $password) = @_;

  $self->get('http://srad.jp/users.pl?light=1');
#  $_->dump for $self->forms(); # for debug

  $self->form_number(1);
  $self->field('unickname', $login);
  $self->field('upasswd', $password);
  $self->submit();
}

sub login_succeed {
  my ($self, $username) = @_;
  $self->get('http://srad.jp/journal.pl?light=1');

  return index($browser->content, $username) >= 0;
}

sub submit_entry {
  my ($self, $e) = @_;

  $self->get('http://srad.jp/journal.pl?op=edit&light=1');

  $self->form_number(1);
  $self->field('tid', $e->{topic_id})  if $e->{topic_id};
  $self->field('journal_discuss', '1') if $e->{comment_on} =~ /[1y]|true/;
  $self->field('description', $e->{title});
  $self->field('article', $e->{body});
  my $f = $e->{'format'};
  if   ($f eq 'text'     || $f eq '1') { $self->field('posttype', '1') }
  elsif($f eq 'html'     || $f eq '2') { $self->field('posttype', '2') }
  elsif($f eq 'puretext' || $f eq '3') { $self->field('posttype', '3') }
  elsif($f eq 'code'     || $f eq '4') { $self->field('posttype', '4') }
  $self->click();

  $self->form_number(2);
  # there are two buttons named 'op'. rename the 1st
  my $input = $self->current_form->find_input('op', 'submit', 1);
  die "Can't post the entry: failed to find the 'SAVE' button.\n"
    unless $input;

  $input->name('op_preview');
#  $self->current_form->dump; # for debug
  $self->click('op');
}
この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

UNIXはただ死んだだけでなく、本当にひどい臭いを放ち始めている -- あるソフトウェアエンジニア

読み込み中...