<commit_before>#!/opt/local/bin/ruby
require 'rubygems'
require 'twitter'
require 'tlsmail'
require 'optparse'

SEC_PER_HOUR = 3600 # Seconds per hour
FUZZ = 5 # Fuzz for ":since => now - ..."; makes sure we don't miss the odd twit

class TwitterSms

  def initialize(config_file="#{ENV['HOME'] + '/.twitter-sms.conf'}", args=ARGV)
    load_config(config_file)
    load_opts(args)

    # Required for gmail smtp (and not a part of standalone Net::SMTP)
    Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
  end

  # Run the twitter-sms bot once or repeatedly, depending on config
  def run
    begin # do-while
      load_config if config_stale?

      tweets = update
      send tweets unless tweets.nil?

      Kernel.sleep @config['wait'] if @config['keep_alive']
    end while @config['keep_alive']
  end

  # Collect a list of recent tweets on a user's timeline (that are not their own)
  def update
    twitter = Twitter::Base.new(@user['name'],@user['password'])
    if twitter.rate_limit_status.remaining_hits > 0
      # For :since the 'FUZZ' is a few seconds used to try and keep missing seconds from
      # cropping up. Consequently it may send duplicate messages
      tweets = twitter.timeline(:friends, :since => Time.now - @config['wait'] - FUZZ)

      # Block own tweets if specified via settings
      tweets.reject! {|t| t.user.screen_name == @user['name']} unless @config['own_tweets']
      tweets.reverse! # reverse-chronological
    else
      puts "Your account has run out of API calls; call not made."
    end
  end

  # Email via Gmail SMTP any tweets to the desired cell phone
  def send(tweets)
    puts "Sending received tweets..."
    Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @bot['email'], "tweeterbot", :login) do |smtp|
      tweets.each do |tweet|
        smtp.send_message(content(tweet),@bot['email'],@user['phone'])
        puts "\tSent: #{tweet.user.screen_name}: #{tweet.text[0..20]}..."
      end
    end
    puts "Messages sent."
  end

  # Parse and store a config file (either as an initial load or as an update)
  def load_config(config_file=@config_file['name'])
    # Load config file -- Add try block here
    config = YAML.load_file(config_file)
    @config_file = { 'name' => config_file,
              'modified_at' => File.mtime(config_file) }


    # Seperate config hashes into easier to use parts
    @user = config['user']
    @bot = config['bot']
    # Merge specified config onto defaults
    @config = { 'own_tweets' => false,
                'keep_alive' => true,
                  'per_hour' => 60}.merge(config['config'])
<commit_msg>Added a few rescues for failing internet actions<commit_after>#!/opt/local/bin/ruby
require 'rubygems'
require 'twitter'
require 'tlsmail'
require 'optparse'

SEC_PER_HOUR = 3600 # Seconds per hour
FUZZ = 5 # Fuzz for ":since => now - ..."; makes sure we don't miss the odd twit

class TwitterSms

  def initialize(config_file="#{ENV['HOME'] + '/.twitter-sms.conf'}", args=ARGV)
    load_config(config_file)
    load_opts(args)

    # Required for gmail smtp (and not a part of standalone Net::SMTP)
    Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
  end

  # Run the twitter-sms bot once or repeatedly, depending on config
  def run
    begin # do-while
      load_config if config_stale?

      tweets = update
      send tweets unless tweets.nil?



      if @config['keep_alive']
        puts "Waiting for #{@config['wait']/60.0} minutes..."
        Kernel.sleep @config['wait']
      end
    end while @config['keep_alive']
  end

  # Collect a list of recent tweets on a user's timeline (that are not their own)
  def update
    twitter = Twitter::Base.new(@user['name'],@user['password'])
    begin
      if twitter.rate_limit_status.remaining_hits > 0
        # For :since the 'FUZZ' is a few seconds used to try and keep missing seconds from
        # cropping up. Consequently it may send duplicate messages

        tweets = twitter.timeline(:friends, :since => Time.now - @config['wait'] - FUZZ)
        # Block own tweets if specified via settings
        tweets.reject! {|t| t.user.screen_name == @user['name']} unless @config['own_tweets']
        tweets.reverse! # reverse-chronological
      else
        puts "Your account has run out of API calls; call not made."
      end
    rescue
      puts "Error occured retreiving timeline. Perhaps Internet is down?"
    end
  end

  # Email via Gmail SMTP any tweets to the desired cell phone
  def send(tweets)
    puts "Sending received tweets..."
    begin
      Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @bot['email'], "tweeterbot", :login) do |smtp|
        tweets.each do |tweet|
          smtp.send_message(content(tweet),@bot['email'],@user['phone']) rescue puts "Error occured sending message:"
          puts "\tSent: #{tweet.user.screen_name}: #{tweet.text[0..20]}..."
        end
      end
      puts "Messages sent."
    rescue
      puts "Error occured starting smtp. Perhaps account info is incorrect or Internet is down?"
    end
  end

  # Parse and store a config file (either as an initial load or as an update)
  def load_config(config_file=@config_file['name'])
    # Load config file -- Add try block here
    config = YAML.load_file(config_file)
    @config_file = { 'name' => config_file,
              'modified_at' => File.mtime(config_file) }


    # Seperate config hashes into easier to use parts
    @user = config['user']
    @bot = config['bot']
    # Merge specified config onto defaults
    @config = { 'own_tweets' => false,
                'keep_alive' => true,
                  'per_hour' => 60}.merge(config['config'])

{'fraction_non_alphanumeric': 0.11948236128346038, 'fraction_numerical': 0.0054954795249069315, 'mean_word_length': 3.041547277936963, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 19, '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': '2680146', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1759, 'n_words': 560}