[Batch] Spotify AdBlock & Misc

Discussion in 'Scripting' started by NormieLyfe, Dec 28, 2017.

  1. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
    #1 NormieLyfe, Dec 28, 2017
    Last edited: Jan 4, 2018
    Hey MDL, this might be a bit off since most threads here are Microsoft Related and such but this is a bit different... the Spotify Adblock idea isn't an original idea of mine, heck there's already a program for that EZBlocker but for those cmd savvy people this might interest you.

    How It Works?
    Simple, it justs adds a network rule that redirects known spotify ad sites to your local IP adress

    How to run it?
    Copy the code, paste it in any text file editor, save it as "SpotifyAdBlock.cmd" and run it (right-click "run as administrator" if needed)

    Special Thanks
    a post in r/Piracy

    Tip
    if you're using the web application of spotify (https://open.spotify.com/) use Ublock Orgin to block its ads
    or... if you like the product why not buy the premium subcription
    for android device try Spotify Dogfood

    P.S
    If you do know more about spotify ad site(s) or found a site in the current list that isn't an ad site please comment down below or message me, I'd like to keep this thread updated :D

    CODE
    Code:
    @echo off
    
    :CheckAdminRights
    set UAC="%temp%\GetAdmin.vbs"
    fltmc >nul 2>&1 || (title Requesting Admin Privileges & (echo Set UAC=CreateObject^("Shell.Application"^):UAC.ShellExecute "%~f0","","","runas",1)>%UAC% & %UAC% & del /f /q %UAC% & exit)
    
    :PreCheck
    set "RegKey=Software\Microsoft\Windows Script Host\Settings"
    for %%a in (HKLM,HKCU) do reg query "%%a\%RegKey%" /v "Enabled">nul 2>&1 && (reg delete "%%a\%RegKey%" /f /v "Enabled">nul 2>&1 & goto PreCheck)
    
    :Payload
    title Spotify AdBlock & color 2F
    setlocal EnableDelayedExpansion
    
    set "IP=127.0.0.1"
    set "path=%windir%\system32\drivers\etc"
    
    set "AdSite1=adclick.g.doublecklick.net,adeventtracker.spotify.com,ads-fa.spotify.com,analytics.spotify.com,audio2.spotify.com,b.scorecardresearch.com,bounceexchange.com"
    set "AdSite2=bs.serving-sys.com,content.bitsontherun.com,core.insightexpressai.com,crashdump.spotify.com,d2gi7ultltnc2u.cloudfront.net,d3rt1990lpmkn.cloudfront.net,desktop.spotify.com"
    set "AdSite3=doubleclick.net,ds.serving-sys.com,googleadservices.com,googleads.g.doubleclick.net,gtssl2-ocsp.geotrust.com,js.moatads.com,media-match.com,omaze.com,pagead46.l.doubleclick.net"
    set "AdSite4=pagead2.googlesyndication.com,partner.googleadservices.com,pubads.g.doubleclick.net,redirector.gvt1.com,s0.2mdn.net,securepubads.g.doubleclick.net,spclient.wg.spotify.com,tpc.googlesyndication.com"
    set "Adsite5=v.jwpcdn.com,video-ad-stats.googlesyndication.com,weblb-wg.gslb.spotify.com,www.googleadservices.com,www.googletagservices.com,www.omaze.com"
    
    ::find Spotify Adblock keyword "Present" in Hosts file, if found Spotify Adblock Implemented
    echo Checking...
    for %%a in ("Hosts files","hosts") do findstr /i "present2" "%windir%\system32\drivers\etc\%%~a">nul 2>&1 && (
        cd /d "%~dp0"
        echo Reblocking Spotify Adsites...
        call :Reblock "hosts file"
        call :Reblock "hosts"
        echo Spotify Ads has been reblocked
        goto end
    )
    for %%a in ("Hosts files","hosts") do findstr /i "present" "%windir%\system32\drivers\etc\%%~a">nul 2>&1 && (
        cd /d "%~dp0"
        echo Unblocking Spotify Adsites...
        call :Deblock "hosts file"
        call :Deblock "hosts"
        echo Spotify Ads has been unblocked
        goto end
    )
    
    cd /d "%~dp0"
    echo Blocking Spotify Ads...
    call :AdBlock "hosts file"
    call :AdBlock "hosts"
    echo Spotify Ads has been blocked
    :end
    cd /d "%~dp0"
    echo.
    echo Please restart your spotify if running
    echo.
    pause & exit
    
    ::Adds Network Rule to Redirect Known Spotify Ad Sites to Localhost IP
    :AdBlock
    echo. >> "%path%\%~1"
    echo #Spotify Ad Sites, Present >> "%path%\%~1"
    for %%a in (1,2,3,4,5) do (
        for %%b in (!AdSite%%a!) do echo %IP% %%b >> "%path%\%~1"
    )
    exit /b
    
    ::Search and deletes known network rule to reverse effect
    :Deblock
    set "tempdir=%path%\%~1.tmp"
    set "filedir=%path%\%~1"
    if exist "%tempdir%" (del /f /q "%tempdir%")
    for /f "tokens=* delims=" %%a in (%filedir%) do (
        set "line=%%a"
        set "line=!line:#Spotify Ad Sites, Present=#Spotify Ad Sites, Present2!"
        for %%a in (1,2,3,4,5) do (
            for %%b in (!AdSite%%a!) do set "line=!line:%IP% %%b=#%IP% %%b!"
        )
        echo.!line!>> "%tempdir%"
    )
    del /f /q "%filedir%"
    ren "%tempdir%" "%~1"
    exit /b
    
    ::Similar to deblock but in reverse
    :Reblock
    set "tempdir=%path%\%~1.tmp"
    set "filedir=%path%\%~1"
    if exist "%tempdir%" (del /f /q "%tempdir%")
    for /f "tokens=* delims=" %%a in (%filedir%) do (
        set "line=%%a"
        set "line=!line:#Spotify Ad Sites, Present2=#Spotify Ad Sites, Present!"
        for %%a in (1,2,3,4,5) do (
            for %%b in (!AdSite%%a!) do set "line=!line:#%IP% %%b=%IP% %%b!"
        )
        echo.!line!>> "%tempdir%"
    )
    del /f /q "%filedir%"
    ren "%tempdir%" "%~1"
    exit /b
    

    Misc
    InstallSpotifyOffline.cmd - Extract Compressed Pre-installed Spotify (it's sort of like an offline installer)
    Code:
    @echo off
    
    :CheckAdminRights
    set UAC="%temp%\GetAdmin.vbs"
    fltmc >nul 2>&1 || (title Requesting Admin Privileges & (echo Set UAC=CreateObject^("Shell.Application"^):UAC.ShellExecute "%~f0","","","runas",1)>%UAC% & %UAC% & del /f /q %UAC% & exit)
    
    :PreCheck
    set "RegKey=Software\Microsoft\Windows Script Host\Settings"
    for %%a in (HKLM,HKCU) do reg query "%%a\%RegKey%" /v "Enabled">nul 2>&1 && (reg delete "%%a\%RegKey%" /f /v "Enabled">nul 2>&1 & goto PreCheck)
    
    cd /d "%~dp0" & color 2F
    title [Offline] Spotify Installer
    
    set "SpotifyData=SpotifyData.zip"
    set "SpotifyExe=%appdata%\Spotify\Spotify.exe"
    set "SpotifyData=%~dp0%SpotifyData%"
    
    echo Checking Spotify...
    if exist "%SpotifyExe%" (echo Spotify already installed, Please uninstall before proceeding & goto errorEnd)
    echo Checking compressed data...
    if not exist "%SpotifyData%" (echo %SpotifyData% not found & goto errorEnd)
    echo Extracting compressed data...
    Call :UnZipFile "%appdata%" "%SpotifyData%"
    if not exist "%SpotifyExe%" (
        echo ERROR: Spotify.exe not found after installation. %SpotifyData% may have been tampered with
        echo or an interruption occured while extracting data, please use the correct %SpotifyData%.
        goto errorEnd
    )
    set shortcut="%temp%\SpotifyShortcut.vbs"
    (
    echo Set oWS=WScript.CreateObject^("WScript.Shell"^)
    echo sLinkFile="%userprofile%\Desktop\Spotify.lnk"
    echo Set oLink=oWS.CreateShortcut^(sLinkFile^)
    echo oLink.TargetPath="%SpotifyExe%"
    echo oLink.IconLocation="%SpotifyExe%"
    echo oLink.Save
    )>"%shortcut%"
    cscript //nologo %shortcut% & del /f /q %shortcut%
    start "" "%SpotifyExe%"
    exit
    
    :errorEnd
    echo. & pause & exit
    
    :UnZipFile
    set vbs="%temp%\ExtractSpotify.vbs"
    if exist %vbs% del /f /q %vbs%
    (
    echo Set fso = CreateObject^("Scripting.FileSystemObject"^)
    echo If NOT fso.FolderExists^(%1^) Then
    echo fso.CreateFolder^(%1^)
    echo End If
    echo set objShell = CreateObject^("Shell.Application"^)
    echo set FilesInZip=objShell.NameSpace^(%2^).items
    echo objShell.NameSpace^(%1^).CopyHere^(FilesInZip^)
    echo Set fso = Nothing
    echo Set objShell = Nothing
    )>>%vbs%
    cscript //nologo %vbs%
    if exist %vbs% del /f /q %vbs%
    

    CompressSpotifyData.cmd - Copies and Compress Installed Spotify for later use
    Code:
    @echo off & color 2F
    setLocal EnableDelayedExpansion & cd /d "%~dp0"
    title Zip Spotify
    
    set "Zip=SpotifyData.zip"
    set "Source=%appdata%\spotify"
    set vbs="%temp%\Import2Zip.vbs"
    
    echo Checking Spotify...
    if not exist "%Source%\Spotify.exe" (echo Spotify not found & goto end)
    echo Checking existing data...
    if exist "%Zip%" (echo Spotify Data exist, delete existing data and run this script again & goto end)
    
    ::Calculates the time needed to compress and folder/file
    call :Dirsize "%Source%"
    echo Compressing Spotify...
    (
    echo Set objArgs = WScript.Arguments
    echo Folder = objArgs^(0^)
    echo Zip = objArgs^(1^)
    echo CreateObject^("Scripting.FileSystemObject"^).CreateTextFile^(Zip, True^).Write "PK" ^& Chr^(5^) ^& Chr^(6^) ^& String^(18, vbNullChar^)
    echo Set objShell = CreateObject^("Shell.Application"^)
    echo Set source = objShell.NameSpace^(Folder^).Items
    echo objShell.NameSpace^(Zip^).CopyHere^(source^)
    echo wScript.Sleep 16000
    )>>"%vbs%"
    cscript //nologo %vbs% "%Source%" "%~dp0%Zip%"
    if exist %vbs% del /f /q %vbs%
    echo.
    echo Spotify sucessfully compressed
    :end
    echo. & pause & exit
    

    UninstallSpotify.cmd - Deletes known spotify directory (registry not affected)
    Code:
    @echo off
    
    :CheckAdminRights
    set UAC="%temp%\GetAdmin.vbs"
    fltmc >nul 2>&1 || (title Requesting Admin Privileges & (echo Set UAC=CreateObject^("Shell.Application"^):UAC.ShellExecute "%~f0","","","runas",1)>%UAC% & %UAC% & del /f /q %UAC% & exit)
    
    :PreCheck
    set "RegKey=Software\Microsoft\Windows Script Host\Settings"
    for %%a in (HKLM,HKCU) do reg query "%%a\%RegKey%" /v "Enabled">nul 2>&1 && (reg delete "%%a\%RegKey%" /f /v "Enabled">nul 2>&1 & goto PreCheck)
    
    cd /d "%~dp0" & color 2F
    title Uninstall Spotify
    set "Source=%appdata%\spotify"
    echo Checking Spotify...
    if not exist "%Source%\Spotify.exe" (echo Spotify not found & goto end)
    echo Checking terminating Spotify processes...
    for %%a in (SpotifyWebHelper,Spotify) do (
        Tasklist /FI "imagename eq %%a.exe" | find /i "%%a.exe">nul && (Taskkill /IM "%%a.exe" /F >nul)
    )
    echo Uninstalling Spotify...
    if exist "%userprofile%\Desktop\Spotify.lnk" del /f /q "%userprofile%\Desktop\Spotify.lnk"
    for %%a in (Roaming,Local) do rd /s /q "%userprofile%\Appdata\%%a\Spotify"
    echo Successfuly Uninstalled Spotify
    :end
    echo. & pause & exit
    
    :Dirsize
    set /a value=0
    set /a sum=0
    set /a time=0
    FOR /R %1 %%I IN (*) DO (
    set /a value=%%~zI/1024
    set /a sum=!sum!+!value!
    )
    set /a time=!sum!/9
    exit /b
    
     
  2. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
    UPDATE:

    Added Misc
    Code:
    InstallSpotifyOffline.cmd 
    CompressSpotifyData.cmd
    UninstallSpotify.cmd
    
     
  3. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
    UPDATE2:

    Added Unblock... it has bit of draw back though it leaves a lot of blank spaces when unblocked, still trying to figure out a way but at least it's reversable now
     
  4. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
    UPDATE3:

    Minor changes to SpotifyAdblock.cmd
     
  5. Erik B

    Erik B MDL Member

    Oct 10, 2008
    105
    26
    10
    Thanks for this. :)
    Been using the web app (with ublock) since I exited premium. Testing the store app right now, so far so good.

    Maybe it would be a good idea for you to publish the script at github (https://github.com). Updates and feedback would probably be easier.
     
  6. Goldrake1

    Goldrake1 MDL Novice

    Jul 28, 2018
    1
    0
    0
    Hello , i'm new here , but if you want the Daily Mix Page , simply remove from ad this

    spclient.wg.spotify.com
    It is on "AdSite4" in the end <3
    Cheeersss :D

     
  7. rafael667

    rafael667 MDL Novice

    Feb 12, 2019
    3
    0
    0
    I'm using this and having trouble save music in the library. It simply doesn't add the song/album.
     
  8. rafael667

    rafael667 MDL Novice

    Feb 12, 2019
    3
    0
    0
    Blocking the 0.0.0.0 spclient.wg.spotify.com is causing the problem. But without blocking it the ads come back.
    Anyone is having the same issue?
     
  9. KaylaTH

    KaylaTH MDL Novice

    May 9, 2019
    1
    0
    0
    Yes, the same as me and still no luck to add songs to my library. But I have found another way to block ads on Spotify Without Premium from TuneFab. You deserve to have a try.
     
  10. rafael667

    rafael667 MDL Novice

    Feb 12, 2019
    3
    0
    0
    Is there any update? still can't add songs to my library with this patch
     
  11. 5lb30zBabyJesus

    5lb30zBabyJesus MDL Novice

    Jul 18, 2017
    1
    0
    0
    Thank you,worked perfectly!