アーカイブ

‘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 feedim
gem install bundler
bundle install
mongoid2betaとか、俺が以前作ったim-kayacのgemとかがインストールされる。


■設定
config.yamlファイルを作る。

cp sample.config.yaml config.yaml
config.yamlファイルを編集する。上の方でim.kayac.comのユーザ名とか、認証タイプを選ぶ。MongoDBのDB名とかも選ぶ。



監視するfeedを列挙する。除外したい内容をfilterに正規表現で書く。
filterは本文とURL両方にかけるfilterで、description_filterとurl_filterはそれぞれ本文とURLどちらかにかけるフィルタ。
# list of feeds
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"
こんな感じに書くと、ほぼ橋本商会とかshokaiは全部漏らさずに、ビリケン商会とか大塚商会とかキラキラ商会とか、邪魔なbotを除外できる。



■動かす
クロールして、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の機能にあるかもしれないけど。

■ドキュメント
■インストール
sudo gem install mongoid
1.9.1を使う。–pre付けるとRails3対応の2.x系統が入る。

■modelを作る
適当にperson class作って、Mongoid::Documentにする

person.rb
require 'rubygems'

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
string以外は型指定する。型はArray, BigDecimal, Boolean, Date, DateTime, Float, Integer, String, Symbol, Timeがある。
Mongoid Documentation: Documents

defaultで現在時刻を入れるようにした。

■mongodbへ接続
Mongoid.configureのブロック内で接続する。
conf.masterに普通のmongoで接続してdbを指定した時の返り値(Mongo::DBオブジェクト)を与えれば、mongoidで使える。
require 'rubygems'
require 'mongoid'
require File.dirname(__FILE__)+'/person'

Mongoid.configure do |conf|
  conf.master = Mongo::Connection.new('localhost', 27017).db('mongoid-test')
end
■modelの操作
新しいpersonオブジェクト作って保存
person = Person.new(:fullname => 'sho hashimoto',
:username => 'shokai',
:age => 25)

puts person.fullname
puts person.age

person.save
保存されてるか、mongoのコンソールで確かめる
personで保存したら、自動的に複数形のpeopleになってた。ActiveRecordっぽい。
% mongo
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 }
2回保存したから複数保存されてた

■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の型になかったけど。

track feed