Skip to content

Instantly share code, notes, and snippets.

@authorNari
Created September 5, 2013 01:47
Show Gist options
  • Save authorNari/6445090 to your computer and use it in GitHub Desktop.
Save authorNari/6445090 to your computer and use it in GitHub Desktop.
気になる動画の変化を検知してメールしてくれるやつ
source 'https://rubygems.org'
gem 'sqlite3', "1.3.7"
gem 'activerecord', "3.2.13"
gem 'nokogiri', "1.5.9"
gem "gmail-mailer", "0.4.5"
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "bundler/setup"
require "open-uri"
require 'time'
require 'yaml'
require 'nkf'
require 'active_record'
require 'nokogiri'
require 'gmail-mailer'
require 'sqlite3'
require 'logger'
@base_dir = File.dirname(__FILE__)
# テーブル作成
ActiveRecord::Base.establish_connection(
"adapter"=>"sqlite3",
"database" => File.join(@base_dir, "data.sqlite")
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
class CreateMovie < ActiveRecord::Migration
def self.up
create_table :movies do|t|
t.string :smid
t.string :title
t.string :tags
t.integer :view_counter, default: 0
t.integer :comment_num, default: 0
t.integer :mylist_counter, default: 0
t.time :first_retrieve
t.string :watch_url
t.string :last_res_body
end
end
end
begin
CreateMovie.new.up
rescue => ex
puts ex.message
end
class Movie < ActiveRecord::Base
def self.fetch(smid)
instance = self.find_or_create_by_smid(smid)
url = "http://ext.nicovideo.jp/api/getthumbinfo/#{smid}"
puts("FETCH -> #{url}")
doc = Nokogiri::XML(open(url)).css("nicovideo_thumb_response")
instance.attributes = {
title: doc.css('title').text,
smid: smid,
tags: doc.css("tag").map{|t| t.text }.sort.join(","),
view_counter: doc.css("view_counter").text.to_i,
comment_num: doc.css("comment_num").text.to_i,
mylist_counter: doc.css("mylist_counter").text.to_i,
first_retrieve: Time.parse(doc.css("first_retrieve").text),
watch_url: doc.css("watch_url").text,
last_res_body: doc.css("last_res_body").text,
}
return instance
end
def report_diff(m)
report = []
if tags != m.tags
report << " new tags: " + (m.tags.split(",") - (tags || []).split(",")).join(",")
end
if view_counter < m.view_counter
report << " view: +#{m.view_counter - view_counter}"
end
if comment_num < m.comment_num
report << " comment: +#{m.comment_num - comment_num}"
report << " #{m.last_res_body}"
end
if mylist_counter < m.mylist_counter
report << " mylist: +#{m.mylist_counter - mylist_counter}"
end
report.compact!
if report.empty?
return nil
else
report.unshift("- #{m.title}: #{m.watch_url}")
return report.join("\n")
end
end
def sm_thread_id
first_retrieve.to_i.to_s
end
end
def sendgmail(subject, body)
config = NREPO_CONFIG
message = GmailMailer::Message.new(config["email"], subject, body)
mailer = GmailMailer::Mailer.new(
smtp_oauth_token: config["smtp_oauth_token"],
smtp_oauth_token_secret: config["smtp_oauth_token_secret"],
email: config["email"],
)
mailer.send(message)
end
NREPO_CONFIG = YAML.load_file(File.join(@base_dir, "setting.yml"))
@check_movies = NREPO_CONFIG["movies"]
body = []
@check_movies.each do |smid|
new = Movie.fetch(smid)
old = Movie.where(smid: smid).first
old.first_retrieve = new.first_retrieve
body << old.report_diff(new)
new.save!
end
body.compact!
if not body.empty?
sendgmail("【nrepo】動画の更新情報", body.join("\n\n"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment