Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过python为mirrorace.org发送api请求_Python_Php_Api - Fatal编程技术网

如何通过python为mirrorace.org发送api请求

如何通过python为mirrorace.org发送api请求,python,php,api,Python,Php,Api,我想上传mirrorace中的文件,但我不理解他们的api。他们给出了一个php代码示例,但我不理解。有人能告诉我如何从python发出post请求吗 这是他们的api文档 请有人帮帮我,我是python新手。我第一次听说mirrorace.org,所以我在几分钟内专门为这个问题编写了这个代码,尽管如此,它确实适用于默认镜像: from requests import Session import mimetypes from pathlib import Path def get_mime(

我想上传mirrorace中的文件,但我不理解他们的api。他们给出了一个php代码示例,但我不理解。有人能告诉我如何从python发出post请求吗 这是他们的api文档 请有人帮帮我,我是python新手。

我第一次听说mirrorace.org,所以我在几分钟内专门为这个问题编写了这个代码,尽管如此,它确实适用于默认镜像:

from requests import Session
import mimetypes
from pathlib import Path

def get_mime(f):
    return mimetypes.guess_type(f)[0]

s = Session()

api_key = "xxx"
api_token = "xxx"
req1_url = "https://mirrorace.com/api/v1/file/upload" # get info needed for req2

data = {"api_key": api_key, "api_token": api_token}
req1 = s.post(req1_url, data=data).json()
if req1['status'] == 'success':
    upload_key = req1['result']['upload_key']
    cTracker = req1['result']['cTracker']
    server_file = req1['result']['server_file']
    max_mirrors = req1['result']['max_mirrors'] # max mirror allowed to upload
    mirrors = list([k for k, v in req1['result']['mirrors'].items() if v])[:int(max_mirrors)] # create a list of enabled mirror id's with cap based on max_mirrors

    if upload_key and cTracker and server_file and mirrors:
        f = "C:/Users/0x/Desktop/A CARNE (letra e vídeo) com SEU JORGE, vídeo MOACIR SILVEIRA (320 kbps).mp3"
        fn = Path(f).name
        print("FILE NAME:", fn)
        mime_type = get_mime(f)
        print("MIME TYPE:", mime_type)

        # file = {"files": open(f,'rb')} # also works
        file = {'files': (fn, open(f,'rb'), mime_type)}
        # file = {'files': ("new_filename.mp3", open(f,'rb'), mime_type)}  # also works
        
        data = {"api_key": api_key, "api_token": api_token, "cTracker": cTracker, "upload_key": upload_key, "mirrors[]": mirrors}
        req2 = s.post(server_file, files=file, data=data).json()
        if req2['status'] == 'success':
            # {'status': 'success', 'result': {'name': 'A CARNE (letra e vídeo) com SEU JORGE, vídeo MOACIR SILVEIRA (320 kbps).mp3', 'size': 11929965, 'slug': '3Jcad', 'url': 'https://mirrorace.org/m/3Jcad', 'info': 'complete'}}
            print(req2['result']['url'])
        else:
            print("Error: ", req2)
    else:
        print("Error data missing: ",  upload_key, cTracker, server_file, mirrors)
else:
    print("Error: ", req1)

如果我是你,我会去图书馆看看

以下是您可以查看的其他一些资源:


当然,您还可以找到许多其他正在使用的API库。我建议您尽可能远离任何支持“异步”的设备。Async对python来说是相对较新的,虽然我经常使用它,但对于刚开始使用该语言的人来说,它肯定是一种火洗礼式的场景。

欢迎观看,很酷的电影!(我喜欢电影。)不幸的是,这是一个非常普通的问题,StackOverflow不一定是最好的地方。您可能希望联系Mirror Ace以获得支持,找到可以提供帮助的留言板或聊天组,或其他资源。祝你好运。你已经试过什么了?你看过文件了吗?如果是这样的话,你为什么不提一下你打算使用哪种上传方式,直接上传还是远程上传?