예제 #1
0
class Client_machine:
    def __init__(self):
        self.files = Files()

    def run(self, json_proses):
        objek = json.loads(json_proses)
        massage = {}
        try:
            perintah = objek['perintah']
            if perintah == 'list':
                massage['list'] = self.files.list_file()
                respon = 'Berhasil'
            elif perintah == 'upload':
                filename = objek['filename']
                data = objek['isi']
                isi = data.encode()
                ret_val = self.files.upload_file(filename, isi)
                respon = 'Berhasil' if ret_val else 'File sudah tersedia'
            elif perintah == 'download':
                filename = objek['filename']
                ret_val, binary = self.files.download_file(filename)
                isi = binary.decode()
                massage['isi'] = isi
                respon = 'Berhasil' if ret_val else 'File tidak ditemukan'
            else:
                respon = 'Perintah salah'
        except:
            print(e.what())
            respon = 'ERROR'
        finally:
            massage['respon'] = respon
            return json.dumps(massage)
def download_files(file_list):
    # Old method using scraping
    # files = Files.get_files(messages)
    if len(file_list) == 0:
        return

    # Download files
    print("")
    for file in file_list:
        success = Files.download_file(args.token,
                                      file,
                                      args.files,
                                      user_map,
                                      overwrite=args.files_overwrite)

        if success:
            Status.tot_files += 1
        else:
            Status.file_failures += 1

    # Status messages
    print("File download complete")
    if Status.files_already_exist == 0:
        return
    if args.files_overwrite:
        print(f"{Status.files_already_exist} files were overwritten")
    else:
        print(
            f"{Status.files_already_exist} files were not downloaded as files with the same name already existed"
        )