アーカイブ
‘mongoid’ タグのついている投稿
エゴサーチツール feedim
2010 年 12 月 2 日
コメントはありません
エゴサーチツール feedimを作った。3ヶ月ぐらい使い続けている。
以前はtwitter検索で “shokai” とか検索した結果をRSSリーダーで読んでいたんだけど、最近ビリケン商会とか大塚商会とかキラキラ商会とか、 “shokai” をユーザ名やURLに含むtweetが増えてきたし、邪魔なbotからのtweetも除外したいのでなんとかするツールを作った。
で、feedを吐くのもいいけどせっかくAndroid持っているから、im.kayac.comを使ってAndroidにpush通知するようにした。
im.kayac.comを使っているのでGoogle TalkやiPhoneのpush通知でも受信できる。
主にtwitterで使っているけど、feedなら何でも定期的に監視できる。
ソースはgithubにある github.com/shokai/feedim
先にim.kayac.comでユーザ登録して通知先を設定しておいてください
■インストール
git clone git://github.com/shokai/feedim.git
■必要なもの
mongodb 1.6以上が必要。起動しておく。
必要なrubygemsは、bundlerで入れる。
cd feedimmongoid2betaとか、俺が以前作ったim-kayacのgemとかがインストールされる。
gem install bundler
bundle install
■設定
config.yamlファイルを作る。
cp sample.config.yaml config.yamlconfig.yamlファイルを編集する。上の方でim.kayac.comのユーザ名とか、認証タイプを選ぶ。MongoDBのDB名とかも選ぶ。
監視するfeedを列挙する。除外したい内容をfilterに正規表現で書く。
filterは本文とURL両方にかけるfilterで、description_filterとurl_filterはそれぞれ本文とURLどちらかにかけるフィルタ。
# list of feedsこんな感じに書くと、ほぼ橋本商会とかshokaiは全部漏らさずに、ビリケン商会とか大塚商会とかキラキラ商会とか、邪魔なbotを除外できる。
feeds :
- "http://search.twitter.com/search.rss?q=shokai"
- "http://search.twitter.com/search.rss?q=%E6%A9%8B%E6%9C%AC%E5%95%86%E4%BC%9A"
- "http://search.twitter.com/search.rss?q=%E6%A9%8B%E6%9C%AC+%E7%BF%94"
- "http://search.twitter.com/search.rss?q=%E3%81%8B%E3%81%9A%E5%8A%A9"
- "http://search.twitter.com/search.rss?q=%E3%81%8B%E3%81%9A%E3%81%99%E3%81%91"
- "http://search.twitter.com/search.rss?q=%E3%83%8F%E3%82%B7%E3%83%A2%E3%83%86%E3%82%A3%E3%82%A6%E3%82%B9"
# filter by "url" and "description" property of entries
filters :
- "honeybee-cd"
# filter by "description" property of entries
description_filters:
- "_shokai"
- "shokai_"
- "\-shokai"
- "shokai\-"
- "shokai\.co"
- "shokai\d"
- "\dshokai"
- "キラキラ商会"
- "ビリケン"
- "大塚商会"
# filter by "url" property of entries
url_filters :
- "twitter\.com\/shokai\/"
- "twitter\.com\/shokai_log\/"
- "bot"
- "kirakira"
■動かす
クロールして、IMとAndroidに送る。
ruby store.rb
ruby publish.rb
crontabで10分おきに実行している。
mongoid使ってみる
2010 年 8 月 10 日
コメントはありません
mongo単体で使ってみててだいたい分かったので、mongoidというmapperを使ってみる。
mongoidの良いのは
- default値を入れておきたい場合も簡単に書ける。created_atとか。
- _idでdocumentを取り出すとき、素のmongoだとcollection.find_one(BSON::ObjectID(id))とかしないとならないけどmongoidだと_idに文字列でID入れればいい
そもそもこういうのmongoの機能にあるかもしれないけど。
■ドキュメント
- mongodbインストール – 橋本詳解 Macにインストールした
- Rubyからmongo使う – 橋本詳解
- Ruby Tutorial – MongoDB
- MongoRuby-1.0.7 mongoドライバのドキュメント
- ハンズオンで分かる MongoDB チュートリアル – ζ*’ワ’)ζ<ちれすですの! mongoのコンソールから使う
- Mongoid Documentation: Documents mongoid公式ドキュメント
- Rails3 対応 MongoDB ORM、Mongoid 詳解—ドキュメント – ζ*’ワ’)ζ<ちれすですの! 公式ドキュメント翻訳中。すげー。
sudo gem install mongoid1.9.1を使う。–pre付けるとRails3対応の2.x系統が入る。
■modelを作る
適当にperson class作って、Mongoid::Documentにする
person.rb
require 'rubygems'string以外は型指定する。型はArray, BigDecimal, Boolean, Date, DateTime, Float, Integer, String, Symbol, Timeがある。
class Person
include Mongoid::Document
field :fullname # 指定無しでtype=>stringになる
field :username
field :age, :type => Integer
field :created_at, :type => DateTime, :default => lambda{Time.now}
end
→ Mongoid Documentation: Documents
defaultで現在時刻を入れるようにした。
■mongodbへ接続
Mongoid.configureのブロック内で接続する。
conf.masterに普通のmongoで接続してdbを指定した時の返り値(Mongo::DBオブジェクト)を与えれば、mongoidで使える。
require 'rubygems'■modelの操作
require 'mongoid'
require File.dirname(__FILE__)+'/person'
Mongoid.configure do |conf|
conf.master = Mongo::Connection.new('localhost', 27017).db('mongoid-test')
end
新しいpersonオブジェクト作って保存
person = Person.new(:fullname => 'sho hashimoto',保存されてるか、mongoのコンソールで確かめる
:username => 'shokai',
:age => 25)
puts person.fullname
puts person.age
person.save
personで保存したら、自動的に複数形のpeopleになってた。ActiveRecordっぽい。
% mongo2回保存したから複数保存されてた
MongoDB shell version: 1.4.4
url: test
connecting to: test
type "help" for help
> show dbs
admin
chirpstream_shokai
local
mongoid-test
people
povietest
test
testdb
> use mongoid-test
switched to db mongoid-test
> show collections
people
system.indexes
> db.people.find()
{ "_id" : "4c61463c2f7306e9fe000001", "created_at" : "Tue Aug 10 2010 21:29:48 GMT+0900 (JST)", "fullname" : "sho hashimoto", "username" : "shokai", "age" : 25 }
{ "_id" : "4c614d652f73060653000001", "created_at" : "Tue Aug 10 2010 22:00:21 GMT+0900 (JST)", "fullname" : "sho hashimoto", "username" : "shokai", "age" : 25 }
■find
探す。Mongoid Documentation: Queryingにqueryの書き方が載ってる。
適当にユーザ名shokaiの最初の一件を取得して、表示する
person = Person.first(:conditions => {:username => 'shokai'})
puts person._id
puts person.username
puts person.created_at
他にも、全件とか色々な書き方ができる。person = Person.find(:first, :conditions => {:username => 'shokai'})
person = Person.all(:conditions => {:username => 'shokai'}).first
person = Person.first(:conditions => {:_id => '4c61463c2f7306e9fe000001'})
person = Person.where(:username => 'shokai').first
allで検索したら結果が1件しか無くても、collectionで返ってくる。eachで回せる。■modelに書いてない値を入れてみる
modelにない、person.placeを入れてみる
person = Person.new(:fullname => 'sho hashimoto',普通に入ってた。このへんは複数人でやるときは何か考えないとならないな。
:username => 'shokai',
:age => 25,
:place => 'fujisawa')
puts person.fullname
puts person.age
puts person.place
person.save
でもクロールしてきた値とかを適当にどんどん入れてしまうのにはすごくいい。twitterのchirp streamとか。
数値はlt,gtで大なり小なり条件指定できるらしい。
あとmongodbは空間型があるはずだけどそれは使えないのかな?filedの型になかったけど。

最近のコメント