Skip to main content

Play youTube videos via Rofi

·
Table of Contents

There are five scripts listed below.

The ones for youTube are:

  • updater.sh
  • updateytsubs
  • subs.opml
  • youtubesubs

The one for newsboat is:

  • nb_updater.sh

Rofi
#

Main window

updater.sh
#

#!/bin/bash
/home/karmst/scripts/youTube/updateytsubs /home/karmst/scripts/youTube/subs.opml

nb_updater.sh
#

#!/bin/sh

newsboat -x reload

updateytsubs
#

#! /bin/bash
# Run this in cron
# First parameter is path your subscription_manager file downloadable from
# Youtube https://www.youtube.com/subscription_manager (Bottom of the page)
# It updates ~/.youtubesubs

# test network connectivity
ping -q -w 1 -c 1 youtube.com > /dev/null || exit 1

# test that the file given as a parameter is atleast somewhat correct
if [ "$(grep 'opml' $1 | wc -l)" -eq 0 ]; then
	echo "invalid parameter"
	exit 1
fi

fetched="$(cat $1 \
	| sed -e 's/\"/\n/g' -e 's/;//g'  \
	| grep https \
	| sed -e 's/^/curl -s /' | sh \
	| grep -A 16 "<entry>" \
	| grep  "<name>\|<title>\|<yt:videoId>\|<published>" \
	| paste - - - - \
	| sed \
		-e "s/\(.*\)<published>\(.*\)<\/published>\(.*\)/\1\3\t\2/" \
		-e "s/\(.*\)<name>\(.*\)<\/name>\(.*\)/\1\3\t\2/" \
		-e "s/\(.*\)<title>\(.*\)<\/title>\(.*\)/\1\3\t\2/" \
		-e "s/\(.*\)<yt:videoId>\(.*\)<\/yt:videoId>\(.*\)/\1\3\t\2/" \
		-e "s/^\s*//" \
		-e 's/\&quot;/"/g' -e 's/\&amp;/\&/g' \
	)"

combined="$(echo -e "$fetched" | cat - ~/.youtubesubs \
       | sort -t$'\t' -u -k4,4 \
       | sort -rn)"
echo "$combined" > /home/karmst/scripts/youTube/youtubesubs

subs.opml
#

<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<body>
    <outline title="YouTube UCNsK-d5q_9Fk3bQ6zPKpNbw" text="YouTube UCNsK-d5q_9Fk3bQ6zPKpNbw">

        <outline title="Tropicana Entertainment"
            text="Tropicana Entertainment"
            xmlUrl="https://www.youtube.com/feeds/videos.xml?channel_id=UC3Zlfyb7WDFrzdTFZ3GreIw"
            htmlUrl="https://www.youtube.com/channel/UC3Zlfyb7WDFrzdTFZ3GreIw" />

        <outline title="Technology Connections"
            text="Explaining Computers"
            xmlUrl="https://www.youtube.com/feeds/videos.xml?channel_id=UCy0tKL1T7wFoYcxCe0xjN6Q"
            htmlUrl="https://www.youtube.com/channel/UCy0tKL1T7wFoYcxCe0xjN6Q" />
    </outline>
</body>
</opml>

youtubesubs
#

#! /bin/bash

YT_FOLDER="$HOME/Videos/yt/"
FILE_EXTENSION='mp4'
SCRIPT_HOME="$HOME/scripts/youTube"
PLAYER="mpv -fs"

ROFI_CONFIG="fzf_finder.rasi"

options="$(cat ~/.youtubesubs | cut -f 1,2,3 | sed -r 's/^([0-9\-]{10})T([0-9:]{5})([0-9:]{3})([0-9\+:]{6})(.+$)/\1 \2 \5/g' | sed -e 's/\t/: /g')"

last_modified_yt=$(stat -c %.19y "$SCRIPT_HOME"/youtubesubs)
last_modified_nb=$(stat -c %.19y "$HOME"/.local/share/newsboat/cache.db)
last_modified_yt_hr=$(sed 's/ / /g' <<< "$last_modified_yt")
last_modified_nb_hr=$(sed 's/ / /g' <<< "$last_modified_nb")

# present dmenu
selected_temp=$(echo -e "Update YouTube\nUpdate Newsboat\n$options" | rofi -dmenu -i -l 25 -theme-str 'window {width: 80%;}' -theme "$ROFI_CONFIG" -p "YouTube ${last_modified_yt_hr}, NewsBoat ${last_modified_nb_hr}")

# 2023-10-04 22:02 - Sky News - Press Preview: Thursday's papers
selected=$(echo $selected_temp | sed -r 's/^([0-9\-]{10}) ([0-9:]{5}) : (.+$)/\3/g')

play_video ()
{
  # make similar transformations again and match the selection. Return url
  video_id="$(cat ~/.youtubesubs | sed -e 's/\t/: /2' | grep "$selected" | cut -f 3)"
  video_url="$(echo "$video_id" | sed -e 's/^/https:\/\/www\.youtube\.com\/watch\?v=/')"
  author="$(grep "$video_id" ~/.youtubesubs | cut -f 2)"
  title="$(grep "$video_id" ~/.youtubesubs | cut -f 3)"
  video_path="$author/$title.$FILE_EXTENSION"
  full_path="$(echo "$video_path" | sed -e 's|^|'$YT_FOLDER'|')"

  # if exist, echo path. Else echo url
  if [ -e "$full_path" ]; then
	$PLAYER "$full_path&vq=hd720"
  else
    # echo $video_url
	$PLAYER "$video_url&vq=small"
  fi
}

if [[ $selected == "Update YouTube" ]]; then
  printf '\a'
  dunstify -t 2500 "YouTube update started"
  aplay /home/karmst/scripts/youTube/beep.wav
  play /home/karmst/scripts/youTube/beep.wav
  myUpdater=$(/home/karmst/scripts/youTube/updater.sh)
  aplay /home/karmst/scripts/youTube/beep.wav
  play /home/karmst/scripts/youTube/beep.wav
  dunstify -t 2500 "YouTube update finished"
elif [[ $selected == "Update Newsboat" ]]; then
  dunstify -t 2500 "Newsboat update started"
  aplay /home/karmst/scripts/youTube/beep.wav
  play /home/karmst/scripts/youTube/beep.wav
  myUpdater=$(/home/karmst/scripts/nb_updater.sh)
  aplay /home/karmst/scripts/youTube/beep.wav
  play /home/karmst/scripts/youTube/beep.wav
  dunstify -t 2500 "Newsboat update finished"
elif [ -n "$selected" ]; then
    play_video
fi