Bugswriter / myyt

Script I wrote to search youtube with command line easily.
85 stars 16 forks source link

use scraper to eliminate the requirement for API KEY #8

Closed madepolli closed 3 years ago

madepolli commented 3 years ago

Since using Youtoube API for simple search seems like too much trouble (and is limited in the number of uses anyways), why not use a webpage scraper?

By a quick google, I found youtube-search (pip3 install youtube-search), and I changed the bash script to use the following python script instead of querying youtube API:

#!/usr/bin/env python3
from youtube_search import YoutubeSearch
from sys import argv

search_terms = str(argv)

results=YoutubeSearch(search_terms, max_results=10).to_dict()
urlString = '\n'.join((r['title']+' '+r['id'] for r in results))
print(urlString)

main bash lines then become:

urlstring=$(./new_python_script_name.py "${query}")
mpv "https://youtu.be/$( echo "${urlstring}" \
    | fzf --with-nth='1..-2' +m \
    | awk '{print $NF}' \
)"

It works great for me. It does require some polishing regarding the location of both scripts which influences how you call python from bash etc. The two scripts could probably be even merged into one (heredocs in bash or use python to call fzf and mpv) for extra points :)

So I'm just leaving this idea here with you, if I ever foolproof it, I'l turn it into a merge request.

notthewave commented 3 years ago

I'm working on something without an API key that does the same thing. It's super rough around the edges and does not show the title of the video yet, but the basic functionality is there.

mpv https://youtube.com"$(curl -s "https://www.youtube.com/results?search_query=$(printf "" | \
        dmenu-wl -p "Search:" | tr ' ' '+')&sp=EgIQAQ%253D%253D" | \
        grep -Po '(/watch\?v=.{11})' | \
        perl -ne '$seen{$_}++ or print;' | \
        dmenu-wl -l 7 || notify-send "Something went wrong...")"

Probably not the best method to use grep and perl with those flags, but it gets the job done

See full prototype script with playlist support at: https://github.com/notthewave/ytlist-src

Spent a few hours curling YouTube search results into my terminal lol...

Bugswriter commented 3 years ago

This is not simple now also there is a guy sayan21 - https://github.com/sayan01/scripts/blob/master/yt He also made similar stuff by getting inspired by my script and he did it with web scraping. SO no need of python now.

ghost commented 3 years ago

It might be a good idea to use the Invidious API for this: https://github.com/iv-org/documentation/blob/master/API.md

Bugswriter commented 3 years ago

https://github.com/pystardust/ytfzf