<commit_before>#!/usr/bin/env ruby
require 'rubygems'
require 'yaml'
require 'net/http'
require 'nokogiri'

$settings = {
  :use_title => true,
  :force_title => false,
  :ask_every_time => true,
  :verbose => false
}
def process_arg(arg)
  case arg
  when 'f', 'force-title' then $settings[:force_title] = true
  when 'n', 'no-title' then $settings[:use_title] = false
  when 'y', 'yes' then $settings[:ask_every_time] = false
  when 'v', 'verbose' then $settings[:verbose] = true
  end
end
ARGV.each do |a|
  if a.length == 1
    process_arg(a)
  elsif a.length > 2 && a[0..1] == '--'
    process_arg(a[2..-1])
  elsif a[0].chr == '-'
    a[1..-1].each_char{|c| process_arg(c) }
  end
end
$settings[:use_title] = true if $settings[:force_title]

if $settings[:use_title]
  yaml_path = File.join(File.expand_path(File.dirname(__FILE__)), 'api.yaml')
  api_yaml = YAML::load(File.open(yaml_path))
  API_PATH = api_yaml['path']
end
unless $settings[:verbose]
  def puts(s)
    nil
  end
end

def rename_season(n=1)
  show = File.basename(Dir.pwd)
  dir = "#{Dir.pwd}/Season #{n}"
  ep = 0
  Dir.foreach(dir) do |item|
    next if item[0].chr == '.'
    if matchdata = /s?\d+[ex](\d+)/i.match(item)
      ep = matchdata[1].to_i
    else
      ep += 1
    end
    ext = item[/\.([^\.]+)$/,1]
    ep_title = if $settings[:force_title]
      title = nil
      while title.nil? do
        title = get_title(show, n, ep)
      end
      title
    else
      $settings[:use_title] ? get_title(show, n, ep) : nil
    end
    new_name = if ep_title
      "#{show} - S#{pad_zero(n)}E#{pad_zero(ep)} - #{ep_title}.#{ext}"
    else
      "#{show} - S#{pad_zero(n)}E#{pad_zero(ep)}.#{ext}"
    end
    next if item == new_name
    if $settings[:ask_every_time]
      puts "Rename \"#{item}\" to \"#{new_name}\"? [yes/no]"
      yn = gets.chomp
      confirm = /^y(?:(?:es)|$)/i.match(yn)
    else
      confirm = true
    end
    if confirm
      begin
        File.rename("#{dir}/#{item}", "#{dir}/#{new_name}")
        puts "-" * 10
        puts "#{item}"
        puts "renamed to"
        puts "#{new_name}."
        puts "-" * 10
      rescue
        puts "There was a problem renaming this file. File skipped."
      end
    else
      puts "File skipped."
    end
  end
end

def get_title(series, season, ep)
  url = URI.parse(URI.encode(API_PATH + "&show=#{series}&ep=#{season}x#{ep}"))
  begin
    res = Net::HTTP.get_response(url)
  rescue Errno::ECONNRESET, EOFError
    puts "Error retrieving episode title from database."
    return nil
  end
  doc = Nokogiri::XML(res.body)
  node = doc.xpath('/show/episode/title').first
  node.nil? ? nil : node.content
end<commit_msg>changed verbose (-v) to be silent (-s). verbose is now default<commit_after>#!/usr/bin/env ruby
require 'rubygems'
require 'yaml'
require 'net/http'
require 'nokogiri'

$settings = {
  :use_title => true,
  :force_title => false,
  :ask_every_time => true,
  :silent => false
}
def process_arg(arg)
  case arg
  when 'f', 'force-title' then $settings[:force_title] = true
  when 'n', 'no-title' then $settings[:use_title] = false
  when 'y', 'yes' then $settings[:ask_every_time] = false
  when 's', 'silent' then $settings[:silent] = true
  end
end
ARGV.each do |a|
  if a.length == 1
    process_arg(a)
  elsif a.length > 2 && a[0..1] == '--'
    process_arg(a[2..-1])
  elsif a[0].chr == '-'
    a[1..-1].each_char{|c| process_arg(c) }
  end
end
$settings[:use_title] = true if $settings[:force_title]

if $settings[:use_title]
  yaml_path = File.join(File.expand_path(File.dirname(__FILE__)), 'api.yaml')
  api_yaml = YAML::load(File.open(yaml_path))
  API_PATH = api_yaml['path']
end
if $settings[:silent]
  def puts(s)
    nil
  end
end

def rename_season(n=1)
  show = File.basename(Dir.pwd)
  dir = "#{Dir.pwd}/Season #{n}"
  ep = 0
  Dir.foreach(dir) do |item|
    next if item[0].chr == '.'
    if matchdata = /s?\d+[ex](\d+)/i.match(item)
      ep = matchdata[1].to_i
    else
      ep += 1
    end
    ext = item[/\.([^\.]+)$/,1]
    ep_title = if $settings[:force_title]
      title = nil
      while title.nil? do
        title = get_title(show, n, ep)
      end
      title
    else
      $settings[:use_title] ? get_title(show, n, ep) : nil
    end
    new_name = if ep_title
      "#{show} - S#{pad_zero(n)}E#{pad_zero(ep)} - #{ep_title}.#{ext}"
    else
      "#{show} - S#{pad_zero(n)}E#{pad_zero(ep)}.#{ext}"
    end
    next if item == new_name
    if $settings[:ask_every_time]
      puts "Rename \"#{item}\" to \"#{new_name}\"? [yes/no]"
      yn = gets.chomp
      confirm = /^y(?:(?:es)|$)/i.match(yn)
    else
      confirm = true
    end
    if confirm
      begin
        File.rename("#{dir}/#{item}", "#{dir}/#{new_name}")
        puts "-" * 10
        puts "#{item}"
        puts "renamed to"
        puts "#{new_name}"
        puts "-" * 10
      rescue
        puts "There was a problem renaming this file. File skipped."
      end
    else
      puts "File skipped."
    end
  end
end

def get_title(series, season, ep)
  url = URI.parse(URI.encode(API_PATH + "&show=#{series}&ep=#{season}x#{ep}"))
  begin
    res = Net::HTTP.get_response(url)
  rescue Errno::ECONNRESET, EOFError
    puts "Error retrieving episode title from database."
    return nil
  end
  doc = Nokogiri::XML(res.body)
  node = doc.xpath('/show/episode/title').first
  node.nil? ? nil : node.content
end
{'fraction_non_alphanumeric': 0.17590182223875048, 'fraction_numerical': 0.007065823726292302, 'mean_word_length': 2.8067940552016983, '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': '600289', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2081, 'n_words': 471}