0

I am trying to make an application that downloads something and puts it on an FTP server. There is not enough space to, even temporarily, put those files that are being downloaded on my computer, so I want them to download into the FTP server.

Specifically, I am using pytube to download a video using:

stream.download()

The .download() can hold a path variable, though I do not know how to use it to send the FTP server this way. I am looking for a way to open an FTP directory, so that I can use the path to fill in there.

Any help would be greatly appreciated.

4

1 回答 1

1

ftputil maintainer here. :-)

I haven't worked with pytube yet, so I can only answer the ftputil part.

If you really don't or can't save a video file temporarily to your computer, you'd need a way to get a file-like object for the video download from Pytube. (I haven't been able to see how. There's an interface for "streams", but I'm not sure if they're directly usable as file-like objects.)

If you have a file object from Pytube, you can create a file-like object for writing on the FTP server for a path of your choice and copy from file object to file object:

import shutil

import ftputil
import pytube


# Get file-like object for video.
source_file = ...

with ftputil.FTPHost(host, user, password) as ftp_host:
    with ftp_host.open('/path/to/target_file', 'wb') as target_file:
        shutil.copyfileobj(source_file, target_file)

I'm aware that the Pytube part is missing. Maybe someone else can contribute that, or you can ask on their ticket system. (I didn't find a mailing list or similar.)

All that said and even if it doesn't answer your actual question, you may be able to download each video file locally and upload it to the FTP server, file by file. That way, you'd only need space for the largest video file.

Links for the code example:

于 2020-11-07T15:24:47.330 回答