<commit_before>require 'sinatra'
require 'twitter'
require 'chartkick'
require 'sinatra/flash'

enable :sessions

get '/' do
  @title = 'Search'
  erb :home
end

post '/profile' do			
		
  @client = Twitter::REST::Client.new do |config|
    # Use Heroku environment variables to authenticate to the Twitter API
    config.consumer_key = ENV['key']
    config.consumer_secret = ENV['secret']		
  end

  @title='Search'
  
  # Log the search (also appears in papertrail) and user-agent
  puts 'Tripper::Search/'+params[:content]
  puts 'User-agent::'+request.user_agent

  begin
    #create the user and show the profile page
    @user = @client.user(params[:content])     
    
    # if the search is successful, print a message
    puts 'Tripper::User exists'

    # create instance variables for the chartkick charts
    @tweetsource ||= Hash.new(0)
    @retweet_total = 0
    @num_tweets = 0
    @tweetday ||= {"Sunday" => 0, "Monday" => 0, "Tuesday" => 0,
                "Wednesday" => 0, "Thursday" => 0, "Friday" => 0,
                "Saturday" => 0}
    @followtz ||= Hash.new(0)
    sourcedays = Hash.new {|h,k| h[k] = []}
    @tweetline = Hash.new(0)

    #note that the user_timeline method returns the 20 most recent Tweets 
    #posted by the specified user
    @timeline = @client.user_timeline(@user.screen_name, :count => 200)
    @timeline.each do |t|
      @num_tweets += 1
      @retweet_total += t.retweet_count
      date = Date.parse(t.created_at.to_s[0..9])
      @tweetday[date.strftime('%A')] += 1
  
      # use a regular expression to get the text enclosed by 
      # >< in the twitter source, then remove the first and last chars
      src = />(.*)</.match(t.source)[0][1..-2]
      @tweetsource[src] += 1
      sourcedays[src] << date.strftime('%Y-%m-%d')
      @tweetline[date] += 1
      
    end
    
  #format the sourcedays data
  @sourceplots = Hash.new {|h,k| h[k] = Hash.new(0)}
  sourcedays.each do |source, data|
    data.each do |date|
      @sourceplots[source][date] += 1
    end
  end
  
  erb :profile

  rescue Twitter::Error::NotFound => e
    puts 'Tripper::NotFound/'+e.message
    flash[:error] = e.message
    redirect '/'
  rescue Twitter::Error::Forbidden => e
    puts 'Tripper::Forbidden/'+e.message
    flash[:error] = e.message
    redirect '/'
  end<commit_msg>Catch exception for protected accounts<commit_after>require 'sinatra'
require 'twitter'
require 'chartkick'
require 'sinatra/flash'

enable :sessions

get '/' do
  @title = 'Search'
  erb :home
end

post '/profile' do			
		
  @client = Twitter::REST::Client.new do |config|
    # Use Heroku environment variables to authenticate to the Twitter API
    config.consumer_key = ENV['key']
    config.consumer_secret = ENV['secret']		
  end

  @title='Search'
  
  # Log the search and user-agent (also appears in papertrail) 
  puts 'Tripper::Search/'+params[:content]
  puts 'Tripper::User-agent/'+request.user_agent

  begin
    #create the user and show the profile page
    @user = @client.user(params[:content])     
    
    # if the search is successful, print a message
    puts 'Tripper::User exists'

    # create instance variables for the chartkick charts
    @tweetsource ||= Hash.new(0)
    @retweet_total = 0
    @num_tweets = 0
    @tweetday ||= {"Sunday" => 0, "Monday" => 0, "Tuesday" => 0,
                "Wednesday" => 0, "Thursday" => 0, "Friday" => 0,
                "Saturday" => 0}
    @followtz ||= Hash.new(0)
    sourcedays = Hash.new {|h,k| h[k] = []}
    @tweetline = Hash.new(0)

    #note that the user_timeline method returns the 20 most recent Tweets 
    #posted by the specified user
    @timeline = @client.user_timeline(@user.screen_name, :count => 200)
    @timeline.each do |t|
      @num_tweets += 1
      @retweet_total += t.retweet_count
      date = Date.parse(t.created_at.to_s[0..9])
      @tweetday[date.strftime('%A')] += 1
  
      # use a regular expression to get the text enclosed by 
      # >< in the twitter source, then remove the first and last chars
      src = />(.*)</.match(t.source)[0][1..-2]
      @tweetsource[src] += 1
      sourcedays[src] << date.strftime('%Y-%m-%d')
      @tweetline[date] += 1
      
    end
    
  #format the sourcedays data
  @sourceplots = Hash.new {|h,k| h[k] = Hash.new(0)}
  sourcedays.each do |source, data|
    data.each do |date|
      @sourceplots[source][date] += 1
    end
  end
  
  erb :profile

  rescue Twitter::Error::NotFound => e
    puts 'Tripper::NotFound/'+e.message
    flash[:error] = e.message
    redirect '/'
  rescue Twitter::Error::Forbidden => e
    puts 'Tripper::Forbidden/'+e.message
    flash[:error] = e.message
    redirect '/'
  rescue Twitter::Error::Unauthorized => e
    puts 'Tripper::Unauthorized/'+e.message
    flash[:error] = 'Account protected (not authorized).'
    redirect '/'
  end
{'fraction_non_alphanumeric': 0.14149772445179976, 'fraction_numerical': 0.011584609019445594, 'mean_word_length': 2.9086499595796282, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '838442', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1633, 'n_words': 436}