0

I try to uplaod media on FB via subprocess and curl, it seems there is some problem with the formatting of the fstrings that pass the args as fstring into the command.

My original working curl command looks like this:

curl -F 'source=@output.mp4' -F 'description=123' 'https://graph.facebook.com/me/videos?access_token=mytoken'

And this is my implementation of this command via subprocess:

import subprocess




def add_static_image_to_audio(text, media):
    if ".mp" in media:
        facebook = ["curl", "-F", f"'source=@{media}'", "-F", f"'description={text}'", "'https://graph.facebook.com/me/videos?access_token=mytoken'"]
        try:
            subprocess.check_output(facebook, shell=True, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
    else:
        facebook = ["curl", "-F", f"'source=@{media}'", "-F", f"'description={text}'", "'https://graph.facebook.com/me/photos?access_token=mytoken'"]
        try:
            subprocess.check_output(facebook, shell=True, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Simple Python script to add a static image to an audio to make a video")
    parser.add_argument("text", help="The text")
    parser.add_argument("media", help="image")
    args = parser.parse_args()
    add_static_image_to_audio(args.text, args.media)

Now the error that occurs when executing the script:

RuntimeError: command '['curl', '-F', "'source=@output.mp4'", '-F', "'description=123'", "'https://graph.facebook.com/me/videos?access_token=mytoken'"]' return with error (code 26): b'curl: (26) Failed to open/read local data from file/application\r\n'

What am I doing wrong?

suessi1028
  • 23
  • 2

0 Answers0