Monday, October 16, 2023

Taming YouTube, Part 5: Command Line Searching

The last piece of my YouTube hacking journey was to implement command line searching for content. The idea is that rather than paging through results in a web browser, I can kick off and process searches at a shell prompt. As with many Unixy endeavors, the approach initially seems less powerful than an interactive UI. But, given the ability to script and post process results, I'm confident that the command line approach will ultimately win the day. 

Let's see this in action. Suppose I want to catch up on a classic outdoorsy topic: Altoids Survival Kits. The idea is to pack an Altoids candy tin with the essentials for dealing with an emergency in the woods.

Here's what my initial search looks like:

$ youtubeassist -a search -q "Altoids Survival Kit" | wc -l
     198

$ youtubeassist -a search -q "Altoids Survival Kit" | head -5
0-fj28qufo0|2023-09-23T01:35:37Z|Rivers End Outdoors|Altoids Tin Can be used for many other things. #Shorts #Bushcraft #Survival.
kqPGdBdB8ow|2023-09-20T22:04:00Z|Best Damn EDC [Taylor Martin]|I put together an EPIC EDC Urban Altoids Survival Kit 2023
LXf-fiAhOlo|2023-08-20T06:53:35Z|Ashton's EDC|My Take on the Altoids Tin: What I Keep in Two Different Altoids EDC Kits
dpwEhyV-VbA|2023-08-18T18:15:04Z|Jon Gadget|My 2023 Altoids EDC Essentials Kit (Everyday Carry)
cECC341Y074|2023-08-10T00:35:18Z|Typical Chris|How to Make A Mini Survival Kit Out of an Altoids Tin!

My search turned up only 198 hits out of the bazillion of these videos on YouTube. That's because I limit my search results to the first 200 hits.

If I wanted to move all 200 of these videos to a playlist for easy watching I could say:

# make the playlist
$ youtubeassist -a playlist-create -n "Search Hits"
PLBuxEPZOHHKZE-uDfDReNTN6bmUu3r_WE|private|Search Hits

# -o 'output's the results to the requested playlist
$ youtubeassist -a search -q "Altoids Survival Kit" -o 'Search Hits'
...bye bye quota...

Outputting to a playlist helps bridge the divide between the command line and interactive worlds.

It's also possible to process the output and add videos to a playlist after the fact. For example:

# Put all fire related videos into a 'Fire Making' playlist
$ youtubeassist -a search -q 'Altoids Survival Kit' -F | \
    grep -i fire | cut -d'|' -f1 | \
    youtubeassist -a playlist-items-add -n "Fire Making"

The -F in this case shows the 'full results', not just the most recently executed search.

Because executing a search is quota heavy, I've set it up so that searches are cached for one hour. After an hour, re-running a search will show you only the new results. For example:

# wait a couple of hours ...
 
$ youtubeassist -a search -q "Altoids Survival Kit" 
EHK820jd32c|2023-07-09T22:58:59Z|Mount Kephart Outdoors|Altoids tin Kits USLESS? Blackie Thomas does it again Bushcraft & Wilderness Survival Gear
PA_TpbwCfQQ|2023-07-01T17:00:22Z|The Modern Rogue|Filling an Altoids Tin with crime tools �
jXWqPw27Yk8|2023-06-28T20:00:10Z|Survival Dispatch|From the City to the Wilderness: EDC Survival Kit | Fuel The Fires
dIuYFZ1zRN4|2023-05-02T21:21:13Z|WayPoint Survival|Perfect Belt Survival Kit?  [ It has ALL the stuff! ]
eHuMgL4Ex-k|2023-03-03T00:12:05Z|Comandos Benin Prepper|EDC melhor kit de sobrevivĂȘncia do mundo. Altoids Ă© passado?
DbnHhFhG2Bo|2023-02-21T01:27:17Z|WayPoint Survival|Old School Pocket Survival Kit!
WCb6kVgMGl0|2023-01-28T16:02:48Z|David Hindin, M.D.|Squeezing an operating room inside an Altoids tin
mZiasleIwxM|2022-10-09T22:58:26Z|Buckeye Bushcraft|Ultralight Survival Kit For Hunters
u5wieAfgR3o|2022-03-12T16:23:17Z|Freddy Ramos|Custom Sheath Bushcraft Utility Pouch for Altoids Tin
4zrjx40_gHc|2021-09-29T22:34:09Z|Grim Granite|Is survival gear REALLY possible from a DOLLAR STORE?
8X4dHYnPdUM|2021-04-09T09:00:03Z|Ultimate Survival Tips|IT WORKS! Cheap DIY Survival Kit Compass Hack - Altoids Tin Micro Compass Kit...
sGyv_dkWLfk|2020-10-14T00:24:13Z|JoeDom|Altoids Survival Tin
h6dQK4tp2yI|2020-10-09T17:43:28Z|Lone wolf Survival|Altoid Tin Survival Kit
IFF1vmMnSjo|2020-10-03T05:28:05Z|Adventures with Cowboy Col|20 Altoids Survival Tin/Kit Items In A Wallet ? The ULTIMATE EDC WALLET!
YgOvhC0_lCw|2020-10-01T01:58:59Z|Overlander Benny|DIY altoid survival kit tin! More than your average tin. Fire, water, hunt, fish from your kit!
jO3il9sdLG8|2020-09-15T15:13:12Z|Rockos Modern Survival|Altoids survival kit
LmfaNcvYS60|2020-09-09T19:49:15Z|Swagger|Altoids Survival Kit
QIJr7RuYgoc|2020-08-19T00:09:08Z|Old Time Skills|How to make an Altoids tin survival kit
UrKMGooOYZc|2019-10-05T17:01:34Z|yamato623|The altoids smalls survival kit $15
$ youtubeassist -a search -q "Altoids Survival Kit"  | wc -l
      19

$ youtubeassist -a search -q "Altoids Survival Kit"  -o "Search Hits"

By default, searches have their sort order set to date, which should only show me newly added content to YouTube. But, as you can see above, the results aren't that strict. I'm seeing 19 'new' hits, even though many of them are from years ago.

In general, YouTube API searches tend to be broad in what they return. For example, not all of the hits above mention 'Altoids' in their title. Generally, this works for me as I'm often looking for more esoteric content rather than the same high-profile videos YouTube show again and again.   I can always narrow down searches by using grep and other tools.

I'm not entirely sure how I'm going to fold this search capability into my YouTube workflow. However, I am confident that running and grep'ing through command line searches will be far more efficient than mindlessly scrolling.

Give all this a try by grabbing youtubeassist from github. Happy hacking!

No comments:

Post a Comment