GoFile API
若要將檔案上傳至gofile.io,您必須使用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
傳回可用於接收上傳的最佳伺服器回應範例
{"status":"ok","data":{"server":"srv-file9"}}
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' ]
回應範例
{"status":"ok","data":{"code":"123Abc","removalCode":"3ZcBq12nTgb4cbSwJVYY"}}
您的訪問連結將如下所示: https://gofile.io/?c=123Abc
://gofile.io/?c=123Abc
要驗證文件並獲取直接鏈接,您需要透過向伺服器發送您的令牌來驗證您的令牌
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 )
您的存取連結將如下所示: https://srv-file9.gofile.io/download/123Abc/file_name.txt