API GoFile
Para fazer upload de um arquivo para gofile.io você deve usar multipart/form-data .
from requests_toolbelt . multipart . encoder import MultipartEncoder
import aiohttp
import asyncio
import sys
import requests
async def encode_file ( name : str ) -> dict :
mp_encoder = MultipartEncoder (
fields = {
'filesUploaded' : ( name , open ( name , 'rb' ))
}
)
return mp_encoder
async def get_server () -> str :
url = "https://api.gofile.io/getServer"
async with aiohttp . ClientSession () as session :
async with session . get ( url ) as resp :
server = await resp . json ()
return server [ 'data' ][ 'server' ]
async def upload_server ( server : str ) -> dict :
try :
url = f"https:// { server } .gofile.io/uploadFile"
data_json = await encode_file ( "music.m4a" )
#Unable to send file using async methodologies like aiohttp, some kind of weird error happens
#It says the uploaded file format is incorrect
#I still don't have a solution for this.
#ERROR: Only io.IOBase, multidict and (name, file) pairs allowed, use .add_field() for passing more complex parameters
response = requests . post (
url ,
data = data_json ,
headers = { 'Content-Type' : data_json . content_type }
)
return response . json ()
except Exception as e :
trace_back = sys . exc_info ()[ 2 ]
line = trace_back . tb_lineno
print ( f"ERROR UPLOAD -> { e } | { line } " )
async def main () -> None :
server_get = await get_server ()
response = await upload_server ( server = server_get )
print ( 'LINK: ' , response [ 'data' ][ 'downloadPage' ])
asyncio . run ( main ())
method GET
https://apiv2.gofile.io/getServer
Retorna o melhor servidor disponível para receber uploads Exemplo de resposta
{"status":"ok","data":{"server":"srv-file9"}}
exemplo em python
from requests_toolbelt . multipart . encoder import MultipartEncoder
import requests
import json
mp_encoder = MultipartEncoder (
fields = {
'filesUploaded' : ( 'file_name.txt' , open ( 'file_name.txt' , 'rb' ))
}
)
r = requests . post (
'https://srv-file9.gofile.io/upload' ,
data = mp_encoder ,
headers = { 'Content-Type' : mp_encoder . content_type }
)
scrap = r . json ()
#send "Token file" 123Abc
Token = scrap [ 'data' ][ 'code' ]
Exemplo de resposta
{"status":"ok","data":{"code":"123Abc","removalCode":"3ZcBq12nTgb4cbSwJVYY"}}
Seu link de acesso ficará assim: https://gofile.io/?c=123Abc
Para validar o arquivo e obter o link direto, você precisará validar seu token no servidor, enviando-lhe seu token
exemplo em python
import requests
session = requests . Session ()
sendToken = 'https://apiv2.gofile.io/getServer?c={}' . format ( Token )
validating = 'https://srv-file9.gofile.io/getUpload?c={}' . format ( Token )
headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 OPR/67.0.3575.137' }
session . get ( sendToken , headers = headers )
session . get ( validating , headers = headers )
r = 'https://srv-file9.gofile.io/download/{}/file_name.txt' . format ( Token )
Seu link de acesso ficará assim: https://srv-file9.gofile.io/download/123Abc/file_name.txt