How do I block ads on YouTube?

this two script are working for me

1º script /etc/pihole/temp.sh

#!/bin/sh
# This script will fetch the Googlevideo ad domains and append them to the Pi-hole block list.
# Run this script daily with a cron job (don't forget to chmod +x)
# More info here: https://discourse.pi-hole.net/t/how-do-i-block-ads-on-youtube/253/136

# File to store the YT ad domains
FILE=/etc/temp/temp.txt

# Wolfram Alfa AppID
APPID="Register at https://www.wolframalpha.com/ put your id here"

# Fetch the list of domains, remove the ip's and save them
curl 'https://api.hackertarget.com/hostsearch/?q=googlevideo.com' \
| awk -F, 'NR>1{print $1}' \
| grep -vE "redirector|manifest" > $FILE

# Replace r*.sn*.googlevideo.com URLs to r*---sn-*.googlevideo.com
# and add those to the list too
curl "http://api.wolframalpha.com/v2/query?input=googlevideo.com&appid=${APPID}&format=plaintext&podstate=WebSiteStatisticsPod:InternetData__Subdomains&podstate=WebSiteStatisticsPod:InternetData__Subdomains_More" \
| grep -Po "r\d+---sn-.+.googlevideo.com" >> $FILE

# Scan log file for previously accessed domains
grep r*.googlevideo.com /var/log/pihole.log \
| awk '{print $6}' \
| grep -v '^googlevideo.com\|redirector\|manifest' \
| sort | uniq >> $FILE

# Replace r*.sn*.googlevideo.com URLs to r*---sn-*.googlevideo.com
# and add those to the list too
cat $FILE | sed -i $FILE -re 's/(^r[[:digit:]]+)(\.)(sn)/\1---\3-/' >> $FILE

#delete duplicates
perl -i -ne 'print if ! $x{$_}++' /etc/temp/temp.txt

#demonstrative
cat $FILE >>  /var/www/html/temp.txt

2º script /etc/pihole/youtube-ads.sh

echo off

#copy file to blacklist
cat /etc/temp/temp.txt >> /var/www/html/youtube.txt

#delete duplicates
perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube.txt

#removes the temporary file on the raspberry pi
rm /var/www/html/temp.txt

#updates pihole blacklist/whitelist
pihole -g

crontab
/35 * * * * /etc/pihole/temp.sh
0
/3 * * * * /etc/pihole/youtube-ads.sh

add http://localhost/youtube.txt blacklist from local

block list:

r5---sn--n4v7sn76.googlevideo.com
r6---sn--n4v7sn76.googlevideo.com
r3---sn--30a7yn76.googlevideo.com
r1---sn--ab5szn76.googlevideo.com

sorry for my English

4 Likes