<commit_before># encoding: utf-8

require 'rubygems'
require 'sinatra'
require 'tumblr_client'

FileUtils.mkdir('log') unless File.directory?('log')
$stdout.reopen(File.new("log/app.log", "a"))
$stderr.reopen($stdout)

# Build global css file
all_css = 'css/all.css'
File.delete(all_css) if File.exist?(all_css)
out_css = File.new(all_css, 'w')
Dir.glob('css/*.css') do |css|
  out_css << File.new(css, 'rb').read
end
out_css.close

get "/" do
  redirect "/blog"
end

get "/blog" do
  html = File.new("blog/index.html", "rb").read
  post_html = File.new("blog/post.html", "rb").read

  # Stored in a file outside of this repo
  cfg = File.new("oauth_config", "rb").readlines.collect{ |l| l.chomp }
  Tumblr.configure do |config|
    config.consumer_key = cfg[0]
    config.consumer_secret = cfg[1]
    config.oauth_token = cfg[2]
    config.oauth_token_secret = cfg[3]
  end

  tumblr = Tumblr::Client.new
  posts = tumblr.posts("fu-er.tumblr.com", :limit => 10)["posts"]
  posts.each { |p|
    p_html = post_html.gsub("TITLE", p["title"]).gsub("DATE", p["date"]).gsub("CONTENT", p["body"]).gsub("LINK", p["post_url"])
    html.gsub!("CONTENT", p_html + "CONTENT")
  }
  html.gsub!("CONTENT", "")

  html
end

get "/gallery" do
  cat = params[:cat]
  if cat == nil
    cat = "vector"
  end

  html = File.new("gallery/index.html", "rb").read
  dir = "gallery/" + cat + "/";
  first = ""
  files = Dir.entries(dir).sort.reverse
  files.each do |file|
    next if file == "." or file == ".." or file.start_with?("skip_")

    if first.length == 0
      first = file
    end

    tmp_dir = dir + file
    desc_file = tmp_dir + "/description.txt"
    preview_file = tmp_dir + "/preview.png"
    thumb_file = tmp_dir + "/thumb.png"<commit_msg>Add support for photo posts<commit_after># encoding: utf-8

require 'rubygems'
require 'sinatra'
require 'tumblr_client'

#FileUtils.mkdir('log') unless File.directory?('log')
#$stdout.reopen(File.new("log/app.log", "a"))
#$stderr.reopen($stdout)

# Build global css file
all_css = 'css/all.css'
File.delete(all_css) if File.exist?(all_css)
out_css = File.new(all_css, 'w')
Dir.glob('css/*.css') do |css|
  out_css << File.new(css, 'rb').read
end
out_css.close

get "/" do
  redirect "/blog"
end

get "/blog" do
  html = File.new("blog/index.html", "rb").read
  post_html = File.new("blog/post.html", "rb").read

  # Stored in a file outside of this repo
  cfg = File.new("oauth_config", "rb").readlines.collect{ |l| l.chomp }
  Tumblr.configure do |config|
    config.consumer_key = cfg[0]
    config.consumer_secret = cfg[1]
    config.oauth_token = cfg[2]
    config.oauth_token_secret = cfg[3]
  end

  tumblr = Tumblr::Client.new
  posts = tumblr.posts("fu-er.tumblr.com", :limit => 10)["posts"]
  posts.each { |p|
    puts p
    type = p["type"]
    p_html = post_html.gsub("DATE", p["date"]).gsub("LINK", p["post_url"])
    if type == "text"
      p_html = p_html.gsub("TITLE", p["title"]).gsub("CONTENT", p["body"])
    elsif type == "photo"
      p_html = p_html.gsub("TITLE", "Blah")
      
      p["photos"].each { |ph|
        p_html = p_html.gsub("CONTENT", "<img style='width: 75%;' src='" + ph["original_size"]["url"] + "'></img>" + p["caption"] + "CONTENT")
      }
      p_html = p_html.gsub("CONTENT", "")
    else
      puts "unsupported post type: " + type
    end
    html.gsub!("CONTENT", p_html + "CONTENT")
  }
  html.gsub!("CONTENT", "")

  html
end

get "/gallery" do
  cat = params[:cat]
  if cat == nil
    cat = "vector"
  end

  html = File.new("gallery/index.html", "rb").read
  dir = "gallery/" + cat + "/";
  first = ""
  files = Dir.entries(dir).sort.reverse
  files.each do |file|
    next if file == "." or file == ".." or file.start_with?("skip_")

    if first.length == 0
      first = file
    end

    tmp_dir = dir + file
    desc_file = tmp_dir + "/description.txt"
    preview_file = tmp_dir + "/preview.png"
    thumb_file = tmp_dir + "/thumb.png"
{'fraction_non_alphanumeric': 0.18804071246819337, 'fraction_numerical': 0.004580152671755725, 'mean_word_length': 3.392178770949721, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4746852', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1490, 'n_words': 323}