import requests import tqdm import tarfile import os import shutil import getopt import sys discordUrl = "https://discord.com/api/download?platform=linux&format=tar.gz" tmpDir = "/tmp/dup/" confDir = "~/.config/" installDir = os.path.expanduser("~") + "/.local/lib/Discord" execDir = os.path.expanduser("~") + "/.local/bin/Discord" def download(url: str, dest: str): with open(tmpDir + dest, 'wb') as f: with requests.get(url, stream=True) as r: r.raise_for_status() totalLength = int(r.headers.get('content-length', 0)) tqdm_params = { 'desc': dest, 'total': totalLength, 'miniters': 1, 'bar_format' : "{desc}: |{bar:40}| {percentage:3.1f}% ", } with tqdm.tqdm(**tqdm_params) as pb: for chunk in r.iter_content(chunk_size=8192): pb.update(len(chunk)) f.write(chunk) def rm(dir: str): try: if os.path.isdir(dir) == True: shutil.rmtree(dir) else: os.remove(dir) except Exception as err: print(err) # def rm(folder: str): # try: # shutil.rmtree(folder) # except Exception as err: # print(err) def mkdir(name: str): try: os.mkdir(name) except Exception as err: print(err) def installDiscord(): rm(tmpDir) mkdir(tmpDir) print("Downloading Discord...") download(discordUrl, "discord.tar.gz") print("Extracting..." ) file = tarfile.open(tmpDir+"/discord.tar.gz") mkdir("/tmp/dup/discord") file.extractall(tmpDir+"/discord") file.close() print("Installing...") rm(installDir) shutil.move(tmpDir+"/discord/Discord", installDir) os.symlink(installDir + "/Discord", execDir) print("Cleaning up...") # os.remove(tmpDir+"discord.tar.gz") rm(tmpDir) def uninstall(): rm(installDir) rm(tmpDir) rm(confDir) rm(execDir) argumentList = sys.argv[1:] # Options options = "hiu" # Long options long_options = ["help", "install", "uninstall"] try: # Parsing argument arguments, values = getopt.getopt(argumentList, options, long_options) # checking each argument for currentArgument, currentValue in arguments: if currentArgument in ("-h", "--help"): print ("Displaying Help") elif currentArgument in ("-i", "--install"): installDiscord() elif currentArgument in ("-u", "--uninstall"): uninstall() except getopt.error as err: # output error, and return with an error code print (str(err))