Created
May 13, 2013 05:52
-
-
Save unamashana/5566397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rb-inotify' | |
require 'sqlite3' | |
require 'active_record' | |
require 'mail' | |
require 'log4r' | |
include Log4r | |
MYLOG = Logger.new 'mylog' | |
file = FileOutputter.new('fileOutputter', :filename => 'hsi_notifications.log', :trunch => false) | |
MYLOG.add(file) | |
MYLOG.level = Log4r::INFO | |
MYLOG.info "Starting HSI Notifier" | |
BASE_DIR = '/home/rails/arc/arc/news' | |
Mail.defaults do | |
delivery_method :smtp, { :address => 'localhost', | |
:enable_starttls_auto => false, | |
:authentication => :plain} | |
end | |
module EmailFunctions | |
def get_email(filename) | |
f = File.open(filename) | |
content = f.readlines | |
content = content[0] | |
match = content.match(/email (.+?)\"\) /) | |
match[1].gsub('"','') | |
end | |
def send_email(comment, email) | |
MYLOG.info "sending email to #{email}" | |
Mail.deliver do | |
from 'noreply@hackerstreet.in' | |
to email | |
subject '[HackerStreet.in] New Reply' | |
body "http://hackerstreet.in/item?id=#{comment.story_id} \n \n #{comment.by} says '#{comment.content}' \n\n To unsubscribe from HSI updates, please remove your email address from your profile" | |
end | |
end | |
end | |
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'hsi.db') | |
class Story < ActiveRecord::Base | |
validates_uniqueness_of :story_id | |
validates_presence_of :story_id, :by | |
after_create :notify_parent | |
include EmailFunctions | |
def self.add_new(filename) | |
begin | |
MYLOG.info("Parsing #{filename}") | |
f = File.open(filename) | |
content = f.readlines | |
content = content[0] | |
id = content.match(/id (\d+)/)[1].to_i #content.match(/id (\d)+/)[0].gsub("id ",'').to_i | |
type = content.match(/type (\w+?)\)/)[1] | |
parent = content.match(/parent (\d)+/) ? content.match(/parent (\d+)/)[1] : nil | |
by = content.match(/by "(\w+)"/)[1] | |
text = type == 'comment' ? content.match(/text "(.*?)"\)/)[1] : nil | |
create(:story_id => id, | |
:parent => parent, | |
:by => by, | |
:story_type => type, | |
:content => text) | |
rescue Exception => e | |
MYLOG.error e.message | |
end | |
end | |
def notify(comment, already_commented = []) | |
if comment.by != by | |
email = get_email("#{BASE_DIR}/profile/#{by}") | |
send_email(comment, email) unless email.empty? or already_commented.include?(email) | |
already_commented << email | |
end | |
Story.find_by_story_id(parent).notify(comment, already_commented) if parent | |
end | |
private | |
def notify_parent | |
Story.find_by_story_id(parent).notify(self) if parent | |
end | |
end | |
# do a quick pseudo migration. This should only get executed on the first run | |
if !Story.table_exists? | |
ActiveRecord::Base.connection.create_table(:stories) do |t| | |
t.column :story_id, :integer | |
t.column :parent, :integer | |
t.column :by, :string | |
t.column :story_type, :string | |
t.column :content, :text | |
t.column :created_at, :datetime | |
t.column :updated_at, :datetime | |
end | |
Dir.glob("#{BASE_DIR}/story/*").each do |filename| | |
Story.add_new(filename) | |
end | |
end | |
#Will run only on linux | |
notifier = INotify::Notifier.new | |
notifier.watch('/home/rails/arc/arc/news/story', :create) do |event| | |
puts event.name | |
Story.add_new('/home/rails/arc/arc/news/story/' + event.name.gsub('.tmp','')) | |
end | |
notifier.run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment