Skip to content

Instantly share code, notes, and snippets.

@yuiseki
Created March 16, 2011 09:31
Show Gist options
  • Save yuiseki/872232 to your computer and use it in GitHub Desktop.
Save yuiseki/872232 to your computer and use it in GitHub Desktop.
ushadihiのtwitterクローラがいじっているmodelをrubyで扱えるようにする
# tweet_results DB投入部分
# reporterインスタンス生成
$reporter = ORM::factory('reporter')
->where('service_id', $service->id)
->where('service_account', $tweet->{'from_user'})
->find();
# reporterの情報をセットし保存
$level = ORM::factory('level')
->where('level_weight', 0)
->find();
$reporter->service_id = $service->id;
$reporter->level_id = $level->id;
$reporter->service_userid = null;
$reporter->service_account = $tweet->{'from_user'};
$reporter->reporter_first = null;
$reporter->reporter_last = null;
$reporter->reporter_email = null;
$reporter->reporter_phone = null;
$reporter->reporter_ip = null;
$reporter->reporter_date = date('Y-m-d');
$reporter->save();
# Messageインスタンス生成、情報保存
$message = new Message_Model();
$message->parent_id = 0;
$message->incident_id = 0;
$message->user_id = 0;
$message->reporter_id = $reporter->id; # ここでreporterのIDをセット
$message->message_from = $tweet->{'from_user'};
$message->message_to = null;
$message->message = $tweet->{'text'};
$message->message_type = 1; // Inbox
$tweet_date = date("Y-m-d H:i:s",strtotime($tweet->{'created_at'}));
$message->message_date = $tweet_date;
$message->service_messageid = $tweet->{'id'};
$message->save();
# Incidentインスタンスを生成、情報保存
$incident = new Incident_Model();
$incident->location_id = $reporter_location->id;
$incident->incident_title = $incident_title;
$incident->incident_description = $message->message;
$incident->incident_date = $tweet_date;
$incident->incident_dateadd = date("Y-m-d H:i:s",time());
$incident->incident_active = 1;
if ($reporter_weight == 2)
{
$incident->incident_verified = 1;
}
$incident->save();
# messageとincidentをひもづける
// Update Message with Incident ID
$message->incident_id = $incident->id;
$message->save();
# incident categorieの設定
$trusted_categories = ORM::factory("category")
->where("category_trusted", 1)
->find();
if ($trusted_categories->loaded)
{
$incident_category = new Incident_Category_Model();
$incident_category->incident_id = $incident->id;
$incident_category->category_id = $trusted_categories->id;
$incident_category->save();
}
require 'rubygems'
require 'sequel'
Sequel::Model.plugin(:schema)
Sequel.connect("mysql:://#{username}:#{password}@#{host}/#{database}")
class Message < Sequel::Model; end
class Incident < Sequel::Model; end
class User < Sequel::Model; end
class Reporter < Sequel::Model; end
class Category < Sequel::Model; end
class IncidentCategory < Sequel::Model; end
# reporterがすでにいるか?いなかったら生成
reporter = Reporter.find_or_create(
:service_id=>3, # twitter
:level_id=>0,
:service_userid=>nil,
:service_account=>tweet['from_user'],
:reporter_first=>nil,
:reporter_last=>nil,
:reporter_email=>nil,
:reporter_phone=>nil,
:reporter_ip=>nil,
).set(
:reporter_date=>Time.now() # datetime
).save_changes
require 'date'
tweet_date = Date.strptime(tweet['created_at'], "%Y-%m-%d %H:%i:%s")
message = Message.insert(
:parent_id=>0,
:incident_id=>0,
:user_id=>0,
:reporter_id=>reporter.id, # created reporter
:message_from=>tweet['from_user'],
:message_to=>nil,
:message=>tweet['text'],
:message_type=>1,
:message_data=>tweet_date, # DATETIME
:service_messageid=>tweet['id'],
)
incident = Incident.insert(
:location_id=>reporter.location.id, # nanikore
:incident_title=>tweet['text'], #TODO string 50len
:incident_description=>tweet['text'],
:incident_date=>tweet_date,
:incident_dateadd=>Time.now(),
:incident_active=>1,
# if reporter.level.level_weight==2
# :incident_verified=>1,
)
message.update(:incident_id = incident.id)
category = Category.filter(:category_trusted=>1)
incident_category = IncidentCategory.insert(
:incident_id=>incident.id,
:category_id=>category.id
)
@send
Copy link

send commented Mar 16, 2011

find_or_create(...).set(...).save_changes のほうがいいかな?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment