~retropikzel/haiku-twitch-favorites

aa4ac7043b02eb3d76c5523e1dede762d935fb7e — retropikzel 4 years ago master
Done
1 files changed, 133 insertions(+), 0 deletions(-)

A haiku-twitch-favorites.rb
A  => haiku-twitch-favorites.rb +133 -0
@@ 1,133 @@
#!/usr/bin/env ruby

puts ARGV[2]
ARGV.each do|a|
  if a == "help" or a == "--help" or a == "-help"
    puts "To use you have to have:"
    puts "ruby"
    puts "yoshi"
    puts "youtube_dl"
    puts "vlc"
    exit
  end
end

require 'fileutils'
base_path = "/boot/home/config/settings/TwitchFavorites"
favorite_path = "/boot/home/config/settings/TwitchFavorites/favorites.txt"

if not File.directory?(base_path)
  FileUtils.mkdir_p base_path
end

if not File.exists?(favorite_path)
  out_file = File.new(favorite_path, "w")
  out_file.close
else
      new_content = ""
      File.read("/boot/home/config/settings/TwitchFavorites/favorites.txt").split "\n" do | fav_line |
        if fav_line != ""
          new_content = new_content + fav_line.to_s + "\n"
        end
      end
      File.open("/boot/home/config/settings/TwitchFavorites/favorites.txt", "w") do | line |
        line.puts new_content
      end
end


def construct_select_window()
  File.open("/tmp/tf-yoshi-select.txt", "w") do |line|
    line.puts "*.title = Twitch Favorites"
    line.puts "radio.type = radiobutton"
    line.puts "radio.label = What you want to watch?"
    line.puts "radio.option1 = Add new"
    line.puts "radio.option2 = Remove one"
    index = 3
    File.read("/boot/home/config/settings/TwitchFavorites/favorites.txt").split "\n" do | url |
      line.puts "radio.option" + index.to_s + " = " + url
      index = index + 1
    end
    line.puts "radio.default = option1"
  end
end

def add_favorite()
  File.open("/tmp/tf-yoshi-add-new.txt", "w") do |line|
    line.puts "tx.type = textfield"
    line.puts "tx.label = Url: "
    line.puts "tx.width = 200"
  end
  system("yoshi /tmp/tf-yoshi-add-new.txt > /tmp/tf-yoshi-add-new-out.txt")
  File.read("/tmp/tf-yoshi-add-new-out.txt").split do | line |
    keyval = line.split "="
    if keyval[0] == "tx"
      File.open("/boot/home/config/settings/TwitchFavorites/favorites.txt", "a") do | line |
        if keyval[1] != ""
          line.puts keyval[1] + "\n"
        end
      end
      select_action
    end
  end
end

def remove_favorite()
  File.open("/tmp/tf-yoshi-remove.txt", "w") do |line|
    line.puts "*.title = Twitch Favorites"
    line.puts "radio.type = radiobutton"
    line.puts "radio.label = Which one you want to remove?"
    index = 1
    File.read("/boot/home/config/settings/TwitchFavorites/favorites.txt").split "\n" do | url |
      line.puts "radio.option" + index.to_s + " = " + url
      index = index + 1
    end
    line.puts "radio.default = option1"
  end
  system("yoshi /tmp/tf-yoshi-remove.txt > /tmp/tf-yoshi-remove-out.txt")
  File.read("/tmp/tf-yoshi-remove-out.txt").split do | line |
    keyval = line.split "="
    if keyval[0] == "radio"
      url_index = keyval[1][-1..-1].to_i
      new_content = ""
      index = 1
      File.read("/boot/home/config/settings/TwitchFavorites/favorites.txt").split "\n" do | fav_line |
        if index != url_index
          new_content = new_content + fav_line.to_s + "\n"
        end
        index = index + 1
      end
      File.open("/boot/home/config/settings/TwitchFavorites/favorites.txt", "w") do | line |
        line.puts new_content
      end
      select_action
    end
  end
end


def select_action()
  construct_select_window
  system("yoshi /tmp/tf-yoshi-select.txt > /tmp/tf-yoshi-select-out.txt")
  File.read("/tmp/tf-yoshi-select-out.txt").split do | line |
    keyval = line.split "="
    if keyval[0] == "radio"
      if keyval[1] == "option1"
        add_favorite
      elsif keyval[1] == "option2"
        remove_favorite
      else
        url_index = keyval[1][-1..-1].to_i - 3
        index = 0
        File.read("/boot/home/config/settings/TwitchFavorites/favorites.txt").split "\n" do | fav_line |
          if index == url_index
            system("youtube-dl " + fav_line + " -o - | vlc -")
          end
          index = index + 1
        end
      end
    end
  end
end

select_action