fixed memory leak related to video files

This commit is contained in:
2026-03-29 13:31:27 +02:00
parent 5662f079eb
commit 4cdccb7227
8 changed files with 36 additions and 22 deletions

View File

@@ -15,18 +15,22 @@ if TYPE_CHECKING:
class TweetMedia:
url : str
file_name : str
is_video: bool
def __init__(self, url, file_name):
def __init__(self, url, file_name, is_video: bool):
self.url = url
self.file_name = file_name
self.is_video = is_video
class DownloadedMedia:
file_bytes : str
file_bytes : bytes
file_name : str
is_video: bool
def __init__(self, bytes, file_name):
def __init__(self, bytes, file_name, is_video: bool):
self.file_bytes = bytes
self.file_name = file_name
self.is_video = is_video
async def GetTweetMedia(tweet : Tweet) -> list[TweetMedia]:
mediaList : list[TweetMedia] = []
@@ -34,12 +38,12 @@ async def GetTweetMedia(tweet : Tweet) -> list[TweetMedia]:
if media.file_format == 'mp4':
best_stream = await media.best_stream()
fileName = f"{tweet.author.screen_name}_{tweet.id}_{idx}.{media.file_format}"
mediaList.append(TweetMedia(best_stream.direct_url, fileName))
mediaList.append(TweetMedia(best_stream.direct_url, fileName, True))
else:
best_stream = await media.best_stream()
extension = best_stream.file_format
fileName = f"{tweet.author.screen_name}_{tweet.id}_{idx}.{extension}"
mediaList.append(TweetMedia(best_stream.direct_url, fileName))
mediaList.append(TweetMedia(best_stream.direct_url, fileName, False))
return mediaList
@@ -47,18 +51,18 @@ async def GetTweetMediaUrls(tweet : Tweet):
mediaList = await GetTweetMedia(tweet)
return [media.url for media in mediaList]
async def DownloadMedia(post_id, account_id, account_name, url_list : list, session) -> list[DownloadedMedia]:
async def DownloadMedia(post_id, account_id, account_name, media_list : list[TweetMedia], session) -> list[DownloadedMedia]:
result : list[DownloadedMedia] = []
path = f"{Global_Config["x_download_path"]}{account_id}"
os.makedirs(path, exist_ok=True)
for idx, file_url in enumerate(url_list):
file_name = get_file_name(account_name, post_id, idx, file_url)
for idx, media in enumerate(media_list):
file_name = get_file_name(account_name, post_id, idx, media.url)
full_path = f"{path}/{file_name}"
photo_bytes = await downloadHelper.save_to_file(file_url, full_path, session)
photo_bytes = await downloadHelper.save_to_file(media.url, full_path, session)
result.append(DownloadedMedia(photo_bytes, file_name))
result.append(DownloadedMedia(photo_bytes, file_name, media.is_video))
return result