#!/usr/bin/env ruby #もう古いので使えない require 'net/http' require 'cgi' require 'nkf' require 'iconv' #個人設定 #mail,pass: 空欄の時は起動時にプロンプトで入力 #idno: 空欄の時は自動取得(アクセスが一回分増える。ただしidが間違っている場合は動作は不明 mail = "" pass = "" idno = "" #ローカルのエンコーディング localEnc = "UTF-8" #mixiのエンコーディング mixiEnc = "EUC-JP" #基本設定 host = 'mixi.jp' page = Hash.new page['login'] = '/login.pl' page['home'] = '/home.pl' page['add_diary'] = '/add_diary.pl' #正規表現の定義 regAlert = // redMessage = /([^<]*)<\/font><\/b><\/br>/ regPostKey = // regIdNumber = /
  • / #stdoutを同期させる("開始 ... 終了" 的なメッセージ出力のため $stdout.sync = TRUE # #POSTで送るデータを作る。multipart/form-data # def makePostData( _hash ) boundary="---------------------------123799671819796002631780976537" #FIXME: 決め打ち☆ postData = "" _hash.each{ |k,v| postData += "--#{boundary}\r\n" postData += "Content-Disposition: form-data; name=\"#{k}\"\r\n" postData += "\r\n" postData += "#{v}\r\n" } postData += "--#{boundary}--\r\n" makePostData = [ postData, boundary ] end # #Cookieをパースして、サーバーに送れる形式に変える # def parseCookie( input ) cookie = ""; CGI::Cookie.parse( input ).delete_if{ |k,v| k == "path" || k == "domain" || k == "expires" || k == "secure" }.each{ |k,v| cookie += "#{k}=#{v[0]}; " } parseCookie = cookie end # #こっからメイン # if mail == "" then puts "メィルアドレスを入力" mail = readline.chomp end if pass == "" then puts "パスワードを入力" begin #パスワード入力のためにローカルエコーを切る。 system "stty -echo" pass = readline.chomp system "stty echo" rescue Interrupt system "stty echo" puts exit end end puts "タイトルを入力" title = Iconv.iconv( mixiEnc, localEnc, readline.chomp ) puts "本文を入力" body = Iconv.iconv( mixiEnc, localEnc, readlines.join ) Net::HTTP.start( host, 80 ) { |http| print "ログイン中 ... " response = http.post( page['login'], "next_url=#{page['home']}&email=#{mail}&password=#{pass}" ) if regAlert =~ response.body then puts "失敗" redMessage =~ response.body errmsg = Regexp.last_match(1) if !errmsg.nil? then puts "Alert: " + Iconv.iconv( localEnc, mixiEnc, errmsg )[0] end exit end cookie = parseCookie( response['set-cookie'] ) #Cookieを設定 header = Hash.new header['cookie'] = cookie puts "成功" #idnoが指定されていたらスキップ if idno == "" then print "ID取得中 ... " response = http.get( page['home'], header ) regIdNumber =~ response.body idno = Regexp.last_match(1) if idno.nil? then puts "失敗" exit end puts "ID:" + idno end print "Post_key取得中 ... " postData, boundary = makePostData( { "id" => idno, "submit" => "main", "diary_title" => title, "diary_body" => body } ) header['Content-Length'] = postData.length.to_s header['Content-Type'] = "multipart/form-data; boundary=#{boundary}" response = http.post( page['add_diary'], postData, header ) if regAlert =~ response.body then puts "失敗" redMessage =~ response.body errmsg = Regexp.last_match(1) if !errmsg.nil? then puts "Alert: " + Iconv.iconv( localEnc, mixiEnc, errmsg )[0] end exit end regPostKey =~ response.body postKey = Regexp.last_match(1) if postKey.nil? then puts "失敗" puts response.body exit end puts "post_key:" + postKey print "日記送信中 ... " postData, boundary = makePostData( { "id" => idno, "submit" => "confirm", "diary_title" => title, "diary_body" => body, "post_key" => postKey } ) header['Content-Length'] = postData.length.to_s header['Content-Type'] = "multipart/form-data; boundary=#{boundary}" response = http.post( page['add_diary'], postData, header ) puts "完了" }