dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

commit c58c9e937b9a253b38305ccb543339b2740ea24e
parent ce5ba59d3c569a486b902523eaabce1d02b29a34
Author: Alex Balgavy <a.balgavy@gmail.com>
Date:   Mon,  5 Oct 2020 12:19:39 +0200

radio: rewrote all in Ruby, no external 'rplayer' anymore

Former-commit-id: 7a6a8274178eff92afd03b39a0c045ba7aaa75d1
Diffstat:
Mscripts/radio | 164+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Dscripts/rplayer | 42------------------------------------------
Avim/after/ftplugin/ruby.vim | 1+
3 files changed, 111 insertions(+), 96 deletions(-)

diff --git a/scripts/radio b/scripts/radio @@ -1,54 +1,110 @@ -#!/usr/bin/env bash - -# Initial config, setting $radios and $radio_names -if [ -e ~/.radio-config ]; then - source ~/.radio-config -elif [ -e ~/.config/radio-config ]; then - source ~/.config/radio-config -else - echo "No radios set up. Please create a config file." - exit 1 -fi - -## MAIN SCRIPT ## - -VISUALISER=false -while getopts ":v" opt; do - case ${opt} in - v ) - VISUALISER=true - esac -done - -clear -echo "Welcome to the command line radio player." -PS3="Select a radio, or type q to exit: " -select RADIO in "${radio_names[@]}" -do - if [[ $RADIO = "q" ]]; then - break - else - for i in "${!radio_names[@]}"; do - if [[ "${radio_names[$i]}" = "${RADIO}" ]]; then - if [[ "$RADIO" = "Play from music subreddit" ]]; then - if command -v rplayer &> /dev/null; then - rplayer - else - echo "rplayer not in path." - fi - else - clear - echo "Now playing $RADIO" - echo "Loading..." - if $VISUALISER; then - nice -n 20 mpv ${radios[$i]} --volume=50 --script="$HOME/.config/mpv/visualizer.lua" --really-quiet -vo caca - else - nice -n 20 mpv ${radios[$i]} --volume=50 - fi - break - fi - fi - done - break - fi -done +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'json' +require 'rest-client' # TODO: get rid of this gem, just use net/http +class Subreddit + def initialize(name) + @url = "https://www.reddit.com/r/#{name}/top.json?t=week&limit=100&show=all" + response = RestClient.get(@url) + data = JSON.parse(response)['data'] + @posts = data['children'] + end + + def posts? + @posts.!empty? + end + + def play + mpv_options = '--no-video --volume=50' + links = [] + @posts.each do |post| + p = post['data'] + if !p['is_self'] && p['post_hint'] != 'image' + links.append(title: p['title'], url: p['url'], reddit: "https://reddit.com#{p['permalink']}") + end + end + + begin + puts "Number of tracks: #{links.length}" + links.each do |link| + puts "\n-----\n" + puts "# #{link[:title]}" + puts "Reddit: #{link[:reddit]}" + puts '(press q to skip, ^C to stop)' + puts "Cannot play #{link[:url]}" unless system("mpv #{mpv_options} '#{link[:url]}'") + end + rescue Interrupt + true + end + end +end + +class Radio + CONFIG_FILE = "#{ENV['HOME']}/.config/radio/urls" + SUBREDDIT_CHANNEL = 'Play from music subreddit' + def initialize + unless system('command -v mpv 1>/dev/null 2>&1') + warn 'mpv not installed.' + exit 1 + end + unless File.exist? "#{ENV['HOME']}/.config/radio/urls" + warn "Please set URLs in #{ENV['HOME']}/.config/radio/urls." + exit 1 + end + @channels = {} + File.open(CONFIG_FILE, 'r').each do |line| + unless line.match?(/^\s*\#/) + parts = line.chomp.split(/(?<=")\s+(?=http)/) + @channels[parts.first.gsub('"', '')] = parts.last + end + end + @channels[SUBREDDIT_CHANNEL] = SUBREDDIT_CHANNEL + end + + def select + puts "\e[H\e[2J" + channel_names = @channels.keys + user_selection = false + begin + until user_selection + channel_names.each_with_index do |name, i| + puts "#{i + 1}: #{name}" + end + print 'Enter number or press ^C to exit> ' + user_selection = (Integer(gets) - 1) rescue false + if user_selection && !@channels[channel_names[user_selection]] + puts 'Invalid selection, please try again.' + user_selection = false + end + end + @channels[channel_names[user_selection]] + rescue Interrupt + nil + end + end + + def play(selection) + if selection == SUBREDDIT_CHANNEL + print 'Enter subreddit name: ' + sub = Subreddit.new(gets.chomp) + until sub.posts? + puts 'Subreddit has no music posts or does not exist.' + print 'Enter subreddit name: ' + sub = Subreddit.new(gets.chomp) + end + sub.play + else + puts "\e[H\e[2J" + puts "Loading #{selection}..." + system 'mpv', selection, '--volume=50' + # visualiser: system 'mpv', selection, '--volume=50 --script="$HOME/.config/mpv/visualizer.lua" --really-quiet -vo caca' + end + end +end + +radio = Radio.new +while (selection = radio.select) + result = radio.play selection + puts "Error playing #{selection}" unless result +end diff --git a/scripts/rplayer b/scripts/rplayer @@ -1,42 +0,0 @@ -#!/usr/bin/env ruby -require 'rest-client' -require 'json' - -mpv_options = "--no-video --volume=50" - -def posts_of sub - url="https://www.reddit.com/r/#{sub}/top.json?t=week&limit=100&show=all" - puts "Requesting posts from #{url}..." - response = RestClient.get(url) - data = JSON.parse(response)["data"] - data["children"] -end - -print "Enter subreddit name: " -sub = gets.chomp -posts = posts_of sub - -while posts.length == 0 - puts "Subreddit has no music posts or does not exist." - print "Enter subreddit name: " - sub = gets.chomp - posts = posts_of sub -end - -links = [] -posts.each do |post| - p = post["data"] - if not p["is_self"] and p["post_hint"] != "image" - links.append(title: p["title"], url: p["url"], reddit: "https://reddit.com#{p["permalink"]}") - end -end - -links.each do |link| - puts "\n-----\n" - puts "# #{link[:title]}" - puts "Reddit: #{link[:reddit]}" - puts "(press q to skip)" - if not system("mpv #{mpv_options} '#{link[:url]}'") - puts "Cannot play #{link[:url]}" - end -end diff --git a/vim/after/ftplugin/ruby.vim b/vim/after/ftplugin/ruby.vim @@ -0,0 +1 @@ +compiler rubocop