Created
June 11, 2012 17:09
-
-
Save katsuma/2911347 to your computer and use it in GitHub Desktop.
Auto zapping
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
require 'rubygems' | |
require 'socket' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'growl' | |
require 'ruby-debug' | |
module PopZap | |
LIVE_URL = 'http://tv2ch.nukos.net/tvres.html' | |
IGNORE_KEYWORDS = ['BS実況', 'スカパー', '番組'] | |
def channel_config | |
{ 'NHK総合' => 1201, 'NHK教育' => 1202, '日本テレビ' => 1204, | |
'テレビ朝日' => 1205, 'TBSテレビ' => 1206, 'テレビ東京' => 1207, | |
'フジテレビ' => 1208 | |
} | |
end | |
def remocon_config | |
{ :ip => '192.168.0.9', :port => 51013 } | |
end | |
def popular_channels | |
channels = [] | |
doc = Nokogiri::HTML(open(LIVE_URL)) | |
trs = doc.css('table.table3 tr') | |
trs.each do |tr| | |
tds = tr.css('td') | |
next unless tds.size > 0 | |
channel = tds[0].inner_text.gsub("\n", '').gsub('の勢い', '') | |
rate = tds[1].inner_text.gsub("\n", '').gsub('res/分', '').to_i | |
program = tds[3].inner_text | |
matches = channel.match(/#{IGNORE_KEYWORDS.join('|')}/) | |
next unless matches.nil? | |
channels << { :channel => channel, :rate => rate, :program => program } | |
end | |
channels.sort {|a, b| a[:rate] <=> b[:rate] }.reverse | |
end | |
def show(channel) | |
channel_id = channel_config[channel] | |
raise if channel_id.nil? | |
sock = TCPSocket.new remocon_config[:ip], remocon_config[:port] | |
sock.write "*is;#{channel_id}\r\n" | |
sock.close | |
end | |
def start | |
prev_channel = '' | |
loop do | |
popular_channel = popular_channels.first | |
unless prev_channel == popular_channel | |
Growl.notify "#{popular_channel[:program]} - #{popular_channel[:channel]}" | |
show popular_channel[:channel] | |
prev_channel = popular_channel | |
end | |
sleep 300 | |
end | |
end | |
end | |
include PopZap | |
start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment