「AかつBで、かつZでない」ものを正規表現で表す

^(?=.*(A))(?!=(?=.*(Z)))(?=.*(B)).*$

例:「(グラビアに出ていそうな女の子の名前)かつ(画像っぽくて)で、かつ(男とか腐)でない」文字列を表す正規表現

^(?=.*(涼|依|衣|松|掘|里|香|奈|梓|紗|沙|華|綾|彩|愛|優|村|恵|由|桃|夏|美))(?!(?=.*(男|腐)))(?=.*(ギャラリ|画像|フォト|写真)).*$

Pla...的な何かに使うと嬉しいかもしんまい。

TumblrAPIをRubyから使う

require 'net/http'
require 'open-uri'
require 'cgi'
require 'xmlsimple'

class Tumblr

  def initialize(email=nil, password=nil)
    Net::HTTP.version_1_2
    @email = email
    @password = password
  end

  def to_query_parameter(hash)
    hash.map{|i| i.map{|j| CGI.escape j.to_s}.join('=') }.join('&')
  end

  def method_missing(method_id, *params)
    params[0][:type] = method_id.to_s
    params[0][:email] = @email if @email
    params[0][:password] = @password if @password
    post(params[0])
  end

  def post(params)
    Net::HTTP.start("www.tumblr.com", 80) do |http|
      response = http.post("/api/write", to_query_parameter(params))
    end
  end

  def get(id, options={})
    xml = open("http://#{id}.tumblr.com/api/read/?#{to_query_parameter(options)}").read
    XmlSimple.xml_in(xml, {'ForceArray' => false})
  end

end
tumblr = Tumblr.new(<email>,<password>)
params = Hash.new
params[:source] = "http://img0.gmodules.com/ig/f/_N3WzsBqUfM/intl/ALL_jp/logo.gif"
tumblr.photo(params)

例えば写真を投稿する場合はこんな感じ

tumblr = Tumblr.new
result = tumblr.get("mostlyfine")
puts result

あるユーザのTumblrを取得する場合はこんな感じ
Hashで結果が返ってくる。

ココから下はタイプとかパラメータのメモ。
APIの詳細はTumblr APIを参照

GET

  • start(default:0)
  • num(default:20)
  • type
    • regular(メモ)
    • quote(引用)
    • photo(写真)
    • link(リンク)
    • conversation(チャット)
    • video(動画)

POST

  • email
  • password
  • type
  • generator(ポストするアプリケーション名らしい)
Regular
  • title(OPTIONAL)
  • body
Photo
  • source
  • data
  • caption(OPTIONAL)
Quoto
  • quote
  • source(OPTIONAL)
Link
  • name(OPTIONAL)
  • url
  • description(OPTIONAL)
Conversation
  • title(OPTIONAL)
  • conversation
Video
  • embed
  • caption(OPTIONAL)
  • data
Audio
  • caption(OPTIONAL)
  • data

Ruby on Rails 2.3.0 RC1をインストールする

まずgemの確認

% gem -v
1.1.1

gemを1.3.1にしないと2.2以降はインストールできないのでアップデート

% sudo update_rubygems

あらためてRailsをインストールする

% gem install rails --source http://gems.rubyonrails.org

確認

% rails -v
2.3

ついでにRSpec on Railsもインストールしておく。
推奨はRSpec自体もpluginとしていれておく。
プラグインは同じバージョンのRSpecでないと動作しないかららしい。

RSpecの最新版はhttp://github.comなのでそこからインストール。

% ruby script/plugin install git://github.com/dchelimsky/rspec.git
% ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git
% ruby script/generate rspec


ためしにプロジェクト作ってみる

% rails test-app

完了!

ActiveResourceを使ってTwitterに投稿する

RESTの恐ろしさを味わうがいい!(いまさら)

class Twitter < ActiveResource::Base
  self.site = "http://www.twitter.com/"
  self.logger = Logger.new($stdout)

  class Status < Twitter
    def self.comment(user, pass, msg)
      self.user = user
      self.password = pass
      self.post(:update, :status => msg)
    end
  end
end

script/consoleから

>> Twitter::Status.comment(<username>, <password>, "thisistest")
POST http://www.twitter.com:80/statuses/update.xml?status=thisistest
--> 200 OK (892b 0.50s)
=> #<Net::HTTPOK 200 OK readbody=true>

Basic認証する場合、よく下記のように書いてあるサンプルを見かけるけど、これだとそれぞれのユーザごとに処理をさせられない。

  self.site = "http://#{USER}:#{PASS}@twitter.com"

ActiveResourceのソースを見たら「user=」や「password=」メソッドがあったりするので、普通にユーザ・パスワード指定して認証できるねってことらしい。

>> Twitter.user="username"
>> Twitter.password="password"
>> Twitter::Status.post(:update, :status => "thisistest")
POST http://www.twitter.com:80/statuses/update.xml?status=thisistest
--> 200 OK (892b 0.50s)
=> #<Net::HTTPOK 200 OK readbody=true>

もちろんこれもOK.

Yahoo!地図APIを使って携帯用の地図を生成する

携帯用の地図を生成するサービスを作ろうと思っていろいろ探してたら、
Yahoo!地図を使ったサンプルを見つけたんだけど、Yahoo! Developer Networkに載ってないAPIを使ってた。
いつまで使えるか、また、本当に使っていいか分からないけど一応メモしとく。

Google Staic Map APIと比べて以下のメリットがありそう(いづれも未確認)

  • ユーザ登録必要なし
  • 画像の最大サイズに制限がない
  • 一日の描画回数制限がない

使い方

リクエストURL

http://img.map.yahoo.co.jp/ymap/mk_map?

パラメータ

名前 説明 データサンプル
width 画像の横幅 240
height 画像の縦幅 240
sc ズームレベル 1
lat 緯度 35.66037716
lon 経度 139.73555977
pointer 中心点描画 on
home 不明 on
mode 不明 map

サンプル

http://img.map.yahoo.co.jp/ymap/mk_map?width=640&height=480&sc=3&lat=35.66037716&lon=139.73555977&pointer=on&home=on&mode=map

※公式APIではないっぽいので、突然利用できなくなる可能性があります。

問題あればコメント欄で教えてください。削除します。

選択日時をGoogleカレンダーに追加するブックマークレット

下のjavascriptをお気に入りやリンクバーなどに登録して、リンクしたいページでクリック。

javascript:(function(){F=function(v){return%20(v<10?'0':'')+v;};DF=function(r){var%20hh=r.getHours();var%20mm=r.getMinutes();var%20ss=r.getSeconds();return%20F(r.getFullYear())+F(r.getMonth()+1)+F(r.getDate())+(hh+mm+ss>0?'T'+F(hh)+F(mm)+F(ss):'');};R=function(v){return%20v.replace(/[%EF%BC%90-%EF%BC%99%EF%BC%8F%EF%BC%9A]/g,function%20($0){return%20String.fromCharCode($0.charCodeAt(0)-65248);}).replace(/[%E5%B9%B4%E6%9C%88]/g,'/').replace(/[%E6%99%82]/g,':').replace(/[^0-9:\/%EF%BD%9E]+/g,'%20');};D=function(v){if(isNaN(new%20Date(v))){v=new%20Date().getFullYear()+'/'+v;}else%20if(v.match(/^\d\d[^\d]/)){v='20'+v;}return%20new%20Date(v);};var%20t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var%20s=String(t).split('%EF%BD%9E');var%20m=t+'\n'+location.href;var%20d='';if(!isNaN(D(R(s[0])))){m=location.href;d='&dates='+DF(D(R(s[0])))+'/'+DF(D(R(s[s.length-1])));}var%20w=window.open('https://www.google.com/calendar/event?action=TEMPLATE&text='+escape(document.title)+'&details='+escape(m)+d,'_blank');})()
  • 日時の間に「〜」が含まれていたら期間になる
  • 日にちのみ選択されていたら終日
  • 今のところFirefox専用
  • カレンダーのタイトルに、開いているページのタイトルを利用
  • 詳細に開いているページのURLを利用
  • 事前にGoogleカレンダーにログインしておくといいよ

ためしにe+でMr.ChildrenのプレオーダーをGoogleカレンダーに登録してみる

こんな感じの日付っぽいところで・・・

日時を適当に選択する

ブックマークレットのリンクをクリックすると、以下の画面が起動する(はず)

できた!

いまのところIEで動かないので、誰か動くようにしてくれるとうれしいと思います(他力本願)