<commit_before># encoding: utf-8

require 'net/irc'
require 'hatena/bookmark'

module InterestIrcGateway
  class Session < Net::IRC::Server::Session
    def server_name
      :InterestIrcGateway
    end

    def server_version
      InterestIrcGateway::VERSION
    end

    def initialize(*args)
      super
      @hatebu = Hatena::Bookmark.new
      @bookmarks = []
      @users = []
    end

    def main_channel
      '#interest'
    end

    def on_user(message)
      super
      @hatebu.login(@real, @pass)

      post(@nick, JOIN, main_channel)

      Thread.start do
        loop do
          @log.info('monitoring bookmarks...')
          monitoring(main_channel)
          @log.info('sleep 300 seconds')
          sleep 300
        end
      end
    rescue => e
      @log.error(e.to_s)
    end

    def on_disconnected
      Thread.list.each do |thread|
        thread.kill
      end
    end

    private
      def monitoring(channel)
        @hatebu.interests(@real).each do |interest, entries|
          unless @users.include?(interest)
            post(interest, JOIN, channel)
            @users << interest
          end

          entries.each do |entry|
            url = entry[:url]
            next if @bookmarks.include?(url)

            privmsg(interest, channel, "#{entry[:title]} #{url} (#{entry[:users]})")
            @bookmarks << url
          end
        end
      rescue Exception => e
        @log.error(e.inspect)
        e.backtrace.each { |l| @log.error "\t#{l}" }
        sleep 300
      end

      def privmsg(nick, channel, message)
        post(nick, PRIVMSG, channel, message)
      end
  end
end<commit_msg>fix failed kill thread when disconnect<commit_after># encoding: utf-8

require 'net/irc'
require 'hatena/bookmark'

module InterestIrcGateway
  class Session < Net::IRC::Server::Session
    def server_name
      :InterestIrcGateway
    end

    def server_version
      InterestIrcGateway::VERSION
    end

    def initialize(*args)
      super
      @hatebu = Hatena::Bookmark.new
      @bookmarks = []
      @users = []
    end

    def main_channel
      '#interest'
    end

    def on_user(message)
      super
      @hatebu.login(@real, @pass)

      post(@nick, JOIN, main_channel)

      @monitoring_thread = Thread.start do
        loop do
          @log.info('monitoring bookmarks...')
          monitoring(main_channel)
          @log.info('sleep 300 seconds')
          sleep 300
        end
      end
    rescue => e
      @log.error(e.to_s)
    end

    def on_disconnected
      @monitoring_thread.kill rescue nil
    end

    private
      def monitoring(channel)
        @hatebu.interests(@real).each do |interest, entries|
          unless @users.include?(interest)
            post(interest, JOIN, channel)
            @users << interest
          end

          entries.each do |entry|
            url = entry[:url]
            next if @bookmarks.include?(url)

            privmsg(interest, channel, "#{entry[:title]} #{url} (#{entry[:users]})")
            @bookmarks << url
          end
        end
      rescue Exception => e
        @log.error(e.inspect)
        e.backtrace.each { |l| @log.error "\t#{l}" }
        sleep 300
      end

      def privmsg(nick, channel, message)
        post(nick, PRIVMSG, channel, message)
      end
  end
end
{'fraction_non_alphanumeric': 0.1057866184448463, 'fraction_numerical': 0.006027727546714889, 'mean_word_length': 1.9063047285464099, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '2179521', 'n_tokens_mistral': 1023, 'n_tokens_neox': 993, 'n_words': 243}