Nushell Youtube Script

There is a neat youtube script that allows you to fuzzy search youtube api from really simple tools and it looks something like this from (https://github.com/MarcoLucidi01/bin/blob/master/ytsearch):

#! /usr/bin/env bash
case "$1" in "-h" | "--help")
  echo "usage: ytsearch query..."
  exit 0
esac

curl -s -G "https://www.youtube.com/results" --data-urlencode "search_query=$*" \
 | tr -d '\n' \
 | sed -e 's#^.*var \+ytInitialData *=##' -e 's#;</script>.*##' \
 | jq -r '..
    | .videoRenderer?
    | select(.)
    | [.title.runs[0].text[:80], (.lengthText.simpleText//"N/A"), (.shortViewCountText.simpleText//"N/A"), (.publishedTimeText.simpleText//"N/A"), .longBylineText.runs[0].text, .videoId]
    | @tsv' \
 | column -s "$(printf '\t')" -t \
 | fzf --with-nth=..-2 \
 | awk '{ print "https://www.youtube.com/watch?v="$NF }'        

I thought it would be a good highlight of nushell's capabilities to do all of this with nushell alone. Yes, that includes fuzzy finder built-in!

http get https://www.youtube.com/results?search_query=(input|url encode)
  | split row "var ytInitialData =" 
  | last 
  | split row ";</script>" 
  | first 
  | from json 
  | get -o contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents.0.itemSectionRenderer.contents.videoRenderer 
  | uniq | sort | drop 
  | select title.runs.text.0 videoId 
  | flatten 
  | input list -f -d "title.runs.text.0" 
  | get videoId | $"https://www.youtube.com/watch?v=($in)"        

There is a certain elegance to this solution and it's somewhat more readable than a lot of these unix tools. The combined size of curl, tr, sed, jq, fzf, awk comes comparably close to nushell's 40 MB(?) binary.

To view or add a comment, sign in

More articles by Matt Ji

  • 100 MB of storage with Nushell SDK

    I've made a simple Nushell SDK for the https://getpantry.cloud service where you can have a JSON store of 100 bins of 1.

  • Server-side WASM APIs is a Social Contract of the Web

    A tiny little experiment thanks to a free account at pythonanywhere.com can go a long way.

  • Ugh, compromises that lead to innovation

    Recently, I switched my resume into typst. And something I am looking forward to doing is compiling glyphs to replace…

    2 Comments
  • I've built my resume in Typst

    There are many ways to make a resume nowadays. But I found the coolest way to differentiate yourself in the job market…

  • TOTP without having to pay for premium Bitwarden

    Here is how you can get totp to work with bitwarden without having to pay for premium. Using nushell as a way to parse…

  • How to make your own offline AI browser to organize your browse history

    Requirements: An existing browser (firefox, chrome, or safari) Obsidian Web Clipper browser extension Ollama any local…

    1 Comment
  • MCP design: Decoupling context and Model Strength

    Many companies are getting on the bandwagon of Anthropic's tool calling API for LLMs called Model Context Protocol…

  • Kelly Criterion in multiple languages

    The kelly criterion is an equation that helps risk managers and decision-makers assess resource allocation on a…

  • Nushell > Bash scripting

    Scripting can be easy or hard depending on what kind of conditions or arguments need to be passed into the function…

  • Managing nushell scripts

    Nushell is a powerful programming language because it makes very good use of the unix piping mechanics. Take this very…

Explore content categories