ruby CHAN-TORU https://tv.so-net.ne.jp/chan-toru/から录画済みリストを取得し,新着を亚马逊SESでお知らせ。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby CHAN-TORU https://tv.so-net.ne.jp/chan-toru/から录画済みリストを取得し,新着を亚马逊SESでお知らせ。相关的知识,希望对你有一定的参考价值。

#encoding: UTF-8
require 'mechanize'
require 'aws-sdk'
require 'json'
require 'kconv'
require 'eventmachine'
require 'logger'

# Sony Entertainment Network Account
ID = ''
PASS = ''

# Email settings
TO_EMAIL   = ''
FROM_EMAIL = ''

class ChanToru_Notifier
  def initialize
    @file_path = 'recorded_list.json'

    # Mechanize
    @a = Mechanize.new {|agent|
      agent.user_agent_alias = 'Mac Safari'
    }

    # Logger
    @l = Logger.new('log', 'daily')
    @l.info('Program started')
  end

  def log_info str
    @l.info(str)
  end

  def log_warn str
    @l.warn(str)
  end

  def save_file dat
    File.open(@file_path, 'w') do |f|
      f.write dat.to_json
    end
  end

  def load_file
    JSON.restore(File.open(@file_path, 'r').read)
  end

  def latest_list
    # Get latest recorded list.
    uri = 'https://tv.so-net.ne.jp/chan-toru/list?index=0&num=10&command=title'
    page = @a.get(uri)
    # Extract title data
    new_title_list = JSON.restore(page.content.to_s)['list'].to_a.collect do |d|
      "#{d['title']}"
    end
    if new_title_list.nil? then
      log_warn 'Faild to get recorded list.'
      return
    end

    # data load & save
    old_title_list = load_file
    save_file new_title_list
    return new_title_list - old_title_list # return list of differential
  end

  def login id, pass
    # Login ID/PASS
    uri = 'https://account.sonyentertainmentnetwork.com/external/auth/login.action?returnURL=https://tv.so-net.ne.jp/chan-toru/sen'
    @a.get(uri) do |page|
      res = page.form_with(id: 'signInForm') do |f|
        f.field_with(name: 'j_username').value = id
        f.field_with(name: 'j_password').value = pass
      end.submit
    end
  end

  def format_mail_body dat, opt=:text
    case opt
    when :text then
      dat.inject(""){|body, x| body += "・#{x}\n"}
    when :html then
      dat.inject(""){|body, x| body += "・#{x}<br />"}
    end
  end

  def send_email opt
    AWS.config(
      access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
    )
    ses = AWS::SimpleEmailService.new

    # format:
    # titleA => ・titleA
    body_text = format_mail_body opt[:body_text], :text
    body_html = format_mail_body opt[:body_html], :html

    # send
    ses.send_email(
      subject: '新着タイトルのお知らせ',
      to: opt[:to_email],
      from: opt[:from_email],
      body_text: body_text,
      body_html: body_html,
      body_text_charset: 'UTF-8'
    )
    log_info 'The message has been send.'
  end

  # Get recorded list
  def get_recorded_list
    return 'Faild to log in.' if (login(ID, PASS)).nil?
    res = latest_list
    unless res.size == 0 then
      # set up mail send
      to_email = TO_EMAIL
      from_email = FROM_EMAIL

      send_email({body_text: res, body_html: res, to_email: to_email, from_email: from_email})
    else
      log_info 'No new recorded list yet.'
    end
  end
end

# Run
c = ChanToru_Notifier.new
c.get_recorded_list

EM.run do
  EM.add_periodic_timer(60 * 60){ # 60sec * 60, Hourly
    c = ChanToru_Notifier.new
    begin
      c.get_recorded_list
    rescue Mechanize::ResponseCodeError => e
      case e.response_code
      when '401' then
        @l.warn('Got HTTP status error code 401')
      when '404' then
        @l.warn('Got HTTP status error code 404')
      when '503' then
        @l.warn('Got HTTP status error code 503')
      end
    end
  }
end

以上是关于ruby CHAN-TORU https://tv.so-net.ne.jp/chan-toru/から录画済みリストを取得し,新着を亚马逊SESでお知らせ。的主要内容,如果未能解决你的问题,请参考以下文章

英语演讲Discover yourself

双11期间,请关爱程序员!

2022央视春晚《这世界那么多人》 演唱:韩红,2022年不易

ruby [Ruby Cheat] Cheatsheet #ruby

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速