<commit_before>#!/usr/bin/env ruby
# encoding: UTF-8
require 'gmail'
require 'yaml'
require 'yo-ruby'

class KapachowYo

    CONFIG = begin
        YAML.load(File.open("config.yml"))
    rescue ArgumentError => e
        puts "Could not parse your config file: #{e.message}"
    end

    # ----

    def initialize
        fail unless CONFIG
        @gmail = nil
        @debug = CONFIG['debug_logging']
        @yo_attempts = 0
        Yo.api_key = CONFIG['yo_api_key']
        check
    end

    def check
        @gmail = Gmail.new(CONFIG['gmail_username'], CONFIG['gmail_password'])
        send_yo if new_subscription_count > 0
        @gmail.logout
    end

    private

    def new_subscription_count
        begin
            unread_emails = @gmail.mailbox(CONFIG['gmail_label']).emails(:unread)
            puts "Found #{unread_emails.size} unread messages" if @debug && unread_emails.size > 0
            unread_emails.each { |email| email.mark(:read) }
            return unread_emails.size
        rescue Net::IMAP::ByeResponseError
            puts "Gmail error. I'll try again next time."
            return 0
        end
    end

    def send_yo
        puts "Yo attempts so far: #{@yo_attempts}" if @debug
        return if @yo_attempts > 5

        link = CONFIG['yo_link']
        begin
            if link
                puts "Sending Yo with link #{link}" if @debug
                Yo.all!({ :link => link })
            else
                puts "Sending Yo with no link" if @debug
                Yo.all!
            end
        rescue YoRateLimitExceeded
            puts "Too many Yos sent. Waiting a minute..." if @debug
            @yo_attempts += 1
            sleep(60)
            send_yo
        end
    end
end

KapachowYo.new
<commit_msg>More error handling.<commit_after>#!/usr/bin/env ruby
# encoding: UTF-8
require 'gmail'
require 'yaml'
require 'yo-ruby'

class KapachowYo

    CONFIG = begin
        YAML.load(File.open("config.yml"))
    rescue ArgumentError => e
        puts "Could not parse your config file: #{e.message}"
    end

    # ----

    def initialize
        fail unless CONFIG
        @gmail = nil
        @debug = CONFIG['debug_logging']
        @yo_attempts = 0
        Yo.api_key = CONFIG['yo_api_key']
        check
    end

    def check
        @gmail = Gmail.new(CONFIG['gmail_username'], CONFIG['gmail_password'])
        send_yo if new_subscription_count > 0
        @gmail.logout
    end

    private

    def new_subscription_count
        begin
            unread_emails = @gmail.mailbox(CONFIG['gmail_label']).emails(:unread)
            puts "Found #{unread_emails.size} unread messages" if @debug && unread_emails.size > 0
            unread_emails.each { |email| email.mark(:read) }
            return unread_emails.size
        rescue Net::IMAP::ByeResponseError
            puts "Gmail ByeResponseError. I'll try again next time."
            return 0
        rescue Net::IMAP::NoResponseError
            puts "Gmail NoResponseError. I'll try again next time."
            return 0
        end
    end

    def send_yo
        puts "Yo attempts so far: #{@yo_attempts}" if @debug
        return if @yo_attempts > 5

        link = CONFIG['yo_link']
        begin
            if link
                puts "Sending Yo with link #{link}" if @debug
                Yo.all!({ :link => link })
            else
                puts "Sending Yo with no link" if @debug
                Yo.all!
            end
        rescue YoRateLimitExceeded
            puts "Too many Yos sent. Waiting a minute..." if @debug
            @yo_attempts += 1
            sleep(60)
            send_yo
        end
    end
end

KapachowYo.new

{'fraction_non_alphanumeric': 0.0926578163099431, 'fraction_numerical': 0.005147656461663506, 'mean_word_length': 1.7634730538922156, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1722731', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1085, 'n_words': 310}