MizunoBrasil

envia-em-massa-para-anonfiles-sem-fazer-pausa

Jul 4th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. import requests
  2. import time
  3. import sys
  4. import os
  5. import datetime
  6. import keyboard
  7.  
  8. def convert_size(size_bytes):
  9.     # Função auxiliar para converter tamanho em bytes para MB e GB
  10.     MB = 1024 * 1024
  11.     GB = MB * 1024
  12.  
  13.     if size_bytes >= GB:
  14.         return f'{size_bytes / GB:.2f} GB'
  15.     elif size_bytes >= MB:
  16.         return f'{size_bytes / MB:.2f} MB'
  17.     else:
  18.         return f'{size_bytes} bytes'
  19.  
  20. def convert_time(seconds):
  21.     # Função auxiliar para converter tempo em segundos para o formato hh:mm:ss
  22.     time_str = str(datetime.timedelta(seconds=seconds))
  23.     return time_str
  24.  
  25. def upload_file(file_path):
  26.     url = 'https://api.anonfiles.com/upload' + "?token=chave-api"  # Adicione sua chave de API aqui
  27.     files = {'file': open(file_path, 'rb')}
  28.     file_size = os.path.getsize(file_path)
  29.  
  30.     start_time = time.time()
  31.     response = requests.post(url, files=files)
  32.     end_time = time.time()
  33.  
  34.     if response.status_code == 200:
  35.         json_data = response.json()
  36.         if json_data['status']:
  37.             file_url = json_data['data']['file']['url']['short']
  38.             print('O arquivo', file_path, 'foi enviado com sucesso!\nURL do arquivo:', file_url)            
  39.         else:
  40.             print('Ocorreu um erro ao enviar o arquivo', file_path + ':', json_data['error']['message'])
  41.     else:
  42.         print('Ocorreu um erro de conexão ao enviar o arquivo', file_path + ':', response.status_code)
  43.  
  44.     upload_time = end_time - start_time
  45.     print('Tempo de upload:', convert_time(upload_time))
  46.     print('************************************************')
  47.     # Salvar a saída do console em um arquivo de texto
  48.     original_filename = file_path.split('/')[-1]
  49.     output_filename = original_filename.split('.')[0] + '_output.txt'
  50.  
  51.     with open(output_filename, 'w') as output_file:
  52.         output_file.write('O arquivo foi enviado com sucesso!\nURL do arquivo: ' + file_url + '\n')
  53.         output_file.write('Tamanho do arquivo: ' + convert_size(file_size) + '\n')
  54.         output_file.write('Tempo de upload: ' + convert_time(upload_time) + '\n')
  55.  
  56. def upload_files_in_folder(folder_path):
  57.     file_list = os.listdir(folder_path)
  58.  
  59.     for file_name in file_list:
  60.         file_path = os.path.join(folder_path, file_name)
  61.         upload_file(file_path)
  62.  
  63. # Exemplo de uso
  64. folder_path = input('Digite o caminho da pasta: ')
  65. upload_files_in_folder(folder_path)
  66.  
Add Comment
Please, Sign In to add comment