: O. Yuanying

Pragger で WWW::Mechanize

Pragger で WWW::Mechanize を簡単に使うプラグインを書いた。 これでまだ機能は貧弱だけどEntryFullText相当のことがログインの必要なサイトでも簡単に書ける。

  • Mechanize::agent
  • Mechanize::basic_auth
  • Mechanize::post
  • Mechanize::Filter::entry_full_text

たとえば、[Plagger][EFT]pixiv.yaml - fubaはてな 相当のことをやるには、

昨日のyaml、

- module: Feed::custom_feed
  config:
    url: http://www.pixiv.net/index.php?tag=C74
    split: "<li>(<a\\ href=\"index\\.php\\?mode=medium&illust_id=\d+?\"><img\\ src=\"http.+?\"\\ border=\"0\"\\ /></a><br\\ /><div\\ class=\"pdgTop5\">.+?</div>)</li>"
    title: "<div\\ class=\"pdgTop5\">(.+?)</div>"

- module: Filter::subs
  config:
    regex: "(index\\.php)"
    to: "http://www.pixiv.net/member_illust.php"

- module: Feed::custom_feed
  config:
    link: "<a\\ href=\"(http://www.pixiv.net/member_illust.php\\?mode=medium&illust_id=\d+?)\">"

これにログイン処理をした後に entry_full_text をfilterしてやる。

- module: Mechanize::agent

- module: Mechanize::post
  config:
    url: http://www.pixiv.net/index.php
    params:
      pixiv_id: yuanying
      pass: password-dayo

- module: Mechanize::Filter::entry_full_text
  config:
    extract_xpath:
      title: '//title/text()'
      dc_creator: "//div[@id='profile']/div/text()"
      author: "//div[@id='profile']/div/text()"
      description: "//div[@id='content2']"

でけたでけた。

それはそうと、WWW::Mechanizeのインスタンスをプラグイン間で共有したかったため、 無理矢理dataに特異メソッドとして data.agent を追加しているんだけど、これはもう少しどうにかしたいなあ。

例えばプラグインが実行される際に、そのプロセスのcontextと言う名のハッシュが受け渡せるようになってたらいいなあ。


def plugin( config, data, context )
  agent = context['agent']
  ...
end

こんな感じ?

そもそもプラグイン間でdata以外のものを共有するのは良くない??

ちなみに

今なら、YapraっていうもうひとつのPragger実装でEFT相当のことができるので、 そっちがおすすめですよ!!

gemでインストールできるしね!

$ sudo gem install yapra

カンタン。