import os from subprocess import PIPE, Popen import ctypes import ssl import urllib2 import datetime import time class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) url="https://script-downloads.itarian.com/testing/SpotifyFullSetup.exe" regUrl ="https://script-downloads.itarian.com/testing/spotify.reg" Down_path=os.environ['TEMP'] fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) RegName = regUrl.split('/')[-1] RegDownTo = os.path.join(Down_path, RegName) def ecmd(command): from subprocess import Popen, PIPE import ctypes with disable_file_system_redirection(): obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE) out, err = obj.communicate() ret=obj.returncode if ret==0: if out: return out.strip() else: return ret else: if err: return err.strip() else: return ret def downloadFile(DownTo, fromURL): headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} context = ssl._create_unverified_context() request = urllib2.Request(fromURL, headers=headers) req = urllib2.urlopen(request,context=context) try: with open(DownTo, 'wb') as f: while True: chunk = req.read(100*1000*1000) if chunk: f.write(chunk) else: break if os.path.isfile(DownTo): return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1024) except: return 'Please Check URL or Download Path!' batchfile = """ %s /extract "C:\Program Files\Spotify" reg import %s REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Spotify /v InstallDate /t REG_SZ /d %s """%(DownTo,RegDownTo,"".join(str(datetime.date.today()).split("-"))) ps_content = """ $TargetFile = "C:\Program Files\Spotify\Spotify.exe" $ShortcutFile = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Spotify.lnk" $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) $Shortcut.TargetPath = $TargetFile $Shortcut.Save() $TargetFile = "C:\Program Files\Spotify\Spotify.exe" $ShortcutFile = "C:\Users\Public\Desktop\Spotify.lnk" $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) $Shortcut.TargetPath = $TargetFile $Shortcut.Save() """ removebatch = """ "C:\Program Files\Spotify\Spotify.exe" /uninstall /silent del "C:\Users\Public\Desktop\Spotify.lnk" /s /q del "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Spotify.lnk" /s /q reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Spotify" /f """ def spm_install(dir, args): with disable_file_system_redirection(): ps_name='powershell_file.ps1' ps_path=os.path.join(os.environ['TEMP'], ps_name) with open(ps_path, 'wb') as wr: wr.write(ps_content) batch_name = 'batch_file.bat' batch_path=os.path.join(os.environ['TEMP'], batch_name) with open(batch_path, 'wb') as wr: wr.write(batchfile) print(downloadFile(DownTo, url)) print(downloadFile(RegDownTo, regUrl)) print(ecmd(batch_path)) ecmd('powershell "Set-ExecutionPolicy RemoteSigned"') print ecmd('powershell "%s"'%ps_path) os.remove(DownTo) os.remove(RegDownTo) os.remove(ps_path) os.remove(batch_path) def spm_uninstall(dir, args): with disable_file_system_redirection(): batch_name = 'batch_file.bat' batch_path=os.path.join(os.environ['TEMP'], batch_name) with open(batch_path, 'wb') as wr: wr.write(removebatch) print(ecmd(batch_path)) os.remove(batch_path) def spm_update(dir, args): with disable_file_system_redirection(): users=os.popen("net users").read().split()[5:-4] fil_users=[i.strip() for i in users if i.strip()!="Administrator" and i.strip()!="Guest"] for i in fil_users: if os.path.exists("C:\Users\%s\AppData\Roaming\Spotify"%(i)): print(ecmd('"C:\Users\%s\AppData\Roaming\Spotify\Spotify.exe" /uninstall /silent'%(i))) if os.path.exists("C:\Program Files\Spotify"): spm_uninstall(None, None) time.sleep(20)