From 696cfa4622eabee914ab2b6c39696a192968aa22 Mon Sep 17 00:00:00 2001 From: Trevor Hobenshield Date: Thu, 7 Dec 2023 19:33:50 -0800 Subject: [PATCH] add new batched endpoint `TweetResultsByRestIds` - fix download fn --- twitter/__version__.py | 2 +- twitter/scraper.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/twitter/__version__.py b/twitter/__version__.py index 3b0dc61..136ae58 100644 --- a/twitter/__version__.py +++ b/twitter/__version__.py @@ -1,5 +1,5 @@ __title__ = "twitter-api-client" __description__ = "Implementation of X/Twitter v1, v2, and GraphQL APIs." -__version__ = "0.10.13" +__version__ = "0.10.14" __author__ = "Trevor Hobenshield" __license__ = "MIT" \ No newline at end of file diff --git a/twitter/scraper.py b/twitter/scraper.py index bf16dcb..9811ead 100644 --- a/twitter/scraper.py +++ b/twitter/scraper.py @@ -238,7 +238,7 @@ def users_by_id(self, user_ids: list[int], **kwargs) -> list[dict]: """ return self._run(Operation.UserByRestId, user_ids, **kwargs) - def download_media(self, ids: list[int], photos: bool = True, videos: bool = True, chunk_size: int = 8192, stream: bool = False) -> None: + def download_media(self, ids: list[int], photos: bool = True, videos: bool = True, chunk_size: int = 8192, stream: bool = False, out: str = 'media') -> None: """ Download media from tweets by tweet ids. @@ -249,9 +249,9 @@ def download_media(self, ids: list[int], photos: bool = True, videos: bool = Tru @params stream: flag to enable downloading raw stream @return: None """ - out = Path('media') + out = Path(out) out.mkdir(parents=True, exist_ok=True) - tweets = self.tweets_by_id(ids) + tweets = self.tweets_by_id(ids) # todo: switch to batch method tweets_by_ids urls = [] for tweet in tweets: tweet_id = find_key(tweet, 'id_str')[0] @@ -285,7 +285,7 @@ async def download(client: AsyncClient, post_url: str, cdn_url: str, stream: boo else: r = await client.get(cdn_url) async with aiofiles.open(fname, 'wb') as fp: - for chunk in r.iter_bytes(chunk_size): + async for chunk in r.aiter_raw(chunk_size): await fp.write(chunk) except Exception as e: