summaryrefslogtreecommitdiffstats
path: root/lass/5pkgs/mpv-poll/default.nix
blob: ee191843e6d342977ddf621e6d0422f2c6734a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{ pkgs, ... }:

pkgs.writeScriptBin "mpv-poll" ''
  #! ${pkgs.bash}/bin/bash

  pl=$1
  hist=''${HISTORY:-"./mpv_history"}
  mpv_options=''${MPV_OPTIONS:-""}

  lastYT=""

  play_video () {
    toPlay=$1
    echo $toPlay >> $hist
    mpv $mpv_options $toPlay
  }

  if ! [ -e $hist ]; then
    touch $hist
  fi

  while :
  do
    if [ -s $pl ]; then
      toPlay=$(head -1 $pl)
      sed -i '1d' $pl
      if $(echo $toPlay | grep -Eq 'https?://(www.)?youtube.com/watch'); then
        lastYT=$toPlay
      fi
      play_video $toPlay
    else
      if [ -n "$lastYT" ]; then
        next=$(yt-next $lastYT)
        lastYT=$next
        play_video $next
      fi
      sleep 1
    fi
  done
''