Skip to content

Instantly share code, notes, and snippets.

@imksoo
Created May 4, 2017 01:21
Show Gist options
  • Save imksoo/6da77e95d49615b157c7ac45c75d1921 to your computer and use it in GitHub Desktop.
Save imksoo/6da77e95d49615b157c7ac45c75d1921 to your computer and use it in GitHub Desktop.
MastodonにYahoo!運行情報をつぶやくだけのbot

MastodonにYahoo!運行情報をつぶやくだけのbot

Ruby Nokogiriの良い練習台になったような気がします。

#!/usr/bin/env ruby

require 'time'
require 'net/http'
require 'uri'
require 'open-uri'
require 'nokogiri'

robot_config = [
    {
        :name => "trainhokkaido",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/2/"
    },
    {
        :name => "traintouhoku",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/3/"
    },
    {
        :name => "trainkanto",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/4/"
    },
    {
        :name => "trainchubu",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/5/"
    },
    {
        :name => "trainkinki",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/6/"
    },
    {
        :name => "trainchugoku",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/8/"
    },
    {
        :name => "trainshikoku",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/9/"
    },
    {
        :name => "trainkyushu",
        :bearer_hash => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        :yahoo_uri => "https://transit.yahoo.co.jp/traininfo/area/7/"
    },
]

robot_config.each do |bot|
  doc = Nokogiri::HTML.parse(open(bot[:yahoo_uri]))
  if doc.css(".elmTblLstLine.trouble a").length == 0 then
    puts "#{Time.now} #{bot[:name]} 事故・遅延情報はありません"
  else
    doc.css(".elmTblLstLine.trouble a").each do |trouble_line|
      line = Nokogiri::HTML.parse(open(trouble_line.attr('href').gsub(/http:/,'https:')))
      name = line.css("h1.title").text
      date = Time.strptime(line.css(".trouble p span").text, '(%m月%d日 %H時%M分')
      text = line.css(".trouble p").text
      puts "#{Time.now} #{bot[:name]} #{name} #{text}"

      if Time.now - 60*5 < date then
        uri = URI.parse('https://mastodon.chotto.moe/api/v1/statuses')
        req = Net::HTTP::Post.new(uri)
        req['Authorization'] = 'Bearer ' + bot[:bearer_hash]
        req.body = "status=[#{name}]#{text}"
        req_options = { use_ssl: true }
        res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
          http.request(req)
        end
        puts "#{Time.now}  toot: code=#{res.code} body=#{res.body}"
      end
    end
  end
  
  doc.css(".icnNormal").each do |normal_line|
    line = Nokogiri::HTML.parse(open(normal_line.parent.parent.css('a').attr('href').text.gsub(/http:/,'https:')))
    name = line.css("h1.title").text
    date = Time.strptime(line.css(".normal p span").text, '(%m月%d日 %H時%M分')
    text = line.css(".normal p").text
    puts "#{Time.now} #{bot[:name]} #{name} #{text}"

    if Time.now - 60*5 < date then
      uri = URI.parse('https://mastodon.chotto.moe/api/v1/statuses')
      req = Net::HTTP::Post.new(uri)
      req['Authorization'] = 'Bearer ' + bot[:bearer_hash]
      req.body = "status=[#{name}]#{text}"
      req_options = { use_ssl: true }
      res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
        http.request(req)
      end
      puts "#{Time.now}  toot: code=#{res.code} body=#{res.body}"
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment