はてなキーワード: RubyGemsとは
| 日記途中だけど緊急事態><、一番下まで呼んで誰か助けてください>< | |
|---|---|
今日から夏休みが始まったのでプログラミングを勉強しようと思う。
3日坊主にならないようにがんばる。
去年の二の舞にならないようにモチベを維持するために日記を書く。
プログラミングができたらかっこいい!
サークルも友達もいなくさびしい夏休みを有意義にしようとするため
都内の大学に通う2年生。
前述のとおりサークルもやってない、友達もいない、典型的なインドアオタク。
でも顔はイケメン。
プログラミングは3年前にJavascriptとHTMLをネトゲでの戦いを整形するために覚えた程度。ほとんど忘れてる。
C言語を大学で習ったが「苦しんで覚えるC言語」というサイトにのっている範囲しかやってない。
一応プログラミングの知識は少しはある。
やっぱり、目標はあったほうがいい。
うーん、やっぱり完全に一致みたいな情報網羅サイトを作ってみようと思う。
色々な技術が手に入りそうだし。
色々な言語も学べそう
| どんなサイトにするか? | 大規模情報サイト |
|---|---|
| 学ぶ言語 | HTML,PHP,Ruby,Javascript,CSS |
とりあえずプログラミングにはエディタとアップロードするやつが必要なので色々いれてみた。
| エディタ | |
|---|---|
| Netbeans | 重い、すごく重い |
| phpエディタ | phpしか編集できない |
| terapad | 色分けが出来ない |
| K2Editor | これが一番最強。でもphpに対応してない |
| HIDEMARU4000YEN | win7じゃ使えなかった。 |
| アップするやつ | |
| FileZilla | 使いづらい、糞 |
| ALFTP | これが一番しっくりきた |
というわけで色々悩んだ結果、基本K2Editor、php編集はNetbeans,アップロードはALFTPにした。
phpとhtmlとrubyやjavascript対応してて色分けも出来るwin7で動くエディタないのが残念
実は1ヶ月前にプログラミングを勉強しようと思ってサーバーは借りてた。
ググったり、2chに質問したりして悩んで悩んでロリポップのチカッパプランを借りる事にした。
http://lolipop.jp/service/plan-chicappa/
| 初期費用 | ¥1,575 |
| 料金(6ヶ月) | ¥3,150 |
| (1ヶ月あたり) | ¥525 |
| 合計金額 | ¥4,725 |
しかしながら無料のレンタルさばより色々といじれて、なにより「お金はもう払った、使わないともったいない」という意識を持つ事でだらけないで出来ると思う。
まず師匠の日記をみてみる。HTMLとCSSからはじめたと書いてあるが、多分これからはじめると飽きる。
なのでWebの情報を自動で集めてくれる(これをスクレイピングという)BOTを作ろうと思う。
日記を読むとMechaniseというのを使ってるらしい。
そうするとRuby jamというもので動いているものらしく、Ruby jamはRubyで動いている。
Rubyは鯖にはいっているがどうやらjamのほうははいっていないらしくMechaniseもはいっていない。ということで
ロリポップの解説ページをみてPuTTyていうのをいれてSSH操作ってやつをしてた。
http://d.hatna.ne.jp/taramonera/20100601/1275388515
このページをみながらやってたら
% vi ~/.cshrc
これを実行して
setenv RUBYLIB /home/アカウント名/lib
setenv GEM_HOME /home/アカウント名/lib/rubygems
:wqで保存終了らしいんだけど、入力したところで終われない><
教えて得ろい人><
結構苦労したので健忘録として。。
環境は以下のとおり
yum install mysql-server
/etc/init.d/mysqld start
mysql_secure_installation
jruby -S gem install rails jruby -S gem install warbler jruby -S gem install activerecord-jdbc-adapter jruby -S gem install activerecord-jdbcmysql-adapter jruby -S gem install jdbc-mysql
mysql -u root -p mysql> create database redmine character set utf8; mysql> grant all privileges on redmine.* to 'redmine'@'localhost' identified by 'redmine'; mysql> exit
(任意の場所にRedmineを解凍して、解凍先のディレクトリに移動した後)
cp config/database.yml.example config/database.yml vi config/database.yml
database.yml
production: adapter: jdbcmysql database: redmine host: localhost username: redmine password: redmine encoding: utf8 #development: # # #test: # #
後のwarbleでのエラーを防ぐため、developmentとtestをコメントアウト
jruby -S rake generate_session_store jruby -S rake db:migrate RAILS_ENV=production jruby -S rake load_default_data RAILS_ENV=production
script/serverで起動し、http://localhost:3000 にアクセスして正常に動作するか確認する
jruby script/server -e production
vi config/environments/production.rb config.logger = Logger.new(config.log_path) config.logger.level = Logger::INFO
warble.rbを生成
jruby -S warble config
warble.rbを修正
vi config/warble.rb config.dirs = %w(app config lib log vendor tmp extra files lang) config.gems = ["jdbc-mysql", "activerecord-jdbcmysql-adapter", "activerecord-jdbc-adapter"] config.gems["rails"] = "2.3.5" config.gems["rack"] = "1.0.1" config.webxml.rails.env = "production"
jruby -S warble
できたwarファイルをTomcatに配置して、Tomcatを起動する
mv redmine-0.9.3.war /usr/local/tomcat/webapps/redmine.war /usr/local/tomcat/bin/startup.sh
warblerのバグ(?)でwarに入らないファイルをコピーして入れる
cp vendor/gems/rubytree-0.5.2/.specification /usr/local/tomcat/webapps/redmine/WEB-INF/vendor/gems/rubytree-0.5.2
非常に頭が悪いやり方ですが、一応動くので置いておきます
# -*- coding: utf-8 -*- require 'uri' require 'open-uri' require 'rubygems' require 'twitter' require 'json' LAST_YEAR = Time.now - 12*31*60*60*24 USERNAME = 'hogehoge' PASSWORD = '********' def following_me? other base = URI.parse('http://twitter.com/friendships/exists.json') base.query = "user_a=#{other}&amp;user_b=#{USERNAME}" times = 0 begin sleep 60 open(base).read == 'true' rescue if times < 2 times += 1 retry else true end end end def posted? hash begin sleep 60 last_post = @client.timeline_for(:user, :id=>hash[:user], :count=>1).first.created_at rescue Twitter::RESTError # protected ~A~K~A following ~A~L 0 ~A | ~P~H~A~Y~T~_~A~W~A~_~_ p "#{$!}" p "passing #{hash[:user]}" return true # ~D~P~F~_~P~F~J~\~A~Q~B~K rescue # twitter~A~L~P~A~A~B~K~Y~B対~V p "#{$!}" sleep 180 retry end p "last_post was #{last_post}" last_post > hash[:from] end @client = Twitter::Client.new(:login => USERNAME, :password => PASSWORD) (1..20).each do |pg| @client.my(:friends, :page=>pg).each do |friend| p "checking #{friend.screen_name}" unless ((following_me? friend.screen_name) &amp;&amp; posted?({:from=>LAST_YEAR, :user=>friend.screen_name})) p "removing #{friend.screen_name}" sleep 60 @client.friend(:remove, friend) end end end
http://anchor18.blog77.fc2.com/blog-entry-195.html
と
http://ameblo.jp/omisedayori/entry-10184130642.html
で
teruyastar
["", "まいったね。 やはりWiiウェアなんかのDL方式か、ネトゲに移行せざるをえないか。"]
complex_cat
["何処をどう描いても完璧なデッサン力。", "これについてはWikiの記述は,確かに使う側を助けてしまっているかも。いや,ここを読むようなユーザーとの重複度は小さい
とは思うが。"]
silverscythe
["昔のはホントすごいよなあ", ""]
yachimon
["", ""]
shoku-in
["", ""]
mike_n
["", ""]
y_arim
["Dr.スランプの時点ですでに完成されていて、そこからさらに進化しているのはすごい。", "違法性の認識とか関係なく、便利なものがあるから使うってだけだろうな。たいていのひとは物事を深く考えないし自分の欲求には逆らわない。/そしてid:utd_sn3781の主張は増田に書かれた http://anond.hatelabo.jp/20081231201301"]
A410
["", ""]
tailtame
["やっぱり手書きの頃がいいなぁ…塗りが…塗りが…。>>66の赤いトカゲ?ドラゴン?の艶とかたまらんなー。画集っていくつあるんだろ…", "蔓延しすぎているってのも あるんだろうな…本屋に行けば目に付く本は……。子供の頃なんてゲームは半年に一本でいいよ。クリエイターも割れしてたしな…"]
dododod
["", "おやおや"]
retlet
["", ""]
kashmir108
["", ""]
tokuhirom
["", ""]
ocha39
["", ""]
whirl
["", ""]
kalmalogy
["「ビッチズ・ライフ」買おう。", "親が買い与えるのは絶対だめ。『公認意識』が子供に芽生える。自腹で買って『本当はよくないけど次善の策』という意識でないと。/ マジコン使うならせめて『良いと思ったモノに投資する』意識を教育すべきでは。"]
came8244
["", ""]
fujii_isana
["", ""]
lizy
["クロノトリガーの絵を見るだけで、頭にあのBGMが流れてくる", "モラルでの解決方法案としては、ゲーム作りの過程を理解してもらう、とかかな。どれだけ多くの人がどれだけ苦労してコストをかけて作っているのかを理解してもらうしか|それでもやる奴はもうダメですね"]
mochige
["", ""]
aohige9610
["中鶴の絵がまじってるので注意", ""]
gm333
["", ""]
posinega
["", ""]
pikayan
["", ""]
asitaki
#!ruby -Ku require "rubygems" require "json" require 'open-uri' url = ["http://anchor18.blog77.fc2.com/blog-entry-195.html", "http://ameblo.jp/omisedayori/entry-10184130642.html"] bookmarks = url.map{|u| open("http://b.hatena.ne.jp/entry/json/"+u){|f| JSON.parse(f.read.gsub(/\A\(|\)\Z/, "")) } } user_comment_map = bookmarks.map{|bm| h = {} bm["bookmarks"].each{|b| h[b["user"]] = b["comment"] } h } overlaped_users = user_comment_map.map{|bm| bm.keys }.inject{|a,b| a&amp;b } overlaped_users.each{|user| puts user p user_comment_map.map{|b| b[user]} puts }
YourFileHostのCAPTCHA画像をなんとかするの続きの続き。
まぁ、なんというか、一応できたので張ってみる。微妙な出来栄えだけど。
decaptcha.rbと同じディレクトリに置いて適当に動かしてみてください。
どっかにいいRSSがないかなぁ。。。
しかし、CAPTCHA画像を相手にしてた時の方が楽しかったなぁ。。。
あと、添削とかいろいろ歓迎です。
なぜか&が&amp;に置き換えられてるみたいなので、適当に&に読み替えてください。
#!/usr/local/bin/ruby $LOAD_PATH << File::dirname(File::expand_path($0)) require 'rubygems' require 'rss' require 'mechanize' require 'decaptcha' # require 'ruby-debug' # require 'pp' DESTDIR = 'files' TMPDIR = '/tmp' class Downloader COOKIE_URI = 'http://www.yourfilehost.com/media.php?cat=video&file=%s' DOWNLOAD_URI = 'http://www.yourfilehost.com/downloadlink.php?cat=video&file=%s&adult=1' VERBOSE = false DEBUG = false def self.fetch(file, destdir) puts "Fetching file: #{file}" destfile = destdir + '/' + file if test(?e, destfile) then puts ' - Already exists. skip' puts return nil end # # Initialize Mech # mech = WWW::Mechanize.new mech.user_agent_alias = 'Windows IE 6' mech.max_history = 3 mech.open_timeout = 15 mech.read_timeout = 3 # # get cookie # uri = COOKIE_URI % file page = mech.get(uri) # # Challenge against CAPTCHA # uri = DOWNLOAD_URI % file page = mech.get(uri) for i in 0...3 captcha_path = (page/'img[@height="38"]').attr(:src) captcha_uri = 'http://www.yourfilehost.com/%s' % captcha_path gif = mech.get_file(captcha_uri) open("#{TMPDIR}/captcha.gif", 'w') {|fd| fd.write(gif) } mech.back code = DeCAPTCHA.decode("#{TMPDIR}/captcha.gif") File.unlink("#{TMPDIR}/captcha.gif") if code.nil? then puts ' - CAPTCHA decode failed. retry' if VERBOSE next end form = mech.page.forms.first form.verify = code page = mech.submit(form) break unless page.links.empty? end if page.links.empty? then puts ' - Failed 3 times. Try another one.' puts return nil end # # download # puts " - Downloading: #{page.links.first.href}" if DEBUG retry_count = 0 video = nil begin video = page.links.first.click rescue Timeout::Error => evar retry_count += 1 if retry_count < 5 then puts " * Timedout, retry" if VERBOSE retry end raise evar end unless video.instance_of?(WWW::Mechanize::File) then if DEBUG then puts " - Something wrong while downloading. skip." puts end return nil end video.save(destfile) return destfile end end # # main # [DESTDIR, TMPDIR].each do |dir| if !test(?d, dir) or !test(?w, dir) then puts "#{dir}: Directory not exists or cannot write." exit end end files = [] # collect URIs from RSS RSS_URI = [ ['http://www.yourfilehost.com/make-rss.php?range=day&af=off', lambda {|rss| rss.items.map {|x| x.link }}, ], ] RSS_URI.each do |uri, preprocessor| rss = RSS::Parser.parse(uri) uris = preprocessor.call(rss) #=> Array of URI of YourFileHost uris.map {|x| /file=([^&]*)/.match(x).to_a[1] }.each do |file| # next if file !~ /\.wmv/ # uncomment it if you need only wmv files << file end end files.uniq! # download files.each do |file| begin Downloader.fetch(file, DESTDIR) rescue SystemCallError, Timeout::Error => evar puts " - error (#{evar.to_s}). skip." puts end end __END__
YourFileHostのCAPTCHA画像をなんとかするの続き。
その後、適当にいじったら、手元環境で1枚あたり25秒くらい→だいたい2.5秒くらいで判別できるようになった。このくらいなら使えるかな。
速度向上に一番効いたのは、Token#importで画像を比較しているところの修正。他は細かい手直し。
使い方は前のやつと変わってません。
あと、テストに100枚くらいCAPTCHA画像食わせてみたけど、とりあえず全部正しく判定できた。
カレントディレクトリ以下にある*.gifなCAPTCHA画像ファイルを適当に判別するスクリプト。動作確認用にどうぞ。
後述のdecaptcha.rbと同じディレクトリに適当に置いてchmod +xしてね。
#!/usr/local/bin/ruby $LOAD_PATH << File::dirname(File::expand_path($0)) require 'decaptcha' STDOUT.sync = true Dir.glob('*.gif').sort.each do |file| correct = File::basename(file, '.*') puts "Processing file: #{file}" start_time = Time.now ret = DeCAPTCHA.decode(file) elapsed = Time.now - start_time puts " Result: #{ret} (=> #{(correct == ret) ? "Ok" : "Fail"})" puts " Elapsed time: #{elapsed}" puts end
#!/usr/local/bin/ruby require 'rubygems' require 'gd2' require 'pp' # #= CAPTCHA画像解析モジュール # CAPTCHA画像ファイルを食わすとあら不思議、Stringが出てくるよ。 # YourFileHostのやつに対応。 # #== Usage # decoded_str = DeCAPTCHA.decode("some_captcha_image.gif") #=> String # 失敗したらnilが返る。 # module DeCAPTCHA DEBUG = false #=== CAPTCHA画像デコード # file:: 画像ファイル名のパス # method:: 未指定でよい。男は細かい事を気にするな。 # returns:: CAPTCHA画像解析結果(String) or nil (デコード失敗時) def self.decode(file, method = DeCAPTCHA::Site::YourFileHost) return method.new(file).decode end #= CAPTCHA画像デコード用クラス # このクラスのサブクラスはimport, tokenize, stream_parseメソッドの # 実装を含む必要がある。 class Site def initialize(file = nil) @pix = nil self.import(file) unless file.nil? end def decode return stream_parse(tokenize()) end end #= YourFileHostのCAPTCHA画像を解析するクラス class Site::YourFileHost < Site def import(file) @pix = PixelMatrix.new.import(file) return self end # importしたイメージ(PixelMatrix)から、文字と思わしきパターンを # 抽出して上下マージンを切り取ってArrayにして返す。 # returns:: Array of PixelMatrix def tokenize ret = [] state = :initial for x in 0...@pix.width case state when :initial if !@pix.vline_blank?(x) then state = :tokenize pixel = PixelMatrix.new(0, 0, true) ret << pixel redo end when :tokenize if @pix.vline_blank?(x) then state = :initial next end x0 = pixel.width for y in 0...@pix.height pixel[x0, y] = @pix[x, y] end else raise 'NOTREACHED' end end ret.map! {|token| Token.new.import(token.cutoff_vmargin!) } end # PixelMatrixのArrayを受け取り、数字を判別。 # tokens:: Array of PixelMatrix # returns:: String (判別結果) def stream_parse(tokens) rs = tokens.map {|x| x.guess.to_s }.join('') if rs.length != 4 then rs = nil if DEBUG then puts '- guess failed. dumping guess result of each token:' tokens.each_index do |i| print "##{i}:#{tokens[i].guess} " pp tokens[i].candidate end puts end end return rs end class Token @@digits = nil attr_accessor :candidate def initialize if @@digits.nil? then # 文字画像サンプルを作っておく @@digits = DIGITS_ASSOC.map {|digit| PixelMatrix.new(0, 0, true).import_array(digit) } end @candidate = Hash.new end # PixelMatrixを受け取り、文字画像サンプルと比較して # 一致率を計算しておく。 # pixel:: PixelMatrix # returns:: self def import(pixel) @@digits.each_index do |i| digit = @@digits[i] if (digit.width - pixel.width).abs > 4 or (digit.height - pixel.height).abs > 4 then @candidate[i] = -1 # サイズが違いすぎな場合、一致させない next end correct_bits = 0 enlarged_width = [digit.width, pixel.width ].max enlarged_height = [digit.height, pixel.height].max for y in 0...enlarged_height dy = (y.to_f / digit.height * enlarged_height).to_i py = (y.to_f / pixel.height * enlarged_height).to_i for x in 0...enlarged_width dx = (x.to_f / digit.width * enlarged_width).to_i px = (x.to_f / pixel.width * enlarged_width).to_i correct_bits += 1 if digit[dx, dy] == pixel[px, py] end end @candidate[i] = correct_bits * 100 / (enlarged_width * enlarged_height) end return self end # importのときの比較結果をもとに文字を推測 # returns:: Fixnum or nil(失敗時) def guess digit, ratio = @candidate.sort {|a, b| a.last <=> b.last}.last digit = nil if ratio < 0 or ratio < 65 return digit end end end #= 画素マトリックスクラス # 画像ファイルを食わせると、各ピクセル(画素)を2値(black(1) or white(0))に # 変換して、内部で保持する。 # 以後、Matrixクラスのような感じで個々の画素にアクセスできる。 class PixelMatrix BLACK = 1 WHITE = 0 attr_accessor :width attr_accessor :height # width:: 幅 # height:: 高さ # is_flexible:: 自動的に伸張するか def initialize(width = 0, height = 0, is_flexible = false) @matrix = Hash.new {|hash, key| hash[key] = Hash.new(WHITE)} @width, @height, @flexible = width, height, is_flexible end # file:: 画像ファイル名のパス # brightness_threshold:: 画素を黒とみなす閾値 (0 - 255, default: 0x40) # returns:: self (DeCAPTCHA::PixelMatrix) def import(file, brightness_threshold = 0x40) gd = GD2::Image.import(file) @width, @height = gd.width, gd.height self.each_with_axis do |x, y| color = gd[x, y] greyscale = (color.red + color.green + color.blue) / 3 self[x, y] = (greyscale > brightness_threshold) ? WHITE : BLACK end return self end def import_array(array) array.each_with_index do |str, y| str.split('').each_with_index do |c, x| self[x, y] = c.to_i end end return self end # PixelMatrixを画像ファイルとしてexport # file:: 新たに作る画像ファイル名のパス def export(file) gd = GD2::Image::IndexedColor.new(@width, @height) gd.palette << GD2::Color::WHITE gd.palette << GD2::Color::BLACK self.each_with_axis do |x, y| gd[x, y] = { WHITE => GD2::Color::WHITE, BLACK => GD2::Color::BLACK, }[self[x, y]] end gd.export(file) return self end # 指定された位置の画素を返す。 # returns:: PixelMatrix::BLACK(1) or WHITE(0) def [](x, y) if !@flexible and !in_range?(x, y) then raise RangeError end return WHITE if !@matrix.has_key?(y) # XXX: for optimize return @matrix[y][x] end # 画素に値を設定。 # returns:: PixelMatrix::BLACK(1) or WHITE(0) def []=(x, y, val) unless in_range?(x, y) then raise RangeError unless @flexible @width = (x >= @width) ? x + 1 : @width @height = (y >= @height) ? y + 1 : @height end @matrix[y][x] = val end def in_range?(x, y) ((0...@width) === x and (0...@height) === y) end # 指定された軸をもとに画素を走査し、Arrayに変換。 # 例えば、to_a(:vertical, 10) とすると、x == 10 な列を取り出して # Arrayにして返す。 # # axis:: 軸を指定 (:vertical または :horizontal) # pos:: 位置を指定。_axis_で指定した軸と直交する軸における位置を指定。 def to_a(axis, pos) {:vertical => lambda { (0...@height).map {|y| self[pos, y]} }, :horizontal => lambda { (0...@width).map {|x| self[x, pos]} }, }[axis].call end # returns:: Array def hline(y) self.to_a(:horizontal, y) end # returns:: Array def vline(x) self.to_a(:vertical, x) end # X軸方向に画素を走査。 # y:: どの位置で走査するか # returns:: 指定された軸の上にドットが存在: false, 無い: true def hline_blank?(y) return true if @matrix.has_key?(y) == false # XXX: for optimize for x in 0...@width return false if self[x, y] == BLACK end return true end # Y軸方向に画素を走査。 # x:: どの位置で走査するか # returns:: 指定された軸の上にドットが存在: false, 無い: true def vline_blank?(x) for y in 0...@height return false if self[x, y] == BLACK end return true end # 上下のマージンを削除した新しいPixelMatrixを返す。 # returns:: PixelMatrix def cutoff_vmargin pixel = PixelMatrix.new(0, 0, true) head = 0 tail = self.height - 1 head.upto(tail) do |y| if !self.hline_blank?(y) then head = y break end end tail.downto(head) do |y| if !self.hline_blank?(y) then tail = y break end end head.upto(tail) do |y| 0.upto(self.width - 1) do |x| pixel[x, y - head] = self[x, y] end end return pixel end # 自身の上下のマージンを削除する。cutoff_vmarginの破壊版。 # 速度稼ぎのために直接@matrixを触ったり、すこしずるをしている。 # 効率は、ほんの少しだけ速くなったような誤差の範囲のような感じ。 # returns:: self (PixelMatrix) def cutoff_vmargin! head = 0 tail = self.height - 1 head.upto(tail) do |y| if !self.hline_blank?(y) then head = y break end end tail.downto(head) do |y| if !self.hline_blank?(y) then tail = y break end @matrix.delete(y) if @matrix.has_key?(y) # XXX end if head > 0 then head.upto(tail) do |y| next if !@matrix.has_key?(y) # XXX @matrix[y - head] = @matrix.delete(y) # XXX end end self.height = tail - head + 1 return self end def each_with_axis for x in 0...@width for y in 0...@height yield(x, y) end end end end end class DeCAPTCHA::Site::YourFileHost::Token DIGITS_ASSOC = [ # 0 ["00000000011111110000000000", "00000001111111111110000000", "00000011111000001111000000", "00001111111100010011110000", "00011111100000110000111000", "00111111000000100001111000", "00111111110001000001111100", "01111111000010000011111110", "01111100000110000111111110", "01111111000100000111111110", "11111100001000001111111111", "11100000001000011111111111", "11111000010000011111111111", "11111000110000111111111111", "10000000100001111111111111", "01100001000001111111111110", "01100010000011111111111110", "01100010000111111111111110", "00111100000111111111111100", "00011100001111111111111000", "00001100011111111111111000", "00001111111111111111100000", "00000011111111111111000000", "00000000111111111100000000"], # 1 ["00001", "00111", "11111", "11111", "10001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001", "00001"], # 2 ["0000011111111110000000", "0001111111111111000000", "0011110000000010000000", "0110000000000100000000", "1100000000001100011110", "1000000000001000001111", "1000000000010000000111", "1000000000110001111111", "1000000000100001111111", "1000000001000000011111", "0100000011000001111111", "0011000010000111111110", "0011000110000001111110", "0000001100000111111100", "0000001000011111110000", "0000011000000111100000", "0000110000000000000000", "0000111111111111111111", "0001111111111111111110", "0011111111111111111100", "0011111111111111111100", "0111111111111111111000", "0111111111111111110000"], # 3 ["000000011111111110000000", "000001111111111111100000", "000011100000011111111000", "000111000000111111111000", "000110000001111111111100", "000100000001111111111100", "000100000011111111111100", "000110000111111111111000", "000010000111111111111000", "000000001111111111100000", "000000011111111111000000", "000000011111111110000000", "000000000000001100000000", "000000000000011100011100", "000000000000111000111110", "000000000000110000001110", "000000000001110001111111", "110000000011100011111111", "111000000111000000111110", "011100000110001111111100", "001111001110000111111000", "000011111100000011100000", "000000011000111000000000"], # 4 ["0000000000011", "0000000000011", "0000000000111", "0000000001111", "0000000001111", "0000000011111", "0000000111111", "0000000110111", "0000001100111", "0000011100111", "0000011000111", "0000110000111", "0001110000111", "0001100000111", "0011000000111", "0111000000111", "0111111111111", "1111111111111", "0000000000111", "0000000000111", "0000000000111", "0000000000111", "0000000000111"], # 5 ["000000001111111111111110", "000000011111111111111100", "000000111111111111111100", "000000111111111111111000", "000001111111111111110000", "000011100000000000000000", "000011011111111110000000", "000111111111111111000000", "001111100000000111000000", "001110000000000110001100", "000000000000000100011110", "000000000000001100000110", "000000000000011000011111", "000000000000011001111111", "000000000000110000011111", "000000000001100000111111", "110000000001100111111110", "011000000011000001111110", "011100000110000011111100", "001111000110011111111000", "000111111100001111110000", "000001111100000011000000", "000000001000011000000000"], # 6 ["000000000000000110000000", "000000010001111111111000", "000001110000011100111100", "000011100000011000001000", "000111000011111000000000", "001111000001110000000000", "001110000000111100000000", "011110001111111111100000", "011100000111000011110000", "011000000010000001111000", "011000011110000011111100", "010000111100000111111110", "100000001000000111111110", "100001111000001111111111", "000011110000011111111111", "000000100000011111111111", "000011100000111111111111", "001111000001111111111110", "000010000001111111111110", "001110000011111111111100", "000111000111111111111000", "000011111111111111110000", "000000111111111111000000", "000000000111111000000000"], # 7 ["0011111111111110001111", "0011111111111100000011", "0111111111111000000110", "1111111111111000111100", "1111111111110000001100", "0000000000000000011000", "0000000000000011111000", "0000000011000000110000", "0000001110000011100000", "0000011110001111100000", "0000111100000011000000", "0001111000000110000000", "0001111000111110000000", "0011110000001100000000", "0011110000001000000000", "0011100011111000000000", "0011000001110000000000", "0001000000110000000000", "0000000111100000000000", "0000000111000000000000", "0000000011000000000000", "0000001110000000000000", "0000001100000000000000"], # 8 ["0000000111111111110000000", "0000011111111111111100000", "0000001111100000011110000", "0000000001110000000111000", "0011000011111000000011100", "0011100001111100000011100", "0011110000001110000011100", "0001111000011111000111000", "0000111100000111101110000", "0000011110000001111100000", "0000001111000011111110000", "0000111111110000011111000", "0011110011111000001111100", "0111100001111100011111100", "0111000000111110000111110", "1111000000011111000000111", "1111000000001111100011111", "1111000000000011111000010", "0111100000000001111100000", "0011110000000000111110000", "0001111110000001111110000", "0000011111111111111000000", "0000000011111110000000000"], # 9 ["000000111111111110000000", "000001111111111111100000", "000111111111111001111000", "001111111111110000010000", "011111111111110000010000", "011111111111100000100000", "011111111111000001100000", "111111111110000001000111", "111111111110000010000001", "111111111100000110000111", "011111111000000100011111", "011111111000001000001111", "001111110000011000011111", "001111100000010001111110", "000111110000100000011110", "000001111111100000111110", "000000011111000111111100", "000000000010000001111100", "000000000110000001111000", "000100001100011111110000", "000111001100001111100000", "000011111000001111000000", "000000010000110000000000"], ] end __END__
そんなわけで、みんな大好きなYourFileHostだけども、最近みてみたら、なんかCAPTCHA認証がついているわけじゃないですか。
でもこれってさーCAPTCHAといっても見るからに危ういというか、見れば見るほど簡単に破れてしまいそうな気がして、どうにもむずがゆいアンニュイな気分になってしまうわけです。
そんで、このたび適当にいじってみたところ、それなりに推測できるコードが書けたので、ここに張ってみますね。
やってることは単純で、こんな感じ
使い方は、こんな感じで
require 'decaptcha.rb' captcha_string = DeCAPTCHA.decode(filename) if !captcha_string.nil? then # 判別成功時の処理 else # 判別失敗時の処理。失敗することもあるのでよしなに。 end
あ、Ruby/GD2入れといてね。sudo gem install gd2とかで入ります。多分。
判別率はそこそこ良い感じになったんだけども、富豪プログラミングがたたってか、いかんせん遅いです。
手元だと1枚判別するのに20秒くらいかかることもある。
そんなわけで誰かチューニングしてくれるとうれしい。
実行速度を上げた改良版はこっちにうpしました。以下のコードは参考程度に。
#!/usr/local/bin/ruby require 'rubygems' require 'gd2' require 'pp' # #= CAPTCHA画像解析モジュール # CAPTCHA画像ファイルを食わすとあら不思議、Stringが出てくるよ。 # YourFileHostのやつに対応。 # #== Usage # decoded_str = DeCAPTCHA.decode("some_captcha_image.gif") #=> String # 失敗したらnilが返る。 # module DeCAPTCHA DEBUG = false #=== CAPTCHA画像デコード # file:: 画像ファイル名のパス # method:: 未指定でよい。男は細かい事を気にするな。 # returns:: CAPTCHA画像解析結果(String) or nil (デコード失敗時) def self.decode(file, method = DeCAPTCHA::Site::YourFileHost) return method.new(file).decode end #= CAPTCHA画像デコード用クラス # このクラスのサブクラスはimport, tokenize, stream_parseメソッドの # 実装を含む必要がある。 class Site def initialize(file = nil) @pix = nil self.import(file) unless file.nil? end def decode return stream_parse(tokenize()) end end #= YourFileHostのCAPTCHA画像を解析するクラス class Site::YourFileHost < Site def import(file) @pix = PixelMatrix.new.import(file) return self end # importしたイメージ(PixelMatrix)から、文字と思わしきパターンを # 抽出して上下マージンを切り取ってArrayにして返す。 # returns:: Array of PixelMatrix def tokenize getter, tokenizer = lambda {|queue| [ lambda { return queue }, lambda {|x, pixel| if pixel.nil? then x, pixel = tokenizer.call(x, PixelMatrix.new(0, 0, true)) queue << pixel return x end if !@pix.in_range?(x, 0) or @pix.vline_blank?(x) then return [x, pixel] end x0 = pixel.width @pix.vline(x).inject(0) do |y, color| pixel[x0, y] = color y + 1 end return tokenizer.call(x + 1, pixel) } ] }.call([]) x = 0 while (x < @pix.width) x = @pix.vline_blank?(x) ? x + 1 : tokenizer.call(x, nil) end getter.call.map do |token| # Token.new.import(token.cutoff_vmargin.shrink) Token.new.import(token.cutoff_vmargin) end end # PixelMatrixのArrayを受け取り、数字を判別。 # tokens:: Array of PixelMatrix # returns:: String (判別結果) def stream_parse(tokens) r = [] tokens.inject(nil) do |prev, cur| r << cur.guess end rs = r.map {|x| x.to_s}.join('') if rs.length != 4 then if DEBUG then puts '- guess failed. dumping guess result of each token:' tokens.each_index do |i| print "##{i}: " pp tokens[i].candidate end puts end return nil end return rs end class Token @@digits = nil attr_accessor :candidate def initialize if @@digits.nil? then # 文字画像サンプルを作っておく @@digits = DIGITS_ASSOC.map {|assoc| PixelMatrix.new(0, 0, true).import_assoc(assoc) } end @candidate = Hash.new end # PixelMatrixを受け取り、文字画像サンプルと比較して # 一致率を計算しておく。 # pixel:: PixelMatrix # returns:: self def import(pixel) gcd = lambda {|a, b| a, b = [b, a] if a < b return a if b == 0 r = a % b return gcd.call(b, r) } lcm = lambda {|a, b| a * b / gcd.call(a, b) } mul_to_lcm = lambda {|a, b| g = gcd.call(a, b) [b / g, a / g] } 0.upto(@@digits.size - 1) do |i| if (@@digits[i].width - pixel.width).abs > 4 or (@@digits[i].height - pixel.height).abs > 4 then @candidate[i] = -1 # 比較対象とサイズが違いすぎ next end mul_ax, mul_bx = mul_to_lcm.call(@@digits[i].width, pixel.width) mul_ay, mul_by = mul_to_lcm.call(@@digits[i].height, pixel.height) enlarged_width = @@digits[i].width * mul_ax enlarged_height = @@digits[i].height * mul_ay # 文字画像サンプルと比較対象画像のサイズをあわせる # 幅・高さをそれぞれ適当に整数倍して、最小公倍数に合わせて比較 # (めんどうだから) correct_bits = 0 (0...enlarged_width).each do |x| (0...enlarged_height).each do |y| if @@digits[i][x/mul_ax, y/mul_ay] == pixel[x/mul_bx, y/mul_by] then correct_bits += 1 end end end @candidate[i] = correct_bits * 100 / (enlarged_width * enlarged_height) end return self end # importのときの比較結果をもとに文字を推測 # returns:: Fixnum or nil(失敗時) def guess digit, ratio = @candidate.sort {|a, b| a.last <=> b.last}.last digit = nil if ratio < 0 or ratio < 80 return digit end end end #= 画素マトリックスクラス # 画像ファイルを食わせると、各ピクセル(画素)を2値(black(1) or white(0))に # 変換して、内部で保持する。 # 以後、Matrixクラスのような感じで個々の画素にアクセスできる。 class PixelMatrix BLACK = 1 WHITE = 0 attr_accessor :width attr_accessor :height # width:: 幅 # height:: 高さ # is_flexible:: 自動的に伸張するか def initialize(width = 0, height = 0, is_flexible = false) @matrix = Hash.new {|hash, key| hash[key] = Hash.new(WHITE)} @width, @height, @flexible = width, height, is_flexible end # file:: 画像ファイル名のパス # brightness_threshold:: 画素を黒とみなす閾値 (0 - 255, default: 0x40) # returns:: self (DeCAPTCHA::PixelMatrix) def import(file, brightness_threshold = 0x40) gd = GD2::Image.import(file) @width, @height = gd.width, gd.height self.each_with_axis do |x, y| color = gd[x, y] greyscale = (color.red + color.green + color.blue) / 3 self[x, y] = (greyscale > brightness_threshold) ? WHITE : BLACK end return self end # reverse function of to_assoc def import_assoc(assoc) assoc.inject(0) do |y, columns| columns.inject(0) do |x, color| self[x, y] = color x + 1 end y + 1 end return self end # PixelMatrixを画像ファイルとしてexport # file:: 新たに作る画像ファイル名のパス def export(file) gd = GD2::Image::IndexedColor.new(@width, @height) gd.palette << GD2::Color::WHITE gd.palette << GD2::Color::BLACK self.each_with_axis do |x, y| gd[x, y] = { WHITE => GD2::Color::WHITE, BLACK => GD2::Color::BLACK, }[self[x, y]] end gd.export(file) return self end def to_assoc (0...@height).map do |y| (0...@width).map do |x| self[x, y] end end end # 指定された位置の画素を返す。 # returns:: PixelMatrix::BLACK(1) or WHITE(0) def [](x, y) if !@flexible and !in_range?(x, y) then raise RangeError end return @matrix[y][x] end # 画素に値を設定。 # returns:: PixelMatrix::BLACK(1) or WHITE(0) def []=(x, y, val) unless in_range?(x, y) then raise RangeError unless @flexible @width = (x >= @width) ? x + 1 : @width @height = (y >= @height) ? y + 1 : @height end @matrix[y][x] = val end def in_range?(x, y) ((0...@width) === x and (0...@height) === y) end # 指定された軸をもとに画素を走査し、Arrayに変換。 # 例えば、to_a(:vertical, 10) とすると、x == 10 な列を取り出して # Arrayにして返す。 # # axis:: 軸を指定 (:vertical または :horizontal) # pos:: 位置を指定。_axis_で指定した軸と直交する軸における位置を指定。 def to_a(axis, pos) {:vertical => lambda { (0...@height).map {|y| self[pos, y]} }, :horizontal => lambda { (0...@width).map {|x| self[x, pos]} }, }[axis].call end # returns:: Array def hline(y) self.to_a(:horizontal, y) end # returns:: Array def vline(x) self.to_a(:vertical, x) end # X軸方向に画素を走査。 # y:: どの位置で走査するか # returns:: 指定された軸の上にドットが存在: false, 無い: true def hline_blank?(y) hline(y).find {|color| color == BLACK }.nil? ? true : false end # Y軸方向に画素を走査。 # x:: どの位置で走査するか # returns:: 指定された軸の上にドットが存在: false, 無い: true def vline_blank?(x) vline(x).find {|color| color == BLACK }.nil? ? true : false end # 上下のマージンを削除した新しいPixelMatrixを返す。 # returns:: PixelMatrix def cutoff_vmargin pixel = PixelMatrix.new(0, 0, true) head = 0.upto(self.height - 1) do |y| break(y) if !self.hline_blank?(y) end tail = (self.height - 1).downto(0) do |y| break(y) if !self.hline_blank?(y) end head.upto(tail) do |y| y0 = pixel.height self.hline(y).inject(0) do |x, color| pixel[x, y0] = color x + 1 end end return pixel end def each_with_axis (0...@width).each {|x| (0...@height).each {|y| yield(x, y)}} end end end class DeCAPTCHA::Site::YourFileHost::Token DIGITS_ASSOC = [ # 0 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0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, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], ] end __END__ Dir.glob('*.gif').each do |file| puts "Processing file: #{file}" p DeCAPTCHA.decode(file) end __END__
百万回繰り返された例の件について書いてみるよ。あ、タイトルは必要ないよね?このタグだけ見ればわかるものw
総評:とにかく微妙というか、中途半端につかいにくい。いまだにPerlが生きていたり、Rubyにキャッチアップされてるのも納得の出来。これがLL界を制覇したらPerlよりうっとうしい。
よもや、レスがついてるとは思わなかった。
興味を持ってもらえてサンクスです。
entry = diary.entry('20070712231804')
でエントリー指定してたからなんなんだろうと思って。
editもできるってことなのかな?
書き込んだあとの編集機能はいまんとこなし。
# get entry from id entry = diary.entry('20070712231804') # puts entry title puts entry.title # puts entry content puts entry.content
で、そもそもRubyに詳しくない自分からするとちゃんとした使い方がそれでもわからない。
バカでごめんねなんだけど、どうやって使えばいいの?
たとえば、エントリのタイトル一覧(1ページ目だけだけど)を出力するなら
diary = Masuda::Diary.new diary.entries do |entry| puts entry.title end
こんな感じかな。
新しいエントリを登録するなら
diary = Masuda::Diary.new diary.login('my_id', 'my_pass') diary.post('koko wa title ne', <<EOS) koko ni kizi no honbun wo kaku EOS
とか。
ずらーっと増田らしきものを読み込む。
そりゃそうだわなと思いながら文字化けの山。
文字化けなのは、たぶん増田のエントリ(UTF-8)をそのまんま取得しているせいだと思う。
~$ ruby hoge.rb | nkf -Ws
出力するときにSJISに変換するとか。
require 'rubygems' require 'masuda' require 'kconv' diary = Masuda::Diary.new diary.entries.each do |entry| puts entry.title.tosjis puts entry.content.tosjis end
あと
session[:diary] = diary.raw
diary = Masuda::Diary.restore(session[:diary])
の...って何でしょうか??
Masuda::Diary#rawはインスタンスをシリアライズするメソッド。
ログイン済みのインスタンスをシリアライズしてセッションにつっこんどいて、次のリクエストでも使いまわすとか。
わかりづらい文章で申し訳ない。
http://rubyforge.org/projects/masuda/
作った。
gem install masuda
require 'rubygems' require 'masuda' diary = Masuda::Diary.new diary.entries.each {|entry| puts entry.content } entry = diary.entry('20070712231804') puts <<EOS #{entry.title} #{entry.content} EOS entry.trackbacks.each {|trackback| puts trackback.snippet } diary.login('my_id', 'my_pass') diary.my_entries.each {|entry| puts entry.content } diary.post('Ruby is ...', <<EOS) A dynamic, open source programming language with a ... EOS session[:diary] = diary.raw ... diary = Masuda::Diary.restore(session[:diary])
http://anond.hatelabo.jp/20070615171101
俺は大学四年まで全くきちんとしたプログラミングをやったことが無くて(大学の講義でJavaの超簡単なのを教わったぐらい)で、卒論でプログラミングをしなくちゃならなくて、そのとき初めて Ruby を触った。
Ruby は OOP ですげーんだぜ、とか一部で云われていた時代で、有名なアプリケーションは tDiary ぐらいしかなかった。はじめはクラスとかも解らずに何が何だか。そのとき tDiary のプラグインはクラス使ってないから簡単に書けるよ、というどこかのチュートリアルをみて見よう見まねで。GD という画像ライブラリを使ったら、サンプルをちょっと弄るだけで画像が作れて面白かったんだ。で、それを日記で公開してみた。今見返すとものすごくしょぼいソース。
そのときたまたま Ruby ハカーの方がそのプラグインをリファクタリングしてくれて、クラスを使って抽象化してくれて、初めて OOP をほんの少しだけ理解して、こうやってクラスって使うんだなぁというのを知った。本当に運が良かった。
その後就職して仕事で php ハカーのすごい先輩にいろいろ教えてもらって php を使って基本的な OOP は理解した(PHP を DIS る人が多いけど、プログラミング初心者には良い言語だと今でも思ってる)。これまた運が良かった。
その後またまた Ruby を使い始めたら今までよくわからなかった部分もするする頭に入ってきてホント面白ろくて没頭して。今では一通りのことは Ruby でできるようになった。
プログラミングが解るなら、Rails のソース(トリッキーなことやりまくってるのでつらいかも。ActiveRecord や ActiveSupport はその中でも解りやすい)を読んで、解らなかったら rubygems で興味のありそうなライブラリのコード読んで、あたりが OOP と Ruby 覚えるには手っ取り早いかも。
今なら Rubyレシピブック 268の技 と Rubyクックブック ―エキスパートのための応用レシピ集 あたり読んでおけば良いんじゃないなぁ。
あと今はてダで Ruby を含む日記を書くともれなく ruby-dev な人たちがキーワードからたどって読んでくれるので、解らないことをつぶやいたりすると結構答えてくれるみたい。のではてダ使って勉強日記とか書くのも良いと思うよ。
とあんまり参考にならないと思うけど書いてみた。なんか目的見つけられて、楽しく覚えていけたら勝ちなんじゃないかな。たぶん。
http://anond.hatelabo.jp/20070429211930
ファイルにurlコピペしといてそこから読み込むようにしたんだぜ?
頭の"h"を補完するようにしたんだぜ?
例えばhoge.txtとして次のような内容にしておく。
ttp://www.yourfilehost.com/media.php?cat=video&file=hoge_001.wmv ttp://www.yourfilehost.com/media.php?cat=video&file=hoge_002.wmv ttp://www.yourfilehost.com/media.php?cat=video&file=hoge_003.wmv
こっちはbukkonuki.rbとして保存。
require 'rubygems' require 'scrapi' require 'open-uri' def get_yourfilehost_src(uri) yourfilehost_src = Scraper.define do process "embed", :src => "@src" result :src end yourfilehost_match = /vidlink\=(.*?)&/ return yourfilehost_match.match(yourfilehost_src.scrape(URI.parse(uri)))[1] end def completion_uri(arg) if arg =~ /^h/ uri = arg else uri = 'h' + arg end return uri end def expansion_list() $*.each do |files| File::open(files) do |file| file.each {|line| system 'wget', get_yourfilehost_src(completion_uri(line)) } end end end expansion_list() #$*.each do |arg| # system 'wget', get_yourfilehost_src(completion_uri(arg)) #end
使うときは
ruby bukkonuki.rb hoge.txt
って感じで頼むぜ。
http://anond.hatelabo.jp/20070325165452
仕様が変わっていたので改良したんだぜ?
使いかたは以前と変わってない。
require 'rubygems' require 'scrapi' require 'open-uri' def get_yourfilehost_src(uri) yourfilehost_src = Scraper.define do process "embed", :src => "@src" result :src end yourfilehost_match = /vidlink\=(.*?)&/ return yourfilehost_match.match(yourfilehost_src.scrape(URI.parse(uri)))[1] end $*.each do |uri| system 'wget', get_yourfilehost_src(uri) end
yourfilehostぶっこぬき
http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv
こんなのからhogehoge.wmvをダウンロードするためのrubyスクリプト
これをbukkonuki.rbとして保存する
require 'rubygems' require 'scrapi' require 'open-uri' yourfilehost_src = Scraper.define do process "embed", :src => "@src" result :src end ARGV.each do |uri| p system "wget", yourfilehost_src.scrape(URI.parse(uri)) end
こんな感じで使うとDLしてくる
ruby bukkonuki.rb "http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv"
複数指定してもDLしてくる
ruby bukkonuki.rb "http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv1" "http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv2"
半日かかったぞ…ふざけやがって