2014年5月23日金曜日

Selenium webdriver: chromeで全画面をキャプチャする方法

見えている範囲だけをキャプチャすることはできたので、多少強引だが
  1. Javascriptを使って見えてる範囲の高さと画面全体の高さを取得
  2. スクロールしながらキャプチャ
  3. 最後の一枚のみ重複ができるのでImageMagickを使ってカット
  4. ImageMagickを使って全ての画像を結合
という流れで全体のキャプチャを実現出来た。
以下rubyの場合。
def chrome_capture(webdriver, fname_prefix)
  #1. Javascriptで画面のサイズを取得
  inner_h = webdriver.execute_script("return window.innerHeight").to_i
  scroll_h = webdriver.execute_script("return document.documentElement.scrollHeight").to_i

  repeat = (scroll_h.to_f/inner_h.to_f).ceil
  dupl_h = inner_h - (scroll_h % inner_h)

  tmp_fnames = []
  tmp_basename = fname_prefix

  #2. スクロールしながらキャプチャ
  repeat.times{|i|
    tmp_fnames << tmp_fname = tmp_basename + sprintf("%06d",i) + ".png"
    webdriver.save_screenshot(tmp_fname)
    sleep (0.3)
    webdriver.execute_script("window.scrollBy(0,#{inner_h})");
    sleep (0.3)
  }

  if tmp_fnames.length > 1
    #一時ファイルが2つ以上の場合
    #3. 最後の一枚のみ重複ができるのでImageMagickを使ってカット
    if dupl_h > 0
      `convert -chop 0x#{dupl_h} #{tmp_fnames.last} #{tmp_fnames.last}`
    end
    #4. ImageMagickを使って全ての画像を結合
    `convert -append #{tmp_fnames.join(' ')} #{tmp_basename}.png`
    File.unlink *tmp_fnames
  else
    #1つの場合は一時ファイルをリネームするだけ
    File.rename(tmp_fnames[0],"#{tmp_basename}.png")
  end

end

ちなみにMacのSafari、Chromeでも同様の方法でいけるが、Retinaの場合は重複カットする際のピクセル数を2倍(正確には、JavascriptのdevicePixelRatioで取得可能な値倍)にする必要があるため要注意。

2014年4月22日火曜日

Vagrantで楽々開発環境構築(失敗)

遅まきながらVagrantに挑戦してみた。
参考文献↓
VagrantでRails開発環境を整備 (CentOS 6.5/Nginx/mysql5.6/rbenv/ruby2.1.0) - 酒と泪とRubyとRailsと
http://morizyun.github.io/blog/vagrant-centos-nginx-mysql-rbenv/
Winodows環境で試したが、
bundle exec berks install --path site-cookbooks
を実行した際に以下のエラーが表示されたため、下記の対応を実施後、Berksfileを下記の通り修正した
エラーメッセージ↓
>bundle exec berks install --path site-cookbooks
Using mysql (4.1.2)
Using database (2.0.0)
Using nginx (2.4.4)
Using base (0.1.0) at './site-cookbooks/base'
Using mysql56 (0.1.0) at './site-cookbooks/mysql56'
Using ruby_build (0.8.0)
Failed to download 'rbenv' from github: 'fnichol/chef-rbenv' with branch: 'master' over protocol: 'git'
Cookbook 'rbenv' not found in any of the default locations
下記コマンド実行↓
>cd site-cookbooks
>git clone https://github.com/fnichol/chef-rbenv.git
>cd ..
Berksfile変更前↓
site :opscode
cookbook 'mysql'
cookbook 'database'
cookbook 'nginx', path: 'site-cookbooks/nginx'
cookbook 'base', path: 'site-cookbooks/base'
cookbook 'mysql56', path: 'site-cookbooks/mysql56'
cookbook 'ruby_build', github: 'fnichol/chef-ruby_build'
cookbook 'rbenv',  github: 'fnichol/chef-rbenv'
cookbook 'ruby_gem', path: 'site-cookbooks/ruby_gem'
Breksfile変更↓(rbenvをpathに変更)
site :opscode
cookbook 'mysql'
cookbook 'database'
cookbook 'nginx', path: 'site-cookbooks/nginx'
cookbook 'base', path: 'site-cookbooks/base'
cookbook 'mysql56', path: 'site-cookbooks/mysql56'
cookbook 'ruby_build', github: 'fnichol/chef-ruby_build'
cookbook 'rbenv',  path: 'site-cookbooks/chef-rbenv'
cookbook 'ruby_gem', path: 'site-cookbooks/ruby_gem'
と思ったら、なんだかエラーが出て失敗した。
今日はここまでにしておこう。。。
エラーメッセージ↓
[2014-04-22T13:20:41+00:00] DEBUG: Re-raising exception: Chef::Exceptions::CookbookNotFound - Cookbook base not found. If you're loading base from another cookbook, make sure you configure the dependency in your metadata
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/cookbook/cookbook_collection.rb:38:in `block in initialize'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.0.2/lib/ohai/mash.rb:77:in`yield'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.0.2/lib/ohai/mash.rb:77:in`default'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.0.2/lib/ohai/mash.rb:77:in`default'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:277:in `[]'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:277:in `each_cookbook_dep'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:255:in `add_cookbook_with_deps'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:88:in `block in cookbook_order'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:86:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:86:in `cookbook_order'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:98:in `compile_libraries'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:71:in `compile'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context.rb:88:in `load'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/policy_builder/expand_node_object.rb:73:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:265:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:429:in `do_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:213:in `block in run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:207:in `fork'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:207:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application.rb:217:in `run_chef_client'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application/solo.rb:221:in `block in run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application/solo.rb:213:in `loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application/solo.rb:213:in `run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application.rb:67:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/bin/chef-solo:25:in `<top (required)>'
/usr/bin/chef-solo:23:in `load'
/usr/bin/chef-solo:23:in `<main>'
[2014-04-22T13:20:41+00:00] ERROR: Running exception handlers
[2014-04-22T13:20:41+00:00] ERROR: Exception handlers complete
[2014-04-22T13:20:41+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2014-04-22T13:20:41+00:00] DEBUG: Chef::Exceptions::CookbookNotFound: Cookbookbase not found. If you're loading base from another cookbook, make sure you configure the dependency in your metadata
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/cookbook/cookbook_collection.rb:38:in `block in initialize'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.0.2/lib/ohai/mash.rb:77:in `yield'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.0.2/lib/ohai/mash.rb:77:in `default'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.0.2/lib/ohai/mash.rb:77:in `default'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:277:in `[]'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:277:in `each_cookbook_dep'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:255:in `add_cookbook_with_deps'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:88:in `block in cookbook_order'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:86:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:86:in `cookbook_order'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:98:in `compile_libraries'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context/cookbook_compiler.rb:71:in `compile'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/run_context.rb:88:in `load'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/policy_builder/expand_node_object.rb:73:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:265:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:429:in `do_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:213:in `block in run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:207:in `fork'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/client.rb:207:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application.rb:217:in `run_chef_client'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application/solo.rb:221:in `block in run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application/solo.rb:213:in `loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application/solo.rb:213:in `run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/lib/chef/application.rb:67:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.2/bin/chef-solo:25:in `<top (required)>'
/usr/bin/chef-solo:23:in `load'
/usr/bin/chef-solo:23:in `<main>'
[2014-04-22T13:20:41+00:00] ERROR: Cookbook base not found. If you're loading base from another cookbook, make sure you configure the dependency in your metadata
[2014-04-22T13:20:41+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the output above. Please fix your recipes so that they properly complete.

2014年4月14日月曜日

WindowsでSublime TextのRubyTestの実行に失敗する場合の対処法

Windows環境でRailsTutorialに沿って進めていたら3.6.4 Tests inside Sublime Textで躓きました。

Build Consoleに下記が表示されていました。

[Decode error - output not utf-8]
[Finished in 0.1s with exit code 1]
[cmd: [' bundle exec rspec spec\\requests\\static_pages_spec.rb -l50']]
[dir: C:\***\sample_app]
[path: C:****;]

いろいろ試行錯誤した結果、run_ryby_test.pyの下記(201行目あたり)を修正することで、何とか実行できるっぽい?ことが判明。

self.view.window().run_command("exec", {
"cmd": [command],
"shell": True,
"working_dir": working_dir,
"file_regex": r"([^ ]*\.rb):?(\d*)"
})

self.view.window().run_command("exec", {
"cmd": command, # ←[]除去
"shell": True,
"working_dir": working_dir,
"file_regex": r"([^ ]*\.rb):?(\d*)"
})

#最初はメッセージに「Decode error」と表示されていたから
エンコードの問題かと思って下記のようにしてみたのですが、

self.view.window().run_command("exec", {
"cmd": [command],
"shell": True,
"working_dir": working_dir,
"file_regex": r"([^ ]*\.rb):?(\d*)",
"encoding": "cp932"
})

こうすると、コンソールに「指定されたパスが見つかりません。」と出たので、
commandが間違っているようだとわかりました。

ちなみに、Windows環境でListing 3-34は「# gem 'win32console', '1.3.2'」のコメントアウトをはずすとエラーになるっぽいです。Ruby2.0では、Win32consoleは必要ないみたいです。

#参考:Error “cannot load such file — 2.0/Console_ext” running unit tests in RubyMine

#この問題は一応本家のrailstutorialに報告してみたので、もしかしたらそのうち修正されているかもしれません。

2014年4月7日月曜日

namazuに代わるサイト内全文検索ツールを模索中

あまりしっくりくるものが見つからない。 fessはどうだろうか。 専用サーバーを用意しないと重いかな。そうすると冗長化とかややこしいな。。 elasticsearchとかsolr+nutch?はどうなんだろうか。かなり作り込みが必要になりそうで気が引けるなぁ。

2014年3月12日水曜日

PowerPointで、大文字しか入力できなくなった時の対処法

caps lockしてないのに大文字しか入力できなくなることがある。

 
フォントの設定が原因。
 
右クリック→フォント
をひらいて「すべて大文字」のチェックを外せば直る。
 
 

2014年3月7日金曜日

PowerPoint2007でPDF保存時に図形が正しく表示されない場合の対処法

時々この問題起きる気がする。

今日も職場の先輩がこまっていたので何が悪さしてるのか調べて見た。
が、早々に諦め、悪さしてたオートシェイプを
右クリック
図として保存
元のオートシェイプと差し替え

で乗り切ることにした。。

ちなみに、悪さしてたオートシェイプが残ってると、なぜかPDFを開く際にエラーが表示され、図(png)も表示されなかった。。

2014年3月6日木曜日

piwikをurchinのように非リアルタイムに使いたい。

Piwikサーバを立てるに当たって、リアルタイム性は問わないので、多少止まっても平気なようにしたいんだが、何か都合の良い方法はないだろうか。

PiwikはGoogle Analyticsのようなリアルタイム集計か、あるいはレガシーなアクセス解析ツールのような後からWebサーバのアクセスログを集計する使い方ができるようだが、その中間とも言えるurchin6のutm方式のような使い方はできないのだろうか。

urchin6のような、とはWebサーバのアクセスログに対してトラッキングに必要な情報を色々記録させるやり方のこと。

具体的には
①Webサイトのルートディレクトリにトラッキングに必要なファイル(urchin.jsと__utm.gif)を設置する
②各ページにトラッキングタグ(上記jsの読み込みとurchinTracker関数を呼び出すscriptタグ)を埋め込む
③ユーザが上記のページにアクセスするとurchinTrackerが発動する。画面解像度やらipアドレスやらクッキーに覚えさせたユーザ個別に割り振った識別IDやら、その他JavaScriptで取得可能な様々な情報が取得され、__utm.gifのURLパラメータという形でWebサイトのアクセスlogに記録される。
④上記のlogを解析する
というような流れのこと。