<commit_before>require 'mechanize'
require 'nokogiri'

=begin

 ex. environment.yaml

  OpenPNENewDiaryCheck:
    sleep: 60
    channel: '#Mitten@freenode'
    uri : 'http://openpne.example.com'
    username: 'username'
    password: 'password'

=end
class OpenPNENewDiaryCheck < Mitten::Plugin
  def initialize(*args)
    super

    @uri      = @config['uri']
    @username = @config['username']
    @password = @config['password']
    @diaries = {}
  end

  def before_hook
    login
  end

  def login
    @agent = Mechanize.new
    @agent.get(@uri) do |login_page|
      login_page.form_with(:name => 'login') do |f|
        f.username = @username
        f.password = <PASSWORD>
      end.submit
    end
  end

  def main
    diary_page = @agent.get "#{@uri}/?m=pc&a=page_h_diary_list_all"
    diaries = Nokogiri::HTML(diary_page.body)/'div.item'

    diaries[1...diaries.size].each do |diary|
      uri = "#{@uri}/#{(diary/'td.photo/a').first.attributes['href']}"
      redo if uri == nil or uri == ''

      unless @diaries.has_key? uri
        @diaries[uri] = true
        nick  = ((diary/'td').to_a)[1].text.gsub(/ \(.*\)$/, '')
        title = ((diary/'td').to_a)[2].text.gsub(/ \([0-9].?\)/, '')
        message = "#{nick}さんが「#{title}」を投稿しました！ (#{uri})"

        @channels.each do |channel|
          notice(channel, message) if @diaries.size > 20
        end
      end
    end
  end
end<commit_msg>fix SDBM<commit_after>require 'sdbm'
require 'mechanize'
require 'nokogiri'

=begin

 ex. environment.yaml

  OpenPNENewDiaryCheck:
    sleep: 60
    channel: '#Mitten@freenode'
    uri : 'http://openpne.example.com'
    username: 'username'
    password: 'password'

=end
class OpenPNENewDiaryCheck < Mitten::Plugin
  def initialize(*args)
    super

    @uri      = @config['uri']
    @username = @config['username']
    @password = @config['password']
  end

  def before_hook
    login
  end

  def login
    @agent = Mechanize.new
    @agent.get(@uri) do |login_page|
      login_page.form_with(:name => 'login') do |f|
        f.username = @username
        f.password = <PASSWORD>
      end.submit
    end
  end

  def main
    begin
      db = SDBM.open("/tmp/openpnenewdiarycheck_#{@username}.db")
      diary_page = @agent.get "#{@uri}/?m=pc&a=page_h_diary_list_all"
      diaries = Nokogiri::HTML(diary_page.body)/'div.item'

      diaries[1...diaries.size].each do |diary|
        uri = "#{@uri}/#{(diary/'td.photo/a').first.attributes['href']}"
        redo if uri == nil or uri == ''

        unless db.include? uri
          db[uri] = '1'
          nick  = ((diary/'td').to_a)[1].text.gsub(/ \(.*\)$/, '')
          title = ((diary/'td').to_a)[2].text.gsub(/ \([0-9].?\)/, '')
          message = "#{nick}さんが「#{title}」を投稿しました！ (#{uri})"

          @channels.each do |channel|
            notice(channel, message)
            sleep 5
          end
        end
      end
    ensure
      db.close
    end
  end
end
{'fraction_non_alphanumeric': 0.16922029281579842, 'fraction_numerical': 0.006128702757916241, 'mean_word_length': 2.440281030444965, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2995823', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1064, 'n_words': 204}