Last active
April 2, 2024 15:29
-
-
Save parnexcodes/df24e9ce16f63c3ed1fde8c976d43d81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
def get_server(): | |
url = "https://api.gofile.io/servers" | |
response = requests.get(url) | |
if response.status_code == 200: | |
servers = response.json()["data"]["servers"] | |
if servers: | |
return servers[0]["name"] # Selecting the first server, you may adjust this logic if needed | |
else: | |
print("No servers available.") | |
return None | |
else: | |
print(f"Failed to get server. Status code: {response.status_code}") | |
return None | |
def upload_to_gofile(path, token): | |
server_name = get_server() | |
if not server_name: | |
return None | |
upload_url = f"https://{server_name}.gofile.io/uploadFile" | |
headers = { | |
"Authorization": f"Bearer {token}" | |
} | |
links = {} | |
if os.path.isdir(path): | |
for root, _, files in os.walk(path): | |
for file_name in files: | |
file_path = os.path.join(root, file_name) | |
with open(file_path, "rb") as file: | |
files = {"file": file} | |
response = requests.post(upload_url, headers=headers, files=files) | |
if response.status_code == 200: | |
data = response.json()["data"] | |
download_page = data["downloadPage"] | |
links[file_name] = download_page | |
else: | |
print(f"Failed to upload {file_path}. Status code: {response.status_code}") | |
else: | |
with open(path, "rb") as file: | |
files = {"file": file} | |
response = requests.post(upload_url, headers=headers, files=files) | |
if response.status_code == 200: | |
data = response.json()["data"] | |
download_page = data["downloadPage"] | |
links[os.path.basename(path)] = download_page | |
else: | |
print(f"Failed to upload {path}. Status code: {response.status_code}") | |
return links | |
# Example usage: | |
if __name__ == "__main__": | |
# Example usage for uploading a folder | |
path = r"/path/to/your/folder" # Specify the file/folder path here | |
token = "YOUR_TOKEN" # Specify your token here | |
uploaded_links = upload_to_gofile(path, token) | |
if uploaded_links: | |
for filename, link in uploaded_links.items(): | |
print(f"{filename} : {link}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment