<commit_before>require 'twitter'
require 'yaml'

module Botolo
  module Bot
    class Engine

      def initialize(options={})
        @start_time = Time.now
        @online = false
        @config = read_conf(options[:config])
        $twitter_client = nil
        authenticate if @config['twitter']['enabled']

        @tasks = @config['task']
        @task_pids = []

        behaviour = File.join(".", @config['bot']['behaviour']) unless @config['bot']['behaviour'].nil?

        $logger.helo name, version
        $logger.log "#{@tasks.size} tasks loaded"

        begin
          load behaviour
          $logger.log "using #{behaviour} as bot behaviour"
          @behaviour = Botolo::Bot::Behaviour.new(@config)
        rescue => e
          $logger.err(e.message)
          require 'botolo/bot/behaviour'
          $logger.log "reverting to default dummy behaviour"
          @behaviour = Botolo::Bot::Behaviour.new(@config)
        end
      end

      def calc_sleep_time(task)
        s = /every (\d) (s|m|h|d|w|y)/.match task

        return 300 if s.nil? # safe fallback is 5 minutes sleeping

        return s[1].to_i                         if s[2] == 's'
        return s[1].to_i * 60                    if s[2] == 'm'
        return s[1].to_i * 60 * 60               if s[2] == 'h'
        return s[1].to_i * 60 * 60 * 24          if s[2] == 'd'
        return s[1].to_i * 60 * 60 * 24 * 7      if s[2] == 'w'
        return s[1].to_i * 60 * 60 * 24 * 7 * 52 if s[2] == 'y'

      end

      def run
        $logger.log "entering main loop"
        @tasks.each do |task|
          @task_pids << Thread.start do
            start_task(task['action'], task['schedule'])
          end
        end
      end

      def infinite_loop
        loop do
          sleep(3600) # => 1 h
          $logger.log " --- mark --- "
        end
      end

      def start_task(name, sleep)
        while true
          begin 
            @behaviour.send(name.to_sym) if @behaviour.respond_to? name.to_sym
          rescue => e
            $logger.err "#{name} failed (#{e.message})"
          end
          sleep calc_sleep_time(sleep)
        end

      end

      def stop
        $logger.log "shutting down threads"
        @task_pids.each do |pid|
          Thread.kill(pid)
          sleep 0.5
          $logger.log "pid #{pid} killed" if ! pid.alive? 
          $logger.err "pid #{pid} not killed" if pid.alive? 
        end

        true
      end

      def authenticate
        begin
          $twitter_client = Twitter::REST::Client.new do |config|
            config.consumer_key         = @config['twitter']['consumer_key']
            config.consumer_secret      = @config['twitter']['consumer_secret']

            # FIXME: This config is deprecated and it will be soon be removed
            $logger.warn("Please note that oauth_token_secret and oauth_token config keys are deprecated.  Use access_token_secret and access_token instead")
            config.access_token         = @config['twitter']['oauth_token']         unless @config['twitter']['oauth_token'].nil?
            config.access_token_secret  = @config['twitter']['oauth_token_secret']  unless @config['twitter']['oauth_token_secret'].nil?

            config.access_token         = @config['twitter']['access_token']        unless @config['twitter']['access_token'].nil?
            config.access_token_secret  = @config['twitter']['access_token_secret'] unless @config['twitter']['access_token_secret'].nil?
          end
          @online = true
        rescue Exception => e
          $logger.err e.message
        end
      end

      def online?
        @online
      end

      def name
        return @config['bot']['name']
      end
      def version
        return @config['bot']['version']
      end

      def uptime
        Time.now - @start_time
      end

      def read_conf(filename=nil)
        return {} if filename.nil? or ! File.exist?(filename) 
        return YAML.load_file(filename)
      end

    end
  end
end
<commit_msg>Twitter authentication is now up to the Tweet API. Behaviour file now is loaded form the same directory where the config file is.<commit_after>require 'yaml'

module Botolo
  module Bot
    class Engine

      def initialize(options={})
        @start_time = Time.now
        @online = false
        @config = read_conf(options[:config])
        $twitter_api = nil

        behaviour_path = File.dirname(options[:config])

        if @config['twitter']['enabled']
          $twitter_api = Botolo::API::Tweet.instance
          $twitter_api.authenticate(@config['twitter']) 
        end

        @tasks = @config['task']
        @task_pids = []

        behaviour = File.join(behaviour_path, @config['bot']['behaviour']) unless @config['bot']['behaviour'].nil?

        $logger.helo name, version
        $logger.log "#{@tasks.size} tasks loaded"

        begin
          load behaviour
          $logger.log "using #{behaviour} as bot behaviour"
          @behaviour = Botolo::Bot::Behaviour.new(@config)
        rescue => e
          $logger.err(e.message)
          require 'botolo/bot/behaviour'
          $logger.log "reverting to default dummy behaviour"
          @behaviour = Botolo::Bot::Behaviour.new(@config)
        end
      end

      def calc_sleep_time(task)
        s = /every (\d) (s|m|h|d|w|y)/.match task

        return 300 if s.nil? # safe fallback is 5 minutes sleeping

        return s[1].to_i                         if s[2] == 's'
        return s[1].to_i * 60                    if s[2] == 'm'
        return s[1].to_i * 60 * 60               if s[2] == 'h'
        return s[1].to_i * 60 * 60 * 24          if s[2] == 'd'
        return s[1].to_i * 60 * 60 * 24 * 7      if s[2] == 'w'
        return s[1].to_i * 60 * 60 * 24 * 7 * 52 if s[2] == 'y'

      end

      def run
        $logger.log "entering main loop"
        @tasks.each do |task|
          @task_pids << Thread.start do
            start_task(task['action'], task['schedule'])
          end
        end
      end

      def infinite_loop
        loop do
          sleep(3600) # => 1 h
          $logger.log " --- mark --- "
        end
      end

      def start_task(name, sleep)
        while true
          begin 
            @behaviour.send(name.to_sym) if @behaviour.respond_to? name.to_sym
          rescue => e
            $logger.err "#{name} failed (#{e.message})"
          end
          sleep calc_sleep_time(sleep)
        end

      end

      def stop
        $logger.log "shutting down threads"
        @task_pids.each do |pid|
          Thread.kill(pid)
          sleep 0.5
          $logger.log "pid #{pid} killed" if ! pid.alive? 
          $logger.err "pid #{pid} not killed" if pid.alive? 
        end

        true
      end

      def online?
        @online
      end

      def name
        return @config['bot']['name']
      end
      def version
        return @config['bot']['version']
      end

      def uptime
        Time.now - @start_time
      end

      def read_conf(filename=nil)
        return {} if filename.nil? or ! File.exist?(filename) 
        return YAML.load_file(filename)
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.11975463543844975, 'fraction_numerical': 0.014219991635299039, 'mean_word_length': 1.7624181748170966, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '4789485', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2102, 'n_words': 561}