Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions TwitterXMediaBatchDownloader_Windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,21 @@ def __init__(self, accounts, outpath, auth_token, filename_format='username_date
async def download_file(self, session, url, filepath):
try:
if os.path.exists(filepath):
return True, True
if os.path.getsize(filepath) == 0:
os.remove(filepath)
else:
return True, True
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The os.path.getsize() call can raise OSError if the file is deleted between the os.path.exists() check and this call, creating a race condition. Consider wrapping this in a try-except block or combining the existence and size checks.

Suggested change
return True, True
try:
if os.path.getsize(filepath) == 0:
os.remove(filepath)
else:
return True, True
except OSError:
# File may have been deleted or is inaccessible; proceed to download
pass

Copilot uses AI. Check for mistakes.
if self.is_stopped:
return False, False

os.makedirs(os.path.dirname(filepath), exist_ok=True)

async with session.get(url) as response:
if response.status == 200:
with open(filepath, 'wb') as f:
f.write(await response.read())
if os.path.getsize(filepath) == 0:
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The os.path.getsize() call can raise OSError if the file was not created successfully or was deleted by another process. This should be wrapped in a try-except block to handle potential file system errors.

Suggested change
if os.path.getsize(filepath) == 0:
try:
if os.path.getsize(filepath) == 0:
return False, False
except OSError:

Copilot uses AI. Check for mistakes.
return False, False
return True, False
return False, False
except Exception as e:
Expand Down