<commit_before>require 'rubygems'
require 'yaml'
require 'feed-normalizer'
require 'open-uri'
require 'haml'

module Audrey2

  class Aggregator
    def initialize(configfile)
      init_config(configfile)
    end

    def feed_me(recipe_name)
      begin
        # Load recipe and theme and make sure everything is in order
        recipe = load_recipe(recipe_name)
        init_theme(recipe['theme'])
        output_file = recipe['output_file']
        verify_output_file(output_file)
        max_entries = recipe['max_entries'] || 1000

        # Download and parse the feeds
        entry_sources = {}
        feeds = recipe['feeds'].collect { |feed| parse_feed(feed, entry_sources) }

        # Aggregate and sort the entries
        entries = []
        feeds.each { |feed| entries += feed.entries }
        sort!(entries)

        # Prepare template evaluation scope including any helper code defined in the theme
        scope = Object.new
        scope.instance_eval(@helper_code) if @helper_code

        # Output the aggregated entries
        output = ''
        engine ||= Haml::Engine.new(@entry_template)

        entries[0..max_entries - 1].each do |entry|
          output << engine.render(scope, :entry => entry, :source => entry_sources[entry])
        end

        File.open(output_file, 'w') { |f| f << output }

      rescue Exception => e
        $stderr.puts "An exception occurred while running recipe #{recipe_name}:\n\n#{e}\n#{e.backtrace}"
        if @email
          email(<<-EOF
An exception occurred while running recipe #{recipe_name}

Exception: #{e}

Backtrace:

#{e.backtrace}
EOF
          )
        else
          exit(1)
        end
      end
    end

  protected
    def email(e)
      return unless @email

      mail = Mail.new
      mail[:from] =    @email['from']
      mail[:to] =      @email['to']
      mail[:subject] = "[AUDREY 2.0] Exception Notification"
      mail[:body]    = e

      case @email['via']
      when 'sendmail'
        mail.delivery_method :sendmail
      when 'smtp'
        raise "Missing SMTP configuration" unless @email['smtp']
        smtp = {
          :address        => @email['smtp']['server']         || 'localhost',
          :port           => @email['smtp']['port']           || 25
        }<commit_msg>Eliminated output to stderr on exceptions if email notification is enabled. This improves behavior when running under cron.<commit_after>require 'rubygems'
require 'yaml'
require 'feed-normalizer'
require 'open-uri'
require 'haml'

module Audrey2

  class Aggregator
    def initialize(configfile)
      init_config(configfile)
    end

    def feed_me(recipe_name)
      begin
        # Load recipe and theme and make sure everything is in order
        recipe = load_recipe(recipe_name)
        init_theme(recipe['theme'])
        output_file = recipe['output_file']
        verify_output_file(output_file)
        max_entries = recipe['max_entries'] || 1000

        # Download and parse the feeds
        entry_sources = {}
        feeds = recipe['feeds'].collect { |feed| parse_feed(feed, entry_sources) }

        # Aggregate and sort the entries
        entries = []
        feeds.each { |feed| entries += feed.entries }
        sort!(entries)

        # Prepare template evaluation scope including any helper code defined in the theme
        scope = Object.new
        scope.instance_eval(@helper_code) if @helper_code

        # Output the aggregated entries
        output = ''
        engine ||= Haml::Engine.new(@entry_template)

        entries[0..max_entries - 1].each do |entry|
          output << engine.render(scope, :entry => entry, :source => entry_sources[entry])
        end

        File.open(output_file, 'w') { |f| f << output }

      rescue Exception => e
        if @email
          email(<<-EOF
An exception occurred while running recipe #{recipe_name}

Exception: #{e}

Backtrace:

#{e.backtrace}
EOF
          )
        else
          $stderr.puts "An exception occurred while running recipe #{recipe_name}:\n\n#{e}\n#{e.backtrace}"
          exit(1)
        end
      end
    end

  protected
    def email(e)
      return unless @email

      mail = Mail.new
      mail[:from] =    @email['from']
      mail[:to] =      @email['to']
      mail[:subject] = "[AUDREY 2.0] Exception Notification"
      mail[:body]    = e

      case @email['via']
      when 'sendmail'
        mail.delivery_method :sendmail
      when 'smtp'
        raise "Missing SMTP configuration" unless @email['smtp']
        smtp = {
          :address        => @email['smtp']['server']         || 'localhost',
          :port           => @email['smtp']['port']           || 25
        }
{'fraction_non_alphanumeric': 0.11201368106028217, 'fraction_numerical': 0.005130397605814451, 'mean_word_length': 2.267458100558659, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2523654', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1336, 'n_words': 365}