mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-11-24 15:23:11 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8ac4b8734 | ||
|
|
99afe59a11 | ||
|
|
8e8c0667df | ||
|
|
03479326b3 | ||
|
|
b39f40e9f9 | ||
|
|
5ed127f226 | ||
|
|
3487edf691 | ||
|
|
5b336df1d4 |
No files matched your search
@@ -3,33 +3,47 @@
|
|||||||

|

|
||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<b>Twitter/X Media Batch Downloader</b> is a GUI tool that allows users to download all media, including images and videos, in their original quality from Twitter/X accounts using <code>gallery-dl</code>
|
<b>Twitter/X Media Batch Downloader</b> is a GUI tool that allows users to download all media, including images and videos, in their original quality from Twitter/X accounts, even withheld accounts, using <code>gallery-dl</code>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
## Download
|
## Download
|
||||||
|
|
||||||
- Download the latest version of [TwitterXMediaBatchDownloader](https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/download/v1.2/TwitterXMediaBatchDownloader.exe)
|
- Download the latest version of [TwitterXMediaBatchDownloader](https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/download/v1.3/TwitterXMediaBatchDownloader.exe)
|
||||||
- If you're familiar with **userscripts**, please install it [here](https://greasyfork.org/en/scripts/523157-twitter-x-media-batch-downloader)
|
- If you're familiar with **userscripts**, please install it [here](https://greasyfork.org/en/scripts/523157-twitter-x-media-batch-downloader)
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Uses the powerful `gallery-dl` library, similar to `yt-dlp`
|
- Uses the powerful `gallery-dl` library, similar to `yt-dlp`
|
||||||
|
- The ability to download media from **Withheld Accounts**
|
||||||
- Simple and compact GUI
|
- Simple and compact GUI
|
||||||
- Download images and videos in original quality
|
- Download images and videos in original quality
|
||||||
- Option to choose the media type to download: **media** (image + video) or just **image/video**
|
- Option to choose the media type to download: **media** (image + GIF + video) or just **image/GIF/video**
|
||||||
- **Batch** download settings, ranging from 10 to 100 files per download
|
- **Batch** download settings, ranging from 10 to 100 files per download
|
||||||
- Filename format customization
|
- Filename format customization
|
||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
> Downloading from **Withheld Accounts**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
## How to Obtain Auth Token
|
## How to Obtain Auth Token
|
||||||
|
|
||||||
|
> [!Warning]
|
||||||
|
> - I suggest not using the **main account** to obtain the token.
|
||||||
|
> - You can use https://temp-mail.io to register a Twitter account.
|
||||||
|
> - Using an auth token or cookies has the potential to get the **account suspended.**
|
||||||
|
|
||||||
1. Go to [Twitter's website](https://www.x.com/)
|
1. Go to [Twitter's website](https://www.x.com/)
|
||||||
2. Log into your account
|
2. Log into your account
|
||||||
3. Open the Developer Tools by pressing `F12`
|
3. Open the Developer Tools by pressing `F12`
|
||||||
|
|||||||
+401
-217
@@ -8,7 +8,8 @@ from datetime import datetime
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
|
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
|
||||||
QHBoxLayout, QLabel, QLineEdit, QPushButton,
|
QHBoxLayout, QLabel, QLineEdit, QPushButton,
|
||||||
QProgressBar, QFileDialog, QRadioButton, QComboBox)
|
QProgressBar, QFileDialog, QRadioButton, QComboBox,
|
||||||
|
QGroupBox, QCheckBox)
|
||||||
from PyQt6.QtCore import QThread, pyqtSignal, Qt, QSettings
|
from PyQt6.QtCore import QThread, pyqtSignal, Qt, QSettings
|
||||||
from PyQt6.QtGui import QIcon, QPixmap, QCursor, QPainter, QPainterPath
|
from PyQt6.QtGui import QIcon, QPixmap, QCursor, QPainter, QPainterPath
|
||||||
from gallery_dl.extractor import twitter
|
from gallery_dl.extractor import twitter
|
||||||
@@ -36,105 +37,147 @@ class MetadataFetcher(QThread):
|
|||||||
finished = pyqtSignal(dict)
|
finished = pyqtSignal(dict)
|
||||||
error = pyqtSignal(str)
|
error = pyqtSignal(str)
|
||||||
|
|
||||||
def __init__(self, username, media_type='media'):
|
def __init__(self, username, media_type='media', use_api=True):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.username = username
|
self.username = username
|
||||||
self.auth_token = None
|
self.auth_token = None
|
||||||
self.media_type = media_type
|
self.media_type = media_type
|
||||||
|
self.use_api = use_api
|
||||||
|
|
||||||
|
def normalize_url(self, url_or_username):
|
||||||
|
url_or_username = url_or_username.strip()
|
||||||
|
username = url_or_username
|
||||||
|
|
||||||
|
if "x.com/" in url_or_username or "twitter.com/" in url_or_username:
|
||||||
|
parts = url_or_username.split('/')
|
||||||
|
for i, part in enumerate(parts):
|
||||||
|
if part in ['x.com', 'twitter.com'] and i + 1 < len(parts):
|
||||||
|
username = parts[i + 1]
|
||||||
|
username = username.split('/')[0]
|
||||||
|
break
|
||||||
|
|
||||||
|
username = username.strip()
|
||||||
|
return username
|
||||||
|
|
||||||
|
async def fetch_from_api(self, username, auth_token):
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
url = f"https://twitterxapis.vercel.app/metadata/{username}/{auth_token}"
|
||||||
|
async with session.get(url) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
data = await response.json()
|
||||||
|
|
||||||
|
if self.media_type != 'media':
|
||||||
|
filtered_timeline = []
|
||||||
|
for item in data['timeline']:
|
||||||
|
if (self.media_type == 'image' and item['type'] == 'photo') or \
|
||||||
|
(self.media_type == 'gif' and item['type'] == 'animated_gif') or \
|
||||||
|
(self.media_type == 'video' and item['type'] == 'video'):
|
||||||
|
filtered_timeline.append(item)
|
||||||
|
data['timeline'] = filtered_timeline
|
||||||
|
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
raise ValueError(f"API request failed with status {response.status}")
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"API request failed: {str(e)}")
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
try:
|
||||||
username = self.username.strip()
|
normalized = self.normalize_url(self.username)
|
||||||
if "x.com/" in username or "twitter.com/" in username:
|
|
||||||
username = username.split("/")[-1]
|
|
||||||
if username.endswith("/timeline"):
|
|
||||||
username = username[:-9]
|
|
||||||
|
|
||||||
url = f"https://x.com/{username}/timeline"
|
try:
|
||||||
|
data = asyncio.run(self.fetch_from_api(normalized, self.auth_token))
|
||||||
match = re.match(twitter.TwitterTimelineExtractor.pattern, url)
|
self.finished.emit(data)
|
||||||
if not match:
|
return
|
||||||
raise ValueError(f"Invalid URL: {url}")
|
except Exception as api_error:
|
||||||
|
try:
|
||||||
extractor = twitter.TwitterTimelineExtractor(match)
|
match = re.match(twitter.TwitterTimelineExtractor.pattern, f"https://x.com/{normalized}/timeline")
|
||||||
extractor.config = lambda key, default=None: {
|
if not match:
|
||||||
"cookies": {
|
raise ValueError(f"Invalid username: {normalized}")
|
||||||
"auth_token": self.auth_token
|
|
||||||
}
|
extractor = twitter.TwitterTimelineExtractor(match)
|
||||||
}.get(key, default)
|
extractor.config = lambda key, default=None: {
|
||||||
|
"cookies": {
|
||||||
extractor.initialize()
|
"auth_token": self.auth_token
|
||||||
|
}
|
||||||
output = {
|
}.get(key, default)
|
||||||
'account_info': {},
|
|
||||||
'timeline': []
|
extractor.initialize()
|
||||||
}
|
|
||||||
|
output = {
|
||||||
for item in extractor:
|
'account_info': {},
|
||||||
if isinstance(item, tuple) and len(item) >= 3:
|
'timeline': []
|
||||||
media_url = item[1]
|
}
|
||||||
tweet_data = item[2]
|
|
||||||
|
for item in extractor:
|
||||||
|
if isinstance(item, tuple) and len(item) >= 3:
|
||||||
|
media_url = item[1]
|
||||||
|
tweet_data = item[2]
|
||||||
|
|
||||||
|
if not output['account_info']:
|
||||||
|
if 'user' in tweet_data:
|
||||||
|
user = tweet_data['user']
|
||||||
|
user_date = user.get('date', '')
|
||||||
|
if isinstance(user_date, datetime):
|
||||||
|
user_date = user_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
output['account_info'] = {
|
||||||
|
'name': user.get('name', ''),
|
||||||
|
'nick': user.get('nick', ''),
|
||||||
|
'date': user_date,
|
||||||
|
'followers_count': user.get('followers_count', 0),
|
||||||
|
'friends_count': user.get('friends_count', 0),
|
||||||
|
'profile_image': user.get('profile_image', ''),
|
||||||
|
'statuses_count': user.get('statuses_count', 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
should_include = False
|
||||||
|
media_type = tweet_data.get('type', '')
|
||||||
|
|
||||||
|
if self.media_type == 'media':
|
||||||
|
should_include = ('pbs.twimg.com' in media_url or
|
||||||
|
'video.twimg.com' in media_url)
|
||||||
|
elif self.media_type == 'image':
|
||||||
|
should_include = ('pbs.twimg.com' in media_url and
|
||||||
|
media_type == 'photo')
|
||||||
|
elif self.media_type == 'gif':
|
||||||
|
should_include = media_type == 'animated_gif'
|
||||||
|
elif self.media_type == 'video':
|
||||||
|
should_include = (media_type == 'video' and
|
||||||
|
'video.twimg.com' in media_url)
|
||||||
|
|
||||||
|
if should_include:
|
||||||
|
tweet_date = tweet_data.get('date', datetime.now())
|
||||||
|
if isinstance(tweet_date, datetime):
|
||||||
|
tweet_date = tweet_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
output['timeline'].append({
|
||||||
|
'url': media_url,
|
||||||
|
'date': tweet_date,
|
||||||
|
'type': media_type,
|
||||||
|
'tweet_id': tweet_data.get('tweet_id', '')
|
||||||
|
})
|
||||||
|
|
||||||
if not output['account_info']:
|
if not output['account_info']:
|
||||||
if 'user' in tweet_data:
|
raise ValueError("Failed to fetch account information. Please check the username and auth token.")
|
||||||
user = tweet_data['user']
|
|
||||||
user_date = user.get('date', '')
|
|
||||||
if isinstance(user_date, datetime):
|
|
||||||
user_date = user_date.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
|
|
||||||
output['account_info'] = {
|
|
||||||
'name': user.get('name', ''),
|
|
||||||
'nick': user.get('nick', ''),
|
|
||||||
'date': user_date,
|
|
||||||
'followers_count': user.get('followers_count', 0),
|
|
||||||
'friends_count': user.get('friends_count', 0),
|
|
||||||
'profile_image': user.get('profile_image', ''),
|
|
||||||
'statuses_count': user.get('statuses_count', 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
should_include = False
|
if not output['timeline']:
|
||||||
media_type = tweet_data.get('type', '')
|
if self.media_type == 'media':
|
||||||
|
message = "No media found in timeline"
|
||||||
|
elif self.media_type == 'image':
|
||||||
|
message = "No images found in timeline"
|
||||||
|
elif self.media_type == 'gif':
|
||||||
|
message = "No GIFs found in timeline"
|
||||||
|
else:
|
||||||
|
message = "No videos found in timeline"
|
||||||
|
raise ValueError(message)
|
||||||
|
|
||||||
if self.media_type == 'media':
|
self.finished.emit(output)
|
||||||
should_include = ('pbs.twimg.com' in media_url or
|
|
||||||
'video.twimg.com' in media_url)
|
except Exception as local_error:
|
||||||
elif self.media_type == 'image':
|
raise ValueError(f"Both API and local methods failed.\nAPI error: {str(api_error)}\nLocal error: {str(local_error)}")
|
||||||
should_include = ('pbs.twimg.com' in media_url and
|
|
||||||
media_type == 'photo')
|
|
||||||
elif self.media_type == 'gif':
|
|
||||||
should_include = media_type == 'animated_gif'
|
|
||||||
elif self.media_type == 'video':
|
|
||||||
should_include = (media_type == 'video' and
|
|
||||||
'video.twimg.com' in media_url)
|
|
||||||
|
|
||||||
if should_include:
|
|
||||||
tweet_date = tweet_data.get('date', datetime.now())
|
|
||||||
if isinstance(tweet_date, datetime):
|
|
||||||
tweet_date = tweet_date.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
|
|
||||||
output['timeline'].append({
|
|
||||||
'url': media_url,
|
|
||||||
'date': tweet_date,
|
|
||||||
'type': media_type,
|
|
||||||
'tweet_id': tweet_data.get('tweet_id', '')
|
|
||||||
})
|
|
||||||
|
|
||||||
if not output['account_info']:
|
|
||||||
raise ValueError("Failed to fetch account information. Please check the username and auth token.")
|
|
||||||
|
|
||||||
if not output['timeline']:
|
|
||||||
if self.media_type == 'media':
|
|
||||||
message = "No media found in timeline"
|
|
||||||
elif self.media_type == 'image':
|
|
||||||
message = "No images found in timeline"
|
|
||||||
elif self.media_type == 'gif':
|
|
||||||
message = "No GIFs found in timeline"
|
|
||||||
else:
|
|
||||||
message = "No videos found in timeline"
|
|
||||||
raise ValueError(message)
|
|
||||||
|
|
||||||
self.finished.emit(output)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.error.emit(str(e))
|
self.error.emit(str(e))
|
||||||
|
|
||||||
@@ -148,88 +191,129 @@ class MediaDownloader(QThread):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.media_list = media_list
|
self.media_list = media_list
|
||||||
self.output_dir = output_dir
|
self.output_dir = output_dir
|
||||||
self.filename_format = filename_format
|
|
||||||
self.username = username
|
self.username = username
|
||||||
|
self.filename_format = filename_format
|
||||||
self.media_type = media_type
|
self.media_type = media_type
|
||||||
self.batch_size = batch_size
|
self.batch_size = batch_size
|
||||||
|
self._is_cancelled = False
|
||||||
|
self._download_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
def cancel(self):
|
||||||
|
self._is_cancelled = True
|
||||||
|
|
||||||
async def download_file(self, session, url, filepath):
|
async def download_file(self, session, url, filepath):
|
||||||
try:
|
try:
|
||||||
|
if os.path.exists(filepath):
|
||||||
|
return True, True
|
||||||
|
|
||||||
|
if self._is_cancelled:
|
||||||
|
return False, False
|
||||||
|
|
||||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||||||
|
|
||||||
async with session.get(url) as response:
|
async with session.get(url) as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
with open(filepath, 'wb') as f:
|
with open(filepath, 'wb') as f:
|
||||||
f.write(await response.read())
|
f.write(await response.read())
|
||||||
return True
|
return True, False
|
||||||
return False
|
return False, False
|
||||||
except Exception:
|
except Exception as e:
|
||||||
return False
|
print(f"Error downloading {url}: {str(e)}")
|
||||||
|
return False, False
|
||||||
|
|
||||||
async def download_all(self):
|
async def download_all(self):
|
||||||
os.makedirs(self.output_dir, exist_ok=True)
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
timeout = aiohttp.ClientTimeout(total=30)
|
||||||
|
connector = aiohttp.TCPConnector(limit=self.batch_size)
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout, connector=connector) as session:
|
||||||
total = len(self.media_list)
|
total = len(self.media_list)
|
||||||
completed = 0
|
completed = 0
|
||||||
|
skipped = 0
|
||||||
|
failed = 0
|
||||||
|
|
||||||
used_filenames = set()
|
used_filenames = set()
|
||||||
|
|
||||||
tasks = []
|
for i in range(0, total, self.batch_size):
|
||||||
for item in self.media_list:
|
if self._is_cancelled:
|
||||||
url = item['url']
|
break
|
||||||
date = datetime.strptime(item['date'], "%Y-%m-%d %H:%M:%S")
|
|
||||||
formatted_date = date.strftime("%Y%m%d_%H%M%S")
|
|
||||||
tweet_id = str(item.get('tweet_id', ''))
|
|
||||||
|
|
||||||
extension = 'mp4' if 'video.twimg.com' in url else 'jpg'
|
batch = self.media_list[i:i + self.batch_size]
|
||||||
|
tasks = []
|
||||||
|
|
||||||
if self.filename_format == "username_date":
|
for item in batch:
|
||||||
base_filename = f"{self.username}_{formatted_date}_{tweet_id}"
|
url = item['url']
|
||||||
else:
|
date = datetime.strptime(item['date'], "%Y-%m-%d %H:%M:%S")
|
||||||
base_filename = f"{formatted_date}_{self.username}_{tweet_id}"
|
formatted_date = date.strftime("%Y%m%d_%H%M%S")
|
||||||
|
tweet_id = str(item.get('tweet_id', ''))
|
||||||
filename = f"{base_filename}.{extension}"
|
|
||||||
counter = 1
|
|
||||||
while filename in used_filenames:
|
|
||||||
filename = f"{base_filename}_{counter:02d}.{extension}"
|
|
||||||
counter += 1
|
|
||||||
|
|
||||||
used_filenames.add(filename)
|
|
||||||
filepath = os.path.join(self.output_dir, filename)
|
|
||||||
|
|
||||||
task = asyncio.create_task(self.download_file(session, url, filepath))
|
|
||||||
tasks.append(task)
|
|
||||||
|
|
||||||
if len(tasks) >= self.batch_size:
|
|
||||||
results = await asyncio.gather(*tasks)
|
|
||||||
completed += sum(1 for r in results if r)
|
|
||||||
progress = int((completed / total) * 100)
|
|
||||||
self.progress.emit(progress)
|
|
||||||
completed_str = f"{completed:,}" if completed >= 1000 else str(completed)
|
|
||||||
total_str = f"{total:,}" if total >= 1000 else str(total)
|
|
||||||
|
|
||||||
media_type_str = "files"
|
extension = 'mp4' if 'video.twimg.com' in url else 'jpg'
|
||||||
if self.media_type == "image":
|
|
||||||
media_type_str = "images"
|
|
||||||
elif self.media_type == "video":
|
|
||||||
media_type_str = "videos"
|
|
||||||
|
|
||||||
self.status_update.emit(f"Downloaded {completed_str} of {total_str} {media_type_str}...")
|
if self.filename_format == "username_date":
|
||||||
tasks = []
|
base_filename = f"{self.username}_{formatted_date}_{tweet_id}"
|
||||||
|
else:
|
||||||
if tasks:
|
base_filename = f"{formatted_date}_{self.username}_{tweet_id}"
|
||||||
results = await asyncio.gather(*tasks)
|
|
||||||
completed += sum(1 for r in results if r)
|
filename = f"{base_filename}.{extension}"
|
||||||
|
counter = 1
|
||||||
|
while filename in used_filenames:
|
||||||
|
filename = f"{base_filename}_{counter:02d}.{extension}"
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
used_filenames.add(filename)
|
||||||
|
filepath = os.path.join(self.output_dir, filename)
|
||||||
|
|
||||||
|
task = asyncio.create_task(self.download_file(session, url, filepath))
|
||||||
|
tasks.append(task)
|
||||||
|
|
||||||
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
|
||||||
|
for result in results:
|
||||||
|
if isinstance(result, tuple):
|
||||||
|
success, was_skipped = result
|
||||||
|
if success:
|
||||||
|
completed += 1
|
||||||
|
if was_skipped:
|
||||||
|
skipped += 1
|
||||||
|
else:
|
||||||
|
failed += 1
|
||||||
|
else:
|
||||||
|
failed += 1
|
||||||
|
|
||||||
progress = int((completed / total) * 100)
|
progress = int((completed / total) * 100)
|
||||||
self.progress.emit(progress)
|
self.progress.emit(progress)
|
||||||
|
|
||||||
return completed
|
completed_str = f"{completed:,}" if completed >= 1000 else str(completed)
|
||||||
|
total_str = f"{total:,}" if total >= 1000 else str(total)
|
||||||
|
skipped_str = f" ({skipped:,} skipped)" if skipped > 0 else ""
|
||||||
|
failed_str = f" ({failed:,} failed)" if failed > 0 else ""
|
||||||
|
|
||||||
|
media_type_str = "files"
|
||||||
|
if self.media_type == "image":
|
||||||
|
media_type_str = "images"
|
||||||
|
elif self.media_type == "video":
|
||||||
|
media_type_str = "videos"
|
||||||
|
|
||||||
|
self.status_update.emit(
|
||||||
|
f"Downloaded {completed_str} of {total_str} {media_type_str}{skipped_str}{failed_str}..."
|
||||||
|
)
|
||||||
|
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
|
return completed, skipped, failed
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
try:
|
||||||
completed = asyncio.run(self.download_all())
|
completed, skipped, failed = asyncio.run(self.download_all())
|
||||||
|
|
||||||
|
if self._is_cancelled:
|
||||||
|
self.status_update.emit("Download cancelled")
|
||||||
|
return
|
||||||
|
|
||||||
completed_str = f"{completed:,}" if completed >= 1000 else str(completed)
|
completed_str = f"{completed:,}" if completed >= 1000 else str(completed)
|
||||||
|
skipped_str = f" ({skipped:,} skipped)" if skipped > 0 else ""
|
||||||
|
failed_str = f" ({failed:,} failed)" if failed > 0 else ""
|
||||||
|
|
||||||
media_type_str = "files"
|
media_type_str = "files"
|
||||||
if self.media_type == "image":
|
if self.media_type == "image":
|
||||||
@@ -237,7 +321,9 @@ class MediaDownloader(QThread):
|
|||||||
elif self.media_type == "video":
|
elif self.media_type == "video":
|
||||||
media_type_str = "videos"
|
media_type_str = "videos"
|
||||||
|
|
||||||
self.finished.emit(f"Downloaded {completed_str} {media_type_str} successfully!")
|
self.finished.emit(
|
||||||
|
f"Downloaded {completed_str} {media_type_str}{skipped_str}{failed_str} successfully!"
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.error.emit(str(e))
|
self.error.emit(str(e))
|
||||||
|
|
||||||
@@ -251,7 +337,7 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
self.setWindowIcon(QIcon(icon_path))
|
self.setWindowIcon(QIcon(icon_path))
|
||||||
|
|
||||||
self.setFixedWidth(600)
|
self.setFixedWidth(600)
|
||||||
self.setFixedHeight(180)
|
self.setFixedHeight(210)
|
||||||
|
|
||||||
self.default_pictures_dir = str(Path.home() / "Pictures")
|
self.default_pictures_dir = str(Path.home() / "Pictures")
|
||||||
os.makedirs(self.default_pictures_dir, exist_ok=True)
|
os.makedirs(self.default_pictures_dir, exist_ok=True)
|
||||||
@@ -262,6 +348,7 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
self.init_ui()
|
self.init_ui()
|
||||||
self.load_settings()
|
self.load_settings()
|
||||||
self.setup_auto_save()
|
self.setup_auto_save()
|
||||||
|
self.clean_username = None
|
||||||
|
|
||||||
def init_ui(self):
|
def init_ui(self):
|
||||||
central_widget = QWidget()
|
central_widget = QWidget()
|
||||||
@@ -319,47 +406,83 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
auth_layout.addWidget(self.auth_input)
|
auth_layout.addWidget(self.auth_input)
|
||||||
input_layout.addLayout(auth_layout)
|
input_layout.addLayout(auth_layout)
|
||||||
|
|
||||||
options_layout = QHBoxLayout()
|
settings_group = QGroupBox("Settings")
|
||||||
|
settings_layout = QVBoxLayout()
|
||||||
|
settings_layout.setSpacing(5)
|
||||||
|
settings_layout.setContentsMargins(5, 5, 5, 5)
|
||||||
|
|
||||||
|
centered_settings_layout = QHBoxLayout()
|
||||||
|
centered_settings_layout.addStretch()
|
||||||
|
|
||||||
|
media_layout = QHBoxLayout()
|
||||||
|
media_layout.setSpacing(5)
|
||||||
|
|
||||||
media_label = QLabel("Media Type:")
|
media_label = QLabel("Media Type:")
|
||||||
media_label.setFixedWidth(100)
|
media_label.setFixedWidth(65)
|
||||||
|
|
||||||
self.media_type_combo = QComboBox()
|
self.media_type_combo = QComboBox()
|
||||||
self.media_type_combo.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
self.media_type_combo.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||||
media_types = [('media', 'Media'), ('image', 'Image'), ('gif', 'GIF'), ('video', 'Video')]
|
media_types = [('media', 'Media'), ('image', 'Image'), ('gif', 'GIF'), ('video', 'Video')]
|
||||||
for value, display in media_types:
|
for value, display in media_types:
|
||||||
self.media_type_combo.addItem(display, value)
|
self.media_type_combo.addItem(display, value)
|
||||||
self.media_type_combo.setFixedWidth(100)
|
self.media_type_combo.setFixedWidth(70)
|
||||||
|
|
||||||
|
media_layout.addWidget(media_label)
|
||||||
|
media_layout.addWidget(self.media_type_combo)
|
||||||
|
media_layout.addSpacing(10)
|
||||||
|
|
||||||
|
self.use_api_checkbox = QCheckBox("Use API")
|
||||||
|
self.use_api_checkbox.setToolTip("Use external API instead of gallery-dl")
|
||||||
|
self.use_api_checkbox.setChecked(True)
|
||||||
|
self.use_api_checkbox.stateChanged.connect(self.auto_save_settings)
|
||||||
|
|
||||||
|
media_layout.addWidget(self.use_api_checkbox)
|
||||||
|
media_layout.addSpacing(10)
|
||||||
|
|
||||||
|
batch_layout = QHBoxLayout()
|
||||||
|
batch_layout.setSpacing(5)
|
||||||
|
|
||||||
batch_label = QLabel("Batch:")
|
batch_label = QLabel("Batch:")
|
||||||
batch_label.setFixedWidth(40)
|
batch_label.setFixedWidth(35)
|
||||||
|
|
||||||
self.batch_size_combo = QComboBox()
|
self.batch_size_combo = QComboBox()
|
||||||
self.batch_size_combo.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
self.batch_size_combo.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||||
self.batch_size_combo.setFixedWidth(50)
|
self.batch_size_combo.setFixedWidth(60)
|
||||||
for size in range(10, 101, 10):
|
for size in range(10, 101, 10):
|
||||||
self.batch_size_combo.addItem(str(size))
|
self.batch_size_combo.addItem(str(size))
|
||||||
|
self.batch_size_combo.setCurrentText("20")
|
||||||
|
|
||||||
|
batch_layout.addWidget(batch_label)
|
||||||
|
batch_layout.addWidget(self.batch_size_combo)
|
||||||
|
batch_layout.addSpacing(10)
|
||||||
|
|
||||||
|
filename_layout = QHBoxLayout()
|
||||||
|
filename_layout.setSpacing(5)
|
||||||
|
|
||||||
format_label = QLabel("Filename:")
|
format_label = QLabel("Filename:")
|
||||||
format_label.setFixedWidth(60)
|
format_label.setFixedWidth(50)
|
||||||
|
|
||||||
self.format_username = QRadioButton("Username")
|
self.format_username = QRadioButton("Username")
|
||||||
self.format_date = QRadioButton("Date")
|
self.format_date = QRadioButton("Date")
|
||||||
self.format_username.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
self.format_username.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||||
self.format_date.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
self.format_date.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
||||||
|
|
||||||
options_layout.addWidget(media_label)
|
filename_layout.addWidget(format_label)
|
||||||
options_layout.addWidget(self.media_type_combo)
|
filename_layout.addWidget(self.format_username)
|
||||||
options_layout.addSpacing(10)
|
filename_layout.addWidget(self.format_date)
|
||||||
options_layout.addWidget(batch_label)
|
|
||||||
options_layout.addWidget(self.batch_size_combo)
|
combined_layout = QHBoxLayout()
|
||||||
options_layout.addSpacing(10)
|
combined_layout.addLayout(media_layout)
|
||||||
options_layout.addWidget(format_label)
|
combined_layout.addLayout(batch_layout)
|
||||||
options_layout.addWidget(self.format_username)
|
combined_layout.addLayout(filename_layout)
|
||||||
options_layout.addWidget(self.format_date)
|
|
||||||
options_layout.addStretch()
|
centered_settings_layout.addLayout(combined_layout)
|
||||||
|
centered_settings_layout.addStretch()
|
||||||
input_layout.addLayout(options_layout)
|
|
||||||
|
settings_layout.addLayout(centered_settings_layout)
|
||||||
|
settings_group.setLayout(settings_layout)
|
||||||
|
|
||||||
|
input_layout.addWidget(settings_group)
|
||||||
|
|
||||||
self.main_layout.addWidget(self.input_widget)
|
self.main_layout.addWidget(self.input_widget)
|
||||||
|
|
||||||
@@ -416,7 +539,6 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
profile_details_layout.addWidget(self.followers_label)
|
profile_details_layout.addWidget(self.followers_label)
|
||||||
profile_details_layout.addWidget(self.following_label)
|
profile_details_layout.addWidget(self.following_label)
|
||||||
profile_details_layout.addWidget(self.posts_label)
|
profile_details_layout.addWidget(self.posts_label)
|
||||||
profile_details_layout.addWidget(self.posts_label)
|
|
||||||
profile_layout.addWidget(profile_details_container, stretch=1)
|
profile_layout.addWidget(profile_details_container, stretch=1)
|
||||||
profile_layout.addStretch()
|
profile_layout.addStretch()
|
||||||
|
|
||||||
@@ -453,6 +575,8 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
self.main_layout.addWidget(self.progress_bar)
|
self.main_layout.addWidget(self.progress_bar)
|
||||||
|
|
||||||
bottom_layout = QHBoxLayout()
|
bottom_layout = QHBoxLayout()
|
||||||
|
bottom_layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
bottom_layout.setSpacing(5)
|
||||||
|
|
||||||
self.status_label = QLabel("")
|
self.status_label = QLabel("")
|
||||||
bottom_layout.addWidget(self.status_label, stretch=1)
|
bottom_layout.addWidget(self.status_label, stretch=1)
|
||||||
@@ -478,6 +602,7 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
bottom_layout.addWidget(self.update_button)
|
bottom_layout.addWidget(self.update_button)
|
||||||
|
|
||||||
self.main_layout.addLayout(bottom_layout)
|
self.main_layout.addLayout(bottom_layout)
|
||||||
|
self.main_layout.setAlignment(bottom_layout, Qt.AlignmentFlag.AlignBottom)
|
||||||
|
|
||||||
def open_update_page(self):
|
def open_update_page(self):
|
||||||
import webbrowser
|
import webbrowser
|
||||||
@@ -489,8 +614,9 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
self.dir_input.textChanged.connect(self.auto_save_settings)
|
self.dir_input.textChanged.connect(self.auto_save_settings)
|
||||||
self.format_username.toggled.connect(self.auto_save_settings)
|
self.format_username.toggled.connect(self.auto_save_settings)
|
||||||
self.media_type_combo.currentTextChanged.connect(self.auto_save_settings)
|
self.media_type_combo.currentTextChanged.connect(self.auto_save_settings)
|
||||||
|
self.use_api_checkbox.stateChanged.connect(self.auto_save_settings)
|
||||||
self.batch_size_combo.currentTextChanged.connect(self.auto_save_settings)
|
self.batch_size_combo.currentTextChanged.connect(self.auto_save_settings)
|
||||||
|
|
||||||
def auto_save_settings(self):
|
def auto_save_settings(self):
|
||||||
self.settings.setValue('url_input', self.url_input.text())
|
self.settings.setValue('url_input', self.url_input.text())
|
||||||
self.settings.setValue('auth_token', self.auth_input.text())
|
self.settings.setValue('auth_token', self.auth_input.text())
|
||||||
@@ -498,6 +624,7 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
self.settings.setValue('filename_format',
|
self.settings.setValue('filename_format',
|
||||||
'username_date' if self.format_username.isChecked() else 'date_username')
|
'username_date' if self.format_username.isChecked() else 'date_username')
|
||||||
self.settings.setValue('media_type', self.media_type_combo.currentData())
|
self.settings.setValue('media_type', self.media_type_combo.currentData())
|
||||||
|
self.settings.setValue('use_api', self.use_api_checkbox.isChecked())
|
||||||
self.settings.setValue('batch_size', self.batch_size_combo.currentText())
|
self.settings.setValue('batch_size', self.batch_size_combo.currentText())
|
||||||
self.settings.sync()
|
self.settings.sync()
|
||||||
|
|
||||||
@@ -515,8 +642,11 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
if self.media_type_combo.itemData(i) == media_type:
|
if self.media_type_combo.itemData(i) == media_type:
|
||||||
self.media_type_combo.setCurrentIndex(i)
|
self.media_type_combo.setCurrentIndex(i)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
use_api = self.settings.value('use_api', True, type=bool)
|
||||||
|
self.use_api_checkbox.setChecked(use_api)
|
||||||
|
|
||||||
batch_size = self.settings.value('batch_size', '10')
|
batch_size = self.settings.value('batch_size', '20')
|
||||||
index = self.batch_size_combo.findText(batch_size)
|
index = self.batch_size_combo.findText(batch_size)
|
||||||
if index >= 0:
|
if index >= 0:
|
||||||
self.batch_size_combo.setCurrentIndex(index)
|
self.batch_size_combo.setCurrentIndex(index)
|
||||||
@@ -552,60 +682,120 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
self.status_label.setText("Fetching profile information...")
|
self.status_label.setText("Fetching profile information...")
|
||||||
|
|
||||||
media_type = self.media_type_combo.currentData()
|
media_type = self.media_type_combo.currentData()
|
||||||
self.fetcher = MetadataFetcher(username, media_type)
|
use_api = self.use_api_checkbox.isChecked()
|
||||||
|
self.fetcher = MetadataFetcher(username, media_type, use_api)
|
||||||
self.fetcher.auth_token = auth_token
|
self.fetcher.auth_token = auth_token
|
||||||
self.fetcher.finished.connect(self.handle_profile_info)
|
self.fetcher.finished.connect(self.handle_profile_info)
|
||||||
self.fetcher.error.connect(self.handle_fetch_error)
|
self.fetcher.error.connect(self.handle_fetch_error)
|
||||||
self.fetcher.start()
|
self.fetcher.start()
|
||||||
|
|
||||||
def handle_profile_info(self, info):
|
def handle_profile_info(self, info):
|
||||||
if not info.get('account_info'):
|
try:
|
||||||
self.status_label.setText("Failed to fetch profile information")
|
if not info or not isinstance(info, dict):
|
||||||
|
raise ValueError("Invalid info data received")
|
||||||
|
|
||||||
|
account_info = info.get('account_info', {})
|
||||||
|
if not account_info:
|
||||||
|
self.status_label.setText("Failed to fetch profile information")
|
||||||
|
self.fetch_button.setEnabled(True)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.media_info = info
|
||||||
self.fetch_button.setEnabled(True)
|
self.fetch_button.setEnabled(True)
|
||||||
return
|
|
||||||
|
|
||||||
self.media_info = info
|
is_withheld = not account_info.get('nick') and not account_info.get('profile_image')
|
||||||
self.fetch_button.setEnabled(True)
|
|
||||||
|
name = account_info.get('name', 'Unknown')
|
||||||
account_info = info['account_info']
|
nick = "Withheld" if is_withheld else account_info.get('nick', 'Unknown')
|
||||||
name = account_info['name']
|
date_str = account_info.get('date', '')
|
||||||
nick = account_info['nick']
|
followers = account_info.get('followers_count', 0)
|
||||||
join_date = datetime.strptime(account_info['date'], "%Y-%m-%d %H:%M:%S")
|
following = account_info.get('friends_count', 0)
|
||||||
followers = account_info['followers_count']
|
posts = account_info.get('statuses_count', 0)
|
||||||
following = account_info['friends_count']
|
|
||||||
posts = account_info['statuses_count']
|
try:
|
||||||
|
join_date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
|
||||||
join_date_str = join_date.strftime("%A, %d %B %Y - %H:%M")
|
join_date_str = join_date.strftime("%A, %d %B %Y - %H:%M")
|
||||||
|
except (ValueError, TypeError):
|
||||||
self.name_label.setText(f"<b>{name}</b> ({nick})")
|
join_date_str = "Unknown Date"
|
||||||
self.join_date_label.setText(f"<b>Join Date:</b> {join_date_str}")
|
|
||||||
self.followers_label.setText(f"<b>Followers:</b> {followers:,}")
|
try:
|
||||||
self.following_label.setText(f"<b>Following:</b> {following:,}")
|
self.name_label.setText(f"<b>{name}</b> ({nick})")
|
||||||
self.posts_label.setText(f"<b>Posts:</b> {posts:,}")
|
self.join_date_label.setText(f"<b>Join Date:</b> {join_date_str}")
|
||||||
|
self.followers_label.setText(f"<b>Followers:</b> {followers:,}")
|
||||||
|
self.following_label.setText(f"<b>Following:</b> {following:,}")
|
||||||
|
self.posts_label.setText(f"<b>Posts:</b> {posts:,}")
|
||||||
|
|
||||||
media_count = len(info['timeline'])
|
timeline = info.get('timeline', [])
|
||||||
self.status_label.setText(f"Successfully fetched {media_count:,} media items")
|
media_count = len(timeline) if isinstance(timeline, list) else 0
|
||||||
|
self.status_label.setText(f"Successfully fetched {media_count:,} media items")
|
||||||
profile_image_url = account_info['profile_image']
|
except Exception as ui_error:
|
||||||
self.image_downloader = ImageDownloader(profile_image_url)
|
self.status_label.setText(f"Error updating UI: {str(ui_error)}")
|
||||||
self.image_downloader.finished.connect(self.update_profile_image)
|
return
|
||||||
self.image_downloader.start()
|
|
||||||
|
|
||||||
self.input_widget.hide()
|
|
||||||
self.profile_widget.show()
|
|
||||||
self.download_button.show()
|
|
||||||
self.cancel_button.show()
|
|
||||||
self.update_button.hide()
|
|
||||||
|
|
||||||
username = self.url_input.text().strip()
|
if is_withheld:
|
||||||
if "x.com/" in username or "twitter.com/" in username:
|
try:
|
||||||
username = username.split("/")[-1]
|
withheld_icon_path = os.path.join(os.path.dirname(__file__), "withheld.svg")
|
||||||
if username.endswith("/timeline"):
|
if os.path.exists(withheld_icon_path):
|
||||||
username = username[:-9]
|
icon = QIcon(withheld_icon_path)
|
||||||
|
pixmap = icon.pixmap(90, 90)
|
||||||
output_base = self.dir_input.text().strip() or self.default_pictures_dir
|
|
||||||
self.user_output_dir = os.path.join(output_base, username)
|
painter = QPainter(pixmap)
|
||||||
os.makedirs(self.user_output_dir, exist_ok=True)
|
painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn)
|
||||||
|
painter.fillRect(pixmap.rect(), self.palette().text().color())
|
||||||
|
painter.end()
|
||||||
|
|
||||||
|
scaled_pixmap = pixmap.scaled(
|
||||||
|
90, 90,
|
||||||
|
Qt.AspectRatioMode.KeepAspectRatio,
|
||||||
|
Qt.TransformationMode.SmoothTransformation
|
||||||
|
)
|
||||||
|
self.profile_image_label.setPixmap(scaled_pixmap)
|
||||||
|
else:
|
||||||
|
print("Withheld icon not found:", withheld_icon_path)
|
||||||
|
except Exception as icon_error:
|
||||||
|
print(f"Error loading withheld icon: {str(icon_error)}")
|
||||||
|
else:
|
||||||
|
profile_image_url = account_info.get('profile_image', '')
|
||||||
|
if profile_image_url:
|
||||||
|
try:
|
||||||
|
self.image_downloader = ImageDownloader(profile_image_url)
|
||||||
|
self.image_downloader.finished.connect(self.update_profile_image)
|
||||||
|
self.image_downloader.start()
|
||||||
|
except Exception as img_error:
|
||||||
|
print(f"Error starting image download: {str(img_error)}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
username = self.url_input.text().strip()
|
||||||
|
if "x.com/" in username or "twitter.com/" in username:
|
||||||
|
parts = username.split('/')
|
||||||
|
for i, part in enumerate(parts):
|
||||||
|
if part in ['x.com', 'twitter.com'] and i + 1 < len(parts):
|
||||||
|
username = parts[i + 1]
|
||||||
|
username = username.split('/')[0]
|
||||||
|
break
|
||||||
|
self.clean_username = username.strip()
|
||||||
|
except Exception as username_error:
|
||||||
|
self.clean_username = "unknown_user"
|
||||||
|
print(f"Error cleaning username: {str(username_error)}")
|
||||||
|
|
||||||
|
self.input_widget.hide()
|
||||||
|
self.profile_widget.show()
|
||||||
|
self.download_button.show()
|
||||||
|
self.cancel_button.show()
|
||||||
|
self.update_button.hide()
|
||||||
|
|
||||||
|
try:
|
||||||
|
output_base = self.dir_input.text().strip() or self.default_pictures_dir
|
||||||
|
self.user_output_dir = os.path.join(output_base, self.clean_username)
|
||||||
|
os.makedirs(self.user_output_dir, exist_ok=True)
|
||||||
|
except Exception as dir_error:
|
||||||
|
self.status_label.setText(f"Error creating output directory: {str(dir_error)}")
|
||||||
|
return
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.status_label.setText(f"Error processing profile info: {str(e)}")
|
||||||
|
self.fetch_button.setEnabled(True)
|
||||||
|
print(f"Full error in handle_profile_info: {str(e)}")
|
||||||
|
|
||||||
def update_profile_image(self, image_data):
|
def update_profile_image(self, image_data):
|
||||||
original_pixmap = QPixmap()
|
original_pixmap = QPixmap()
|
||||||
@@ -653,18 +843,12 @@ class TwitterMediaDownloaderGUI(QMainWindow):
|
|||||||
filename_format = "username_date" if self.format_username.isChecked() else "date_username"
|
filename_format = "username_date" if self.format_username.isChecked() else "date_username"
|
||||||
batch_size = int(self.batch_size_combo.currentText())
|
batch_size = int(self.batch_size_combo.currentText())
|
||||||
media_type = self.media_type_combo.currentData()
|
media_type = self.media_type_combo.currentData()
|
||||||
|
|
||||||
username = self.url_input.text().strip()
|
|
||||||
if "x.com/" in username or "twitter.com/" in username:
|
|
||||||
username = username.split("/")[-1]
|
|
||||||
if username.endswith("/timeline"):
|
|
||||||
username = username[:-9]
|
|
||||||
|
|
||||||
self.worker = MediaDownloader(
|
self.worker = MediaDownloader(
|
||||||
self.media_info['timeline'],
|
self.media_info['timeline'],
|
||||||
self.user_output_dir,
|
self.user_output_dir,
|
||||||
filename_format,
|
filename_format,
|
||||||
username,
|
self.clean_username,
|
||||||
media_type,
|
media_type,
|
||||||
batch_size
|
batch_size
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user