POSIX_Scripts/invcli

75 lines
2.1 KiB
Plaintext
Raw Normal View History

2021-10-16 18:55:03 -04:00
#!/bin/sh -e
search()
{
rm -f /tmp/invidious_results
query_sanitized="$(printf "%s\n" "$1"|sed -e 's/ */%20/g')"
2021-10-16 18:55:03 -04:00
curl --output /tmp/invidious_results "https://vid.puffyan.us/search?q=$query_sanitized"
}
2021-12-24 10:42:30 -05:00
2021-10-16 18:55:03 -04:00
extract_names()
{
2021-12-24 10:42:30 -05:00
viable=$(grep watch /tmp/invidious_results|grep youtube|expr $(wc -l) - 1)
sed -ne 's/<p dir.*>\(.*\)<\/p>/\1/p' /tmp/invidious_results|sed -ne 's/ *//' -e '/^$/d' -e p|sed ${viable}q
2021-10-16 18:55:03 -04:00
}
extract_urls()
{
for id in $(sed -ne 's/ */ /g' -e 's/<a style=.* href="\(\/watch?v=.*\)">/\1/p' /tmp/invidious_results|sed -ne 's/ *//p')
do
printf "https://vid.puffyan.us%s\n" $id
done
}
watch_vid()
{
2021-12-24 10:42:30 -05:00
ffplay "https://vid.puffyan.us/latest_version?id=$id&itag=$itag" 2>>/dev/stdout|sed -ne 's/< Location: \(.*\)/\1/p'
2021-10-16 18:55:03 -04:00
}
fetch_itag()
{
# 22 seems to be the itag associated with audio+video MP4s at 720p...
curl --silent $1|sed -ne 's/.*"itag":"\([0-9][0-9]*\)".*/\1/p' #|grep 22
2021-10-16 18:55:03 -04:00
}
fetch_id()
{
curl --silent $(extract_urls|sed $chosen_video!d)|grep itag|sed -ne 's/.*"id":"\(.*\)","itag":.*/\1/p'|sed 1!d
}
generate_locations()
{
2021-12-24 10:42:30 -05:00
# 18 is for 360p, 22 is for 720p
2022-01-26 13:27:04 -05:00
# In the future, this will be automatic.
2021-12-24 10:42:30 -05:00
for itag in 18 #$(fetch_itag $(extract_urls|sed $chosen_video!d)|sed $chosen_video!d)
2021-10-16 18:55:03 -04:00
do
watch_vid
done
}
2021-12-24 10:42:30 -05:00
#Use this snippet to only retrieve URLs that are actually in the results...
#grep 'watch?v=' /tmp/invidious_results|grep youtube
#
#Hopefully this is the key to fixing the weird interactions between extract_names and fetch_itag
2021-10-16 18:55:03 -04:00
main()
{
printf "Enter your search terms: "
read -r query
search "$query"
extract_names|nl
printf "Choose the desired media: "
read -r chosen_video
printf "Attempting to retrieve primary source for: "
printf "\"%s\"" "$(extract_names|sed $chosen_video!d)"
printf "...\nPlease wait.\n\n"
2021-12-24 10:42:30 -05:00
chosen_video=$(expr $chosen_video - 1)
2021-10-16 18:55:03 -04:00
itag=$(echo $(fetch_itag $(extract_urls|sed $chosen_video!d))|cut -f$chosen_video -d' ')
id=$(fetch_id)
media="$(generate_locations|sed 1!d)"
printf "Loading %s...\n" "$media"
2021-12-24 10:42:30 -05:00
# ffplay -autoexit "$media"
2021-10-16 18:55:03 -04:00
}
main