async def download(self, path: str, load_chunk_size: int = 1024) -> None:
        """ニコニコ動画の動画をダウンロードします。  
        mp4形式でダウンロードされます。

        Examples
        --------
        ```python
        url = "https://www.nicovideo.jp/watch/sm9720246"
        async with NicoNicoVideoAsync(url) as nico:
            data = await nico.get_info()
            title = data["video"]["title"]
            await nico.download(title + ".mp4")
        ```

        Notes
        -----
        もし`async with`構文を使用しないでダウンロードする場合は、ダウンロード前に`connect`を、ダウンロード終了後に`close`を実行してください。  
        動画データの通信に必要なHeartbeatが永遠に動き続けることになります。

        Parameters
        ----------
        path : str
            ダウンロードするニコニコ動画の動画の保存先です。
        load_chunk_size : int, default 1024
            一度にどれほどの量をダウンロードするかです。"""
        self.print("Now loading...")
        url = await self.get_download_link()

        params = (
            ("ht2_nicovideo",
             self.result_data["content_auth"]["content_auth_info"]["value"]), )
        headers = self._headers[1]
        headers["Content-Type"] = "video/mp4"

        BASE = "Downloading video... :"
        self.print(BASE, "Now loading...", first="\r", end="")

        async with ClientSession(raise_for_status=True) as session:
            async with session.get(url, headers=headers, params=params) as r:
                size, now_size = r.content_length, 0

                self.print(BASE, "Making a null file...", first="\r", end="")

                async with async_open(path, "wb") as f:
                    await f.write(b"")
                async with async_open(path, "ab") as f:
                    async for chunk in r.content.iter_chunked(load_chunk_size):
                        if chunk:
                            now_size += len(chunk)
                            await f.write(chunk)
                            self.print(
                                BASE,
                                f"{int(now_size/size*100)}% ({now_size}/{size})",
                                first="\r",
                                end="")

        self.print("Done.")
Exemple #2
0
 async def load(self, source: PathLike, buffer_size: int = 16384) -> None:
     path = Path(source)
     self.filename = path.name
     async with async_open(path, "rb") as file_:
         data = await file_.read(buffer_size)
         while data != b"":
             self.stream.write(data)
             data = await file_.read(buffer_size)
Exemple #3
0
async def restore_state():
    global trolls

    try:
        async with async_open(STATE_PATH) as state_file:
            state = loads(await state_file.read())
            trolls = state["trolls"]
    except (FileNotFoundError, JSONDecodeError, TypeError) as exc:
        pass
Exemple #4
0
async def test_aiofile_upload_download(objectService, tmp_path):
    src = tmp_path / "src"
    dest = tmp_path / "dest"

    data = secrets.token_bytes(102400)
    with open(src, "wb") as f:
        f.write(data)

    name = f"taskcluster/test/client-py/{taskcluster.slugid.v4()}"
    async with async_open(src, "rb") as file:
        async def readerFactory():
            file.seek(0)
            return file

        await upload.upload(
            projectId="taskcluster",
            name=name,
            contentType="text/plain",
            contentLength=len(data),
            expires=taskcluster.fromNow('1 hour'),
            readerFactory=readerFactory,
            objectService=objectService)

    async with async_open(dest, "wb") as file:
        async def writerFactory():
            file.seek(0)
            file.truncate()
            return file

        contentType = await download.download(
            name=name,
            writerFactory=writerFactory,
            objectService=objectService)

    with open(dest, "rb") as f:
        got = f.read()

    assert got == data
    assert contentType == 'text/plain'
Exemple #5
0
    async def save(self,
                   destination: PathLike,
                   buffer_size: int = 16384) -> None:  # type: ignore
        """Save the file to the destination.

        Arguments:
            destination: A filename (str) or file object to write to.
            buffer_size: Buffer size to keep in memory.
        """
        async with async_open(destination, "wb") as file_:
            data = self.stream.read(buffer_size)
            while data != b"":
                await file_.write(data)
                data = self.stream.read(buffer_size)
Exemple #6
0
async def send_file(
    filename: FilePath,
    add_etags: bool = True,
    cache_timeout: Optional[int] = None,
    last_modified: Optional[datetime] = None,
) -> Response:
    """Return a Reponse to send the filename given.

    Arguments:
        filename: The filename (path) to send, remember to use
            :func:`safe_join`.
        add_etags: Set etags based on the filename, size and
            modification time.
        cache_timeout: Time in seconds for the response to be cached.
        last_modified: Used to override the last modified value.

    """
    file_path = os.fspath(filename)
    mimetype = mimetypes.guess_type(
        os.path.basename(file_path))[0] or DEFAULT_MIMETYPE
    async with async_open(file_path, mode='rb') as file_:
        data = await file_.read()
    response = current_app.response_class(data, mimetype=mimetype)

    if last_modified is not None:
        response.last_modified = last_modified
    else:
        response.last_modified = datetime.fromtimestamp(
            os.path.getmtime(file_path))

    response.cache_control.public = True
    cache_timeout = cache_timeout or current_app.get_send_file_max_age(
        file_path)
    if cache_timeout is not None:
        response.cache_control.max_age = cache_timeout
        response.expires = datetime.utcnow() + timedelta(seconds=cache_timeout)

    if add_etags:
        file_tag = file_path.encode('utf-8') if isinstance(file_path,
                                                           str) else file_path
        response.set_etag(
            '{}-{}-{}'.format(
                os.path.getmtime(file_path),
                os.path.getsize(file_path),
                adler32(file_tag),
            ), )
    return response
Exemple #7
0
    async def open_resource(
        self,
        path: FilePath,
        mode: str = "rb",
    ) -> AiofilesContextManager[None, None, AsyncBufferedReader]:
        """Open a file for reading.

        Use as

        .. code-block:: python

            async with await app.open_resource(path) as file_:
                await file_.read()
        """
        if mode not in {"r", "rb"}:
            raise ValueError("Files can only be opened for reading")
        return async_open(self.root_path / file_path_to_path(path),
                          mode)  # type: ignore
Exemple #8
0
async def save_state():
    async with async_open(STATE_PATH, "w") as state_file:
        state = {"trolls": trolls}
        await state_file.write(dumps(state))
Exemple #9
0
 async def __aenter__(self) -> "FileBody":
     self.file_manager = async_open(self.file_path, mode="rb")
     self.file = await self.file_manager.__aenter__()
     await self.file.seek(self.begin)
     return self
Exemple #10
0
    async def setup_database(self):
        db = await asyncpg.create_pool(**self.credentials)
        async with async_open("setup.sql") as sql:
            await db.execute(await sql.read())

        self.bot.db = db
async def write_file_to_tmp_dir(path: str, body: bytes):
    async with async_open(path, 'wb+') as f:
        await f.write(body)
Exemple #12
0
async def send_file(filename: str) -> Response:
    mimetype = mimetypes.guess_type(
        os.path.basename(filename))[0] or DEFAULT_MIMETYPE
    async with async_open(filename, mode='rb') as file_:
        data = await file_.read()
    return current_app.response_class(data, mimetype=mimetype)