2008/9/20 土曜日

ldrの未読フィードからインライン画像リスト出す

Filed under: インターネット,プログラミング — staki @ 23:59:32

img.list っていうファイルに改行区切りでリスト出します。gethtml だの polipo に先行して食わせて高速化したりオフライン化のお供にしたり、ソレ専用アカウントな感じで画像収集したりとか。
simple-json が別途必要。あ、あとrubyです。

#クッキーとかいい加減です。
#ていうか何か汚い感じが・・・どうすれば良いんだろ?

ファイル

require "net/http"
require "zlib"
require "simple-json"
require "stringio"

username = "ゆーざーめい"
password = "ぱすわーど"

PROXY_ADDR = nil # 必要なら
PROXY_PORT = nil # 必要なら

login_parm= "livedoor_id=" + username + "&password=" + password #URI.encode必要?
$apikey = nil
$cookie = {}
$header =  {
  'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language' => 'ja,en-us;q=0.7,en;q=0.3',
  'Accept-Charset' => 'Shift_JIS,utf-8;q=0.7,*;q=0.7',
  "User-Agent" => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;.NET CLR 1.1.4322)',
  "Accept-Encoding" => 'gzip,deflate',
  "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"
}

def httppost(path, param=nil,server="reader.livedoor.com")
  Net::HTTP.start(server, 80, PROXY_ADDR, PROXY_PORT) {|http|
    param = ["ApiKey=" + $apikey, param].compact.join('&') if $apikey
    resp = http.post(path, param, $header)

    if resp.key?('Set-Cookie')
      resp.get_fields('Set-Cookie').each { |str|
        k,v = str[0...str.index(';')].split('=')
        $cookie.store(k,v)
      }
      $apikey = $cookie['reader_sid']
      $header.merge!({"Cookie" => ($cookie.to_a.map{|value|value.join("=")}).join("; ") })
    end
    return  Zlib::GzipReader.wrap(StringIO.new(resp.body)){|z|z.read}
  }
end

httppost("/login/index", login_parm, "member.livedoor.com" )#login
httppost("/reader/") #apikey 取得

sids = {}
JsonParser.new.parse(httppost("/api/subs?unread=1&from_id=&limit=")).each do |item|
  sids.store(item['title'], item['subscribe_id'])
end

sids.each do |title, sid|
  feed = JsonParser.new.parse(httppost("/api/unread", "subscribe_id=" + sid.to_s))
  feed['items'].each do |item|
    puts 'blog:' + title + "---" + item['title']
    File.open("img.list","a+"){|f|
      URI.extract(item['body']).each do |i|
        f.print( i + "\n") if i =~ /(jpg|jpeg|png|gif)$/
      end
    }
  end
  sleep 2
end

Powered by WordPress

stakilog