<commit_before># encoding: utf-8

require 'weibo_2'
require 'time-ago-in-words'
require 'json'
require 'mongo'

%w(rubygems bundler).each { |dependency| require dependency }
Bundler.setup
%w(sinatra haml sass).each { |dependency| require dependency }
enable :sessions

WeiboOAuth2::Config.api_key = ENV['KEY']
WeiboOAuth2::Config.api_secret = ENV['SECRET']
WeiboOAuth2::Config.redirect_uri = ENV['REDIR_URI']

get '/' do
  client = WeiboOAuth2::Client.new
  if session[:access_token] && !client.authorized?
    token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
    # p "*" * 80 + "validated"
    # p token.inspect
    # p token.validated?

    unless token.validated?
      reset_session
      redirect '/connect'
      return
    end
  end
  if session[:uid]
    @user = client.users.show_by_uid(session[:uid])
    @statuses = client.statuses
  end
  haml :index
end

get '/connect' do
  client = WeiboOAuth2::Client.new
  redirect client.authorize_url
end

get '/callback' do
  client = WeiboOAuth2::Client.new
  access_token = client.auth_code.get_token(params[:code].to_s)
  p "^" + params[:code]
  session[:uid] = access_token.params["uid"]
  session[:access_token] = access_token.token
  # p "#" * 80 + access_token.token
  session[:expires_at] = access_token.expires_at
  # p access_token.inspect
  @user = client.users.show_by_uid(session[:uid].to_i)
  redirect '/'
end

get '/logout' do
  reset_session
  redirect '/'
end

get '/screen.css' do
  content_type 'text/css'
  sass :screen
end

# add /statuses interface to show latest statuses as the json output
get '/statuses' do
  client = WeiboOAuth2::Client.new

  # Check the session and get the token obj
  if session[:access_token] && !client.authorized?
    token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
    unless token.validated?
      reset_session
      redirect '/connect'
      return
    end
  end

  # Get the latest statuses timeline
  if session[:uid]
    @user = client.users.show_by_uid(session[:uid])
    @statuses = client.statuses
  end

  coll = Mongo::Connection.new('localhost', 27017).db('weibo')['statuses']

  @statuses.friends_timeline.statuses.each { |status| coll.insert(status) }

  coll.find.each { |row| puts row.inspect }
  
end

post '/update' do
  client = WeiboOAuth2::Client.new
  client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})<commit_msg>make conditional query by since_id for /statuses interface<commit_after># encoding: utf-8

require 'weibo_2'
require 'time-ago-in-words'
require 'json'
require 'mongo'

%w(rubygems bundler).each { |dependency| require dependency }
Bundler.setup
%w(sinatra haml sass).each { |dependency| require dependency }
enable :sessions

WeiboOAuth2::Config.api_key = ENV['KEY']
WeiboOAuth2::Config.api_secret = ENV['SECRET']
WeiboOAuth2::Config.redirect_uri = ENV['REDIR_URI']

get '/' do
  client = WeiboOAuth2::Client.new
  if session[:access_token] && !client.authorized?
    token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
    # p "*" * 80 + "validated"
    # p token.inspect
    # p token.validated?

    unless token.validated?
      reset_session
      redirect '/connect'
      return
    end
  end
  if session[:uid]
    @user = client.users.show_by_uid(session[:uid])
    @statuses = client.statuses
  end
  haml :index
end

get '/connect' do
  client = WeiboOAuth2::Client.new
  redirect client.authorize_url
end

get '/callback' do
  client = WeiboOAuth2::Client.new
  access_token = client.auth_code.get_token(params[:code].to_s)
  p "^" + params[:code]
  session[:uid] = access_token.params["uid"]
  session[:access_token] = access_token.token
  # p "#" * 80 + access_token.token
  session[:expires_at] = access_token.expires_at
  # p access_token.inspect
  @user = client.users.show_by_uid(session[:uid].to_i)
  redirect '/'
end

get '/logout' do
  reset_session
  redirect '/'
end

get '/screen.css' do
  content_type 'text/css'
  sass :screen
end

# add /statuses interface to show latest statuses as the json output
get '/statuses' do
  client = WeiboOAuth2::Client.new

  # Check the session and get the token obj
  if session[:access_token] && !client.authorized?
    token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
    unless token.validated?
      reset_session
      redirect '/connect'
      return
    end
  end

  # Get the latest statuses timeline
  if session[:uid]
    @user = client.users.show_by_uid(session[:uid])
    @statuses = client.statuses
  end

  coll = Mongo::Connection.new('localhost', 27017).db('weibo')['statuses']

  #@statuses.friends_timeline({:since_id => params[:since_id].to_i}).statuses.each { |status| coll.insert(status) }

  JSON.generate(@statuses.friends_timeline({:since_id => params[:since_id].to_i}).statuses)
  
end

post '/update' do
  client = WeiboOAuth2::Client.new
  client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
{'fraction_non_alphanumeric': 0.13234444230398767, 'fraction_numerical': 0.007320362165286072, 'mean_word_length': 4.3525773195876285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3577313', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1784, 'n_words': 377}