diff --git a/TwitterXMediaBatchDownloader_Linux.py b/TwitterXMediaBatchDownloader_Linux.py index d572973..f9466a0 100644 --- a/TwitterXMediaBatchDownloader_Linux.py +++ b/TwitterXMediaBatchDownloader_Linux.py @@ -1,2635 +1,16 @@ import sys import os -import asyncio -import aiohttp -import requests -import subprocess -import imageio_ffmpeg -import json -import tempfile -from datetime import datetime -from pathlib import Path -from packaging import version -from dataclasses import dataclass -from PyQt6.QtWidgets import ( - QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, - QLabel, QFileDialog, QListWidget, QTextEdit, QTabWidget, QAbstractItemView, QProgressBar, QCheckBox, QDialog, - QDialogButtonBox, QComboBox, QListWidgetItem, QMessageBox -) -from PyQt6.QtCore import Qt, QThread, pyqtSignal, QUrl, QTimer, QTime, QSettings, QSize -from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest -from PyQt6.QtGui import QIcon, QTextCursor, QDesktopServices, QPixmap, QPainter, QPainterPath -from PyQt6.QtSvg import QSvgRenderer -from getMetadata import get_metadata - -@dataclass -class Account: - username: str - nick: str - followers: int - following: int - posts: int - media_type: str - profile_image: str = None - media_list: list = None - fetch_mode: str = 'all' - fetch_timestamp: str = None - -class MetadataFetchWorker(QThread): - finished = pyqtSignal(dict) - error = pyqtSignal(str) - - def __init__(self, username, media_type='all', batch_mode=False, batch_size=0, page=0): - super().__init__() - self.username = username - self.media_type = media_type - self.batch_mode = batch_mode - self.batch_size = batch_size - self.page = page - self.auth_token = None - - 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 - - def run(self): - try: - normalized = self.normalize_url(self.username) - data = get_metadata( - username=normalized, - auth_token=self.auth_token, - timeline_type='media', - batch_size=self.batch_size if self.batch_mode else 0, - page=self.page, - media_type=self.media_type - ) - self.finished.emit(data) - except Exception as e: - self.error.emit(str(e)) - -class DownloadWorker(QThread): - finished = pyqtSignal(bool, str) - progress = pyqtSignal(str, int) - conversion_progress = pyqtSignal(str, int) - download_progress = pyqtSignal(str, int) - - def __init__(self, accounts, outpath, auth_token, filename_format='username_date', - download_batch_size=25, convert_gif=False, gif_resolution='original', gif_conversion_mode='better'): - super().__init__() - self.accounts = accounts - self.outpath = outpath - self.auth_token = auth_token - self.filename_format = filename_format - self.download_batch_size = download_batch_size - self.convert_gif = convert_gif - self.gif_resolution = gif_resolution - self.gif_conversion_mode = gif_conversion_mode - self.is_paused = False - self.is_stopped = False - self.filepath_map = [] - - async def download_file(self, session, url, filepath): - try: - if os.path.exists(filepath): - return True, True - 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()) - return True, False - return False, False - except Exception as e: - return False, False - - async def download_account_media(self, account): - if not account.media_list: - return 0, 0, 0 - - account_output_dir = os.path.join(self.outpath, account.username) - os.makedirs(account_output_dir, exist_ok=True) - timeout = aiohttp.ClientTimeout(total=30) - connector = aiohttp.TCPConnector(limit=self.download_batch_size) - - async with aiohttp.ClientSession(timeout=timeout, connector=connector) as session: - total = len(account.media_list) - completed = 0 - skipped = 0 - failed = 0 - - used_filenames = set() - - for i in range(0, total, self.download_batch_size): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - return completed, skipped, failed - await asyncio.sleep(0.1) - - batch = account.media_list[i:i + self.download_batch_size] - tasks = [] - - for item in batch: - url = item['url'] - 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', '')) - - item_type = item.get('type', '') - if item_type == 'animated_gif': - media_type_folder = 'gif' - extension = 'mp4' - elif item_type == 'video' or 'video.twimg.com' in url: - media_type_folder = 'video' - extension = 'mp4' - else: - media_type_folder = 'image' - extension = 'jpg' - media_output_dir = os.path.join(account_output_dir, media_type_folder) - - if self.filename_format == "username_date": - base_filename = f"{account.username}_{formatted_date}_{tweet_id}" - else: - base_filename = f"{formatted_date}_{account.username}_{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(media_output_dir, filename) - self.filepath_map.append((item, filepath)) - 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_percent = int((completed + failed) / total * 100) - media_type_display = account.media_type if account.media_type != 'all' else 'media' - self.download_progress.emit(f"Downloading {account.username}'s {media_type_display}: {completed + failed:,}/{total:,}", progress_percent) - - await asyncio.sleep(0.1) - - return completed, skipped, failed - - def run(self): - try: - total_accounts = len(self.accounts) - overall_completed = 0 - overall_skipped = 0 - overall_failed = 0 - - for i, account in enumerate(self.accounts): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - return - self.msleep(100) - media_type_display = account.media_type if account.media_type != 'all' else 'media' - self.progress.emit(f"Downloading from account: {account.username} ({media_type_display}) - ({i+1}/{total_accounts})", - int((i) / total_accounts * 100)) - - completed, skipped, failed = asyncio.run(self.download_account_media(account)) - overall_completed += completed - overall_skipped += skipped - overall_failed += failed - self.progress.emit(f"Account {account.username} ({media_type_display}): {completed:,} downloaded, {skipped:,} skipped, {failed:,} failed", - int((i + 1) / total_accounts * 100)) - - if not self.is_stopped: - if self.convert_gif: - try: - ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe() - gif_items = [(item, fp) for item, fp in self.filepath_map if item.get('type') == 'animated_gif'] - total_gifs = len(gif_items) - if total_gifs > 0: - creationflags = subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0 - converted_count = 0 - skipped_count = 0 - self.progress.emit("Starting GIF conversion...", 0) - - for idx, (item, fp) in enumerate(gif_items, start=1): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - break - self.msleep(100) - - if self.is_stopped: - break - - gif_fp = fp.rsplit('.', 1)[0] + '.gif' - - if os.path.exists(gif_fp): - try: - os.remove(fp) - skipped_count += 1 - conv_progress = int((idx / total_gifs) * 100) - quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" - self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display} - Skipped (exists)", conv_progress) - continue - except Exception: - pass - - - if self.gif_conversion_mode == 'fast': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-y', gif_fp] - else: - if self.gif_resolution == 'high': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=800:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=800:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '15', '-y', gif_fp] - elif self.gif_resolution == 'medium': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=600:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=600:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '10', '-y', gif_fp] - elif self.gif_resolution == 'low': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=400:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=400:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '8', '-y', gif_fp] - else: - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'palettegen=stats_mode=full[palette];[0:v][palette]paletteuse=dither=sierra2_4a', '-y', gif_fp] - - result = subprocess.run(ffmpeg_args, capture_output=True, creationflags=creationflags) - if result.returncode == 0 and os.path.exists(gif_fp): - try: - os.remove(fp) - converted_count += 1 - except Exception: - pass - else: - pass - - conv_progress = int((idx / total_gifs) * 100) - quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" - self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display}", conv_progress) - if converted_count > 0 or skipped_count > 0: - completion_msg = f"GIF conversion completed: {converted_count:,} converted" - if skipped_count > 0: - completion_msg += f", {skipped_count:,} skipped (already exists)" - self.progress.emit(completion_msg, 100) - else: - self.progress.emit("GIF conversion completed", 100) - except Exception as conv_e: - self.progress.emit(f"GIF conversion error: {conv_e}", 0) - - success_message = f"Download completed! {overall_completed:,} files downloaded" - if overall_skipped > 0: - success_message += f", {overall_skipped:,} skipped" - if overall_failed > 0: - success_message += f", {overall_failed:,} failed" - self.finished.emit(True, success_message) - - except Exception as e: - self.finished.emit(False, str(e)) - - def pause(self): - self.is_paused = True - - def resume(self): - self.is_paused = False - - def stop(self): - self.is_stopped = True - self.is_paused = False - -class UpdateDialog(QDialog): - def __init__(self, current_version, new_version, parent=None): - super().__init__(parent) - self.setWindowTitle("Update Now") - self.setFixedWidth(400) - self.setModal(True) - - layout = QVBoxLayout() - - message = QLabel(f"Twitter/X Media Batch Downloader v{new_version} Available!") - message.setWordWrap(True) - layout.addWidget(message) - - button_box = QDialogButtonBox() - self.update_button = QPushButton("Check") - self.update_button.setCursor(Qt.CursorShape.PointingHandCursor) - self.cancel_button = QPushButton("Later") - self.cancel_button.setCursor(Qt.CursorShape.PointingHandCursor) - - button_box.addButton(self.update_button, QDialogButtonBox.ButtonRole.AcceptRole) - button_box.addButton(self.cancel_button, QDialogButtonBox.ButtonRole.RejectRole) - - layout.addWidget(button_box) - - self.setLayout(layout) - - self.update_button.clicked.connect(self.accept) - self.cancel_button.clicked.connect(self.reject) - -class TwitterMediaDownloaderGUI(QWidget): - def __init__(self): - super().__init__() - self.current_version = "3.7" - self.accounts = [] - self.temp_dir = os.path.join(Path.home(), ".twitterxmediabatchdownloader") - os.makedirs(self.temp_dir, exist_ok=True) - self.reset_state() - - self.settings = QSettings('TwitterMediaDownloader', 'Settings') - self.last_output_path = self.settings.value('output_path', str(Path.home() / "Pictures")) - self.last_url = self.settings.value('twitter_url', '') - self.last_auth_token = self.settings.value('auth_token', '') - self.filename_format = self.settings.value('filename_format', 'username_date') - self.download_batch_size = self.settings.value('download_batch_size', 25, type=int) - self.batch_mode = self.settings.value('batch_mode', False, type=bool) - self.batch_size = self.settings.value('batch_size', 100, type=int) - self.timeline_type = self.settings.value('timeline_type', 'media') - self.media_type = self.settings.value('media_type', 'all') - self.convert_gif = self.settings.value('convert_gif', False, type=bool) - self.gif_resolution = self.settings.value('gif_resolution', 'original') - self.gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') - self.gif_conversion_quality = self.settings.value('gif_conversion_quality', 'original') - self.check_for_updates = self.settings.value('check_for_updates', True, type=bool) - - self.is_auto_fetching = False - self.current_fetch_username = None - self.current_fetch_media_type = None - self.current_fetch_metadata = None - self.is_multiple_user_mode = False - - self.profile_image_cache = {} - self.pending_downloads = {} - self.network_manager = QNetworkAccessManager() - - self.elapsed_time = QTime(0, 0, 0) - self.timer = QTimer(self) - self.timer.timeout.connect(self.update_timer) - self.initUI() - self.load_settings() - self.load_all_cached_accounts() - - if self.check_for_updates: - QTimer.singleShot(0, self.check_updates) - - def get_time_ago(self, timestamp_str): - if not timestamp_str: - return "Unknown" - - try: - fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) - now = datetime.now(fetch_time.tzinfo) if fetch_time.tzinfo else datetime.now() - - diff = now - fetch_time - days = diff.days - hours = diff.seconds // 3600 - minutes = (diff.seconds % 3600) // 60 - - if days > 0: - return f"{days} day{'s' if days != 1 else ''} ago" - elif hours > 0: - return f"{hours} hour{'s' if hours != 1 else ''} ago" - elif minutes > 0: - return f"{minutes} minute{'s' if minutes != 1 else ''} ago" - else: - return "Just now" - except: - return "Unknown" - - def format_fetch_info(self, timestamp_str): - if not timestamp_str: - return "Fetched: Unknown" - - try: - fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) - formatted_datetime = fetch_time.strftime("%Y/%m/%d • %H:%M") - age = self.get_time_ago(timestamp_str) - return f"Fetched: {formatted_datetime} ({age})" - except: - return "Fetched: Unknown" - - def check_updates(self): - try: - response = requests.get("https://raw.githubusercontent.com/afkarxyz/Twitter-X-Media-Batch-Downloader/refs/heads/main/version.json") - if response.status_code == 200: - data = response.json() - new_version = data.get("version") - - if new_version and version.parse(new_version) > version.parse(self.current_version): - dialog = UpdateDialog(self.current_version, new_version, self) - result = dialog.exec() - - if result == QDialog.DialogCode.Accepted: - QDesktopServices.openUrl(QUrl("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases")) - - except Exception as e: - pass - - def reset_state(self): - self.accounts.clear() - - def reset_ui(self): - self.account_list.clear() - self.log_output.clear() - self.progress_bar.setValue(0) - self.progress_bar.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.pause_resume_btn.setText('Pause') - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - self.is_multiple_user_mode = False - self.enable_batch_buttons() - self.hide_account_buttons() - - def initUI(self): - self.setWindowTitle('Twitter/X Media Batch Downloader') - self.setMinimumWidth(670) - self.setMinimumHeight(350) - - icon_path = os.path.join(os.path.dirname(__file__), "icons", "icon.svg") - if os.path.exists(icon_path): - self.setWindowIcon(QIcon(icon_path)) - - self.main_layout = QVBoxLayout() - - self.setup_twitter_section() - self.setup_tabs() - - self.setLayout(self.main_layout) - - def setup_twitter_section(self): - twitter_layout = QHBoxLayout() - twitter_label = QLabel('Username/URL:') - twitter_label.setFixedWidth(100) - - self.twitter_url = QLineEdit() - self.twitter_url.setPlaceholderText("e.g. Takomayuyi or https://x.com/Takomayuyi or user1,user2,user3") - self.twitter_url.setClearButtonEnabled(True) - self.twitter_url.setText(self.last_url) - self.twitter_url.textChanged.connect(self.save_url) - self.fetch_btn = QPushButton('Fetch') - self.fetch_btn.setFixedWidth(100) - self.fetch_btn.setCursor(Qt.CursorShape.PointingHandCursor) - self.fetch_btn.clicked.connect(self.fetch_account) - - twitter_layout.addWidget(twitter_label) - twitter_layout.addWidget(self.twitter_url) - twitter_layout.addWidget(self.fetch_btn) - self.main_layout.addLayout(twitter_layout) - - def setup_tabs(self): - self.tab_widget = QTabWidget() - self.main_layout.addWidget(self.tab_widget) - - self.setup_dashboard_tab() - self.setup_process_tab() - self.setup_settings_tab() - self.setup_about_tab() - - def setup_dashboard_tab(self): - dashboard_tab = QWidget() - dashboard_layout = QVBoxLayout() - - self.account_list = QListWidget() - self.account_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) - self.account_list.itemSelectionChanged.connect(self.update_button_states) - self.account_list.setIconSize(QSize(48, 48)) - self.account_list.setWordWrap(True) - self.account_list.setStyleSheet(""" - QListWidget { - background-color: palette(base); - padding: 4px; - outline: none; - } - QListWidget::item { - padding: 3px 6px; - margin: 0px 0px; - border: none; - outline: none; - } - QListWidget::item:selected { - background-color: palette(highlight); - border: none; - outline: none; - } - QListWidget::item:focus { - border: none; - outline: none; - } - QListWidget::item:hover { - background-color: palette(highlight); - } - """) - - dashboard_layout.addWidget(self.account_list) - - self.setup_account_buttons() - dashboard_layout.addLayout(self.btn_layout) - dashboard_tab.setLayout(dashboard_layout) - self.tab_widget.addTab(dashboard_tab, "Dashboard") - - self.hide_account_buttons() - - def setup_account_buttons(self): - self.btn_layout = QHBoxLayout() - self.import_btn = QPushButton(' Import') - self.export_btn = QPushButton(' Export') - self.download_selected_btn = QPushButton(' Download') - self.update_selected_btn = QPushButton(' Update') - self.delete_btn = QPushButton(' Delete') - - icon_dir = os.path.join(os.path.dirname(__file__), 'icons') - icon_color = self.palette().color(self.palette().ColorRole.ButtonText).name() - - self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), icon_color)) - self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), icon_color)) - self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), icon_color)) - self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), icon_color)) - self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), icon_color)) - - for btn in [self.import_btn, self.export_btn, self.download_selected_btn, - self.update_selected_btn, self.delete_btn]: - btn.setMinimumWidth(100) - btn.setCursor(Qt.CursorShape.PointingHandCursor) - btn.setIconSize(QSize(18, 18)) - - self.import_btn.clicked.connect(self.import_accounts) - self.export_btn.clicked.connect(self.export_accounts) - self.download_selected_btn.clicked.connect(self.download_selected) - self.update_selected_btn.clicked.connect(self.update_selected) - self.delete_btn.clicked.connect(self.delete_accounts) - - self.btn_layout.addStretch() - for btn in [self.import_btn, self.export_btn, self.download_selected_btn, - self.update_selected_btn, self.delete_btn]: - self.btn_layout.addWidget(btn, 1) - self.btn_layout.addStretch() - - def update_button_icons(self): - if not hasattr(self, 'import_btn'): - return - - icon_dir = os.path.join(os.path.dirname(__file__), 'icons') - icon_color = self.palette().color(self.palette().ColorRole.ButtonText).name() - - self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), icon_color)) - self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), icon_color)) - self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), icon_color)) - self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), icon_color)) - self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), icon_color)) - - def setup_process_tab(self): - self.process_tab = QWidget() - process_layout = QVBoxLayout() - process_layout.setSpacing(5) - - self.log_output = QTextEdit() - self.log_output.setReadOnly(True) - process_layout.addWidget(self.log_output) - - progress_time_layout = QVBoxLayout() - progress_time_layout.setSpacing(2) - - self.progress_bar = QProgressBar() - progress_time_layout.addWidget(self.progress_bar) - - self.time_label = QLabel("00:00:00") - self.time_label.setAlignment(Qt.AlignmentFlag.AlignCenter) - progress_time_layout.addWidget(self.time_label) - - process_layout.addLayout(progress_time_layout) - - download_control_layout = QHBoxLayout() - self.stop_btn = QPushButton('Stop') - self.pause_resume_btn = QPushButton('Pause') - - self.stop_btn.setFixedWidth(120) - self.pause_resume_btn.setFixedWidth(120) - self.stop_btn.setCursor(Qt.CursorShape.PointingHandCursor) - self.pause_resume_btn.setCursor(Qt.CursorShape.PointingHandCursor) - - self.stop_btn.clicked.connect(self.stop_download) - self.pause_resume_btn.clicked.connect(self.toggle_pause_resume) - - download_control_layout.addStretch() - download_control_layout.addWidget(self.stop_btn) - download_control_layout.addWidget(self.pause_resume_btn) - download_control_layout.addStretch() - process_layout.addLayout(download_control_layout) - - batch_control_layout = QHBoxLayout() - self.next_batch_btn = QPushButton('Next Batch') - self.auto_batch_btn = QPushButton('Auto Batch') - self.stop_fetch_btn = QPushButton('Stop Fetch') - self.cancel_fetch_btn = QPushButton('Cancel Fetch') - - for btn in [self.next_batch_btn, self.auto_batch_btn, self.stop_fetch_btn, self.cancel_fetch_btn]: - btn.setFixedWidth(120) - btn.setCursor(Qt.CursorShape.PointingHandCursor) - - self.next_batch_btn.setToolTip("Fetch next batch manually") - self.auto_batch_btn.setToolTip("Start automatic batch fetching") - self.stop_fetch_btn.setToolTip("Stop automatic batch fetching") - self.cancel_fetch_btn.setToolTip("Cancel current fetch operation") - - self.next_batch_btn.clicked.connect(self.fetch_next_batch) - self.auto_batch_btn.clicked.connect(self.start_auto_batch) - self.stop_fetch_btn.clicked.connect(self.stop_auto_fetch) - self.cancel_fetch_btn.clicked.connect(self.cancel_multiple_user_fetch) - - batch_control_layout.addStretch() - batch_control_layout.addWidget(self.next_batch_btn) - batch_control_layout.addWidget(self.auto_batch_btn) - batch_control_layout.addWidget(self.stop_fetch_btn) - batch_control_layout.addWidget(self.cancel_fetch_btn) - batch_control_layout.addStretch() - process_layout.addLayout(batch_control_layout) - - self.process_tab.setLayout(process_layout) - - self.tab_widget.addTab(self.process_tab, "Process") - - self.progress_bar.hide() - self.time_label.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - def setup_settings_tab(self): - settings_tab = QWidget() - settings_layout = QVBoxLayout() - settings_layout.setSpacing(0) - settings_layout.setContentsMargins(9, 9, 9, 9) - - output_group = QWidget() - output_layout = QVBoxLayout(output_group) - output_layout.setSpacing(5) - - output_label = QLabel('Output Directory') - output_label.setStyleSheet("font-weight: bold;") - output_layout.addWidget(output_label) - - output_dir_layout = QHBoxLayout() - self.output_dir = QLineEdit() - self.output_dir.setText(self.last_output_path) - self.output_dir.textChanged.connect(self.save_settings) - self.output_browse = QPushButton('Browse') - self.output_browse.setFixedWidth(100) - self.output_browse.setCursor(Qt.CursorShape.PointingHandCursor) - self.output_browse.clicked.connect(self.browse_output) - - output_dir_layout.addWidget(self.output_dir) - output_dir_layout.addWidget(self.output_browse) - output_layout.addLayout(output_dir_layout) - - settings_layout.addWidget(output_group) - - auth_group = QWidget() - auth_layout = QVBoxLayout(auth_group) - auth_layout.setSpacing(5) - - auth_label = QLabel('Authentication') - auth_label.setStyleSheet("font-weight: bold;") - auth_layout.addWidget(auth_label) - - auth_token_layout = QHBoxLayout() - auth_token_label = QLabel('Auth Token:') - - self.auth_token_input = QLineEdit() - self.auth_token_input.setPlaceholderText("Enter Auth Token") - self.auth_token_input.setText(self.last_auth_token) - self.auth_token_input.textChanged.connect(self.save_settings) - self.auth_token_input.setClearButtonEnabled(True) - - auth_token_layout.addWidget(auth_token_label) - auth_token_layout.addWidget(self.auth_token_input) - auth_layout.addLayout(auth_token_layout) - - settings_layout.addWidget(auth_group) - - gallery_dl_group = QWidget() - gallery_dl_layout = QVBoxLayout(gallery_dl_group) - gallery_dl_layout.setSpacing(5) - - gallery_dl_label = QLabel('gallery-dl Settings') - gallery_dl_label.setStyleSheet("font-weight: bold;") - gallery_dl_layout.addWidget(gallery_dl_label) - - first_row_layout = QHBoxLayout() - first_row_layout.setSpacing(5) - - timeline_label = QLabel("Timeline:") - self.timeline_type_combo = QComboBox() - self.timeline_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.timeline_type_combo.setFixedWidth(75) - timeline_types = [ - ('media', 'Media'), - ('timeline', 'Post'), - ('tweets', 'Tweets'), - ('with_replies', 'Replies') - ] - for value, display in timeline_types: - self.timeline_type_combo.addItem(display, value) - self.timeline_type_combo.currentTextChanged.connect(self.save_settings) - first_row_layout.addWidget(timeline_label) - first_row_layout.addWidget(self.timeline_type_combo) - - first_row_layout.addSpacing(5) - - media_label = QLabel("Media:") - self.media_type_combo = QComboBox() - self.media_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.media_type_combo.setFixedWidth(85) - media_types = [('all', 'All'), ('image', 'Image'), ('video', 'Video'), ('gif', 'GIF')] - for value, display in media_types: - self.media_type_combo.addItem(display, value) - self.media_type_combo.currentTextChanged.connect(self.save_settings) - first_row_layout.addWidget(media_label) - first_row_layout.addWidget(self.media_type_combo) - - first_row_layout.addSpacing(5) - - self.batch_checkbox = QCheckBox("Fetch Batch") - self.batch_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) - self.batch_checkbox.setToolTip("Enable for accounts with thousands of media") - self.batch_checkbox.stateChanged.connect(self.handle_batch_checkbox) - self.batch_checkbox.setChecked(self.batch_mode) - first_row_layout.addWidget(self.batch_checkbox) - - self.size_label = QLabel("Size:") - first_row_layout.addWidget(self.size_label) - self.batch_size_combo = QComboBox() - self.batch_size_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.batch_size_combo.setFixedWidth(60) - for size in [50, 100, 150, 200]: - self.batch_size_combo.addItem(str(size)) - self.batch_size_combo.setCurrentIndex(1) - self.batch_size_combo.currentTextChanged.connect(self.save_batch_size) - first_row_layout.addWidget(self.batch_size_combo) - - self.size_label.hide() - self.batch_size_combo.hide() - - first_row_layout.addStretch() - gallery_dl_layout.addLayout(first_row_layout) - - settings_layout.addWidget(gallery_dl_group) - - download_conversion_group = QWidget() - download_conversion_layout = QVBoxLayout(download_conversion_group) - download_conversion_layout.setSpacing(5) - - labels_layout = QHBoxLayout() - - download_label_container = QWidget() - download_label_layout = QHBoxLayout(download_label_container) - download_label_layout.setContentsMargins(0, 0, 0, 0) - download_label = QLabel('Download Settings') - download_label.setStyleSheet("font-weight: bold;") - download_label_layout.addWidget(download_label) - download_label_layout.addStretch() - download_label_container.setFixedWidth(180) - labels_layout.addWidget(download_label_container) - - conversion_label = QLabel('Conversion Settings') - conversion_label.setStyleSheet("font-weight: bold;") - labels_layout.addWidget(conversion_label) - labels_layout.addStretch() - download_conversion_layout.addLayout(labels_layout) - - controls_layout = QHBoxLayout() - - download_controls_container = QWidget() - download_controls_layout = QHBoxLayout(download_controls_container) - download_controls_layout.setContentsMargins(0, 0, 0, 0) - - batch_label = QLabel('Concurrents:') - download_controls_layout.addWidget(batch_label) - - self.download_batch_combo = QComboBox() - self.download_batch_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.download_batch_combo.setFixedWidth(80) - for size in range(5, 101, 5): - self.download_batch_combo.addItem(str(size)) - self.download_batch_combo.setCurrentText(str(self.download_batch_size)) - self.download_batch_combo.currentTextChanged.connect(self.save_settings) - download_controls_layout.addWidget(self.download_batch_combo) - download_controls_layout.addStretch() - - download_controls_container.setFixedWidth(180) - controls_layout.addWidget(download_controls_container) - - self.convert_gif_checkbox = QCheckBox("Convert GIF") - self.convert_gif_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) - self.convert_gif_checkbox.setChecked(self.convert_gif) - self.convert_gif_checkbox.toggled.connect(self.handle_conversion_checkbox) - controls_layout.addWidget(self.convert_gif_checkbox) - - controls_layout.addSpacing(5) - - mode_label = QLabel("Mode:") - self.conversion_mode_combo = QComboBox() - self.conversion_mode_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.conversion_mode_combo.setFixedWidth(80) - conversion_modes = [('better', 'Better'), ('fast', 'Fast')] - for value, display in conversion_modes: - self.conversion_mode_combo.addItem(display, value) - self.conversion_mode_combo.currentTextChanged.connect(self.save_settings) - controls_layout.addWidget(mode_label) - controls_layout.addWidget(self.conversion_mode_combo) - - controls_layout.addSpacing(5) - - quality_label = QLabel("Quality:") - self.conversion_quality_combo = QComboBox() - self.conversion_quality_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.conversion_quality_combo.setFixedWidth(85) - gif_qualities = [ - ('original', 'Original'), - ('high', 'High'), - ('medium', 'Medium'), - ('low', 'Low') - ] - for value, display in gif_qualities: - self.conversion_quality_combo.addItem(display, value) - self.conversion_quality_combo.currentTextChanged.connect(self.save_settings) - controls_layout.addWidget(quality_label) - controls_layout.addWidget(self.conversion_quality_combo) - - self.mode_label = mode_label - self.quality_label = quality_label - - if not self.convert_gif: - mode_label.hide() - self.conversion_mode_combo.hide() - quality_label.hide() - self.conversion_quality_combo.hide() - - controls_layout.addStretch() - download_conversion_layout.addLayout(controls_layout) - - settings_layout.addWidget(download_conversion_group) - settings_layout.addStretch() - - settings_tab.setLayout(settings_layout) - self.tab_widget.addTab(settings_tab, "Settings") - - def setup_about_tab(self): - about_tab = QWidget() - about_layout = QVBoxLayout() - about_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) - about_layout.setSpacing(15) - - sections = [ - ("Check for Updates", "Check", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases"), - ("Report an Issue", "Report", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/issues"), - ("gallery-dl Repository", "Visit", "https://github.com/mikf/gallery-dl") - ] - - for title, button_text, url in sections: - section_widget = QWidget() - section_layout = QVBoxLayout(section_widget) - section_layout.setSpacing(10) - section_layout.setContentsMargins(0, 0, 0, 0) - - label = QLabel(title) - label.setStyleSheet("font-weight: bold;") - label.setAlignment(Qt.AlignmentFlag.AlignCenter) - section_layout.addWidget(label) - - button = QPushButton(button_text) - button.setFixedSize(100, 25) - button.setCursor(Qt.CursorShape.PointingHandCursor) - button.clicked.connect(lambda _, url=url: QDesktopServices.openUrl(QUrl(url if url.startswith(('http://', 'https://')) else f'https://{url}'))) - section_layout.addWidget(button, alignment=Qt.AlignmentFlag.AlignCenter) - - about_layout.addWidget(section_widget) - - footer_label = QLabel(f"v{self.current_version} | gallery-dl v1.30.9 | October 2025") - about_layout.addWidget(footer_label, alignment=Qt.AlignmentFlag.AlignCenter) - - about_tab.setLayout(about_layout) - self.tab_widget.addTab(about_tab, "About") - - def save_url(self): - self.settings.setValue('twitter_url', self.twitter_url.text().strip()) - self.settings.sync() - - def save_batch_size(self): - self.batch_size = int(self.batch_size_combo.currentText()) - self.settings.setValue('batch_size', self.batch_size) - self.settings.sync() - - def save_settings(self): - self.settings.setValue('output_path', self.output_dir.text().strip()) - self.settings.setValue('auth_token', self.auth_token_input.text().strip()) - self.settings.setValue('media_type', self.media_type_combo.currentData()) - self.settings.setValue('download_batch_size', int(self.download_batch_combo.currentText())) - - if hasattr(self, 'convert_gif_checkbox'): - self.settings.setValue('convert_gif', self.convert_gif_checkbox.isChecked()) - self.convert_gif = self.convert_gif_checkbox.isChecked() - - if hasattr(self, 'conversion_mode_combo'): - self.settings.setValue('gif_conversion_mode', self.conversion_mode_combo.currentData()) - self.gif_conversion_mode = self.conversion_mode_combo.currentData() - - if hasattr(self, 'conversion_quality_combo'): - self.settings.setValue('gif_conversion_quality', self.conversion_quality_combo.currentData()) - self.gif_conversion_quality = self.conversion_quality_combo.currentData() - - self.settings.sync() - - def load_settings(self): - try: - if not hasattr(self, 'batch_checkbox') or not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): - return - - batch_mode = self.settings.value('batch_mode', False, type=bool) - - self.batch_checkbox.blockSignals(True) - self.batch_checkbox.setChecked(batch_mode) - self.batch_checkbox.blockSignals(False) - - if batch_mode: - self.size_label.show() - self.batch_size_combo.show() - self.fetch_btn.setText('Fetch Batch') - else: - self.size_label.hide() - self.batch_size_combo.hide() - self.fetch_btn.setText('Fetch') - - fetch_batch_size = str(self.settings.value('batch_size', 100)) - index = self.batch_size_combo.findText(fetch_batch_size) - if index >= 0: - self.batch_size_combo.setCurrentIndex(index) - else: - self.batch_size_combo.setCurrentIndex(1) - - download_batch_size = str(self.settings.value('download_batch_size', 25)) - index = self.download_batch_combo.findText(download_batch_size) - if index >= 0: - self.download_batch_combo.setCurrentIndex(index) - else: - self.download_batch_combo.setCurrentIndex(0) - media_type = self.settings.value('media_type', 'all') - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == media_type: - self.media_type_combo.setCurrentIndex(i) - break - - convert_gif = self.settings.value('convert_gif', False, type=bool) - if hasattr(self, 'convert_gif_checkbox'): - self.convert_gif_checkbox.blockSignals(True) - self.convert_gif_checkbox.setChecked(convert_gif) - self.convert_gif_checkbox.blockSignals(False) - - if convert_gif: - if hasattr(self, 'mode_label'): - self.mode_label.show() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.show() - if hasattr(self, 'quality_label'): - self.quality_label.show() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.show() - else: - if hasattr(self, 'mode_label'): - self.mode_label.hide() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.hide() - if hasattr(self, 'quality_label'): - self.quality_label.hide() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.hide() - - gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') - if hasattr(self, 'conversion_mode_combo'): - for i in range(self.conversion_mode_combo.count()): - if self.conversion_mode_combo.itemData(i) == gif_conversion_mode: - self.conversion_mode_combo.setCurrentIndex(i) - break - - gif_conversion_quality = self.settings.value('gif_conversion_quality', 'original') - if hasattr(self, 'conversion_quality_combo'): - for i in range(self.conversion_quality_combo.count()): - if self.conversion_quality_combo.itemData(i) == gif_conversion_quality: - self.conversion_quality_combo.setCurrentIndex(i) - break - - last_username_url = self.settings.value('twitter_url', '') - if last_username_url and hasattr(self, 'twitter_url'): - self.twitter_url.setText(last_username_url) - - except Exception as e: - pass - - def handle_batch_checkbox(self, state): - if not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): - return - - self.batch_mode = self.batch_checkbox.isChecked() - self.settings.setValue('batch_mode', self.batch_mode) - - if self.batch_checkbox.isChecked(): - self.size_label.show() - self.batch_size_combo.show() - self.fetch_btn.setText('Fetch Batch') - else: - self.size_label.hide() - self.batch_size_combo.hide() - self.fetch_btn.setText('Fetch') - self.update_button_states() - - def handle_conversion_checkbox(self, checked): - self.convert_gif = checked - self.save_settings() - - if checked: - if hasattr(self, 'mode_label'): - self.mode_label.show() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.show() - if hasattr(self, 'quality_label'): - self.quality_label.show() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.show() - else: - if hasattr(self, 'mode_label'): - self.mode_label.hide() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.hide() - if hasattr(self, 'quality_label'): - self.quality_label.hide() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.hide() - - def browse_output(self): - directory = QFileDialog.getExistingDirectory(self, "Select Output Directory") - if directory: - self.output_dir.setText(directory) - self.save_settings() - - def get_cache_file_path(self, username, media_type, is_batch=None): - timeline_type = self.timeline_type_combo.currentData() or 'media' - - filename = f"{username}_{timeline_type}_{media_type}.json" - return os.path.join(self.temp_dir, filename) - - def load_cached_data(self, username, media_type, is_batch=None): - cache_path = self.get_cache_file_path(username, media_type, is_batch) - if os.path.exists(cache_path): - try: - with open(cache_path, 'r', encoding='utf-8') as f: - return json.load(f) - except: - pass - return None - - def save_cached_data(self, username, media_type, data, is_batch=None): - cache_path = self.get_cache_file_path(username, media_type, is_batch) - try: - os.makedirs(os.path.dirname(cache_path), exist_ok=True) - with open(cache_path, 'w', encoding='utf-8') as f: - json.dump(data, f, ensure_ascii=False, indent=2) - except: - pass - - def load_all_cached_accounts(self): - try: - if not os.path.exists(self.temp_dir): - return - - cache_files = [f for f in os.listdir(self.temp_dir) if f.endswith('.json')] - - if not cache_files: - return - - loaded_count = 0 - - for cache_file in cache_files: - try: - filename_parts = cache_file.replace('.json', '').split('_') - - if len(filename_parts) >= 3: - media_type = filename_parts[-1] - - if len(filename_parts) == 4 and filename_parts[1] == 'batch': - username = filename_parts[0] - timeline_type = filename_parts[2] - is_batch_file = True - elif len(filename_parts) == 3: - username = filename_parts[0] - timeline_type = filename_parts[1] - is_batch_file = False - else: - username = '_'.join(filename_parts[:-2]) - timeline_type = filename_parts[-2] - is_batch_file = False - - already_exists = any( - acc.username == username and acc.media_type == media_type - for acc in self.accounts - ) - if not already_exists: - cached_data = self.load_cached_data(username, media_type, is_batch=is_batch_file) - if cached_data: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - loaded_count += 1 - except Exception as e: - continue - - if self.accounts: - self.update_account_list() - - except Exception as e: - pass - - def fetch_account(self): - url_input = self.twitter_url.text().strip() - - if not url_input: - self.log_output.append('Warning: Please enter a Twitter username/URL.') - return - - if not self.auth_token_input.text().strip(): - self.log_output.append('Warning: Please enter your auth token.') - return - - urls = [url.strip() for url in url_input.split(',') if url.strip()] - - if len(urls) > 1: - self.log_output.append(f'Processing {len(urls)} accounts: {", ".join(urls)}') - self.tab_widget.setCurrentWidget(self.process_tab) - - self.accounts_to_fetch = [] - media_type = self.media_type_combo.currentData() - - for url in urls: - username = self.normalize_url_to_username(url) - if username: - self.accounts_to_fetch.append((username, media_type)) - - if self.accounts_to_fetch: - self.current_fetch_index = 0 - self.is_multiple_user_mode = True - self.is_initial_fetch = True - - if self.batch_mode: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append('Multiple user batch mode: Auto-fetching all batches for all users...') - else: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append('Multiple user mode: Auto-fetching all users...') - - self.fetch_next_account_in_batch() - return - - url = urls[0] - username = self.normalize_url_to_username(url) - if not username: - self.log_output.append('Warning: Invalid username/URL format.') - return - - media_type = self.media_type_combo.currentData() - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - self.accounts.remove(existing_account) - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') - self.update_account_list() - - if not existing_account: - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data: - try: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - self.update_account_list() - self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - self.twitter_url.clear() - return - except: - pass - - try: - self.reset_ui() - - self.is_auto_fetching = False - self.is_multiple_user_mode = False - self.current_fetch_username = username - self.current_fetch_media_type = media_type - - self.disable_batch_buttons() - - self.log_output.append(f'Fetching metadata for {username}...') - self.tab_widget.setCurrentWidget(self.process_tab) - - self.cancel_fetch_btn.show() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start metadata fetch: {str(e)}') - self.update_account_list() - - def normalize_url_to_username(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 if username else None - - def fetch_next_account_in_batch(self): - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - self.twitter_url.clear() - self.tab_widget.setCurrentIndex(0) - return - - if not hasattr(self, 'accounts_to_fetch') or self.current_fetch_index >= len(self.accounts_to_fetch): - total_accounts = len(self.accounts_to_fetch) if hasattr(self, 'accounts_to_fetch') else 0 - self.log_output.append(f'Batch processing completed! Processed {total_accounts} accounts.') - self.twitter_url.clear() - - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - self.tab_widget.setCurrentIndex(0) - return - - username, media_type = self.accounts_to_fetch[self.current_fetch_index] - self.log_output.append(f'Processing account {self.current_fetch_index + 1}/{len(self.accounts_to_fetch)}: {username} ({media_type})') - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not getattr(self, 'is_initial_fetch', True): - self.log_output.append(f'Resuming from existing progress for {username} ({media_type}) - {len(existing_account.media_list):,} items already fetched') - - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data and cached_data.get('metadata', {}).get('has_more', False): - current_page = cached_data.get('metadata', {}).get('page', 0) - next_page = current_page + 1 - self.log_output.append(f'Continuing from batch {next_page + 1} for {username}...') - self.fetch_next_batch_for_current_user(username, media_type, next_page, cached_data.get('metadata', {})) - return - else: - self.log_output.append(f'Account {username} already completed. Moving to next account.') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - elif existing_account: - self.accounts.remove(existing_account) - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') - self.update_account_list() - - if not existing_account: - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data: - try: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - self.update_account_list() - self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - except: - pass - - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.batch_mode and not self.is_auto_fetching: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - else: - self.is_auto_fetching = False - - self.current_fetch_username = username - self.current_fetch_media_type = media_type - - self.disable_batch_buttons() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_batch_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start metadata fetch for {username}: {str(e)}') - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - return - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def on_batch_metadata_fetched(self, data, username, media_type): - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append(f'Ignoring result for {username} - auto-fetch was stopped.') - return - - if 'error' in data: - self.log_output.append(f'Error fetching {username}: {data["error"]}') - if self.is_auto_fetching: - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - account_info = data.get('account_info', {}) - timeline = data.get('timeline', []) - metadata = data.get('metadata', {}) - - if not account_info: - self.log_output.append(f'Error: Invalid account data received for {username}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - if not timeline: - media_type_display = media_type if media_type != 'all' else 'media' - self.log_output.append(f'Warning: No {media_type_display} found for account {username}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - existing_account.media_list.extend(timeline) - new_items = len(timeline) - total_items = len(existing_account.media_list) - self.log_output.append(f'Added {new_items:,} items to {username}. Total: {total_items:,} media items') - - updated_data = { - 'account_info': account_info, - 'timeline': existing_account.media_list, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': existing_account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - else: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if self.batch_mode else 'all', - fetch_timestamp=datetime.now().isoformat() - ) - - self.accounts.append(account) - existing_account = account - self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - - updated_data = { - 'account_info': account_info, - 'timeline': timeline, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - - self.update_account_list() - - if self.batch_mode and metadata.get('has_more', False): - current_page = metadata.get('page', 0) - next_page = current_page + 1 - - if self.is_auto_fetching: - self.log_output.append(f'Batch {current_page + 1} completed for {username}. Auto-fetching batch {next_page + 1}...') - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - self.log_output.append(f'Batch {current_page + 1} completed for {username}. Next batch ({next_page + 1}) available.') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if not self.is_auto_fetching: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append(f'Auto-fetching next batch for {username}...') - elif self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - if self.batch_mode: - total_items = len(existing_account.media_list) - self.log_output.append(f'All batches completed for {username}! Total: {total_items:,} media items') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - else: - self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') - return - else: - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - except Exception as e: - self.log_output.append(f'Error processing {username}: {str(e)}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def fetch_next_batch_for_current_user(self, username, media_type, page, metadata): - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Batch processing halted.') - return - - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=True, - batch_size=self.batch_size, - page=page - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_batch_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start next batch fetch for {username}: {str(e)}') - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - return - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def on_batch_metadata_error(self, error_message): - if hasattr(self, 'accounts_to_fetch') and hasattr(self, 'current_fetch_index'): - username, media_type = self.accounts_to_fetch[self.current_fetch_index] - self.log_output.append(f'Error fetching {username}: {error_message}') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Error handling halted.') - return - - if self.is_auto_fetching and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - else: - self.on_metadata_error(error_message) - - - def on_metadata_fetched(self, data, username, media_type): - try: - if 'error' in data: - self.log_output.append(f'Error: {data["error"]}') - self.update_account_list() - return - - account_info = data.get('account_info', {}) - timeline = data.get('timeline', []) - metadata = data.get('metadata', {}) - if not account_info: - self.log_output.append('Error: Invalid account data received') - self.update_account_list() - return - - if not timeline: - media_type_display = media_type if media_type != 'all' else 'media' - self.log_output.append(f'Error: No {media_type_display} found for account {username}') - self.update_account_list() - return - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - existing_account.media_list.extend(timeline) - new_items = len(timeline) - total_items = len(existing_account.media_list) - self.log_output.append(f'Added {new_items:,} items. Total: {total_items:,} media items for {username}') - else: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if self.batch_mode else 'all', - fetch_timestamp=datetime.now().isoformat() - ) - - self.accounts.append(account) - self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - existing_account = account - - updated_data = { - 'account_info': account_info, - 'timeline': existing_account.media_list, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': existing_account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - - self.update_account_list() - - self.current_fetch_username = username - self.current_fetch_media_type = media_type - self.current_fetch_metadata = metadata - - if self.batch_mode and metadata.get('has_more', False): - current_page = metadata.get('page', 0) - next_page = current_page + 1 - self.log_output.append(f'Batch {current_page + 1} completed. Next batch ({next_page + 1}) available.') - - if self.is_auto_fetching: - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_internal() - else: - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.show() - self.enable_batch_buttons() - self.log_output.append('Use "Next Batch" for manual fetch or "Auto Batch" for automatic fetching.') - else: - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - self.enable_batch_buttons() - - if self.batch_mode: - total_items = len(existing_account.media_list) - self.log_output.append(f'All batches complete! Total: {total_items:,} media items for {username}') - - self.twitter_url.clear() - - if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): - self.current_update_index += 1 - self.update_next_account() - else: - self.tab_widget.setCurrentIndex(0) - except Exception as e: - self.log_output.append(f'Error: {str(e)}') - self.update_account_list() - - def on_metadata_error(self, error_message): - self.log_output.append(f'Error: {error_message}') - - if self.is_auto_fetching: - self.stop_auto_fetch() - - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - self.enable_batch_buttons() - - if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): - self.current_update_index += 1 - self.update_next_account() - - self.update_account_list() - - def update_account_list(self): - self.account_list.clear() - for i, account in enumerate(self.accounts, 1): - media_count = len(account.media_list) if account.media_list else 0 - media_type_display = "GIF" if account.media_type.lower() == "gif" else account.media_type.title() - - line1 = f"{i}. {account.username} ({account.nick})" - line2 = f"Followers: {account.followers:,} • Following: {account.following:,} • Posts: {account.posts:,} • {media_type_display}: {media_count:,}" - line3 = self.format_fetch_info(account.fetch_timestamp) - display_text = f"{line1}\n{line2}\n{line3}" - item = QListWidgetItem() - item.setText(display_text) - item.setSizeHint(QSize(0, 70)) - - if account.profile_image: - if account.profile_image in self.profile_image_cache: - item.setIcon(QIcon(self.profile_image_cache[account.profile_image])) - else: - self.download_profile_image(account.profile_image) - placeholder = self.create_placeholder_icon(48) - if placeholder: - item.setIcon(QIcon(placeholder)) - else: - placeholder = self.create_placeholder_icon(48) - if placeholder: - item.setIcon(QIcon(placeholder)) - - self.account_list.addItem(item) - - self.update_button_states() - - def create_colored_icon(self, svg_path, color): - with open(svg_path, 'r', encoding='utf-8') as f: - svg_content = f.read() - - svg_content = svg_content.replace('stroke="currentColor"', f'stroke="{color}"') - svg_content = svg_content.replace('fill="currentColor"', f'fill="{color}"') - - pixmap = QPixmap(24, 24) - pixmap.fill(Qt.GlobalColor.transparent) - - renderer = QSvgRenderer(svg_content.encode('utf-8')) - painter = QPainter(pixmap) - renderer.render(painter) - painter.end() - - return QIcon(pixmap) - - def create_placeholder_icon(self, size=48): - pixmap = QPixmap(size, size) - pixmap.fill(Qt.GlobalColor.transparent) - - painter = QPainter(pixmap) - painter.setRenderHint(QPainter.RenderHint.Antialiasing) - - painter.setBrush(Qt.GlobalColor.gray) - painter.setPen(Qt.PenStyle.NoPen) - painter.drawRoundedRect(0, 0, size, size, 8, 8) - - painter.setPen(Qt.GlobalColor.white) - font_size = int(size * 0.4) - painter.setFont(painter.font()) - painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "👤") - - painter.end() - return pixmap - - def create_square_pixmap(self, original_pixmap, size=48): - if original_pixmap.isNull(): - return self.create_placeholder_icon(size) - - square_pixmap = QPixmap(size, size) - square_pixmap.fill(Qt.GlobalColor.transparent) - - painter = QPainter(square_pixmap) - painter.setRenderHint(QPainter.RenderHint.Antialiasing) - - path = QPainterPath() - path.addRoundedRect(0, 0, size, size, 8, 8) - painter.setClipPath(path) - - scaled_pixmap = original_pixmap.scaled( - size, size, - Qt.AspectRatioMode.KeepAspectRatioByExpanding, - Qt.TransformationMode.SmoothTransformation - ) - - x = (size - scaled_pixmap.width()) // 2 - y = (size - scaled_pixmap.height()) // 2 - painter.drawPixmap(x, y, scaled_pixmap) - - painter.end() - return square_pixmap - - def download_profile_image(self, url): - if not url or url in self.profile_image_cache or url in self.pending_downloads: - return - - try: - request = QNetworkRequest(QUrl(url)) - request.setHeader(QNetworkRequest.KnownHeaders.UserAgentHeader, - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36") - - reply = self.network_manager.get(request) - self.pending_downloads[url] = reply - reply.finished.connect(lambda: self.on_profile_image_downloaded(reply, url)) - except Exception as e: - pass - - def on_profile_image_downloaded(self, reply, image_url): - try: - if reply.error() == reply.NetworkError.NoError: - data = reply.readAll() - pixmap = QPixmap() - if pixmap.loadFromData(data): - square_pixmap = self.create_square_pixmap(pixmap, 48) - self.profile_image_cache[image_url] = square_pixmap - - self.update_account_list() - - if image_url in self.pending_downloads: - del self.pending_downloads[image_url] - - except Exception as e: - pass - finally: - reply.deleteLater() - - def update_button_states(self): - has_accounts = len(self.accounts) > 0 - - has_selected = len(self.account_list.selectedItems()) > 0 - - self.import_btn.setEnabled(True) - self.export_btn.setEnabled(has_accounts) - self.download_selected_btn.setEnabled(has_accounts) - self.update_selected_btn.setEnabled(has_accounts) - self.delete_btn.setEnabled(has_accounts) - - self.import_btn.show() - self.export_btn.show() - self.download_selected_btn.show() - self.update_selected_btn.show() - self.delete_btn.show() - - def hide_account_buttons(self): - self.import_btn.setEnabled(True) - self.export_btn.setEnabled(False) - self.download_selected_btn.setEnabled(False) - self.update_selected_btn.setEnabled(False) - self.delete_btn.setEnabled(False) - - self.import_btn.show() - self.export_btn.show() - self.download_selected_btn.show() - self.update_selected_btn.show() - self.delete_btn.show() - - def download_selected(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Download All', - f'No accounts selected. Download all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.Yes: - self.download_accounts(list(range(len(self.accounts)))) - return - - self.download_accounts([self.account_list.row(item) for item in selected_items]) - - def download_all(self): - self.download_accounts(range(len(self.accounts))) - - def update_selected(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Update All', - f'No accounts selected. Update all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - selected_accounts = self.accounts.copy() - else: - selected_accounts = [] - for item in selected_items: - index = self.account_list.row(item) - if index < len(self.accounts): - selected_accounts.append(self.accounts[index]) - - if not selected_accounts: - return - - self.tab_widget.setCurrentIndex(1) - - self.log_output.clear() - self.log_output.append(f'Starting update for {len(selected_accounts)} account(s)...') - - self.accounts_to_update = selected_accounts.copy() - self.current_update_index = 0 - - self.update_next_account() - - def update_next_account(self): - if not hasattr(self, 'accounts_to_update') or self.current_update_index >= len(self.accounts_to_update): - self.log_output.append('All selected accounts have been updated!') - if hasattr(self, 'accounts_to_update'): - delattr(self, 'accounts_to_update') - if hasattr(self, 'current_update_index'): - delattr(self, 'current_update_index') - self.tab_widget.setCurrentIndex(0) - return - - account = self.accounts_to_update[self.current_update_index] - self.log_output.append(f'Updating account {self.current_update_index + 1}/{len(self.accounts_to_update)}: {account.username} ({account.media_type})') - self.update_account(account) - - def update_account(self, account): - original_batch_mode = self.batch_mode - original_batch_size = self.batch_size - original_media_type = self.media_type_combo.currentData() - - self.batch_mode = (account.fetch_mode == 'batch') - if self.batch_mode: - self.batch_size = self.settings.value('batch_size', 100, type=int) - - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == account.media_type: - self.media_type_combo.setCurrentIndex(i) - break - - self.twitter_url.setText(account.username) - - self.tab_widget.setCurrentIndex(1) - - self.log_output.append(f'Updating {account.username} ({account.media_type}) using {account.fetch_mode} mode...') - - username = account.username - media_type = account.media_type - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - for i, existing_account in enumerate(self.accounts): - if existing_account.username == account.username and existing_account.media_type == account.media_type: - self.accounts.pop(i) - break - - self.update_account_list() - - self.start_fetch_process(username, media_type) - - self.batch_mode = original_batch_mode - self.batch_size = original_batch_size - - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == original_media_type: - self.media_type_combo.setCurrentIndex(i) - break - - def start_fetch_process(self, username, media_type): - if not self.auth_token_input.text().strip(): - self.log_output.append("Error: Please enter your auth token") - return - - self.current_page = 0 - self.disable_batch_buttons() - - self.worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.worker.auth_token = self.auth_token_input.text().strip() - self.worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) - self.worker.error.connect(self.on_metadata_error) - self.worker.start() - - def download_accounts(self, indices): - self.log_output.clear() - outpath = self.output_dir.text() - if not os.path.exists(outpath): - self.log_output.append('Warning: Invalid output directory.') - return - - if not self.auth_token_input.text().strip(): - self.log_output.append("Error: Please enter your auth token") - return - - accounts_to_download = [self.accounts[i] for i in indices] - - try: - self.start_download_worker(accounts_to_download, outpath) - except Exception as e: - self.log_output.append(f"Error: An error occurred while starting the download: {str(e)}") - - def start_download_worker(self, accounts_to_download, outpath): - self.worker = DownloadWorker( - accounts_to_download, - outpath, - self.auth_token_input.text().strip(), - self.filename_format, - self.download_batch_size, - self.convert_gif, - self.gif_conversion_quality, - self.gif_conversion_mode - ) - self.worker.finished.connect(self.on_download_finished) - self.worker.progress.connect(self.update_progress) - self.worker.conversion_progress.connect(self.update_conversion_progress) - self.worker.download_progress.connect(self.update_download_progress) - self.worker.start() - self.start_timer() - self.update_ui_for_download_start(len(accounts_to_download)) - - def update_ui_for_download_start(self, account_count): - self.download_selected_btn.setEnabled(False) - self.update_selected_btn.setEnabled(False) - self.stop_btn.show() - self.pause_resume_btn.show() - - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - self.progress_bar.show() - self.progress_bar.setValue(0) - - self.tab_widget.setCurrentWidget(self.process_tab) - - def update_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - current_text = cursor.selectedText() - progress_indicators = [ - "Downloading", - "Account", - "files downloaded" - ] - - should_replace = False - for indicator in progress_indicators: - if indicator in current_text and indicator in message: - should_replace = True - break - if should_replace and current_text.strip(): - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def update_conversion_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - current_text = cursor.selectedText() - if "Converting GIF" in current_text: - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def update_download_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - - current_text = cursor.selectedText() - if "Downloading" in current_text and ":" in current_text and "/" in current_text: - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def stop_download(self): - if hasattr(self, 'worker'): - self.worker.stop() - self.stop_timer() - self.on_download_finished(True, "Download stopped by user.") - - def on_download_finished(self, success, message): - self.progress_bar.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.pause_resume_btn.setText('Pause') - self.stop_timer() - - self.download_selected_btn.setEnabled(True) - self.update_selected_btn.setEnabled(True) - - if (self.batch_mode and self.current_fetch_metadata and - self.current_fetch_metadata.get('has_more', False) and not self.is_auto_fetching): - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.enable_batch_buttons() - - if success: - self.log_output.append(f"\nStatus: {message}") - else: - self.log_output.append(f"Error: {message}") - - self.tab_widget.setCurrentWidget(self.process_tab) - - def toggle_pause_resume(self): - if hasattr(self, 'worker'): - if self.worker.is_paused: - self.worker.resume() - self.pause_resume_btn.setText('Pause') - self.timer.start(1000) - else: - self.worker.pause() - self.pause_resume_btn.setText('Resume') - - def delete_accounts(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Delete All', - f'No accounts selected. Delete all {len(self.accounts)} account(s) and their cache files?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - - for account in self.accounts: - username = account.username - if username: - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, account.media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed temp file: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove temp file {os.path.basename(cache_file)}: {str(e)}') - - self.reset_state() - self.reset_ui() - self.tab_widget.setCurrentIndex(0) - else: - reply = QMessageBox.question( - self, - 'Confirm Delete', - f'Delete {len(selected_items)} selected account(s) and their cache files?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - - selected_indices = sorted([self.account_list.row(item) for item in selected_items], reverse=True) - - for index in selected_indices: - account = self.accounts[index] - username = account.username - if username: - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, account.media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed temp file: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove temp file {os.path.basename(cache_file)}: {str(e)}') - - self.accounts.pop(index) - - self.update_account_list() - self.update_button_states() - - def export_accounts(self): - self.tab_widget.setCurrentIndex(1) - - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Export All', - f'No accounts selected. Export all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - self.log_output.append('Export cancelled') - return - selected_accounts = self.accounts.copy() - else: - selected_accounts = [] - for item in selected_items: - row = self.account_list.row(item) - if 0 <= row < len(self.accounts): - selected_accounts.append(self.accounts[row]) - - if not selected_accounts: - self.log_output.append('No valid accounts found for export') - return - - export_dir = QFileDialog.getExistingDirectory(self, "Select Export Directory", self.last_output_path) - if not export_dir: - self.log_output.append('Export cancelled') - return - - self.log_output.append(f'Starting export of {len(selected_accounts)} account(s)...') - exported_count = 0 - - for account in selected_accounts: - try: - now = datetime.now() - date_str = now.strftime("%Y%m%d_%H%M%S") - - if account.fetch_mode == 'batch': - filename = f"{account.username}_batch_{date_str}.json" - else: - filename = f"{account.username}_{date_str}.json" - - filepath = os.path.join(export_dir, filename) - - export_data = { - 'username': account.username, - 'nick': account.nick, - 'followers': account.followers, - 'following': account.following, - 'posts': account.posts, - 'media_type': account.media_type, - 'profile_image': account.profile_image, - 'media_list': account.media_list, - 'fetch_mode': account.fetch_mode, - 'fetch_timestamp': account.fetch_timestamp, - 'export_timestamp': now.isoformat() - } - - with open(filepath, 'w', encoding='utf-8') as f: - json.dump(export_data, f, indent=2, ensure_ascii=False) - - exported_count += 1 - self.log_output.append(f'Exported: {filename}') - - except Exception as e: - self.log_output.append(f'Error exporting {account.username}: {str(e)}') - - self.log_output.append(f'Export completed: {exported_count}/{len(selected_accounts)} account(s) exported') - self.log_output.append(f'Export location: {export_dir}') - - def import_accounts(self): - self.tab_widget.setCurrentIndex(1) - - file_paths, _ = QFileDialog.getOpenFileNames( - self, - "Select JSON Files to Import", - self.last_output_path, - "JSON Files (*.json)" - ) - - if not file_paths: - self.log_output.append('Import cancelled') - return - - self.log_output.append(f'Starting import of {len(file_paths)} file(s)...') - imported_count = 0 - skipped_count = 0 - error_count = 0 - - for filepath in file_paths: - try: - with open(filepath, 'r', encoding='utf-8') as f: - data = json.load(f) - - username = data.get('username') - if not username: - self.log_output.append(f'Skipped {os.path.basename(filepath)}: No username found') - skipped_count += 1 - continue - - existing_account = next((acc for acc in self.accounts if acc.username == username), None) - if existing_account: - self.log_output.append(f'Skipped {username}: Already exists in dashboard') - skipped_count += 1 - continue - - account = Account( - username=username, - nick=data.get('nick', ''), - followers=data.get('followers', 0), - following=data.get('following', 0), - posts=data.get('posts', 0), - media_type=data.get('media_type', 'all'), - profile_image=data.get('profile_image'), - media_list=data.get('media_list', []), - fetch_mode=data.get('fetch_mode', 'all'), - fetch_timestamp=data.get('fetch_timestamp') - ) - - self.accounts.append(account) - - account_info = { - 'followers_count': account.followers, - 'friends_count': account.following, - 'statuses_count': account.posts, - 'nick': account.nick, - 'name': account.nick, - 'profile_image': account.profile_image - } - - cache_data = { - 'account_info': account_info, - 'timeline': account.media_list, - 'metadata': {}, - 'is_batch': (account.fetch_mode == 'batch'), - 'fetch_timestamp': account.fetch_timestamp - } - - self.save_cached_data(username, account.media_type, cache_data, is_batch=(account.fetch_mode == 'batch')) - - imported_count += 1 - media_count = len(account.media_list) if account.media_list else 0 - self.log_output.append(f'Imported: {username} ({media_count:,} media)') - - except Exception as e: - self.log_output.append(f'Error importing {os.path.basename(filepath)}: {str(e)}') - error_count += 1 - - self.update_account_list() - self.update_button_states() - self.log_output.append(f'Import completed: {imported_count} imported, {skipped_count} skipped, {error_count} errors') - self.tab_widget.setCurrentIndex(0) - - - - def update_timer(self): - self.elapsed_time = self.elapsed_time.addSecs(1) - self.time_label.setText(self.elapsed_time.toString("hh:mm:ss")) - - def start_timer(self): - self.elapsed_time = QTime(0, 0, 0) - self.time_label.setText("00:00:00") - self.time_label.show() - self.timer.start(1000) - - def stop_timer(self): - self.timer.stop() - self.time_label.hide() - - def fetch_next_batch(self): - if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): - self.log_output.append('No more batches available.') - return - - self.disable_batch_buttons() - self.fetch_next_batch_internal() - - def start_auto_batch(self): - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.is_auto_fetching = True - self.is_initial_fetch = False - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.log_output.append('Resuming multiple user batch processing...') - self.fetch_next_account_in_batch() - return - - if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): - self.log_output.append('No more batches available.') - return - - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.log_output.append('Auto batch mode enabled.') - self.fetch_next_batch_internal() - - def stop_auto_fetch(self): - self.is_auto_fetching = False - self.stop_fetch_btn.hide() - - if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): - self.metadata_worker.terminate() - self.metadata_worker.wait() - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.auto_batch_btn.show() - self.auto_batch_btn.setEnabled(True) - self.next_batch_btn.hide() - self.cancel_fetch_btn.show() - self.log_output.append('Multiple user batch processing stopped.') - elif self.current_fetch_metadata and self.current_fetch_metadata.get('has_more', False): - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.cancel_fetch_btn.show() - self.enable_batch_buttons() - self.log_output.append('Auto batch mode stopped.') - else: - self.cancel_fetch_btn.show() - self.log_output.append('Auto batch mode stopped.') - - def cancel_multiple_user_fetch(self): - if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): - self.metadata_worker.terminate() - self.metadata_worker.wait() - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.log_output.append('Multiple user batch processing cancelled.') - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - else: - self.log_output.append('Single user batch processing cancelled.') - - self.current_fetch_username = None - self.current_fetch_media_type = None - self.current_fetch_metadata = None - - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - - self.twitter_url.clear() - self.tab_widget.setCurrentIndex(0) - - def disable_batch_buttons(self): - self.next_batch_btn.setEnabled(False) - self.auto_batch_btn.setEnabled(False) - - def enable_batch_buttons(self): - self.next_batch_btn.setEnabled(True) - self.auto_batch_btn.setEnabled(True) - - def fetch_next_batch_internal(self): - if not self.current_fetch_username or not self.current_fetch_media_type or not self.current_fetch_metadata: - return - - self.disable_batch_buttons() - - current_page = self.current_fetch_metadata.get('page', 0) - next_page = current_page + 1 - - self.log_output.append(f'Fetching batch {next_page + 1} for {self.current_fetch_username}...') - - self.metadata_worker = MetadataFetchWorker( - self.current_fetch_username, - self.current_fetch_media_type, - batch_mode=True, - batch_size=self.batch_size, - page=next_page - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, self.current_fetch_username, self.current_fetch_media_type)) - self.metadata_worker.error.connect(self.on_metadata_error) - self.metadata_worker.start() - - def show_account_buttons(self): - self.download_selected_btn.show() - self.update_selected_btn.show() - self.remove_btn.show() - self.clear_btn.show() +from PyQt6.QtWidgets import QApplication +from ui.mainWindow_Unix import TwitterXMediaBatchDownloaderGUI_Unix def main(): if getattr(sys, 'frozen', False): os.chdir(os.path.dirname(sys.executable)) app = QApplication(sys.argv) - app.setStyle('fusion') - window = TwitterMediaDownloaderGUI() + app.setStyle('Fusion') + + window = TwitterXMediaBatchDownloaderGUI_Unix() window.show() sys.exit(app.exec()) diff --git a/TwitterXMediaBatchDownloader_Mac.py b/TwitterXMediaBatchDownloader_Mac.py index d572973..f9466a0 100644 --- a/TwitterXMediaBatchDownloader_Mac.py +++ b/TwitterXMediaBatchDownloader_Mac.py @@ -1,2635 +1,16 @@ import sys import os -import asyncio -import aiohttp -import requests -import subprocess -import imageio_ffmpeg -import json -import tempfile -from datetime import datetime -from pathlib import Path -from packaging import version -from dataclasses import dataclass -from PyQt6.QtWidgets import ( - QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, - QLabel, QFileDialog, QListWidget, QTextEdit, QTabWidget, QAbstractItemView, QProgressBar, QCheckBox, QDialog, - QDialogButtonBox, QComboBox, QListWidgetItem, QMessageBox -) -from PyQt6.QtCore import Qt, QThread, pyqtSignal, QUrl, QTimer, QTime, QSettings, QSize -from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest -from PyQt6.QtGui import QIcon, QTextCursor, QDesktopServices, QPixmap, QPainter, QPainterPath -from PyQt6.QtSvg import QSvgRenderer -from getMetadata import get_metadata - -@dataclass -class Account: - username: str - nick: str - followers: int - following: int - posts: int - media_type: str - profile_image: str = None - media_list: list = None - fetch_mode: str = 'all' - fetch_timestamp: str = None - -class MetadataFetchWorker(QThread): - finished = pyqtSignal(dict) - error = pyqtSignal(str) - - def __init__(self, username, media_type='all', batch_mode=False, batch_size=0, page=0): - super().__init__() - self.username = username - self.media_type = media_type - self.batch_mode = batch_mode - self.batch_size = batch_size - self.page = page - self.auth_token = None - - 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 - - def run(self): - try: - normalized = self.normalize_url(self.username) - data = get_metadata( - username=normalized, - auth_token=self.auth_token, - timeline_type='media', - batch_size=self.batch_size if self.batch_mode else 0, - page=self.page, - media_type=self.media_type - ) - self.finished.emit(data) - except Exception as e: - self.error.emit(str(e)) - -class DownloadWorker(QThread): - finished = pyqtSignal(bool, str) - progress = pyqtSignal(str, int) - conversion_progress = pyqtSignal(str, int) - download_progress = pyqtSignal(str, int) - - def __init__(self, accounts, outpath, auth_token, filename_format='username_date', - download_batch_size=25, convert_gif=False, gif_resolution='original', gif_conversion_mode='better'): - super().__init__() - self.accounts = accounts - self.outpath = outpath - self.auth_token = auth_token - self.filename_format = filename_format - self.download_batch_size = download_batch_size - self.convert_gif = convert_gif - self.gif_resolution = gif_resolution - self.gif_conversion_mode = gif_conversion_mode - self.is_paused = False - self.is_stopped = False - self.filepath_map = [] - - async def download_file(self, session, url, filepath): - try: - if os.path.exists(filepath): - return True, True - 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()) - return True, False - return False, False - except Exception as e: - return False, False - - async def download_account_media(self, account): - if not account.media_list: - return 0, 0, 0 - - account_output_dir = os.path.join(self.outpath, account.username) - os.makedirs(account_output_dir, exist_ok=True) - timeout = aiohttp.ClientTimeout(total=30) - connector = aiohttp.TCPConnector(limit=self.download_batch_size) - - async with aiohttp.ClientSession(timeout=timeout, connector=connector) as session: - total = len(account.media_list) - completed = 0 - skipped = 0 - failed = 0 - - used_filenames = set() - - for i in range(0, total, self.download_batch_size): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - return completed, skipped, failed - await asyncio.sleep(0.1) - - batch = account.media_list[i:i + self.download_batch_size] - tasks = [] - - for item in batch: - url = item['url'] - 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', '')) - - item_type = item.get('type', '') - if item_type == 'animated_gif': - media_type_folder = 'gif' - extension = 'mp4' - elif item_type == 'video' or 'video.twimg.com' in url: - media_type_folder = 'video' - extension = 'mp4' - else: - media_type_folder = 'image' - extension = 'jpg' - media_output_dir = os.path.join(account_output_dir, media_type_folder) - - if self.filename_format == "username_date": - base_filename = f"{account.username}_{formatted_date}_{tweet_id}" - else: - base_filename = f"{formatted_date}_{account.username}_{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(media_output_dir, filename) - self.filepath_map.append((item, filepath)) - 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_percent = int((completed + failed) / total * 100) - media_type_display = account.media_type if account.media_type != 'all' else 'media' - self.download_progress.emit(f"Downloading {account.username}'s {media_type_display}: {completed + failed:,}/{total:,}", progress_percent) - - await asyncio.sleep(0.1) - - return completed, skipped, failed - - def run(self): - try: - total_accounts = len(self.accounts) - overall_completed = 0 - overall_skipped = 0 - overall_failed = 0 - - for i, account in enumerate(self.accounts): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - return - self.msleep(100) - media_type_display = account.media_type if account.media_type != 'all' else 'media' - self.progress.emit(f"Downloading from account: {account.username} ({media_type_display}) - ({i+1}/{total_accounts})", - int((i) / total_accounts * 100)) - - completed, skipped, failed = asyncio.run(self.download_account_media(account)) - overall_completed += completed - overall_skipped += skipped - overall_failed += failed - self.progress.emit(f"Account {account.username} ({media_type_display}): {completed:,} downloaded, {skipped:,} skipped, {failed:,} failed", - int((i + 1) / total_accounts * 100)) - - if not self.is_stopped: - if self.convert_gif: - try: - ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe() - gif_items = [(item, fp) for item, fp in self.filepath_map if item.get('type') == 'animated_gif'] - total_gifs = len(gif_items) - if total_gifs > 0: - creationflags = subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0 - converted_count = 0 - skipped_count = 0 - self.progress.emit("Starting GIF conversion...", 0) - - for idx, (item, fp) in enumerate(gif_items, start=1): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - break - self.msleep(100) - - if self.is_stopped: - break - - gif_fp = fp.rsplit('.', 1)[0] + '.gif' - - if os.path.exists(gif_fp): - try: - os.remove(fp) - skipped_count += 1 - conv_progress = int((idx / total_gifs) * 100) - quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" - self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display} - Skipped (exists)", conv_progress) - continue - except Exception: - pass - - - if self.gif_conversion_mode == 'fast': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-y', gif_fp] - else: - if self.gif_resolution == 'high': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=800:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=800:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '15', '-y', gif_fp] - elif self.gif_resolution == 'medium': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=600:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=600:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '10', '-y', gif_fp] - elif self.gif_resolution == 'low': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=400:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=400:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '8', '-y', gif_fp] - else: - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'palettegen=stats_mode=full[palette];[0:v][palette]paletteuse=dither=sierra2_4a', '-y', gif_fp] - - result = subprocess.run(ffmpeg_args, capture_output=True, creationflags=creationflags) - if result.returncode == 0 and os.path.exists(gif_fp): - try: - os.remove(fp) - converted_count += 1 - except Exception: - pass - else: - pass - - conv_progress = int((idx / total_gifs) * 100) - quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" - self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display}", conv_progress) - if converted_count > 0 or skipped_count > 0: - completion_msg = f"GIF conversion completed: {converted_count:,} converted" - if skipped_count > 0: - completion_msg += f", {skipped_count:,} skipped (already exists)" - self.progress.emit(completion_msg, 100) - else: - self.progress.emit("GIF conversion completed", 100) - except Exception as conv_e: - self.progress.emit(f"GIF conversion error: {conv_e}", 0) - - success_message = f"Download completed! {overall_completed:,} files downloaded" - if overall_skipped > 0: - success_message += f", {overall_skipped:,} skipped" - if overall_failed > 0: - success_message += f", {overall_failed:,} failed" - self.finished.emit(True, success_message) - - except Exception as e: - self.finished.emit(False, str(e)) - - def pause(self): - self.is_paused = True - - def resume(self): - self.is_paused = False - - def stop(self): - self.is_stopped = True - self.is_paused = False - -class UpdateDialog(QDialog): - def __init__(self, current_version, new_version, parent=None): - super().__init__(parent) - self.setWindowTitle("Update Now") - self.setFixedWidth(400) - self.setModal(True) - - layout = QVBoxLayout() - - message = QLabel(f"Twitter/X Media Batch Downloader v{new_version} Available!") - message.setWordWrap(True) - layout.addWidget(message) - - button_box = QDialogButtonBox() - self.update_button = QPushButton("Check") - self.update_button.setCursor(Qt.CursorShape.PointingHandCursor) - self.cancel_button = QPushButton("Later") - self.cancel_button.setCursor(Qt.CursorShape.PointingHandCursor) - - button_box.addButton(self.update_button, QDialogButtonBox.ButtonRole.AcceptRole) - button_box.addButton(self.cancel_button, QDialogButtonBox.ButtonRole.RejectRole) - - layout.addWidget(button_box) - - self.setLayout(layout) - - self.update_button.clicked.connect(self.accept) - self.cancel_button.clicked.connect(self.reject) - -class TwitterMediaDownloaderGUI(QWidget): - def __init__(self): - super().__init__() - self.current_version = "3.7" - self.accounts = [] - self.temp_dir = os.path.join(Path.home(), ".twitterxmediabatchdownloader") - os.makedirs(self.temp_dir, exist_ok=True) - self.reset_state() - - self.settings = QSettings('TwitterMediaDownloader', 'Settings') - self.last_output_path = self.settings.value('output_path', str(Path.home() / "Pictures")) - self.last_url = self.settings.value('twitter_url', '') - self.last_auth_token = self.settings.value('auth_token', '') - self.filename_format = self.settings.value('filename_format', 'username_date') - self.download_batch_size = self.settings.value('download_batch_size', 25, type=int) - self.batch_mode = self.settings.value('batch_mode', False, type=bool) - self.batch_size = self.settings.value('batch_size', 100, type=int) - self.timeline_type = self.settings.value('timeline_type', 'media') - self.media_type = self.settings.value('media_type', 'all') - self.convert_gif = self.settings.value('convert_gif', False, type=bool) - self.gif_resolution = self.settings.value('gif_resolution', 'original') - self.gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') - self.gif_conversion_quality = self.settings.value('gif_conversion_quality', 'original') - self.check_for_updates = self.settings.value('check_for_updates', True, type=bool) - - self.is_auto_fetching = False - self.current_fetch_username = None - self.current_fetch_media_type = None - self.current_fetch_metadata = None - self.is_multiple_user_mode = False - - self.profile_image_cache = {} - self.pending_downloads = {} - self.network_manager = QNetworkAccessManager() - - self.elapsed_time = QTime(0, 0, 0) - self.timer = QTimer(self) - self.timer.timeout.connect(self.update_timer) - self.initUI() - self.load_settings() - self.load_all_cached_accounts() - - if self.check_for_updates: - QTimer.singleShot(0, self.check_updates) - - def get_time_ago(self, timestamp_str): - if not timestamp_str: - return "Unknown" - - try: - fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) - now = datetime.now(fetch_time.tzinfo) if fetch_time.tzinfo else datetime.now() - - diff = now - fetch_time - days = diff.days - hours = diff.seconds // 3600 - minutes = (diff.seconds % 3600) // 60 - - if days > 0: - return f"{days} day{'s' if days != 1 else ''} ago" - elif hours > 0: - return f"{hours} hour{'s' if hours != 1 else ''} ago" - elif minutes > 0: - return f"{minutes} minute{'s' if minutes != 1 else ''} ago" - else: - return "Just now" - except: - return "Unknown" - - def format_fetch_info(self, timestamp_str): - if not timestamp_str: - return "Fetched: Unknown" - - try: - fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) - formatted_datetime = fetch_time.strftime("%Y/%m/%d • %H:%M") - age = self.get_time_ago(timestamp_str) - return f"Fetched: {formatted_datetime} ({age})" - except: - return "Fetched: Unknown" - - def check_updates(self): - try: - response = requests.get("https://raw.githubusercontent.com/afkarxyz/Twitter-X-Media-Batch-Downloader/refs/heads/main/version.json") - if response.status_code == 200: - data = response.json() - new_version = data.get("version") - - if new_version and version.parse(new_version) > version.parse(self.current_version): - dialog = UpdateDialog(self.current_version, new_version, self) - result = dialog.exec() - - if result == QDialog.DialogCode.Accepted: - QDesktopServices.openUrl(QUrl("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases")) - - except Exception as e: - pass - - def reset_state(self): - self.accounts.clear() - - def reset_ui(self): - self.account_list.clear() - self.log_output.clear() - self.progress_bar.setValue(0) - self.progress_bar.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.pause_resume_btn.setText('Pause') - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - self.is_multiple_user_mode = False - self.enable_batch_buttons() - self.hide_account_buttons() - - def initUI(self): - self.setWindowTitle('Twitter/X Media Batch Downloader') - self.setMinimumWidth(670) - self.setMinimumHeight(350) - - icon_path = os.path.join(os.path.dirname(__file__), "icons", "icon.svg") - if os.path.exists(icon_path): - self.setWindowIcon(QIcon(icon_path)) - - self.main_layout = QVBoxLayout() - - self.setup_twitter_section() - self.setup_tabs() - - self.setLayout(self.main_layout) - - def setup_twitter_section(self): - twitter_layout = QHBoxLayout() - twitter_label = QLabel('Username/URL:') - twitter_label.setFixedWidth(100) - - self.twitter_url = QLineEdit() - self.twitter_url.setPlaceholderText("e.g. Takomayuyi or https://x.com/Takomayuyi or user1,user2,user3") - self.twitter_url.setClearButtonEnabled(True) - self.twitter_url.setText(self.last_url) - self.twitter_url.textChanged.connect(self.save_url) - self.fetch_btn = QPushButton('Fetch') - self.fetch_btn.setFixedWidth(100) - self.fetch_btn.setCursor(Qt.CursorShape.PointingHandCursor) - self.fetch_btn.clicked.connect(self.fetch_account) - - twitter_layout.addWidget(twitter_label) - twitter_layout.addWidget(self.twitter_url) - twitter_layout.addWidget(self.fetch_btn) - self.main_layout.addLayout(twitter_layout) - - def setup_tabs(self): - self.tab_widget = QTabWidget() - self.main_layout.addWidget(self.tab_widget) - - self.setup_dashboard_tab() - self.setup_process_tab() - self.setup_settings_tab() - self.setup_about_tab() - - def setup_dashboard_tab(self): - dashboard_tab = QWidget() - dashboard_layout = QVBoxLayout() - - self.account_list = QListWidget() - self.account_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) - self.account_list.itemSelectionChanged.connect(self.update_button_states) - self.account_list.setIconSize(QSize(48, 48)) - self.account_list.setWordWrap(True) - self.account_list.setStyleSheet(""" - QListWidget { - background-color: palette(base); - padding: 4px; - outline: none; - } - QListWidget::item { - padding: 3px 6px; - margin: 0px 0px; - border: none; - outline: none; - } - QListWidget::item:selected { - background-color: palette(highlight); - border: none; - outline: none; - } - QListWidget::item:focus { - border: none; - outline: none; - } - QListWidget::item:hover { - background-color: palette(highlight); - } - """) - - dashboard_layout.addWidget(self.account_list) - - self.setup_account_buttons() - dashboard_layout.addLayout(self.btn_layout) - dashboard_tab.setLayout(dashboard_layout) - self.tab_widget.addTab(dashboard_tab, "Dashboard") - - self.hide_account_buttons() - - def setup_account_buttons(self): - self.btn_layout = QHBoxLayout() - self.import_btn = QPushButton(' Import') - self.export_btn = QPushButton(' Export') - self.download_selected_btn = QPushButton(' Download') - self.update_selected_btn = QPushButton(' Update') - self.delete_btn = QPushButton(' Delete') - - icon_dir = os.path.join(os.path.dirname(__file__), 'icons') - icon_color = self.palette().color(self.palette().ColorRole.ButtonText).name() - - self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), icon_color)) - self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), icon_color)) - self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), icon_color)) - self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), icon_color)) - self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), icon_color)) - - for btn in [self.import_btn, self.export_btn, self.download_selected_btn, - self.update_selected_btn, self.delete_btn]: - btn.setMinimumWidth(100) - btn.setCursor(Qt.CursorShape.PointingHandCursor) - btn.setIconSize(QSize(18, 18)) - - self.import_btn.clicked.connect(self.import_accounts) - self.export_btn.clicked.connect(self.export_accounts) - self.download_selected_btn.clicked.connect(self.download_selected) - self.update_selected_btn.clicked.connect(self.update_selected) - self.delete_btn.clicked.connect(self.delete_accounts) - - self.btn_layout.addStretch() - for btn in [self.import_btn, self.export_btn, self.download_selected_btn, - self.update_selected_btn, self.delete_btn]: - self.btn_layout.addWidget(btn, 1) - self.btn_layout.addStretch() - - def update_button_icons(self): - if not hasattr(self, 'import_btn'): - return - - icon_dir = os.path.join(os.path.dirname(__file__), 'icons') - icon_color = self.palette().color(self.palette().ColorRole.ButtonText).name() - - self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), icon_color)) - self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), icon_color)) - self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), icon_color)) - self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), icon_color)) - self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), icon_color)) - - def setup_process_tab(self): - self.process_tab = QWidget() - process_layout = QVBoxLayout() - process_layout.setSpacing(5) - - self.log_output = QTextEdit() - self.log_output.setReadOnly(True) - process_layout.addWidget(self.log_output) - - progress_time_layout = QVBoxLayout() - progress_time_layout.setSpacing(2) - - self.progress_bar = QProgressBar() - progress_time_layout.addWidget(self.progress_bar) - - self.time_label = QLabel("00:00:00") - self.time_label.setAlignment(Qt.AlignmentFlag.AlignCenter) - progress_time_layout.addWidget(self.time_label) - - process_layout.addLayout(progress_time_layout) - - download_control_layout = QHBoxLayout() - self.stop_btn = QPushButton('Stop') - self.pause_resume_btn = QPushButton('Pause') - - self.stop_btn.setFixedWidth(120) - self.pause_resume_btn.setFixedWidth(120) - self.stop_btn.setCursor(Qt.CursorShape.PointingHandCursor) - self.pause_resume_btn.setCursor(Qt.CursorShape.PointingHandCursor) - - self.stop_btn.clicked.connect(self.stop_download) - self.pause_resume_btn.clicked.connect(self.toggle_pause_resume) - - download_control_layout.addStretch() - download_control_layout.addWidget(self.stop_btn) - download_control_layout.addWidget(self.pause_resume_btn) - download_control_layout.addStretch() - process_layout.addLayout(download_control_layout) - - batch_control_layout = QHBoxLayout() - self.next_batch_btn = QPushButton('Next Batch') - self.auto_batch_btn = QPushButton('Auto Batch') - self.stop_fetch_btn = QPushButton('Stop Fetch') - self.cancel_fetch_btn = QPushButton('Cancel Fetch') - - for btn in [self.next_batch_btn, self.auto_batch_btn, self.stop_fetch_btn, self.cancel_fetch_btn]: - btn.setFixedWidth(120) - btn.setCursor(Qt.CursorShape.PointingHandCursor) - - self.next_batch_btn.setToolTip("Fetch next batch manually") - self.auto_batch_btn.setToolTip("Start automatic batch fetching") - self.stop_fetch_btn.setToolTip("Stop automatic batch fetching") - self.cancel_fetch_btn.setToolTip("Cancel current fetch operation") - - self.next_batch_btn.clicked.connect(self.fetch_next_batch) - self.auto_batch_btn.clicked.connect(self.start_auto_batch) - self.stop_fetch_btn.clicked.connect(self.stop_auto_fetch) - self.cancel_fetch_btn.clicked.connect(self.cancel_multiple_user_fetch) - - batch_control_layout.addStretch() - batch_control_layout.addWidget(self.next_batch_btn) - batch_control_layout.addWidget(self.auto_batch_btn) - batch_control_layout.addWidget(self.stop_fetch_btn) - batch_control_layout.addWidget(self.cancel_fetch_btn) - batch_control_layout.addStretch() - process_layout.addLayout(batch_control_layout) - - self.process_tab.setLayout(process_layout) - - self.tab_widget.addTab(self.process_tab, "Process") - - self.progress_bar.hide() - self.time_label.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - def setup_settings_tab(self): - settings_tab = QWidget() - settings_layout = QVBoxLayout() - settings_layout.setSpacing(0) - settings_layout.setContentsMargins(9, 9, 9, 9) - - output_group = QWidget() - output_layout = QVBoxLayout(output_group) - output_layout.setSpacing(5) - - output_label = QLabel('Output Directory') - output_label.setStyleSheet("font-weight: bold;") - output_layout.addWidget(output_label) - - output_dir_layout = QHBoxLayout() - self.output_dir = QLineEdit() - self.output_dir.setText(self.last_output_path) - self.output_dir.textChanged.connect(self.save_settings) - self.output_browse = QPushButton('Browse') - self.output_browse.setFixedWidth(100) - self.output_browse.setCursor(Qt.CursorShape.PointingHandCursor) - self.output_browse.clicked.connect(self.browse_output) - - output_dir_layout.addWidget(self.output_dir) - output_dir_layout.addWidget(self.output_browse) - output_layout.addLayout(output_dir_layout) - - settings_layout.addWidget(output_group) - - auth_group = QWidget() - auth_layout = QVBoxLayout(auth_group) - auth_layout.setSpacing(5) - - auth_label = QLabel('Authentication') - auth_label.setStyleSheet("font-weight: bold;") - auth_layout.addWidget(auth_label) - - auth_token_layout = QHBoxLayout() - auth_token_label = QLabel('Auth Token:') - - self.auth_token_input = QLineEdit() - self.auth_token_input.setPlaceholderText("Enter Auth Token") - self.auth_token_input.setText(self.last_auth_token) - self.auth_token_input.textChanged.connect(self.save_settings) - self.auth_token_input.setClearButtonEnabled(True) - - auth_token_layout.addWidget(auth_token_label) - auth_token_layout.addWidget(self.auth_token_input) - auth_layout.addLayout(auth_token_layout) - - settings_layout.addWidget(auth_group) - - gallery_dl_group = QWidget() - gallery_dl_layout = QVBoxLayout(gallery_dl_group) - gallery_dl_layout.setSpacing(5) - - gallery_dl_label = QLabel('gallery-dl Settings') - gallery_dl_label.setStyleSheet("font-weight: bold;") - gallery_dl_layout.addWidget(gallery_dl_label) - - first_row_layout = QHBoxLayout() - first_row_layout.setSpacing(5) - - timeline_label = QLabel("Timeline:") - self.timeline_type_combo = QComboBox() - self.timeline_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.timeline_type_combo.setFixedWidth(75) - timeline_types = [ - ('media', 'Media'), - ('timeline', 'Post'), - ('tweets', 'Tweets'), - ('with_replies', 'Replies') - ] - for value, display in timeline_types: - self.timeline_type_combo.addItem(display, value) - self.timeline_type_combo.currentTextChanged.connect(self.save_settings) - first_row_layout.addWidget(timeline_label) - first_row_layout.addWidget(self.timeline_type_combo) - - first_row_layout.addSpacing(5) - - media_label = QLabel("Media:") - self.media_type_combo = QComboBox() - self.media_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.media_type_combo.setFixedWidth(85) - media_types = [('all', 'All'), ('image', 'Image'), ('video', 'Video'), ('gif', 'GIF')] - for value, display in media_types: - self.media_type_combo.addItem(display, value) - self.media_type_combo.currentTextChanged.connect(self.save_settings) - first_row_layout.addWidget(media_label) - first_row_layout.addWidget(self.media_type_combo) - - first_row_layout.addSpacing(5) - - self.batch_checkbox = QCheckBox("Fetch Batch") - self.batch_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) - self.batch_checkbox.setToolTip("Enable for accounts with thousands of media") - self.batch_checkbox.stateChanged.connect(self.handle_batch_checkbox) - self.batch_checkbox.setChecked(self.batch_mode) - first_row_layout.addWidget(self.batch_checkbox) - - self.size_label = QLabel("Size:") - first_row_layout.addWidget(self.size_label) - self.batch_size_combo = QComboBox() - self.batch_size_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.batch_size_combo.setFixedWidth(60) - for size in [50, 100, 150, 200]: - self.batch_size_combo.addItem(str(size)) - self.batch_size_combo.setCurrentIndex(1) - self.batch_size_combo.currentTextChanged.connect(self.save_batch_size) - first_row_layout.addWidget(self.batch_size_combo) - - self.size_label.hide() - self.batch_size_combo.hide() - - first_row_layout.addStretch() - gallery_dl_layout.addLayout(first_row_layout) - - settings_layout.addWidget(gallery_dl_group) - - download_conversion_group = QWidget() - download_conversion_layout = QVBoxLayout(download_conversion_group) - download_conversion_layout.setSpacing(5) - - labels_layout = QHBoxLayout() - - download_label_container = QWidget() - download_label_layout = QHBoxLayout(download_label_container) - download_label_layout.setContentsMargins(0, 0, 0, 0) - download_label = QLabel('Download Settings') - download_label.setStyleSheet("font-weight: bold;") - download_label_layout.addWidget(download_label) - download_label_layout.addStretch() - download_label_container.setFixedWidth(180) - labels_layout.addWidget(download_label_container) - - conversion_label = QLabel('Conversion Settings') - conversion_label.setStyleSheet("font-weight: bold;") - labels_layout.addWidget(conversion_label) - labels_layout.addStretch() - download_conversion_layout.addLayout(labels_layout) - - controls_layout = QHBoxLayout() - - download_controls_container = QWidget() - download_controls_layout = QHBoxLayout(download_controls_container) - download_controls_layout.setContentsMargins(0, 0, 0, 0) - - batch_label = QLabel('Concurrents:') - download_controls_layout.addWidget(batch_label) - - self.download_batch_combo = QComboBox() - self.download_batch_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.download_batch_combo.setFixedWidth(80) - for size in range(5, 101, 5): - self.download_batch_combo.addItem(str(size)) - self.download_batch_combo.setCurrentText(str(self.download_batch_size)) - self.download_batch_combo.currentTextChanged.connect(self.save_settings) - download_controls_layout.addWidget(self.download_batch_combo) - download_controls_layout.addStretch() - - download_controls_container.setFixedWidth(180) - controls_layout.addWidget(download_controls_container) - - self.convert_gif_checkbox = QCheckBox("Convert GIF") - self.convert_gif_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) - self.convert_gif_checkbox.setChecked(self.convert_gif) - self.convert_gif_checkbox.toggled.connect(self.handle_conversion_checkbox) - controls_layout.addWidget(self.convert_gif_checkbox) - - controls_layout.addSpacing(5) - - mode_label = QLabel("Mode:") - self.conversion_mode_combo = QComboBox() - self.conversion_mode_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.conversion_mode_combo.setFixedWidth(80) - conversion_modes = [('better', 'Better'), ('fast', 'Fast')] - for value, display in conversion_modes: - self.conversion_mode_combo.addItem(display, value) - self.conversion_mode_combo.currentTextChanged.connect(self.save_settings) - controls_layout.addWidget(mode_label) - controls_layout.addWidget(self.conversion_mode_combo) - - controls_layout.addSpacing(5) - - quality_label = QLabel("Quality:") - self.conversion_quality_combo = QComboBox() - self.conversion_quality_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.conversion_quality_combo.setFixedWidth(85) - gif_qualities = [ - ('original', 'Original'), - ('high', 'High'), - ('medium', 'Medium'), - ('low', 'Low') - ] - for value, display in gif_qualities: - self.conversion_quality_combo.addItem(display, value) - self.conversion_quality_combo.currentTextChanged.connect(self.save_settings) - controls_layout.addWidget(quality_label) - controls_layout.addWidget(self.conversion_quality_combo) - - self.mode_label = mode_label - self.quality_label = quality_label - - if not self.convert_gif: - mode_label.hide() - self.conversion_mode_combo.hide() - quality_label.hide() - self.conversion_quality_combo.hide() - - controls_layout.addStretch() - download_conversion_layout.addLayout(controls_layout) - - settings_layout.addWidget(download_conversion_group) - settings_layout.addStretch() - - settings_tab.setLayout(settings_layout) - self.tab_widget.addTab(settings_tab, "Settings") - - def setup_about_tab(self): - about_tab = QWidget() - about_layout = QVBoxLayout() - about_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) - about_layout.setSpacing(15) - - sections = [ - ("Check for Updates", "Check", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases"), - ("Report an Issue", "Report", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/issues"), - ("gallery-dl Repository", "Visit", "https://github.com/mikf/gallery-dl") - ] - - for title, button_text, url in sections: - section_widget = QWidget() - section_layout = QVBoxLayout(section_widget) - section_layout.setSpacing(10) - section_layout.setContentsMargins(0, 0, 0, 0) - - label = QLabel(title) - label.setStyleSheet("font-weight: bold;") - label.setAlignment(Qt.AlignmentFlag.AlignCenter) - section_layout.addWidget(label) - - button = QPushButton(button_text) - button.setFixedSize(100, 25) - button.setCursor(Qt.CursorShape.PointingHandCursor) - button.clicked.connect(lambda _, url=url: QDesktopServices.openUrl(QUrl(url if url.startswith(('http://', 'https://')) else f'https://{url}'))) - section_layout.addWidget(button, alignment=Qt.AlignmentFlag.AlignCenter) - - about_layout.addWidget(section_widget) - - footer_label = QLabel(f"v{self.current_version} | gallery-dl v1.30.9 | October 2025") - about_layout.addWidget(footer_label, alignment=Qt.AlignmentFlag.AlignCenter) - - about_tab.setLayout(about_layout) - self.tab_widget.addTab(about_tab, "About") - - def save_url(self): - self.settings.setValue('twitter_url', self.twitter_url.text().strip()) - self.settings.sync() - - def save_batch_size(self): - self.batch_size = int(self.batch_size_combo.currentText()) - self.settings.setValue('batch_size', self.batch_size) - self.settings.sync() - - def save_settings(self): - self.settings.setValue('output_path', self.output_dir.text().strip()) - self.settings.setValue('auth_token', self.auth_token_input.text().strip()) - self.settings.setValue('media_type', self.media_type_combo.currentData()) - self.settings.setValue('download_batch_size', int(self.download_batch_combo.currentText())) - - if hasattr(self, 'convert_gif_checkbox'): - self.settings.setValue('convert_gif', self.convert_gif_checkbox.isChecked()) - self.convert_gif = self.convert_gif_checkbox.isChecked() - - if hasattr(self, 'conversion_mode_combo'): - self.settings.setValue('gif_conversion_mode', self.conversion_mode_combo.currentData()) - self.gif_conversion_mode = self.conversion_mode_combo.currentData() - - if hasattr(self, 'conversion_quality_combo'): - self.settings.setValue('gif_conversion_quality', self.conversion_quality_combo.currentData()) - self.gif_conversion_quality = self.conversion_quality_combo.currentData() - - self.settings.sync() - - def load_settings(self): - try: - if not hasattr(self, 'batch_checkbox') or not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): - return - - batch_mode = self.settings.value('batch_mode', False, type=bool) - - self.batch_checkbox.blockSignals(True) - self.batch_checkbox.setChecked(batch_mode) - self.batch_checkbox.blockSignals(False) - - if batch_mode: - self.size_label.show() - self.batch_size_combo.show() - self.fetch_btn.setText('Fetch Batch') - else: - self.size_label.hide() - self.batch_size_combo.hide() - self.fetch_btn.setText('Fetch') - - fetch_batch_size = str(self.settings.value('batch_size', 100)) - index = self.batch_size_combo.findText(fetch_batch_size) - if index >= 0: - self.batch_size_combo.setCurrentIndex(index) - else: - self.batch_size_combo.setCurrentIndex(1) - - download_batch_size = str(self.settings.value('download_batch_size', 25)) - index = self.download_batch_combo.findText(download_batch_size) - if index >= 0: - self.download_batch_combo.setCurrentIndex(index) - else: - self.download_batch_combo.setCurrentIndex(0) - media_type = self.settings.value('media_type', 'all') - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == media_type: - self.media_type_combo.setCurrentIndex(i) - break - - convert_gif = self.settings.value('convert_gif', False, type=bool) - if hasattr(self, 'convert_gif_checkbox'): - self.convert_gif_checkbox.blockSignals(True) - self.convert_gif_checkbox.setChecked(convert_gif) - self.convert_gif_checkbox.blockSignals(False) - - if convert_gif: - if hasattr(self, 'mode_label'): - self.mode_label.show() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.show() - if hasattr(self, 'quality_label'): - self.quality_label.show() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.show() - else: - if hasattr(self, 'mode_label'): - self.mode_label.hide() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.hide() - if hasattr(self, 'quality_label'): - self.quality_label.hide() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.hide() - - gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') - if hasattr(self, 'conversion_mode_combo'): - for i in range(self.conversion_mode_combo.count()): - if self.conversion_mode_combo.itemData(i) == gif_conversion_mode: - self.conversion_mode_combo.setCurrentIndex(i) - break - - gif_conversion_quality = self.settings.value('gif_conversion_quality', 'original') - if hasattr(self, 'conversion_quality_combo'): - for i in range(self.conversion_quality_combo.count()): - if self.conversion_quality_combo.itemData(i) == gif_conversion_quality: - self.conversion_quality_combo.setCurrentIndex(i) - break - - last_username_url = self.settings.value('twitter_url', '') - if last_username_url and hasattr(self, 'twitter_url'): - self.twitter_url.setText(last_username_url) - - except Exception as e: - pass - - def handle_batch_checkbox(self, state): - if not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): - return - - self.batch_mode = self.batch_checkbox.isChecked() - self.settings.setValue('batch_mode', self.batch_mode) - - if self.batch_checkbox.isChecked(): - self.size_label.show() - self.batch_size_combo.show() - self.fetch_btn.setText('Fetch Batch') - else: - self.size_label.hide() - self.batch_size_combo.hide() - self.fetch_btn.setText('Fetch') - self.update_button_states() - - def handle_conversion_checkbox(self, checked): - self.convert_gif = checked - self.save_settings() - - if checked: - if hasattr(self, 'mode_label'): - self.mode_label.show() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.show() - if hasattr(self, 'quality_label'): - self.quality_label.show() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.show() - else: - if hasattr(self, 'mode_label'): - self.mode_label.hide() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.hide() - if hasattr(self, 'quality_label'): - self.quality_label.hide() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.hide() - - def browse_output(self): - directory = QFileDialog.getExistingDirectory(self, "Select Output Directory") - if directory: - self.output_dir.setText(directory) - self.save_settings() - - def get_cache_file_path(self, username, media_type, is_batch=None): - timeline_type = self.timeline_type_combo.currentData() or 'media' - - filename = f"{username}_{timeline_type}_{media_type}.json" - return os.path.join(self.temp_dir, filename) - - def load_cached_data(self, username, media_type, is_batch=None): - cache_path = self.get_cache_file_path(username, media_type, is_batch) - if os.path.exists(cache_path): - try: - with open(cache_path, 'r', encoding='utf-8') as f: - return json.load(f) - except: - pass - return None - - def save_cached_data(self, username, media_type, data, is_batch=None): - cache_path = self.get_cache_file_path(username, media_type, is_batch) - try: - os.makedirs(os.path.dirname(cache_path), exist_ok=True) - with open(cache_path, 'w', encoding='utf-8') as f: - json.dump(data, f, ensure_ascii=False, indent=2) - except: - pass - - def load_all_cached_accounts(self): - try: - if not os.path.exists(self.temp_dir): - return - - cache_files = [f for f in os.listdir(self.temp_dir) if f.endswith('.json')] - - if not cache_files: - return - - loaded_count = 0 - - for cache_file in cache_files: - try: - filename_parts = cache_file.replace('.json', '').split('_') - - if len(filename_parts) >= 3: - media_type = filename_parts[-1] - - if len(filename_parts) == 4 and filename_parts[1] == 'batch': - username = filename_parts[0] - timeline_type = filename_parts[2] - is_batch_file = True - elif len(filename_parts) == 3: - username = filename_parts[0] - timeline_type = filename_parts[1] - is_batch_file = False - else: - username = '_'.join(filename_parts[:-2]) - timeline_type = filename_parts[-2] - is_batch_file = False - - already_exists = any( - acc.username == username and acc.media_type == media_type - for acc in self.accounts - ) - if not already_exists: - cached_data = self.load_cached_data(username, media_type, is_batch=is_batch_file) - if cached_data: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - loaded_count += 1 - except Exception as e: - continue - - if self.accounts: - self.update_account_list() - - except Exception as e: - pass - - def fetch_account(self): - url_input = self.twitter_url.text().strip() - - if not url_input: - self.log_output.append('Warning: Please enter a Twitter username/URL.') - return - - if not self.auth_token_input.text().strip(): - self.log_output.append('Warning: Please enter your auth token.') - return - - urls = [url.strip() for url in url_input.split(',') if url.strip()] - - if len(urls) > 1: - self.log_output.append(f'Processing {len(urls)} accounts: {", ".join(urls)}') - self.tab_widget.setCurrentWidget(self.process_tab) - - self.accounts_to_fetch = [] - media_type = self.media_type_combo.currentData() - - for url in urls: - username = self.normalize_url_to_username(url) - if username: - self.accounts_to_fetch.append((username, media_type)) - - if self.accounts_to_fetch: - self.current_fetch_index = 0 - self.is_multiple_user_mode = True - self.is_initial_fetch = True - - if self.batch_mode: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append('Multiple user batch mode: Auto-fetching all batches for all users...') - else: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append('Multiple user mode: Auto-fetching all users...') - - self.fetch_next_account_in_batch() - return - - url = urls[0] - username = self.normalize_url_to_username(url) - if not username: - self.log_output.append('Warning: Invalid username/URL format.') - return - - media_type = self.media_type_combo.currentData() - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - self.accounts.remove(existing_account) - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') - self.update_account_list() - - if not existing_account: - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data: - try: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - self.update_account_list() - self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - self.twitter_url.clear() - return - except: - pass - - try: - self.reset_ui() - - self.is_auto_fetching = False - self.is_multiple_user_mode = False - self.current_fetch_username = username - self.current_fetch_media_type = media_type - - self.disable_batch_buttons() - - self.log_output.append(f'Fetching metadata for {username}...') - self.tab_widget.setCurrentWidget(self.process_tab) - - self.cancel_fetch_btn.show() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start metadata fetch: {str(e)}') - self.update_account_list() - - def normalize_url_to_username(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 if username else None - - def fetch_next_account_in_batch(self): - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - self.twitter_url.clear() - self.tab_widget.setCurrentIndex(0) - return - - if not hasattr(self, 'accounts_to_fetch') or self.current_fetch_index >= len(self.accounts_to_fetch): - total_accounts = len(self.accounts_to_fetch) if hasattr(self, 'accounts_to_fetch') else 0 - self.log_output.append(f'Batch processing completed! Processed {total_accounts} accounts.') - self.twitter_url.clear() - - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - self.tab_widget.setCurrentIndex(0) - return - - username, media_type = self.accounts_to_fetch[self.current_fetch_index] - self.log_output.append(f'Processing account {self.current_fetch_index + 1}/{len(self.accounts_to_fetch)}: {username} ({media_type})') - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not getattr(self, 'is_initial_fetch', True): - self.log_output.append(f'Resuming from existing progress for {username} ({media_type}) - {len(existing_account.media_list):,} items already fetched') - - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data and cached_data.get('metadata', {}).get('has_more', False): - current_page = cached_data.get('metadata', {}).get('page', 0) - next_page = current_page + 1 - self.log_output.append(f'Continuing from batch {next_page + 1} for {username}...') - self.fetch_next_batch_for_current_user(username, media_type, next_page, cached_data.get('metadata', {})) - return - else: - self.log_output.append(f'Account {username} already completed. Moving to next account.') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - elif existing_account: - self.accounts.remove(existing_account) - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') - self.update_account_list() - - if not existing_account: - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data: - try: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - self.update_account_list() - self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - except: - pass - - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.batch_mode and not self.is_auto_fetching: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - else: - self.is_auto_fetching = False - - self.current_fetch_username = username - self.current_fetch_media_type = media_type - - self.disable_batch_buttons() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_batch_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start metadata fetch for {username}: {str(e)}') - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - return - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def on_batch_metadata_fetched(self, data, username, media_type): - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append(f'Ignoring result for {username} - auto-fetch was stopped.') - return - - if 'error' in data: - self.log_output.append(f'Error fetching {username}: {data["error"]}') - if self.is_auto_fetching: - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - account_info = data.get('account_info', {}) - timeline = data.get('timeline', []) - metadata = data.get('metadata', {}) - - if not account_info: - self.log_output.append(f'Error: Invalid account data received for {username}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - if not timeline: - media_type_display = media_type if media_type != 'all' else 'media' - self.log_output.append(f'Warning: No {media_type_display} found for account {username}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - existing_account.media_list.extend(timeline) - new_items = len(timeline) - total_items = len(existing_account.media_list) - self.log_output.append(f'Added {new_items:,} items to {username}. Total: {total_items:,} media items') - - updated_data = { - 'account_info': account_info, - 'timeline': existing_account.media_list, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': existing_account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - else: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if self.batch_mode else 'all', - fetch_timestamp=datetime.now().isoformat() - ) - - self.accounts.append(account) - existing_account = account - self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - - updated_data = { - 'account_info': account_info, - 'timeline': timeline, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - - self.update_account_list() - - if self.batch_mode and metadata.get('has_more', False): - current_page = metadata.get('page', 0) - next_page = current_page + 1 - - if self.is_auto_fetching: - self.log_output.append(f'Batch {current_page + 1} completed for {username}. Auto-fetching batch {next_page + 1}...') - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - self.log_output.append(f'Batch {current_page + 1} completed for {username}. Next batch ({next_page + 1}) available.') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if not self.is_auto_fetching: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append(f'Auto-fetching next batch for {username}...') - elif self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - if self.batch_mode: - total_items = len(existing_account.media_list) - self.log_output.append(f'All batches completed for {username}! Total: {total_items:,} media items') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - else: - self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') - return - else: - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - except Exception as e: - self.log_output.append(f'Error processing {username}: {str(e)}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def fetch_next_batch_for_current_user(self, username, media_type, page, metadata): - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Batch processing halted.') - return - - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=True, - batch_size=self.batch_size, - page=page - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_batch_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start next batch fetch for {username}: {str(e)}') - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - return - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def on_batch_metadata_error(self, error_message): - if hasattr(self, 'accounts_to_fetch') and hasattr(self, 'current_fetch_index'): - username, media_type = self.accounts_to_fetch[self.current_fetch_index] - self.log_output.append(f'Error fetching {username}: {error_message}') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Error handling halted.') - return - - if self.is_auto_fetching and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - else: - self.on_metadata_error(error_message) - - - def on_metadata_fetched(self, data, username, media_type): - try: - if 'error' in data: - self.log_output.append(f'Error: {data["error"]}') - self.update_account_list() - return - - account_info = data.get('account_info', {}) - timeline = data.get('timeline', []) - metadata = data.get('metadata', {}) - if not account_info: - self.log_output.append('Error: Invalid account data received') - self.update_account_list() - return - - if not timeline: - media_type_display = media_type if media_type != 'all' else 'media' - self.log_output.append(f'Error: No {media_type_display} found for account {username}') - self.update_account_list() - return - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - existing_account.media_list.extend(timeline) - new_items = len(timeline) - total_items = len(existing_account.media_list) - self.log_output.append(f'Added {new_items:,} items. Total: {total_items:,} media items for {username}') - else: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if self.batch_mode else 'all', - fetch_timestamp=datetime.now().isoformat() - ) - - self.accounts.append(account) - self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - existing_account = account - - updated_data = { - 'account_info': account_info, - 'timeline': existing_account.media_list, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': existing_account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - - self.update_account_list() - - self.current_fetch_username = username - self.current_fetch_media_type = media_type - self.current_fetch_metadata = metadata - - if self.batch_mode and metadata.get('has_more', False): - current_page = metadata.get('page', 0) - next_page = current_page + 1 - self.log_output.append(f'Batch {current_page + 1} completed. Next batch ({next_page + 1}) available.') - - if self.is_auto_fetching: - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_internal() - else: - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.show() - self.enable_batch_buttons() - self.log_output.append('Use "Next Batch" for manual fetch or "Auto Batch" for automatic fetching.') - else: - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - self.enable_batch_buttons() - - if self.batch_mode: - total_items = len(existing_account.media_list) - self.log_output.append(f'All batches complete! Total: {total_items:,} media items for {username}') - - self.twitter_url.clear() - - if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): - self.current_update_index += 1 - self.update_next_account() - else: - self.tab_widget.setCurrentIndex(0) - except Exception as e: - self.log_output.append(f'Error: {str(e)}') - self.update_account_list() - - def on_metadata_error(self, error_message): - self.log_output.append(f'Error: {error_message}') - - if self.is_auto_fetching: - self.stop_auto_fetch() - - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - self.enable_batch_buttons() - - if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): - self.current_update_index += 1 - self.update_next_account() - - self.update_account_list() - - def update_account_list(self): - self.account_list.clear() - for i, account in enumerate(self.accounts, 1): - media_count = len(account.media_list) if account.media_list else 0 - media_type_display = "GIF" if account.media_type.lower() == "gif" else account.media_type.title() - - line1 = f"{i}. {account.username} ({account.nick})" - line2 = f"Followers: {account.followers:,} • Following: {account.following:,} • Posts: {account.posts:,} • {media_type_display}: {media_count:,}" - line3 = self.format_fetch_info(account.fetch_timestamp) - display_text = f"{line1}\n{line2}\n{line3}" - item = QListWidgetItem() - item.setText(display_text) - item.setSizeHint(QSize(0, 70)) - - if account.profile_image: - if account.profile_image in self.profile_image_cache: - item.setIcon(QIcon(self.profile_image_cache[account.profile_image])) - else: - self.download_profile_image(account.profile_image) - placeholder = self.create_placeholder_icon(48) - if placeholder: - item.setIcon(QIcon(placeholder)) - else: - placeholder = self.create_placeholder_icon(48) - if placeholder: - item.setIcon(QIcon(placeholder)) - - self.account_list.addItem(item) - - self.update_button_states() - - def create_colored_icon(self, svg_path, color): - with open(svg_path, 'r', encoding='utf-8') as f: - svg_content = f.read() - - svg_content = svg_content.replace('stroke="currentColor"', f'stroke="{color}"') - svg_content = svg_content.replace('fill="currentColor"', f'fill="{color}"') - - pixmap = QPixmap(24, 24) - pixmap.fill(Qt.GlobalColor.transparent) - - renderer = QSvgRenderer(svg_content.encode('utf-8')) - painter = QPainter(pixmap) - renderer.render(painter) - painter.end() - - return QIcon(pixmap) - - def create_placeholder_icon(self, size=48): - pixmap = QPixmap(size, size) - pixmap.fill(Qt.GlobalColor.transparent) - - painter = QPainter(pixmap) - painter.setRenderHint(QPainter.RenderHint.Antialiasing) - - painter.setBrush(Qt.GlobalColor.gray) - painter.setPen(Qt.PenStyle.NoPen) - painter.drawRoundedRect(0, 0, size, size, 8, 8) - - painter.setPen(Qt.GlobalColor.white) - font_size = int(size * 0.4) - painter.setFont(painter.font()) - painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "👤") - - painter.end() - return pixmap - - def create_square_pixmap(self, original_pixmap, size=48): - if original_pixmap.isNull(): - return self.create_placeholder_icon(size) - - square_pixmap = QPixmap(size, size) - square_pixmap.fill(Qt.GlobalColor.transparent) - - painter = QPainter(square_pixmap) - painter.setRenderHint(QPainter.RenderHint.Antialiasing) - - path = QPainterPath() - path.addRoundedRect(0, 0, size, size, 8, 8) - painter.setClipPath(path) - - scaled_pixmap = original_pixmap.scaled( - size, size, - Qt.AspectRatioMode.KeepAspectRatioByExpanding, - Qt.TransformationMode.SmoothTransformation - ) - - x = (size - scaled_pixmap.width()) // 2 - y = (size - scaled_pixmap.height()) // 2 - painter.drawPixmap(x, y, scaled_pixmap) - - painter.end() - return square_pixmap - - def download_profile_image(self, url): - if not url or url in self.profile_image_cache or url in self.pending_downloads: - return - - try: - request = QNetworkRequest(QUrl(url)) - request.setHeader(QNetworkRequest.KnownHeaders.UserAgentHeader, - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36") - - reply = self.network_manager.get(request) - self.pending_downloads[url] = reply - reply.finished.connect(lambda: self.on_profile_image_downloaded(reply, url)) - except Exception as e: - pass - - def on_profile_image_downloaded(self, reply, image_url): - try: - if reply.error() == reply.NetworkError.NoError: - data = reply.readAll() - pixmap = QPixmap() - if pixmap.loadFromData(data): - square_pixmap = self.create_square_pixmap(pixmap, 48) - self.profile_image_cache[image_url] = square_pixmap - - self.update_account_list() - - if image_url in self.pending_downloads: - del self.pending_downloads[image_url] - - except Exception as e: - pass - finally: - reply.deleteLater() - - def update_button_states(self): - has_accounts = len(self.accounts) > 0 - - has_selected = len(self.account_list.selectedItems()) > 0 - - self.import_btn.setEnabled(True) - self.export_btn.setEnabled(has_accounts) - self.download_selected_btn.setEnabled(has_accounts) - self.update_selected_btn.setEnabled(has_accounts) - self.delete_btn.setEnabled(has_accounts) - - self.import_btn.show() - self.export_btn.show() - self.download_selected_btn.show() - self.update_selected_btn.show() - self.delete_btn.show() - - def hide_account_buttons(self): - self.import_btn.setEnabled(True) - self.export_btn.setEnabled(False) - self.download_selected_btn.setEnabled(False) - self.update_selected_btn.setEnabled(False) - self.delete_btn.setEnabled(False) - - self.import_btn.show() - self.export_btn.show() - self.download_selected_btn.show() - self.update_selected_btn.show() - self.delete_btn.show() - - def download_selected(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Download All', - f'No accounts selected. Download all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.Yes: - self.download_accounts(list(range(len(self.accounts)))) - return - - self.download_accounts([self.account_list.row(item) for item in selected_items]) - - def download_all(self): - self.download_accounts(range(len(self.accounts))) - - def update_selected(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Update All', - f'No accounts selected. Update all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - selected_accounts = self.accounts.copy() - else: - selected_accounts = [] - for item in selected_items: - index = self.account_list.row(item) - if index < len(self.accounts): - selected_accounts.append(self.accounts[index]) - - if not selected_accounts: - return - - self.tab_widget.setCurrentIndex(1) - - self.log_output.clear() - self.log_output.append(f'Starting update for {len(selected_accounts)} account(s)...') - - self.accounts_to_update = selected_accounts.copy() - self.current_update_index = 0 - - self.update_next_account() - - def update_next_account(self): - if not hasattr(self, 'accounts_to_update') or self.current_update_index >= len(self.accounts_to_update): - self.log_output.append('All selected accounts have been updated!') - if hasattr(self, 'accounts_to_update'): - delattr(self, 'accounts_to_update') - if hasattr(self, 'current_update_index'): - delattr(self, 'current_update_index') - self.tab_widget.setCurrentIndex(0) - return - - account = self.accounts_to_update[self.current_update_index] - self.log_output.append(f'Updating account {self.current_update_index + 1}/{len(self.accounts_to_update)}: {account.username} ({account.media_type})') - self.update_account(account) - - def update_account(self, account): - original_batch_mode = self.batch_mode - original_batch_size = self.batch_size - original_media_type = self.media_type_combo.currentData() - - self.batch_mode = (account.fetch_mode == 'batch') - if self.batch_mode: - self.batch_size = self.settings.value('batch_size', 100, type=int) - - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == account.media_type: - self.media_type_combo.setCurrentIndex(i) - break - - self.twitter_url.setText(account.username) - - self.tab_widget.setCurrentIndex(1) - - self.log_output.append(f'Updating {account.username} ({account.media_type}) using {account.fetch_mode} mode...') - - username = account.username - media_type = account.media_type - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - for i, existing_account in enumerate(self.accounts): - if existing_account.username == account.username and existing_account.media_type == account.media_type: - self.accounts.pop(i) - break - - self.update_account_list() - - self.start_fetch_process(username, media_type) - - self.batch_mode = original_batch_mode - self.batch_size = original_batch_size - - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == original_media_type: - self.media_type_combo.setCurrentIndex(i) - break - - def start_fetch_process(self, username, media_type): - if not self.auth_token_input.text().strip(): - self.log_output.append("Error: Please enter your auth token") - return - - self.current_page = 0 - self.disable_batch_buttons() - - self.worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.worker.auth_token = self.auth_token_input.text().strip() - self.worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) - self.worker.error.connect(self.on_metadata_error) - self.worker.start() - - def download_accounts(self, indices): - self.log_output.clear() - outpath = self.output_dir.text() - if not os.path.exists(outpath): - self.log_output.append('Warning: Invalid output directory.') - return - - if not self.auth_token_input.text().strip(): - self.log_output.append("Error: Please enter your auth token") - return - - accounts_to_download = [self.accounts[i] for i in indices] - - try: - self.start_download_worker(accounts_to_download, outpath) - except Exception as e: - self.log_output.append(f"Error: An error occurred while starting the download: {str(e)}") - - def start_download_worker(self, accounts_to_download, outpath): - self.worker = DownloadWorker( - accounts_to_download, - outpath, - self.auth_token_input.text().strip(), - self.filename_format, - self.download_batch_size, - self.convert_gif, - self.gif_conversion_quality, - self.gif_conversion_mode - ) - self.worker.finished.connect(self.on_download_finished) - self.worker.progress.connect(self.update_progress) - self.worker.conversion_progress.connect(self.update_conversion_progress) - self.worker.download_progress.connect(self.update_download_progress) - self.worker.start() - self.start_timer() - self.update_ui_for_download_start(len(accounts_to_download)) - - def update_ui_for_download_start(self, account_count): - self.download_selected_btn.setEnabled(False) - self.update_selected_btn.setEnabled(False) - self.stop_btn.show() - self.pause_resume_btn.show() - - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - self.progress_bar.show() - self.progress_bar.setValue(0) - - self.tab_widget.setCurrentWidget(self.process_tab) - - def update_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - current_text = cursor.selectedText() - progress_indicators = [ - "Downloading", - "Account", - "files downloaded" - ] - - should_replace = False - for indicator in progress_indicators: - if indicator in current_text and indicator in message: - should_replace = True - break - if should_replace and current_text.strip(): - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def update_conversion_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - current_text = cursor.selectedText() - if "Converting GIF" in current_text: - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def update_download_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - - current_text = cursor.selectedText() - if "Downloading" in current_text and ":" in current_text and "/" in current_text: - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def stop_download(self): - if hasattr(self, 'worker'): - self.worker.stop() - self.stop_timer() - self.on_download_finished(True, "Download stopped by user.") - - def on_download_finished(self, success, message): - self.progress_bar.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.pause_resume_btn.setText('Pause') - self.stop_timer() - - self.download_selected_btn.setEnabled(True) - self.update_selected_btn.setEnabled(True) - - if (self.batch_mode and self.current_fetch_metadata and - self.current_fetch_metadata.get('has_more', False) and not self.is_auto_fetching): - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.enable_batch_buttons() - - if success: - self.log_output.append(f"\nStatus: {message}") - else: - self.log_output.append(f"Error: {message}") - - self.tab_widget.setCurrentWidget(self.process_tab) - - def toggle_pause_resume(self): - if hasattr(self, 'worker'): - if self.worker.is_paused: - self.worker.resume() - self.pause_resume_btn.setText('Pause') - self.timer.start(1000) - else: - self.worker.pause() - self.pause_resume_btn.setText('Resume') - - def delete_accounts(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Delete All', - f'No accounts selected. Delete all {len(self.accounts)} account(s) and their cache files?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - - for account in self.accounts: - username = account.username - if username: - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, account.media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed temp file: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove temp file {os.path.basename(cache_file)}: {str(e)}') - - self.reset_state() - self.reset_ui() - self.tab_widget.setCurrentIndex(0) - else: - reply = QMessageBox.question( - self, - 'Confirm Delete', - f'Delete {len(selected_items)} selected account(s) and their cache files?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - - selected_indices = sorted([self.account_list.row(item) for item in selected_items], reverse=True) - - for index in selected_indices: - account = self.accounts[index] - username = account.username - if username: - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, account.media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed temp file: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove temp file {os.path.basename(cache_file)}: {str(e)}') - - self.accounts.pop(index) - - self.update_account_list() - self.update_button_states() - - def export_accounts(self): - self.tab_widget.setCurrentIndex(1) - - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Export All', - f'No accounts selected. Export all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - self.log_output.append('Export cancelled') - return - selected_accounts = self.accounts.copy() - else: - selected_accounts = [] - for item in selected_items: - row = self.account_list.row(item) - if 0 <= row < len(self.accounts): - selected_accounts.append(self.accounts[row]) - - if not selected_accounts: - self.log_output.append('No valid accounts found for export') - return - - export_dir = QFileDialog.getExistingDirectory(self, "Select Export Directory", self.last_output_path) - if not export_dir: - self.log_output.append('Export cancelled') - return - - self.log_output.append(f'Starting export of {len(selected_accounts)} account(s)...') - exported_count = 0 - - for account in selected_accounts: - try: - now = datetime.now() - date_str = now.strftime("%Y%m%d_%H%M%S") - - if account.fetch_mode == 'batch': - filename = f"{account.username}_batch_{date_str}.json" - else: - filename = f"{account.username}_{date_str}.json" - - filepath = os.path.join(export_dir, filename) - - export_data = { - 'username': account.username, - 'nick': account.nick, - 'followers': account.followers, - 'following': account.following, - 'posts': account.posts, - 'media_type': account.media_type, - 'profile_image': account.profile_image, - 'media_list': account.media_list, - 'fetch_mode': account.fetch_mode, - 'fetch_timestamp': account.fetch_timestamp, - 'export_timestamp': now.isoformat() - } - - with open(filepath, 'w', encoding='utf-8') as f: - json.dump(export_data, f, indent=2, ensure_ascii=False) - - exported_count += 1 - self.log_output.append(f'Exported: {filename}') - - except Exception as e: - self.log_output.append(f'Error exporting {account.username}: {str(e)}') - - self.log_output.append(f'Export completed: {exported_count}/{len(selected_accounts)} account(s) exported') - self.log_output.append(f'Export location: {export_dir}') - - def import_accounts(self): - self.tab_widget.setCurrentIndex(1) - - file_paths, _ = QFileDialog.getOpenFileNames( - self, - "Select JSON Files to Import", - self.last_output_path, - "JSON Files (*.json)" - ) - - if not file_paths: - self.log_output.append('Import cancelled') - return - - self.log_output.append(f'Starting import of {len(file_paths)} file(s)...') - imported_count = 0 - skipped_count = 0 - error_count = 0 - - for filepath in file_paths: - try: - with open(filepath, 'r', encoding='utf-8') as f: - data = json.load(f) - - username = data.get('username') - if not username: - self.log_output.append(f'Skipped {os.path.basename(filepath)}: No username found') - skipped_count += 1 - continue - - existing_account = next((acc for acc in self.accounts if acc.username == username), None) - if existing_account: - self.log_output.append(f'Skipped {username}: Already exists in dashboard') - skipped_count += 1 - continue - - account = Account( - username=username, - nick=data.get('nick', ''), - followers=data.get('followers', 0), - following=data.get('following', 0), - posts=data.get('posts', 0), - media_type=data.get('media_type', 'all'), - profile_image=data.get('profile_image'), - media_list=data.get('media_list', []), - fetch_mode=data.get('fetch_mode', 'all'), - fetch_timestamp=data.get('fetch_timestamp') - ) - - self.accounts.append(account) - - account_info = { - 'followers_count': account.followers, - 'friends_count': account.following, - 'statuses_count': account.posts, - 'nick': account.nick, - 'name': account.nick, - 'profile_image': account.profile_image - } - - cache_data = { - 'account_info': account_info, - 'timeline': account.media_list, - 'metadata': {}, - 'is_batch': (account.fetch_mode == 'batch'), - 'fetch_timestamp': account.fetch_timestamp - } - - self.save_cached_data(username, account.media_type, cache_data, is_batch=(account.fetch_mode == 'batch')) - - imported_count += 1 - media_count = len(account.media_list) if account.media_list else 0 - self.log_output.append(f'Imported: {username} ({media_count:,} media)') - - except Exception as e: - self.log_output.append(f'Error importing {os.path.basename(filepath)}: {str(e)}') - error_count += 1 - - self.update_account_list() - self.update_button_states() - self.log_output.append(f'Import completed: {imported_count} imported, {skipped_count} skipped, {error_count} errors') - self.tab_widget.setCurrentIndex(0) - - - - def update_timer(self): - self.elapsed_time = self.elapsed_time.addSecs(1) - self.time_label.setText(self.elapsed_time.toString("hh:mm:ss")) - - def start_timer(self): - self.elapsed_time = QTime(0, 0, 0) - self.time_label.setText("00:00:00") - self.time_label.show() - self.timer.start(1000) - - def stop_timer(self): - self.timer.stop() - self.time_label.hide() - - def fetch_next_batch(self): - if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): - self.log_output.append('No more batches available.') - return - - self.disable_batch_buttons() - self.fetch_next_batch_internal() - - def start_auto_batch(self): - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.is_auto_fetching = True - self.is_initial_fetch = False - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.log_output.append('Resuming multiple user batch processing...') - self.fetch_next_account_in_batch() - return - - if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): - self.log_output.append('No more batches available.') - return - - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.log_output.append('Auto batch mode enabled.') - self.fetch_next_batch_internal() - - def stop_auto_fetch(self): - self.is_auto_fetching = False - self.stop_fetch_btn.hide() - - if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): - self.metadata_worker.terminate() - self.metadata_worker.wait() - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.auto_batch_btn.show() - self.auto_batch_btn.setEnabled(True) - self.next_batch_btn.hide() - self.cancel_fetch_btn.show() - self.log_output.append('Multiple user batch processing stopped.') - elif self.current_fetch_metadata and self.current_fetch_metadata.get('has_more', False): - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.cancel_fetch_btn.show() - self.enable_batch_buttons() - self.log_output.append('Auto batch mode stopped.') - else: - self.cancel_fetch_btn.show() - self.log_output.append('Auto batch mode stopped.') - - def cancel_multiple_user_fetch(self): - if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): - self.metadata_worker.terminate() - self.metadata_worker.wait() - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.log_output.append('Multiple user batch processing cancelled.') - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - else: - self.log_output.append('Single user batch processing cancelled.') - - self.current_fetch_username = None - self.current_fetch_media_type = None - self.current_fetch_metadata = None - - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - - self.twitter_url.clear() - self.tab_widget.setCurrentIndex(0) - - def disable_batch_buttons(self): - self.next_batch_btn.setEnabled(False) - self.auto_batch_btn.setEnabled(False) - - def enable_batch_buttons(self): - self.next_batch_btn.setEnabled(True) - self.auto_batch_btn.setEnabled(True) - - def fetch_next_batch_internal(self): - if not self.current_fetch_username or not self.current_fetch_media_type or not self.current_fetch_metadata: - return - - self.disable_batch_buttons() - - current_page = self.current_fetch_metadata.get('page', 0) - next_page = current_page + 1 - - self.log_output.append(f'Fetching batch {next_page + 1} for {self.current_fetch_username}...') - - self.metadata_worker = MetadataFetchWorker( - self.current_fetch_username, - self.current_fetch_media_type, - batch_mode=True, - batch_size=self.batch_size, - page=next_page - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, self.current_fetch_username, self.current_fetch_media_type)) - self.metadata_worker.error.connect(self.on_metadata_error) - self.metadata_worker.start() - - def show_account_buttons(self): - self.download_selected_btn.show() - self.update_selected_btn.show() - self.remove_btn.show() - self.clear_btn.show() +from PyQt6.QtWidgets import QApplication +from ui.mainWindow_Unix import TwitterXMediaBatchDownloaderGUI_Unix def main(): if getattr(sys, 'frozen', False): os.chdir(os.path.dirname(sys.executable)) app = QApplication(sys.argv) - app.setStyle('fusion') - window = TwitterMediaDownloaderGUI() + app.setStyle('Fusion') + + window = TwitterXMediaBatchDownloaderGUI_Unix() window.show() sys.exit(app.exec()) diff --git a/TwitterXMediaBatchDownloader_Windows.py b/TwitterXMediaBatchDownloader_Windows.py index 17c1f5b..0555f7e 100644 --- a/TwitterXMediaBatchDownloader_Windows.py +++ b/TwitterXMediaBatchDownloader_Windows.py @@ -1,2782 +1,9 @@ import sys import os -import asyncio -import aiohttp -import requests -import subprocess -import imageio_ffmpeg -import json -import tempfile -from datetime import datetime -from pathlib import Path -from packaging import version -from dataclasses import dataclass import qdarktheme -from PyQt6.QtWidgets import ( - QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, - QLabel, QFileDialog, QListWidget, QTextEdit, QTabWidget, QAbstractItemView, QProgressBar, QCheckBox, QDialog, - QDialogButtonBox, QComboBox, QListWidgetItem, QMessageBox -) -from PyQt6.QtCore import Qt, QThread, pyqtSignal, QUrl, QTimer, QTime, QSettings, QSize -from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest -from PyQt6.QtGui import QIcon, QTextCursor, QDesktopServices, QPixmap, QPainter, QPainterPath -from PyQt6.QtSvg import QSvgRenderer -from getMetadata import get_metadata - -@dataclass -class Account: - username: str - nick: str - followers: int - following: int - posts: int - media_type: str - profile_image: str = None - media_list: list = None - fetch_mode: str = 'all' - fetch_timestamp: str = None - -class MetadataFetchWorker(QThread): - finished = pyqtSignal(dict) - error = pyqtSignal(str) - - def __init__(self, username, media_type='all', batch_mode=False, batch_size=0, page=0): - super().__init__() - self.username = username - self.media_type = media_type - self.batch_mode = batch_mode - self.batch_size = batch_size - self.page = page - self.auth_token = None - - 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 - - def run(self): - try: - normalized = self.normalize_url(self.username) - data = get_metadata( - username=normalized, - auth_token=self.auth_token, - timeline_type='media', - batch_size=self.batch_size if self.batch_mode else 0, - page=self.page, - media_type=self.media_type - ) - self.finished.emit(data) - except Exception as e: - self.error.emit(str(e)) - -class DownloadWorker(QThread): - finished = pyqtSignal(bool, str) - progress = pyqtSignal(str, int) - conversion_progress = pyqtSignal(str, int) - download_progress = pyqtSignal(str, int) - - def __init__(self, accounts, outpath, auth_token, filename_format='username_date', - download_batch_size=25, convert_gif=False, gif_resolution='original', gif_conversion_mode='better'): - super().__init__() - self.accounts = accounts - self.outpath = outpath - self.auth_token = auth_token - self.filename_format = filename_format - self.download_batch_size = download_batch_size - self.convert_gif = convert_gif - self.gif_resolution = gif_resolution - self.gif_conversion_mode = gif_conversion_mode - self.is_paused = False - self.is_stopped = False - self.filepath_map = [] - - async def download_file(self, session, url, filepath): - try: - if os.path.exists(filepath): - return True, True - 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()) - return True, False - return False, False - except Exception as e: - return False, False - - async def download_account_media(self, account): - if not account.media_list: - return 0, 0, 0 - - account_output_dir = os.path.join(self.outpath, account.username) - os.makedirs(account_output_dir, exist_ok=True) - timeout = aiohttp.ClientTimeout(total=30) - connector = aiohttp.TCPConnector(limit=self.download_batch_size) - - async with aiohttp.ClientSession(timeout=timeout, connector=connector) as session: - total = len(account.media_list) - completed = 0 - skipped = 0 - failed = 0 - - used_filenames = set() - - for i in range(0, total, self.download_batch_size): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - return completed, skipped, failed - await asyncio.sleep(0.1) - - batch = account.media_list[i:i + self.download_batch_size] - tasks = [] - - for item in batch: - url = item['url'] - 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', '')) - - item_type = item.get('type', '') - if item_type == 'animated_gif': - media_type_folder = 'gif' - extension = 'mp4' - elif item_type == 'video' or 'video.twimg.com' in url: - media_type_folder = 'video' - extension = 'mp4' - else: - media_type_folder = 'image' - extension = 'jpg' - media_output_dir = os.path.join(account_output_dir, media_type_folder) - - if self.filename_format == "username_date": - base_filename = f"{account.username}_{formatted_date}_{tweet_id}" - else: - base_filename = f"{formatted_date}_{account.username}_{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(media_output_dir, filename) - self.filepath_map.append((item, filepath)) - 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_percent = int((completed + failed) / total * 100) - media_type_display = account.media_type if account.media_type != 'all' else 'media' - self.download_progress.emit(f"Downloading {account.username}'s {media_type_display}: {completed + failed:,}/{total:,}", progress_percent) - - await asyncio.sleep(0.1) - - return completed, skipped, failed - - def run(self): - try: - total_accounts = len(self.accounts) - overall_completed = 0 - overall_skipped = 0 - overall_failed = 0 - - for i, account in enumerate(self.accounts): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - return - self.msleep(100) - media_type_display = account.media_type if account.media_type != 'all' else 'media' - self.progress.emit(f"Downloading from account: {account.username} ({media_type_display}) - ({i+1}/{total_accounts})", - int((i) / total_accounts * 100)) - - completed, skipped, failed = asyncio.run(self.download_account_media(account)) - overall_completed += completed - overall_skipped += skipped - overall_failed += failed - self.progress.emit(f"Account {account.username} ({media_type_display}): {completed:,} downloaded, {skipped:,} skipped, {failed:,} failed", - int((i + 1) / total_accounts * 100)) - - if not self.is_stopped: - if self.convert_gif: - try: - ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe() - gif_items = [(item, fp) for item, fp in self.filepath_map if item.get('type') == 'animated_gif'] - total_gifs = len(gif_items) - if total_gifs > 0: - creationflags = subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0 - converted_count = 0 - skipped_count = 0 - self.progress.emit("Starting GIF conversion...", 0) - - for idx, (item, fp) in enumerate(gif_items, start=1): - if self.is_stopped: - break - - while self.is_paused: - if self.is_stopped: - break - self.msleep(100) - - if self.is_stopped: - break - - gif_fp = fp.rsplit('.', 1)[0] + '.gif' - - if os.path.exists(gif_fp): - try: - os.remove(fp) - skipped_count += 1 - conv_progress = int((idx / total_gifs) * 100) - quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" - self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display} - Skipped (exists)", conv_progress) - continue - except Exception: - pass - - - if self.gif_conversion_mode == 'fast': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-y', gif_fp] - else: - if self.gif_resolution == 'high': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=800:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=800:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '15', '-y', gif_fp] - elif self.gif_resolution == 'medium': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=600:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=600:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '10', '-y', gif_fp] - elif self.gif_resolution == 'low': - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=400:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=400:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '8', '-y', gif_fp] - else: - ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'palettegen=stats_mode=full[palette];[0:v][palette]paletteuse=dither=sierra2_4a', '-y', gif_fp] - - result = subprocess.run(ffmpeg_args, capture_output=True, creationflags=creationflags) - if result.returncode == 0 and os.path.exists(gif_fp): - try: - os.remove(fp) - converted_count += 1 - except Exception: - pass - - conv_progress = int((idx / total_gifs) * 100) - quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" - self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display}", conv_progress) - if converted_count > 0 or skipped_count > 0: - completion_msg = f"GIF conversion completed: {converted_count:,} converted" - if skipped_count > 0: - completion_msg += f", {skipped_count:,} skipped (already exists)" - self.progress.emit(completion_msg, 100) - else: - self.progress.emit("GIF conversion completed", 100) - except Exception as conv_e: - self.progress.emit(f"GIF conversion error: {conv_e}", 0) - - success_message = f"Download completed! {overall_completed:,} files downloaded" - if overall_skipped > 0: - success_message += f", {overall_skipped:,} skipped" - if overall_failed > 0: - success_message += f", {overall_failed:,} failed" - self.finished.emit(True, success_message) - - except Exception as e: - self.finished.emit(False, str(e)) - - def pause(self): - self.is_paused = True - - def resume(self): - self.is_paused = False - - def stop(self): - self.is_stopped = True - self.is_paused = False - -class UpdateDialog(QDialog): - def __init__(self, current_version, new_version, parent=None): - super().__init__(parent) - self.setWindowTitle("Update Now") - self.setFixedWidth(400) - self.setModal(True) - - layout = QVBoxLayout() - - message = QLabel(f"Twitter/X Media Batch Downloader v{new_version} Available!") - message.setWordWrap(True) - layout.addWidget(message) - - button_box = QDialogButtonBox() - self.update_button = QPushButton("Check") - self.update_button.setCursor(Qt.CursorShape.PointingHandCursor) - self.cancel_button = QPushButton("Later") - self.cancel_button.setCursor(Qt.CursorShape.PointingHandCursor) - - button_box.addButton(self.update_button, QDialogButtonBox.ButtonRole.AcceptRole) - button_box.addButton(self.cancel_button, QDialogButtonBox.ButtonRole.RejectRole) - - layout.addWidget(button_box) - - self.setLayout(layout) - - self.update_button.clicked.connect(self.accept) - self.cancel_button.clicked.connect(self.reject) - -class TwitterMediaDownloaderGUI(QWidget): - def __init__(self): - super().__init__() - self.current_version = "3.7" - self.accounts = [] - self.temp_dir = os.path.join(Path.home(), ".twitterxmediabatchdownloader") - os.makedirs(self.temp_dir, exist_ok=True) - self.reset_state() - - self.settings = QSettings('TwitterMediaDownloader', 'Settings') - self.last_output_path = self.settings.value('output_path', str(Path.home() / "Pictures")) - self.last_url = self.settings.value('twitter_url', '') - self.last_auth_token = self.settings.value('auth_token', '') - self.filename_format = self.settings.value('filename_format', 'username_date') - self.download_batch_size = self.settings.value('download_batch_size', 25, type=int) - self.batch_mode = self.settings.value('batch_mode', False, type=bool) - self.batch_size = self.settings.value('batch_size', 100, type=int) - self.timeline_type = self.settings.value('timeline_type', 'media') - self.media_type = self.settings.value('media_type', 'all') - self.convert_gif = self.settings.value('convert_gif', False, type=bool) - self.gif_resolution = self.settings.value('gif_resolution', 'original') - self.gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') - self.gif_conversion_quality = self.settings.value('gif_conversion_quality', 'original') - self.check_for_updates = self.settings.value('check_for_updates', True, type=bool) - self.current_theme_color = self.settings.value('theme_color', '#2196F3') - - self.is_auto_fetching = False - self.current_fetch_username = None - self.current_fetch_media_type = None - self.current_fetch_metadata = None - self.is_multiple_user_mode = False - - self.profile_image_cache = {} - self.pending_downloads = {} - self.network_manager = QNetworkAccessManager() - - self.elapsed_time = QTime(0, 0, 0) - self.timer = QTimer(self) - self.timer.timeout.connect(self.update_timer) - self.initUI() - self.load_settings() - self.load_all_cached_accounts() - - if self.check_for_updates: - QTimer.singleShot(0, self.check_updates) - - def get_time_ago(self, timestamp_str): - if not timestamp_str: - return "Unknown" - - try: - fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) - now = datetime.now(fetch_time.tzinfo) if fetch_time.tzinfo else datetime.now() - - diff = now - fetch_time - days = diff.days - hours = diff.seconds // 3600 - minutes = (diff.seconds % 3600) // 60 - - if days > 0: - return f"{days} day{'s' if days != 1 else ''} ago" - elif hours > 0: - return f"{hours} hour{'s' if hours != 1 else ''} ago" - elif minutes > 0: - return f"{minutes} minute{'s' if minutes != 1 else ''} ago" - else: - return "Just now" - except: - return "Unknown" - - def format_fetch_info(self, timestamp_str): - if not timestamp_str: - return "Fetched: Unknown" - - try: - fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) - formatted_datetime = fetch_time.strftime("%Y/%m/%d • %H:%M") - age = self.get_time_ago(timestamp_str) - return f"Fetched: {formatted_datetime} ({age})" - except: - return "Fetched: Unknown" - - def check_updates(self): - try: - response = requests.get("https://raw.githubusercontent.com/afkarxyz/Twitter-X-Media-Batch-Downloader/refs/heads/main/version.json") - if response.status_code == 200: - data = response.json() - new_version = data.get("version") - - if new_version and version.parse(new_version) > version.parse(self.current_version): - dialog = UpdateDialog(self.current_version, new_version, self) - result = dialog.exec() - - if result == QDialog.DialogCode.Accepted: - QDesktopServices.openUrl(QUrl("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases")) - - except Exception as e: - pass - - def reset_state(self): - self.accounts.clear() - - def reset_ui(self): - self.account_list.clear() - self.log_output.clear() - self.progress_bar.setValue(0) - self.progress_bar.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.pause_resume_btn.setText('Pause') - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - self.is_multiple_user_mode = False - self.enable_batch_buttons() - self.hide_account_buttons() - - def initUI(self): - self.setWindowTitle('Twitter/X Media Batch Downloader') - self.setFixedWidth(650) - self.setMinimumHeight(350) - - icon_path = os.path.join(os.path.dirname(__file__), "icons", "icon.svg") - if os.path.exists(icon_path): - self.setWindowIcon(QIcon(icon_path)) - - self.main_layout = QVBoxLayout() - - self.setup_twitter_section() - self.setup_tabs() - - self.setLayout(self.main_layout) - - def setup_twitter_section(self): - twitter_layout = QHBoxLayout() - twitter_label = QLabel('Username/URL:') - twitter_label.setFixedWidth(100) - - self.twitter_url = QLineEdit() - self.twitter_url.setPlaceholderText("e.g. Takomayuyi or https://x.com/Takomayuyi or user1,user2,user3") - self.twitter_url.setClearButtonEnabled(True) - self.twitter_url.setText(self.last_url) - self.twitter_url.textChanged.connect(self.save_url) - self.fetch_btn = QPushButton('Fetch') - self.fetch_btn.setFixedWidth(80) - self.fetch_btn.setCursor(Qt.CursorShape.PointingHandCursor) - self.fetch_btn.clicked.connect(self.fetch_account) - - twitter_layout.addWidget(twitter_label) - twitter_layout.addWidget(self.twitter_url) - twitter_layout.addWidget(self.fetch_btn) - self.main_layout.addLayout(twitter_layout) - - def setup_tabs(self): - self.tab_widget = QTabWidget() - self.tab_widget.currentChanged.connect(self.on_tab_changed) - self.main_layout.addWidget(self.tab_widget) - - self.setup_dashboard_tab() - self.setup_process_tab() - self.setup_settings_tab() - self.setup_theme_tab() - self.setup_about_tab() - - def setup_dashboard_tab(self): - dashboard_tab = QWidget() - dashboard_layout = QVBoxLayout() - - self.account_list = QListWidget() - self.account_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) - self.account_list.itemSelectionChanged.connect(self.update_button_states) - self.account_list.setIconSize(QSize(48, 48)) - self.account_list.setStyleSheet(""" - QListWidget { - padding: 4px; - outline: none; - } - QListWidget::item { - padding: 3px 6px; - margin: 0px 0px; - border: none; - outline: none; - } - QListWidget::item:selected { - border: none; - outline: none; - } - QListWidget::item:focus { - border: none; - outline: none; - } - """) - - dashboard_layout.addWidget(self.account_list) - - self.setup_account_buttons() - dashboard_layout.addLayout(self.btn_layout) - dashboard_tab.setLayout(dashboard_layout) - self.tab_widget.addTab(dashboard_tab, "Dashboard") - - self.hide_account_buttons() - - def setup_account_buttons(self): - self.btn_layout = QHBoxLayout() - self.import_btn = QPushButton(' Import') - self.export_btn = QPushButton(' Export') - self.download_selected_btn = QPushButton(' Download') - self.update_selected_btn = QPushButton(' Update') - self.delete_btn = QPushButton(' Delete') - - icon_dir = os.path.join(os.path.dirname(__file__), 'icons') - accent_color = self.current_theme_color - - self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), accent_color)) - self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), accent_color)) - self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), accent_color)) - self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), accent_color)) - self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), accent_color)) - - for btn in [self.import_btn, self.export_btn, self.download_selected_btn, - self.update_selected_btn, self.delete_btn]: - btn.setMinimumWidth(100) - btn.setCursor(Qt.CursorShape.PointingHandCursor) - btn.setIconSize(QSize(18, 18)) - - self.import_btn.clicked.connect(self.import_accounts) - self.export_btn.clicked.connect(self.export_accounts) - self.download_selected_btn.clicked.connect(self.download_selected) - self.update_selected_btn.clicked.connect(self.update_selected) - self.delete_btn.clicked.connect(self.delete_accounts) - - self.btn_layout.addStretch() - for btn in [self.import_btn, self.export_btn, self.download_selected_btn, - self.update_selected_btn, self.delete_btn]: - self.btn_layout.addWidget(btn, 1) - self.btn_layout.addStretch() - - def update_button_icons(self): - if not hasattr(self, 'import_btn'): - return - - icon_dir = os.path.join(os.path.dirname(__file__), 'icons') - accent_color = self.current_theme_color - - self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), accent_color)) - self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), accent_color)) - self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), accent_color)) - self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), accent_color)) - self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), accent_color)) - - def setup_process_tab(self): - self.process_tab = QWidget() - process_layout = QVBoxLayout() - process_layout.setSpacing(5) - - self.log_output = QTextEdit() - self.log_output.setReadOnly(True) - process_layout.addWidget(self.log_output) - - progress_time_layout = QVBoxLayout() - progress_time_layout.setSpacing(2) - - self.progress_bar = QProgressBar() - progress_time_layout.addWidget(self.progress_bar) - - self.time_label = QLabel("00:00:00") - self.time_label.setAlignment(Qt.AlignmentFlag.AlignCenter) - progress_time_layout.addWidget(self.time_label) - - process_layout.addLayout(progress_time_layout) - - download_control_layout = QHBoxLayout() - self.stop_btn = QPushButton('Stop') - self.pause_resume_btn = QPushButton('Pause') - - self.stop_btn.setFixedWidth(120) - self.pause_resume_btn.setFixedWidth(120) - self.stop_btn.setCursor(Qt.CursorShape.PointingHandCursor) - self.pause_resume_btn.setCursor(Qt.CursorShape.PointingHandCursor) - - self.stop_btn.clicked.connect(self.stop_download) - self.pause_resume_btn.clicked.connect(self.toggle_pause_resume) - - download_control_layout.addStretch() - download_control_layout.addWidget(self.stop_btn) - download_control_layout.addWidget(self.pause_resume_btn) - download_control_layout.addStretch() - process_layout.addLayout(download_control_layout) - - batch_control_layout = QHBoxLayout() - self.next_batch_btn = QPushButton('Next Batch') - self.auto_batch_btn = QPushButton('Auto Batch') - self.stop_fetch_btn = QPushButton('Stop Fetch') - self.cancel_fetch_btn = QPushButton('Cancel Fetch') - - for btn in [self.next_batch_btn, self.auto_batch_btn, self.stop_fetch_btn, self.cancel_fetch_btn]: - btn.setFixedWidth(120) - btn.setCursor(Qt.CursorShape.PointingHandCursor) - - self.next_batch_btn.setToolTip("Fetch next batch manually") - self.auto_batch_btn.setToolTip("Start automatic batch fetching") - self.stop_fetch_btn.setToolTip("Stop automatic batch fetching") - self.cancel_fetch_btn.setToolTip("Cancel batch processing and return to dashboard") - - self.next_batch_btn.clicked.connect(self.fetch_next_batch) - self.auto_batch_btn.clicked.connect(self.start_auto_batch) - self.stop_fetch_btn.clicked.connect(self.stop_auto_fetch) - self.cancel_fetch_btn.clicked.connect(self.cancel_multiple_user_fetch) - - batch_control_layout.addStretch() - batch_control_layout.addWidget(self.next_batch_btn) - batch_control_layout.addWidget(self.auto_batch_btn) - batch_control_layout.addWidget(self.stop_fetch_btn) - batch_control_layout.addWidget(self.cancel_fetch_btn) - batch_control_layout.addStretch() - process_layout.addLayout(batch_control_layout) - - self.process_tab.setLayout(process_layout) - - self.tab_widget.addTab(self.process_tab, "Process") - - self.progress_bar.hide() - self.time_label.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - - def setup_settings_tab(self): - settings_tab = QWidget() - settings_layout = QVBoxLayout() - settings_layout.setSpacing(0) - settings_layout.setContentsMargins(9, 9, 9, 9) - - output_group = QWidget() - output_layout = QVBoxLayout(output_group) - output_layout.setSpacing(5) - - output_label = QLabel('Output Directory') - output_label.setStyleSheet("font-weight: bold;") - output_layout.addWidget(output_label) - - output_dir_layout = QHBoxLayout() - self.output_dir = QLineEdit() - self.output_dir.setText(self.last_output_path) - self.output_dir.textChanged.connect(self.save_settings) - self.output_browse = QPushButton('Browse') - self.output_browse.setFixedWidth(80) - self.output_browse.setCursor(Qt.CursorShape.PointingHandCursor) - self.output_browse.clicked.connect(self.browse_output) - - output_dir_layout.addWidget(self.output_dir) - output_dir_layout.addWidget(self.output_browse) - output_layout.addLayout(output_dir_layout) - - settings_layout.addWidget(output_group) - - auth_group = QWidget() - auth_layout = QVBoxLayout(auth_group) - auth_layout.setSpacing(5) - - auth_label = QLabel('Authentication') - auth_label.setStyleSheet("font-weight: bold;") - auth_layout.addWidget(auth_label) - - auth_token_layout = QHBoxLayout() - auth_token_label = QLabel('Auth Token:') - - self.auth_token_input = QLineEdit() - self.auth_token_input.setPlaceholderText("Enter Auth Token") - self.auth_token_input.setText(self.last_auth_token) - self.auth_token_input.textChanged.connect(self.save_settings) - self.auth_token_input.setClearButtonEnabled(True) - - auth_token_layout.addWidget(auth_token_label) - auth_token_layout.addWidget(self.auth_token_input) - auth_layout.addLayout(auth_token_layout) - - settings_layout.addWidget(auth_group) - - gallery_dl_group = QWidget() - gallery_dl_layout = QVBoxLayout(gallery_dl_group) - gallery_dl_layout.setSpacing(5) - - gallery_dl_label = QLabel('gallery-dl Settings') - gallery_dl_label.setStyleSheet("font-weight: bold;") - gallery_dl_layout.addWidget(gallery_dl_label) - - first_row_layout = QHBoxLayout() - first_row_layout.setSpacing(5) - - timeline_label = QLabel("Timeline:") - self.timeline_type_combo = QComboBox() - self.timeline_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.timeline_type_combo.setFixedWidth(75) - timeline_types = [ - ('media', 'Media'), - ('timeline', 'Post'), - ('tweets', 'Tweets'), - ('with_replies', 'Replies') - ] - for value, display in timeline_types: - self.timeline_type_combo.addItem(display, value) - self.timeline_type_combo.currentTextChanged.connect(self.save_settings) - first_row_layout.addWidget(timeline_label) - first_row_layout.addWidget(self.timeline_type_combo) - - first_row_layout.addSpacing(5) - - media_label = QLabel("Media:") - self.media_type_combo = QComboBox() - self.media_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.media_type_combo.setFixedWidth(85) - media_types = [('all', 'All'), ('image', 'Image'), ('video', 'Video'), ('gif', 'GIF')] - for value, display in media_types: - self.media_type_combo.addItem(display, value) - self.media_type_combo.currentTextChanged.connect(self.save_settings) - first_row_layout.addWidget(media_label) - first_row_layout.addWidget(self.media_type_combo) - - first_row_layout.addSpacing(5) - - self.batch_checkbox = QCheckBox("Fetch Batch") - self.batch_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) - self.batch_checkbox.setToolTip("Enable for accounts with thousands of media") - self.batch_checkbox.stateChanged.connect(self.handle_batch_checkbox) - self.batch_checkbox.setChecked(self.batch_mode) - first_row_layout.addWidget(self.batch_checkbox) - - self.size_label = QLabel("Size:") - first_row_layout.addWidget(self.size_label) - self.batch_size_combo = QComboBox() - self.batch_size_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.batch_size_combo.setFixedWidth(60) - for size in [50, 100, 150, 200]: - self.batch_size_combo.addItem(str(size)) - self.batch_size_combo.setCurrentIndex(1) - self.batch_size_combo.currentTextChanged.connect(self.save_batch_size) - first_row_layout.addWidget(self.batch_size_combo) - - self.size_label.hide() - self.batch_size_combo.hide() - - first_row_layout.addStretch() - gallery_dl_layout.addLayout(first_row_layout) - - settings_layout.addWidget(gallery_dl_group) - - download_conversion_group = QWidget() - download_conversion_layout = QVBoxLayout(download_conversion_group) - download_conversion_layout.setSpacing(5) - - labels_layout = QHBoxLayout() - - download_label_container = QWidget() - download_label_layout = QHBoxLayout(download_label_container) - download_label_layout.setContentsMargins(0, 0, 0, 0) - download_label = QLabel('Download Settings') - download_label.setStyleSheet("font-weight: bold;") - download_label_layout.addWidget(download_label) - download_label_layout.addStretch() - download_label_container.setFixedWidth(180) - labels_layout.addWidget(download_label_container) - - conversion_label = QLabel('Conversion Settings') - conversion_label.setStyleSheet("font-weight: bold;") - labels_layout.addWidget(conversion_label) - labels_layout.addStretch() - download_conversion_layout.addLayout(labels_layout) - - controls_layout = QHBoxLayout() - - download_controls_container = QWidget() - download_controls_layout = QHBoxLayout(download_controls_container) - download_controls_layout.setContentsMargins(0, 0, 0, 0) - - batch_label = QLabel('Concurrents:') - download_controls_layout.addWidget(batch_label) - - self.download_batch_combo = QComboBox() - self.download_batch_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.download_batch_combo.setFixedWidth(80) - for size in range(5, 101, 5): - self.download_batch_combo.addItem(str(size)) - self.download_batch_combo.setCurrentText(str(self.download_batch_size)) - self.download_batch_combo.currentTextChanged.connect(self.save_settings) - download_controls_layout.addWidget(self.download_batch_combo) - download_controls_layout.addStretch() - - download_controls_container.setFixedWidth(180) - controls_layout.addWidget(download_controls_container) - - self.convert_gif_checkbox = QCheckBox("Convert GIF") - self.convert_gif_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) - self.convert_gif_checkbox.setChecked(self.convert_gif) - self.convert_gif_checkbox.toggled.connect(self.handle_conversion_checkbox) - controls_layout.addWidget(self.convert_gif_checkbox) - - controls_layout.addSpacing(5) - - mode_label = QLabel("Mode:") - self.conversion_mode_combo = QComboBox() - self.conversion_mode_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.conversion_mode_combo.setFixedWidth(80) - conversion_modes = [('better', 'Better'), ('fast', 'Fast')] - for value, display in conversion_modes: - self.conversion_mode_combo.addItem(display, value) - self.conversion_mode_combo.currentTextChanged.connect(self.save_settings) - controls_layout.addWidget(mode_label) - controls_layout.addWidget(self.conversion_mode_combo) - - controls_layout.addSpacing(5) - - quality_label = QLabel("Quality:") - self.conversion_quality_combo = QComboBox() - self.conversion_quality_combo.setCursor(Qt.CursorShape.PointingHandCursor) - self.conversion_quality_combo.setFixedWidth(85) - gif_qualities = [ - ('original', 'Original'), - ('high', 'High'), - ('medium', 'Medium'), - ('low', 'Low') - ] - for value, display in gif_qualities: - self.conversion_quality_combo.addItem(display, value) - self.conversion_quality_combo.currentTextChanged.connect(self.save_settings) - controls_layout.addWidget(quality_label) - controls_layout.addWidget(self.conversion_quality_combo) - - self.mode_label = mode_label - self.quality_label = quality_label - - if not self.convert_gif: - mode_label.hide() - self.conversion_mode_combo.hide() - quality_label.hide() - self.conversion_quality_combo.hide() - - controls_layout.addStretch() - download_conversion_layout.addLayout(controls_layout) - - settings_layout.addWidget(download_conversion_group) - settings_layout.addStretch() - - settings_tab.setLayout(settings_layout) - self.tab_widget.addTab(settings_tab, "Settings") - - def setup_theme_tab(self): - theme_tab = QWidget() - theme_layout = QVBoxLayout() - theme_layout.setSpacing(8) - theme_layout.setContentsMargins(8, 15, 15, 15) - - grid_layout = QVBoxLayout() - - self.color_buttons = {} - - first_row_palettes = [ - ("Red", [ - ("#FFCDD2", "100"), ("#EF9A9A", "200"), ("#E57373", "300"), ("#EF5350", "400"), ("#F44336", "500"), ("#E53935", "600"), ("#D32F2F", "700"), ("#C62828", "800"), ("#B71C1C", "900"), ("#FF8A80", "A100"), ("#FF5252", "A200"), ("#FF1744", "A400"), ("#D50000", "A700") - ]), - ("Pink", [ - ("#F8BBD0", "100"), ("#F48FB1", "200"), ("#F06292", "300"), ("#EC407A", "400"), ("#E91E63", "500"), ("#D81B60", "600"), ("#C2185B", "700"), ("#AD1457", "800"), ("#880E4F", "900"), ("#FF80AB", "A100"), ("#FF4081", "A200"), ("#F50057", "A400"), ("#C51162", "A700") - ]), - ("Purple", [ - ("#E1BEE7", "100"), ("#CE93D8", "200"), ("#BA68C8", "300"), ("#AB47BC", "400"), ("#9C27B0", "500"), ("#8E24AA", "600"), ("#7B1FA2", "700"), ("#6A1B9A", "800"), ("#4A148C", "900"), ("#EA80FC", "A100"), ("#E040FB", "A200"), ("#D500F9", "A400"), ("#AA00FF", "A700") - ]) - ] - - second_row_palettes = [ - ("Deep Purple", [ - ("#D1C4E9", "100"), ("#B39DDB", "200"), ("#9575CD", "300"), ("#7E57C2", "400"), ("#673AB7", "500"), ("#5E35B1", "600"), ("#512DA8", "700"), ("#4527A0", "800"), ("#311B92", "900"), ("#B388FF", "A100"), ("#7C4DFF", "A200"), ("#651FFF", "A400"), ("#6200EA", "A700") - ]), - ("Indigo", [ - ("#C5CAE9", "100"), ("#9FA8DA", "200"), ("#7986CB", "300"), ("#5C6BC0", "400"), ("#3F51B5", "500"), ("#3949AB", "600"), ("#303F9F", "700"), ("#283593", "800"), ("#1A237E", "900"), ("#8C9EFF", "A100"), ("#536DFE", "A200"), ("#3D5AFE", "A400"), ("#304FFE", "A700") - ]), - ("Blue", [ - ("#BBDEFB", "100"), ("#90CAF9", "200"), ("#64B5F6", "300"), ("#42A5F5", "400"), ("#2196F3", "500"), ("#1E88E5", "600"), ("#1976D2", "700"), ("#1565C0", "800"), ("#0D47A1", "900"), ("#82B1FF", "A100"), ("#448AFF", "A200"), ("#2979FF", "A400"), ("#2962FF", "A700") - ]) - ] - - third_row_palettes = [ - ("Light Blue", [ - ("#B3E5FC", "100"), ("#81D4FA", "200"), ("#4FC3F7", "300"), ("#29B6F6", "400"), ("#03A9F4", "500"), ("#039BE5", "600"), ("#0288D1", "700"), ("#0277BD", "800"), ("#01579B", "900"), ("#80D8FF", "A100"), ("#40C4FF", "A200"), ("#00B0FF", "A400"), ("#0091EA", "A700") - ]), - ("Cyan", [ - ("#B2EBF2", "100"), ("#80DEEA", "200"), ("#4DD0E1", "300"), ("#26C6DA", "400"), ("#00BCD4", "500"), ("#00ACC1", "600"), ("#0097A7", "700"), ("#00838F", "800"), ("#006064", "900"), ("#84FFFF", "A100"), ("#18FFFF", "A200"), ("#00E5FF", "A400"), ("#00B8D4", "A700") - ]), - ("Teal", [ - ("#B2DFDB", "100"), ("#80CBC4", "200"), ("#4DB6AC", "300"), ("#26A69A", "400"), ("#009688", "500"), ("#00897B", "600"), ("#00796B", "700"), ("#00695C", "800"), ("#004D40", "900"), ("#A7FFEB", "A100"), ("#64FFDA", "A200"), ("#1DE9B6", "A400"), ("#00BFA5", "A700") - ]) - ] - - fourth_row_palettes = [ - ("Green", [ - ("#C8E6C9", "100"), ("#A5D6A7", "200"), ("#81C784", "300"), ("#66BB6A", "400"), ("#4CAF50", "500"), ("#43A047", "600"), ("#388E3C", "700"), ("#2E7D32", "800"), ("#1B5E20", "900"), ("#B9F6CA", "A100"), ("#69F0AE", "A200"), ("#00E676", "A400"), ("#00C853", "A700") - ]), - ("Light Green", [ - ("#DCEDC8", "100"), ("#C5E1A5", "200"), ("#AED581", "300"), ("#9CCC65", "400"), ("#8BC34A", "500"), ("#7CB342", "600"), ("#689F38", "700"), ("#558B2F", "800"), ("#33691E", "900"), ("#CCFF90", "A100"), ("#B2FF59", "A200"), ("#76FF03", "A400"), ("#64DD17", "A700") - ]), - ("Lime", [ - ("#F0F4C3", "100"), ("#E6EE9C", "200"), ("#DCE775", "300"), ("#D4E157", "400"), ("#CDDC39", "500"), ("#C0CA33", "600"), ("#AFB42B", "700"), ("#9E9D24", "800"), ("#827717", "900"), ("#F4FF81", "A100"), ("#EEFF41", "A200"), ("#C6FF00", "A400"), ("#AEEA00", "A700") - ]) - ] - - fifth_row_palettes = [ - ("Yellow", [ - ("#FFF9C4", "100"), ("#FFF59D", "200"), ("#FFF176", "300"), ("#FFEE58", "400"), ("#FFEB3B", "500"), ("#FDD835", "600"), ("#FBC02D", "700"), ("#F9A825", "800"), ("#F57F17", "900"), ("#FFFF8D", "A100"), ("#FFFF00", "A200"), ("#FFEA00", "A400"), ("#FFD600", "A700") - ]), - ("Amber", [ - ("#FFECB3", "100"), ("#FFE082", "200"), ("#FFD54F", "300"), ("#FFCA28", "400"), ("#FFC107", "500"), ("#FFB300", "600"), ("#FFA000", "700"), ("#FF8F00", "800"), ("#FF6F00", "900"), ("#FFE57F", "A100"), ("#FFD740", "A200"), ("#FFC400", "A400"), ("#FFAB00", "A700") - ]), - ("Orange", [ - ("#FFE0B2", "100"), ("#FFCC80", "200"), ("#FFB74D", "300"), ("#FFA726", "400"), ("#FF9800", "500"), ("#FB8C00", "600"), ("#F57C00", "700"), ("#EF6C00", "800"), ("#E65100", "900"), ("#FFD180", "A100"), ("#FFAB40", "A200"), ("#FF9100", "A400"), ("#FF6D00", "A700") - ]) - ] - - for row_palettes in [first_row_palettes, second_row_palettes, third_row_palettes, fourth_row_palettes, fifth_row_palettes]: - row_layout = QHBoxLayout() - row_layout.setSpacing(15) - - for palette_name, colors in row_palettes: - column_layout = QVBoxLayout() - column_layout.setSpacing(3) - - palette_label = QLabel(palette_name) - palette_label.setStyleSheet("margin-bottom: 2px;") - palette_label.setAlignment(Qt.AlignmentFlag.AlignCenter) - column_layout.addWidget(palette_label) - - color_buttons_layout = QHBoxLayout() - color_buttons_layout.setSpacing(3) - - for color_hex, color_name in colors: - color_btn = QPushButton() - color_btn.setFixedSize(18, 18) - - is_current = color_hex == self.current_theme_color - border_style = "2px solid #fff" if is_current else "none" - - color_btn.setStyleSheet(f""" - QPushButton {{ - background-color: {color_hex}; - border: {border_style}; - border-radius: 9px; - }} - QPushButton:hover {{ - border: 2px solid #fff; - }} - QPushButton:pressed {{ - border: 2px solid #fff; - }} - """) - color_btn.setCursor(Qt.CursorShape.PointingHandCursor) - color_btn.setToolTip(f"{palette_name} {color_name}\n{color_hex}") - color_btn.clicked.connect(lambda checked, color=color_hex, btn=color_btn: self.change_theme_color(color, btn)) - - self.color_buttons[color_hex] = color_btn - - color_buttons_layout.addWidget(color_btn) - - column_layout.addLayout(color_buttons_layout) - row_layout.addLayout(column_layout) - - grid_layout.addLayout(row_layout) - - theme_layout.addLayout(grid_layout) - theme_layout.addStretch() - - theme_tab.setLayout(theme_layout) - self.tab_widget.addTab(theme_tab, "Theme") - - def change_theme_color(self, color, clicked_btn=None): - if hasattr(self, 'color_buttons'): - for color_hex, btn in self.color_buttons.items(): - if color_hex == self.current_theme_color: - btn.setStyleSheet(f""" - QPushButton {{ - background-color: {color_hex}; - border: none; - border-radius: 9px; - }} - QPushButton:hover {{ - border: 2px solid #fff; - }} - QPushButton:pressed {{ - border: 2px solid #fff; - }} - """) - break - - self.current_theme_color = color - self.settings.setValue('theme_color', color) - self.settings.sync() - - if clicked_btn: - clicked_btn.setStyleSheet(f""" - QPushButton {{ - background-color: {color}; - border: 2px solid #fff; - border-radius: 9px; - }} - QPushButton:hover {{ - border: 2px solid #fff; - }} - QPushButton:pressed {{ - border: 2px solid #fff; - }} - """) - - qdarktheme.setup_theme( - custom_colors={ - "[dark]": { - "primary": color, - } - } - ) - - self.update_button_icons() - - def setup_about_tab(self): - about_tab = QWidget() - about_layout = QVBoxLayout() - about_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) - about_layout.setSpacing(15) - - sections = [ - ("Check for Updates", "Check", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases"), - ("Report an Issue", "Report", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/issues"), - ("gallery-dl Repository", "Visit", "https://github.com/mikf/gallery-dl") - ] - - for title, button_text, url in sections: - section_widget = QWidget() - section_layout = QVBoxLayout(section_widget) - section_layout.setSpacing(10) - section_layout.setContentsMargins(0, 0, 0, 0) - - label = QLabel(title) - label.setStyleSheet("font-weight: bold;") - label.setAlignment(Qt.AlignmentFlag.AlignCenter) - section_layout.addWidget(label) - - button = QPushButton(button_text) - button.setFixedSize(100, 25) - button.setCursor(Qt.CursorShape.PointingHandCursor) - button.clicked.connect(lambda _, url=url: QDesktopServices.openUrl(QUrl(url if url.startswith(('http://', 'https://')) else f'https://{url}'))) - section_layout.addWidget(button, alignment=Qt.AlignmentFlag.AlignCenter) - - about_layout.addWidget(section_widget) - - footer_label = QLabel(f"v{self.current_version} | gallery-dl v1.30.9 | October 2025") - about_layout.addWidget(footer_label, alignment=Qt.AlignmentFlag.AlignCenter) - - about_tab.setLayout(about_layout) - self.tab_widget.addTab(about_tab, "About") - - def save_url(self): - self.settings.setValue('twitter_url', self.twitter_url.text().strip()) - self.settings.sync() - - def save_batch_size(self): - self.batch_size = int(self.batch_size_combo.currentText()) - self.settings.setValue('batch_size', self.batch_size) - self.settings.sync() - - def save_settings(self): - self.settings.setValue('output_path', self.output_dir.text().strip()) - self.settings.setValue('auth_token', self.auth_token_input.text().strip()) - self.settings.setValue('media_type', self.media_type_combo.currentData()) - self.settings.setValue('download_batch_size', int(self.download_batch_combo.currentText())) - - if hasattr(self, 'convert_gif_checkbox'): - self.settings.setValue('convert_gif', self.convert_gif_checkbox.isChecked()) - self.convert_gif = self.convert_gif_checkbox.isChecked() - - if hasattr(self, 'conversion_mode_combo'): - self.settings.setValue('gif_conversion_mode', self.conversion_mode_combo.currentData()) - self.gif_conversion_mode = self.conversion_mode_combo.currentData() - - if hasattr(self, 'conversion_quality_combo'): - self.settings.setValue('gif_conversion_quality', self.conversion_quality_combo.currentData()) - self.gif_conversion_quality = self.conversion_quality_combo.currentData() - - self.settings.sync() - - def load_settings(self): - try: - if not hasattr(self, 'batch_checkbox') or not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): - return - - batch_mode = self.settings.value('batch_mode', False, type=bool) - - self.batch_checkbox.blockSignals(True) - self.batch_checkbox.setChecked(batch_mode) - self.batch_checkbox.blockSignals(False) - - if batch_mode: - self.size_label.show() - self.batch_size_combo.show() - self.fetch_btn.setText('Fetch Batch') - else: - self.size_label.hide() - self.batch_size_combo.hide() - self.fetch_btn.setText('Fetch') - - fetch_batch_size = str(self.settings.value('batch_size', 100)) - index = self.batch_size_combo.findText(fetch_batch_size) - if index >= 0: - self.batch_size_combo.setCurrentIndex(index) - else: - self.batch_size_combo.setCurrentIndex(1) - - download_batch_size = str(self.settings.value('download_batch_size', 25)) - index = self.download_batch_combo.findText(download_batch_size) - if index >= 0: - self.download_batch_combo.setCurrentIndex(index) - else: - self.download_batch_combo.setCurrentIndex(0) - media_type = self.settings.value('media_type', 'all') - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == media_type: - self.media_type_combo.setCurrentIndex(i) - break - - convert_gif = self.settings.value('convert_gif', False, type=bool) - if hasattr(self, 'convert_gif_checkbox'): - self.convert_gif_checkbox.blockSignals(True) - self.convert_gif_checkbox.setChecked(convert_gif) - self.convert_gif_checkbox.blockSignals(False) - - if convert_gif: - if hasattr(self, 'mode_label'): - self.mode_label.show() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.show() - if hasattr(self, 'quality_label'): - self.quality_label.show() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.show() - else: - if hasattr(self, 'mode_label'): - self.mode_label.hide() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.hide() - if hasattr(self, 'quality_label'): - self.quality_label.hide() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.hide() - - gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') - if hasattr(self, 'conversion_mode_combo'): - for i in range(self.conversion_mode_combo.count()): - if self.conversion_mode_combo.itemData(i) == gif_conversion_mode: - self.conversion_mode_combo.setCurrentIndex(i) - break - - gif_conversion_quality = self.settings.value('gif_conversion_quality', 'original') - if hasattr(self, 'conversion_quality_combo'): - for i in range(self.conversion_quality_combo.count()): - if self.conversion_quality_combo.itemData(i) == gif_conversion_quality: - self.conversion_quality_combo.setCurrentIndex(i) - break - - last_username_url = self.settings.value('twitter_url', '') - if last_username_url and hasattr(self, 'twitter_url'): - self.twitter_url.setText(last_username_url) - - except Exception as e: - pass - - def handle_batch_checkbox(self, state): - if not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): - return - - self.batch_mode = self.batch_checkbox.isChecked() - self.settings.setValue('batch_mode', self.batch_mode) - - if self.batch_checkbox.isChecked(): - self.size_label.show() - self.batch_size_combo.show() - self.fetch_btn.setText('Fetch Batch') - else: - self.size_label.hide() - self.batch_size_combo.hide() - self.fetch_btn.setText('Fetch') - self.update_button_states() - - def handle_conversion_checkbox(self, checked): - self.convert_gif = checked - self.save_settings() - - if checked: - if hasattr(self, 'mode_label'): - self.mode_label.show() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.show() - if hasattr(self, 'quality_label'): - self.quality_label.show() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.show() - else: - if hasattr(self, 'mode_label'): - self.mode_label.hide() - if hasattr(self, 'conversion_mode_combo'): - self.conversion_mode_combo.hide() - if hasattr(self, 'quality_label'): - self.quality_label.hide() - if hasattr(self, 'conversion_quality_combo'): - self.conversion_quality_combo.hide() - - def browse_output(self): - directory = QFileDialog.getExistingDirectory(self, "Select Output Directory") - if directory: - self.output_dir.setText(directory) - self.save_settings() - - def get_cache_file_path(self, username, media_type, is_batch=None): - timeline_type = self.timeline_type_combo.currentData() or 'media' - - filename = f"{username}_{timeline_type}_{media_type}.json" - return os.path.join(self.temp_dir, filename) - - def load_cached_data(self, username, media_type, is_batch=None): - cache_path = self.get_cache_file_path(username, media_type, is_batch) - if os.path.exists(cache_path): - try: - with open(cache_path, 'r', encoding='utf-8') as f: - return json.load(f) - except: - pass - return None - - def save_cached_data(self, username, media_type, data, is_batch=None): - cache_path = self.get_cache_file_path(username, media_type, is_batch) - try: - os.makedirs(os.path.dirname(cache_path), exist_ok=True) - with open(cache_path, 'w', encoding='utf-8') as f: - json.dump(data, f, ensure_ascii=False, indent=2) - except: - pass - - def load_all_cached_accounts(self): - try: - if not os.path.exists(self.temp_dir): - return - - cache_files = [f for f in os.listdir(self.temp_dir) if f.endswith('.json')] - - if not cache_files: - return - - loaded_count = 0 - - for cache_file in cache_files: - try: - filename_parts = cache_file.replace('.json', '').split('_') - - if len(filename_parts) >= 3: - media_type = filename_parts[-1] - - if len(filename_parts) == 4 and filename_parts[1] == 'batch': - username = filename_parts[0] - timeline_type = filename_parts[2] - is_batch_file = True - elif len(filename_parts) == 3: - username = filename_parts[0] - timeline_type = filename_parts[1] - is_batch_file = False - else: - username = '_'.join(filename_parts[:-2]) - timeline_type = filename_parts[-2] - is_batch_file = False - - already_exists = any( - acc.username == username and acc.media_type == media_type - for acc in self.accounts - ) - if not already_exists: - cached_data = self.load_cached_data(username, media_type, is_batch=is_batch_file) - if cached_data: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - loaded_count += 1 - except Exception as e: - continue - - if self.accounts: - self.update_account_list() - - except Exception as e: - pass - - def fetch_account(self): - url_input = self.twitter_url.text().strip() - - if not url_input: - self.log_output.append('Warning: Please enter a Twitter username/URL.') - return - - if not self.auth_token_input.text().strip(): - self.log_output.append('Warning: Please enter your auth token.') - return - - urls = [url.strip() for url in url_input.split(',') if url.strip()] - - if len(urls) > 1: - - self.log_output.append(f'Processing {len(urls)} accounts: {", ".join(urls)}') - self.tab_widget.setCurrentWidget(self.process_tab) - - self.accounts_to_fetch = [] - media_type = self.media_type_combo.currentData() - - for url in urls: - username = self.normalize_url_to_username(url) - if username: - self.accounts_to_fetch.append((username, media_type)) - - if self.accounts_to_fetch: - self.current_fetch_index = 0 - self.is_multiple_user_mode = True - self.is_initial_fetch = True - - if self.batch_mode: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append('Multiple user batch mode: Auto-fetching all batches for all users...') - else: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append('Multiple user mode: Auto-fetching all users...') - - self.fetch_next_account_in_batch() - return - - url = urls[0] - username = self.normalize_url_to_username(url) - if not username: - self.log_output.append('Warning: Invalid username/URL format.') - return - - media_type = self.media_type_combo.currentData() - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - self.accounts.remove(existing_account) - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') - self.update_account_list() - - if not existing_account: - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data: - try: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - self.update_account_list() - self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - self.twitter_url.clear() - return - except: - pass - - try: - self.reset_ui() - - self.is_auto_fetching = False - self.is_multiple_user_mode = False - self.current_fetch_username = username - self.current_fetch_media_type = media_type - - self.disable_batch_buttons() - - self.log_output.append(f'Fetching metadata for {username}...') - self.tab_widget.setCurrentWidget(self.process_tab) - - self.cancel_fetch_btn.show() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start metadata fetch: {str(e)}') - self.update_account_list() - - def normalize_url_to_username(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 if username else None - - def fetch_next_account_in_batch(self): - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - self.twitter_url.clear() - self.tab_widget.setCurrentIndex(0) - return - - if not hasattr(self, 'accounts_to_fetch') or self.current_fetch_index >= len(self.accounts_to_fetch): - total_accounts = len(self.accounts_to_fetch) if hasattr(self, 'accounts_to_fetch') else 0 - self.log_output.append(f'Batch processing completed! Processed {total_accounts} accounts.') - self.twitter_url.clear() - - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.hide() - self.is_auto_fetching = False - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - self.tab_widget.setCurrentIndex(0) - return - - username, media_type = self.accounts_to_fetch[self.current_fetch_index] - self.log_output.append(f'Processing account {self.current_fetch_index + 1}/{len(self.accounts_to_fetch)}: {username} ({media_type})') - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not getattr(self, 'is_initial_fetch', True): - self.log_output.append(f'Resuming from existing progress for {username} ({media_type}) - {len(existing_account.media_list):,} items already fetched') - - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data and cached_data.get('metadata', {}).get('has_more', False): - current_page = cached_data.get('metadata', {}).get('page', 0) - next_page = current_page + 1 - self.log_output.append(f'Continuing from batch {next_page + 1} for {username}...') - self.fetch_next_batch_for_current_user(username, media_type, next_page, cached_data.get('metadata', {})) - return - else: - self.log_output.append(f'Account {username} already completed. Moving to next account.') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - elif existing_account: - self.accounts.remove(existing_account) - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') - self.update_account_list() - - if not existing_account: - cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) - if cached_data: - try: - account_info = cached_data.get('account_info', {}) - timeline = cached_data.get('timeline', []) - if account_info and timeline: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - media_list=timeline, - fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', - fetch_timestamp=cached_data.get('fetch_timestamp') - ) - - self.accounts.append(account) - self.update_account_list() - self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - except: - pass - - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.batch_mode and not self.is_auto_fetching: - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - else: - self.is_auto_fetching = False - - self.current_fetch_username = username - self.current_fetch_media_type = media_type - - self.disable_batch_buttons() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_batch_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start metadata fetch for {username}: {str(e)}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def on_batch_metadata_fetched(self, data, username, media_type): - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append(f'Ignoring result for {username} - auto-fetch was stopped.') - return - - if 'error' in data: - self.log_output.append(f'Error fetching {username}: {data["error"]}') - if self.is_auto_fetching: - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - account_info = data.get('account_info', {}) - timeline = data.get('timeline', []) - metadata = data.get('metadata', {}) - - if not account_info: - self.log_output.append(f'Error: Invalid account data received for {username}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - if not timeline: - media_type_display = media_type if media_type != 'all' else 'media' - self.log_output.append(f'Warning: No {media_type_display} found for account {username}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - return - - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - existing_account.media_list.extend(timeline) - new_items = len(timeline) - total_items = len(existing_account.media_list) - self.log_output.append(f'Added {new_items:,} items to {username}. Total: {total_items:,} media items') - - updated_data = { - 'account_info': account_info, - 'timeline': existing_account.media_list, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': existing_account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - else: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if self.batch_mode else 'all', - fetch_timestamp=datetime.now().isoformat() - ) - - self.accounts.append(account) - existing_account = account - self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - - updated_data = { - 'account_info': account_info, - 'timeline': timeline, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - - self.update_account_list() - - if self.batch_mode and metadata.get('has_more', False): - current_page = metadata.get('page', 0) - next_page = current_page + 1 - - if self.is_auto_fetching: - self.log_output.append(f'Batch {current_page + 1} completed for {username}. Auto-fetching batch {next_page + 1}...') - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - self.log_output.append(f'Batch {current_page + 1} completed for {username}. Next batch ({next_page + 1}) available.') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - self.log_output.append(f'Auto-fetching next batch for {username}...') - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - self.log_output.append('Auto-fetch stopped by user. Batch processing halted.') - return - elif self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) - else: - return - else: - if self.batch_mode: - total_items = len(existing_account.media_list) - self.log_output.append(f'All batches completed for {username}! Total: {total_items:,} media items') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - else: - self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') - return - else: - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - except Exception as e: - self.log_output.append(f'Error processing {username}: {str(e)}') - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def fetch_next_batch_for_current_user(self, username, media_type, page, metadata): - try: - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Batch processing halted.') - return - - if self.is_auto_fetching: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - - self.metadata_worker = MetadataFetchWorker( - username, - media_type, - batch_mode=True, - batch_size=self.batch_size, - page=page - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) - self.metadata_worker.error.connect(self.on_batch_metadata_error) - self.metadata_worker.start() - - except Exception as e: - self.log_output.append(f'Error: Failed to start next batch fetch for {username}: {str(e)}') - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - return - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - - def on_batch_metadata_error(self, error_message): - if hasattr(self, 'accounts_to_fetch') and hasattr(self, 'current_fetch_index'): - username, media_type = self.accounts_to_fetch[self.current_fetch_index] - self.log_output.append(f'Error fetching {username}: {error_message}') - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: - self.log_output.append('Auto-fetch stopped by user. Error handling halted.') - return - - if self.is_auto_fetching and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.show() - - self.current_fetch_index += 1 - self.fetch_next_account_in_batch() - else: - self.on_metadata_error(error_message) - - def on_metadata_fetched(self, data, username, media_type): - try: - if 'error' in data: - self.log_output.append(f'Error: {data["error"]}') - self.update_account_list() - return - - account_info = data.get('account_info', {}) - timeline = data.get('timeline', []) - metadata = data.get('metadata', {}) - if not account_info: - self.log_output.append('Error: Invalid account data received') - self.update_account_list() - return - - if not timeline: - media_type_display = media_type if media_type != 'all' else 'media' - self.log_output.append(f'Error: No {media_type_display} found for account {username}') - self.update_account_list() - return - existing_account = None - for account in self.accounts: - if account.username == username and account.media_type == media_type: - existing_account = account - break - - if existing_account: - existing_account.media_list.extend(timeline) - new_items = len(timeline) - total_items = len(existing_account.media_list) - self.log_output.append(f'Added {new_items:,} items. Total: {total_items:,} media items for {username}') - else: - followers = account_info.get('followers_count', 0) - following = account_info.get('friends_count', 0) - posts = account_info.get('statuses_count', 0) - nick = account_info.get('nick', account_info.get('name', '')) - profile_image_url = account_info.get('profile_image', '') - - account = Account( - username=username, - nick=nick, - followers=followers, - following=following, - posts=posts, - media_type=media_type, - profile_image=profile_image_url, - media_list=timeline, - fetch_mode='batch' if self.batch_mode else 'all', - fetch_timestamp=datetime.now().isoformat() - ) - - self.accounts.append(account) - self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') - existing_account = account - - updated_data = { - 'account_info': account_info, - 'timeline': existing_account.media_list, - 'metadata': metadata, - 'is_batch': self.batch_mode, - 'fetch_timestamp': existing_account.fetch_timestamp - } - self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) - - self.update_account_list() - - self.current_fetch_username = username - self.current_fetch_media_type = media_type - self.current_fetch_metadata = metadata - - if self.batch_mode and metadata.get('has_more', False): - current_page = metadata.get('page', 0) - next_page = current_page + 1 - self.log_output.append(f'Batch {current_page + 1} completed. Next batch ({next_page + 1}) available.') - - if self.is_auto_fetching: - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - self.fetch_next_batch_internal() - else: - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.cancel_fetch_btn.show() - self.stop_fetch_btn.hide() - self.enable_batch_buttons() - else: - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - - if self.batch_mode: - total_items = len(existing_account.media_list) - self.log_output.append(f'All batches complete! Total: {total_items:,} media items for {username}') - - self.twitter_url.clear() - - if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): - self.current_update_index += 1 - self.update_next_account() - else: - self.tab_widget.setCurrentIndex(0) - except Exception as e: - self.log_output.append(f'Error: {str(e)}') - self.update_account_list() - - def on_metadata_error(self, error_message): - self.log_output.append(f'Error: {error_message}') - - if self.is_auto_fetching: - self.stop_auto_fetch() - - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - - self.enable_batch_buttons() - - if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): - self.current_update_index += 1 - self.update_next_account() - - self.update_account_list() - - def update_account_list(self): - self.account_list.clear() - for i, account in enumerate(self.accounts, 1): - media_count = len(account.media_list) if account.media_list else 0 - media_type_display = "GIF" if account.media_type.lower() == "gif" else account.media_type.title() - - line1 = f"{i}. {account.username} ({account.nick})" - line2 = f"Followers: {account.followers:,} • Following: {account.following:,} • Posts: {account.posts:,} • {media_type_display}: {media_count:,}" - line3 = self.format_fetch_info(account.fetch_timestamp) - display_text = f"{line1}\n{line2}\n{line3}" - item = QListWidgetItem() - item.setText(display_text) - item.setSizeHint(QSize(0, 70)) - - if account.profile_image: - if account.profile_image in self.profile_image_cache: - item.setIcon(QIcon(self.profile_image_cache[account.profile_image])) - else: - self.download_profile_image(account.profile_image) - placeholder = self.create_placeholder_icon(48) - if placeholder: - item.setIcon(QIcon(placeholder)) - else: - placeholder = self.create_placeholder_icon(48) - if placeholder: - item.setIcon(QIcon(placeholder)) - - self.account_list.addItem(item) - - self.update_button_states() - - def create_colored_icon(self, svg_path, color): - with open(svg_path, 'r', encoding='utf-8') as f: - svg_content = f.read() - - svg_content = svg_content.replace('stroke="currentColor"', f'stroke="{color}"') - svg_content = svg_content.replace('fill="currentColor"', f'fill="{color}"') - - pixmap = QPixmap(24, 24) - pixmap.fill(Qt.GlobalColor.transparent) - - from PyQt6.QtSvg import QSvgRenderer - renderer = QSvgRenderer(svg_content.encode('utf-8')) - painter = QPainter(pixmap) - renderer.render(painter) - painter.end() - - return QIcon(pixmap) - - def create_placeholder_icon(self, size=48): - pixmap = QPixmap(size, size) - pixmap.fill(Qt.GlobalColor.transparent) - - painter = QPainter(pixmap) - painter.setRenderHint(QPainter.RenderHint.Antialiasing) - - painter.setBrush(Qt.GlobalColor.gray) - painter.setPen(Qt.PenStyle.NoPen) - painter.drawRoundedRect(0, 0, size, size, 8, 8) - - painter.setPen(Qt.GlobalColor.white) - font_size = int(size * 0.4) - painter.setFont(painter.font()) - painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "👤") - - painter.end() - return pixmap - - def create_square_pixmap(self, original_pixmap, size=48): - if original_pixmap.isNull(): - return self.create_placeholder_icon(size) - - square_pixmap = QPixmap(size, size) - square_pixmap.fill(Qt.GlobalColor.transparent) - - painter = QPainter(square_pixmap) - painter.setRenderHint(QPainter.RenderHint.Antialiasing) - - path = QPainterPath() - path.addRoundedRect(0, 0, size, size, 8, 8) - painter.setClipPath(path) - - scaled_pixmap = original_pixmap.scaled( - size, size, - Qt.AspectRatioMode.KeepAspectRatioByExpanding, - Qt.TransformationMode.SmoothTransformation - ) - - x = (size - scaled_pixmap.width()) // 2 - y = (size - scaled_pixmap.height()) // 2 - painter.drawPixmap(x, y, scaled_pixmap) - - painter.end() - return square_pixmap - - def download_profile_image(self, url): - if not url or url in self.profile_image_cache or url in self.pending_downloads: - return - try: - request = QNetworkRequest(QUrl(url)) - request.setHeader(QNetworkRequest.KnownHeaders.UserAgentHeader, - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36") - - reply = self.network_manager.get(request) - self.pending_downloads[url] = reply - reply.finished.connect(lambda: self.on_profile_image_downloaded(reply, url)) - except Exception as e: - pass - - def on_profile_image_downloaded(self, reply, image_url): - try: - if reply.error() == reply.NetworkError.NoError: - data = reply.readAll() - pixmap = QPixmap() - if pixmap.loadFromData(data): - square_pixmap = self.create_square_pixmap(pixmap, 48) - self.profile_image_cache[image_url] = square_pixmap - - self.update_account_list() - - if image_url in self.pending_downloads: - del self.pending_downloads[image_url] - - except Exception as e: - pass - finally: - reply.deleteLater() - - def update_button_states(self): - has_accounts = len(self.accounts) > 0 - has_selected = len(self.account_list.selectedItems()) > 0 - - self.import_btn.setEnabled(True) - self.export_btn.setEnabled(has_accounts) - self.download_selected_btn.setEnabled(has_accounts) - self.update_selected_btn.setEnabled(has_accounts) - self.delete_btn.setEnabled(has_accounts) - - self.import_btn.show() - self.export_btn.show() - self.download_selected_btn.show() - self.update_selected_btn.show() - self.delete_btn.show() - - def hide_account_buttons(self): - self.import_btn.setEnabled(True) - self.export_btn.setEnabled(False) - self.download_selected_btn.setEnabled(False) - self.update_selected_btn.setEnabled(False) - self.delete_btn.setEnabled(False) - - self.import_btn.show() - self.export_btn.show() - self.download_selected_btn.show() - self.update_selected_btn.show() - self.delete_btn.show() - - def download_selected(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Download All', - f'No accounts selected. Download all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.Yes: - self.download_accounts(list(range(len(self.accounts)))) - return - - self.download_accounts([self.account_list.row(item) for item in selected_items]) - - def update_selected(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Update All', - f'No accounts selected. Update all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - selected_accounts = self.accounts.copy() - else: - selected_accounts = [] - for item in selected_items: - index = self.account_list.row(item) - if index < len(self.accounts): - selected_accounts.append(self.accounts[index]) - - if not selected_accounts: - return - - self.tab_widget.setCurrentIndex(1) - - self.log_output.clear() - self.log_output.append(f'Starting update for {len(selected_accounts)} account(s)...') - - self.accounts_to_update = selected_accounts.copy() - self.current_update_index = 0 - - self.update_next_account() - - def update_next_account(self): - if not hasattr(self, 'accounts_to_update') or self.current_update_index >= len(self.accounts_to_update): - self.log_output.append('All selected accounts have been updated!') - if hasattr(self, 'accounts_to_update'): - delattr(self, 'accounts_to_update') - if hasattr(self, 'current_update_index'): - delattr(self, 'current_update_index') - self.tab_widget.setCurrentIndex(0) - return - - account = self.accounts_to_update[self.current_update_index] - self.log_output.append(f'Updating account {self.current_update_index + 1}/{len(self.accounts_to_update)}: {account.username} ({account.media_type})') - self.update_account(account) - - def update_account(self, account): - original_batch_mode = self.batch_mode - original_batch_size = self.batch_size - original_media_type = self.media_type_combo.currentData() - - self.batch_mode = (account.fetch_mode == 'batch') - if self.batch_mode: - self.batch_size = self.settings.value('batch_size', 100, type=int) - - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == account.media_type: - self.media_type_combo.setCurrentIndex(i) - break - - self.twitter_url.setText(account.username) - - self.tab_widget.setCurrentIndex(1) - - self.log_output.clear() - self.log_output.append(f'Updating {account.username} ({account.media_type}) using {account.fetch_mode} mode...') - - username = account.username - media_type = account.media_type - - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') - - for i, existing_account in enumerate(self.accounts): - if existing_account.username == account.username and existing_account.media_type == account.media_type: - self.accounts.pop(i) - break - - self.update_account_list() - - self.start_fetch_process(username, media_type) - - self.batch_mode = original_batch_mode - self.batch_size = original_batch_size - - for i in range(self.media_type_combo.count()): - if self.media_type_combo.itemData(i) == original_media_type: - self.media_type_combo.setCurrentIndex(i) - break - - def start_fetch_process(self, username, media_type): - if not self.auth_token_input.text().strip(): - self.log_output.append("Error: Please enter your auth token") - return - - self.disable_batch_buttons() - - self.worker = MetadataFetchWorker( - username, - media_type, - batch_mode=self.batch_mode, - batch_size=self.batch_size if self.batch_mode else 0, - page=0 - ) - self.worker.auth_token = self.auth_token_input.text().strip() - self.worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) - self.worker.error.connect(self.on_metadata_error) - self.worker.start() - - def download_accounts(self, indices): - self.log_output.clear() - outpath = self.output_dir.text() - if not os.path.exists(outpath): - self.log_output.append('Warning: Invalid output directory.') - return - - if not self.auth_token_input.text().strip(): - self.log_output.append("Error: Please enter your auth token") - return - - accounts_to_download = [self.accounts[i] for i in indices] - - try: - self.start_download_worker(accounts_to_download, outpath) - except Exception as e: - self.log_output.append(f"Error: An error occurred while starting the download: {str(e)}") - - def start_download_worker(self, accounts_to_download, outpath): - self.worker = DownloadWorker( - accounts_to_download, - outpath, - self.auth_token_input.text().strip(), - self.filename_format, - self.download_batch_size, - self.convert_gif, - self.gif_conversion_quality, - self.gif_conversion_mode - ) - self.worker.finished.connect(self.on_download_finished) - self.worker.progress.connect(self.update_progress) - self.worker.conversion_progress.connect(self.update_conversion_progress) - self.worker.download_progress.connect(self.update_download_progress) - self.worker.start() - self.start_timer() - self.update_ui_for_download_start(len(accounts_to_download)) - - def update_ui_for_download_start(self, account_count): - self.download_selected_btn.setEnabled(False) - self.update_selected_btn.setEnabled(False) - self.stop_btn.show() - self.pause_resume_btn.show() - - self.next_batch_btn.hide() - self.auto_batch_btn.hide() - self.stop_fetch_btn.hide() - - self.progress_bar.show() - self.progress_bar.setValue(0) - - self.tab_widget.setCurrentWidget(self.process_tab) - - def update_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - current_text = cursor.selectedText() - progress_indicators = [ - "Downloading", - "Account", - "files downloaded" - ] - - should_replace = False - for indicator in progress_indicators: - if indicator in current_text and indicator in message: - should_replace = True - break - if should_replace and current_text.strip(): - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def update_conversion_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - current_text = cursor.selectedText() - if "Converting GIF" in current_text: - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def update_download_progress(self, message, percentage): - cursor = self.log_output.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.End) - cursor.select(QTextCursor.SelectionType.LineUnderCursor) - - current_text = cursor.selectedText() - if "Downloading" in current_text and ":" in current_text and "/" in current_text: - cursor.removeSelectedText() - cursor.deletePreviousChar() - - self.log_output.append(message) - self.log_output.moveCursor(QTextCursor.MoveOperation.End) - - if percentage > 0: - self.progress_bar.setValue(percentage) - - def stop_download(self): - if hasattr(self, 'worker'): - self.worker.stop() - self.stop_timer() - self.on_download_finished(True, "Download stopped by user.") - - def on_download_finished(self, success, message): - self.progress_bar.hide() - self.stop_btn.hide() - self.pause_resume_btn.hide() - self.pause_resume_btn.setText('Pause') - self.stop_timer() - - self.download_selected_btn.setEnabled(True) - self.update_selected_btn.setEnabled(True) - - if (self.batch_mode and self.current_fetch_metadata and - self.current_fetch_metadata.get('has_more', False) and not self.is_auto_fetching): - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.enable_batch_buttons() - - if success: - self.log_output.append(f"\nStatus: {message}") - else: - self.log_output.append(f"Error: {message}") - - self.tab_widget.setCurrentWidget(self.process_tab) - - def toggle_pause_resume(self): - if hasattr(self, 'worker'): - if self.worker.is_paused: - self.worker.resume() - self.pause_resume_btn.setText('Pause') - self.timer.start(1000) - else: - self.worker.pause() - self.pause_resume_btn.setText('Resume') - - def delete_accounts(self): - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Delete All', - f'No accounts selected. Delete all {len(self.accounts)} account(s) and their cache files?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - - for account in self.accounts: - username = account.username - if username: - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, account.media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed temp file: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove temp file {os.path.basename(cache_file)}: {str(e)}') - - self.reset_state() - self.reset_ui() - self.tab_widget.setCurrentIndex(0) - else: - reply = QMessageBox.question( - self, - 'Confirm Delete', - f'Delete {len(selected_items)} selected account(s) and their cache files?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - return - - selected_indices = sorted([self.account_list.row(item) for item in selected_items], reverse=True) - - for index in selected_indices: - account = self.accounts[index] - username = account.username - if username: - for is_batch in [True, False]: - cache_file = self.get_cache_file_path(username, account.media_type, is_batch=is_batch) - try: - if os.path.exists(cache_file): - os.remove(cache_file) - self.log_output.append(f'Removed temp file: {os.path.basename(cache_file)}') - except Exception as e: - self.log_output.append(f'Warning: Could not remove temp file {os.path.basename(cache_file)}: {str(e)}') - - self.accounts.pop(index) - - self.update_account_list() - self.update_button_states() - - def export_accounts(self): - self.tab_widget.setCurrentIndex(1) - - selected_items = self.account_list.selectedItems() - - if not selected_items: - reply = QMessageBox.question( - self, - 'Confirm Export All', - f'No accounts selected. Export all {len(self.accounts)} account(s)?', - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - QMessageBox.StandardButton.No - ) - if reply == QMessageBox.StandardButton.No: - self.log_output.append('Export cancelled') - return - selected_accounts = self.accounts.copy() - else: - selected_accounts = [] - for item in selected_items: - row = self.account_list.row(item) - if 0 <= row < len(self.accounts): - selected_accounts.append(self.accounts[row]) - - if not selected_accounts: - self.log_output.append('No valid accounts found for export') - return - - export_dir = QFileDialog.getExistingDirectory(self, "Select Export Directory", self.last_output_path) - if not export_dir: - self.log_output.append('Export cancelled') - return - - self.log_output.append(f'Starting export of {len(selected_accounts)} account(s)...') - exported_count = 0 - - for account in selected_accounts: - try: - now = datetime.now() - date_str = now.strftime("%Y%m%d_%H%M%S") - - if account.fetch_mode == 'batch': - filename = f"{account.username}_batch_{date_str}.json" - else: - filename = f"{account.username}_{date_str}.json" - - filepath = os.path.join(export_dir, filename) - - export_data = { - 'username': account.username, - 'nick': account.nick, - 'followers': account.followers, - 'following': account.following, - 'posts': account.posts, - 'media_type': account.media_type, - 'profile_image': account.profile_image, - 'media_list': account.media_list, - 'fetch_mode': account.fetch_mode, - 'fetch_timestamp': account.fetch_timestamp, - 'export_timestamp': now.isoformat() - } - - with open(filepath, 'w', encoding='utf-8') as f: - json.dump(export_data, f, indent=2, ensure_ascii=False) - - exported_count += 1 - self.log_output.append(f'Exported: {filename}') - - except Exception as e: - self.log_output.append(f'Error exporting {account.username}: {str(e)}') - - self.log_output.append(f'Export completed: {exported_count}/{len(selected_accounts)} account(s) exported') - self.log_output.append(f'Export location: {export_dir}') - - def import_accounts(self): - self.tab_widget.setCurrentIndex(1) - - file_paths, _ = QFileDialog.getOpenFileNames( - self, - "Select JSON Files to Import", - self.last_output_path, - "JSON Files (*.json)" - ) - - if not file_paths: - self.log_output.append('Import cancelled') - return - - self.log_output.append(f'Starting import of {len(file_paths)} file(s)...') - imported_count = 0 - skipped_count = 0 - error_count = 0 - - for filepath in file_paths: - try: - with open(filepath, 'r', encoding='utf-8') as f: - data = json.load(f) - - username = data.get('username') - if not username: - self.log_output.append(f'Skipped {os.path.basename(filepath)}: No username found') - skipped_count += 1 - continue - - existing_account = next((acc for acc in self.accounts if acc.username == username), None) - if existing_account: - self.log_output.append(f'Skipped {username}: Already exists in dashboard') - skipped_count += 1 - continue - - account = Account( - username=username, - nick=data.get('nick', ''), - followers=data.get('followers', 0), - following=data.get('following', 0), - posts=data.get('posts', 0), - media_type=data.get('media_type', 'all'), - profile_image=data.get('profile_image'), - media_list=data.get('media_list', []), - fetch_mode=data.get('fetch_mode', 'all'), - fetch_timestamp=data.get('fetch_timestamp') - ) - - self.accounts.append(account) - - account_info = { - 'followers_count': account.followers, - 'friends_count': account.following, - 'statuses_count': account.posts, - 'nick': account.nick, - 'name': account.nick, - 'profile_image': account.profile_image - } - - cache_data = { - 'account_info': account_info, - 'timeline': account.media_list, - 'metadata': {}, - 'is_batch': (account.fetch_mode == 'batch'), - 'fetch_timestamp': account.fetch_timestamp - } - - self.save_cached_data(username, account.media_type, cache_data, is_batch=(account.fetch_mode == 'batch')) - - imported_count += 1 - media_count = len(account.media_list) if account.media_list else 0 - self.log_output.append(f'Imported: {username} ({media_count:,} media)') - - except Exception as e: - self.log_output.append(f'Error importing {os.path.basename(filepath)}: {str(e)}') - error_count += 1 - - self.update_account_list() - self.update_button_states() - self.log_output.append(f'Import completed: {imported_count} imported, {skipped_count} skipped, {error_count} errors') - self.tab_widget.setCurrentIndex(0) - - def update_timer(self): - self.elapsed_time = self.elapsed_time.addSecs(1) - self.time_label.setText(self.elapsed_time.toString("hh:mm:ss")) - - def start_timer(self): - self.elapsed_time = QTime(0, 0, 0) - self.time_label.setText("00:00:00") - self.time_label.show() - self.timer.start(1000) - - def stop_timer(self): - self.timer.stop() - self.time_label.hide() - - def fetch_next_batch(self): - if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): - self.log_output.append('No more batches available.') - return - - self.cancel_fetch_btn.hide() - self.disable_batch_buttons() - self.fetch_next_batch_internal() - - def start_auto_batch(self): - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.is_auto_fetching = True - self.is_initial_fetch = False - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.log_output.append('Resuming multiple user batch processing...') - - self.fetch_next_account_in_batch() - return - - if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): - self.log_output.append('No more batches available.') - return - - self.is_auto_fetching = True - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.cancel_fetch_btn.hide() - self.stop_fetch_btn.show() - - self.log_output.append('Auto batch mode enabled.') - self.fetch_next_batch_internal() - - def stop_auto_fetch(self): - self.is_auto_fetching = False - self.stop_fetch_btn.hide() - - if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): - self.metadata_worker.terminate() - self.metadata_worker.wait() - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.auto_batch_btn.show() - self.auto_batch_btn.setEnabled(True) - self.next_batch_btn.hide() - self.cancel_fetch_btn.show() - self.log_output.append('Multiple user batch processing stopped.') - elif self.current_fetch_metadata and self.current_fetch_metadata.get('has_more', False): - self.next_batch_btn.show() - self.auto_batch_btn.show() - self.cancel_fetch_btn.show() - self.enable_batch_buttons() - self.log_output.append('Auto batch mode stopped.') - else: - self.cancel_fetch_btn.show() - self.log_output.append('Auto batch mode stopped.') - - def cancel_multiple_user_fetch(self): - if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): - self.metadata_worker.terminate() - self.metadata_worker.wait() - - if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: - self.log_output.append('Multiple user batch processing cancelled.') - - if hasattr(self, 'accounts_to_fetch'): - delattr(self, 'accounts_to_fetch') - if hasattr(self, 'current_fetch_index'): - delattr(self, 'current_fetch_index') - if hasattr(self, 'is_multiple_user_mode'): - delattr(self, 'is_multiple_user_mode') - if hasattr(self, 'is_initial_fetch'): - delattr(self, 'is_initial_fetch') - else: - self.log_output.append('Single user batch processing cancelled.') - - self.current_fetch_username = None - self.current_fetch_media_type = None - self.current_fetch_metadata = None - - self.auto_batch_btn.hide() - self.next_batch_btn.hide() - self.stop_fetch_btn.hide() - self.cancel_fetch_btn.hide() - self.is_auto_fetching = False - - self.twitter_url.clear() - self.tab_widget.setCurrentIndex(0) - - def disable_batch_buttons(self): - self.next_batch_btn.setEnabled(False) - self.auto_batch_btn.setEnabled(False) - - def enable_batch_buttons(self): - self.next_batch_btn.setEnabled(True) - self.auto_batch_btn.setEnabled(True) - - def fetch_next_batch_internal(self): - if not self.current_fetch_username or not self.current_fetch_media_type or not self.current_fetch_metadata: - return - - self.disable_batch_buttons() - - current_page = self.current_fetch_metadata.get('page', 0) - next_page = current_page + 1 - - self.log_output.append(f'Fetching batch {next_page + 1} for {self.current_fetch_username}...') - - self.metadata_worker = MetadataFetchWorker( - self.current_fetch_username, - self.current_fetch_media_type, - batch_mode=True, - batch_size=self.batch_size, - page=next_page - ) - self.metadata_worker.auth_token = self.auth_token_input.text().strip() - self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, self.current_fetch_username, self.current_fetch_media_type)) - self.metadata_worker.error.connect(self.on_metadata_error) - self.metadata_worker.start() - - def on_tab_changed(self, index): - if index == 0: - self.update_button_states() +from PyQt6.QtWidgets import QApplication +from PyQt6.QtCore import QSettings +from ui.mainWindow import TwitterXMediaBatchDownloaderGUI def main(): if getattr(sys, 'frozen', False): @@ -2784,7 +11,7 @@ def main(): app = QApplication(sys.argv) - settings = QSettings('TwitterMediaDownloader', 'Settings') + settings = QSettings('TwitterXMediaBatchDownloader', 'Settings') theme_color = settings.value('theme_color', '#2196F3') qdarktheme.setup_theme( @@ -2794,7 +21,7 @@ def main(): } } ) - window = TwitterMediaDownloaderGUI() + window = TwitterXMediaBatchDownloaderGUI() window.show() sys.exit(app.exec()) diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..28d76c0 --- /dev/null +++ b/core/__init__.py @@ -0,0 +1,14 @@ +from core.databaseManager import DatabaseManager +from core.metadataFetcher import get_metadata, get_metadata_by_date +from core.downloadWorker import DownloadWorker +from core.metadataWorker import MetadataFetchWorker +from core.accountModel import Account + +__all__ = [ + 'DatabaseManager', + 'get_metadata', + 'get_metadata_by_date', + 'DownloadWorker', + 'MetadataFetchWorker', + 'Account' +] \ No newline at end of file diff --git a/core/accountModel.py b/core/accountModel.py new file mode 100644 index 0000000..88008f2 --- /dev/null +++ b/core/accountModel.py @@ -0,0 +1,15 @@ +from dataclasses import dataclass + +@dataclass +class Account: + username: str + nick: str + followers: int + following: int + posts: int + media_type: str + profile_image: str = None + media_list: list = None + fetch_mode: str = 'all' + fetch_timestamp: str = None + group_id: int = None \ No newline at end of file diff --git a/core/databaseManager.py b/core/databaseManager.py new file mode 100644 index 0000000..dc18d9e --- /dev/null +++ b/core/databaseManager.py @@ -0,0 +1,420 @@ +import sqlite3 +import os +import json +import threading +from datetime import datetime +from pathlib import Path +from typing import List, Dict, Optional, Tuple + +class DatabaseManager: + def __init__(self, db_path: str): + self.db_path = db_path + self.conn = None + self._lock = threading.Lock() + self.init_database() + + def init_database(self): + self.conn = sqlite3.connect(self.db_path, check_same_thread=False) + self.conn.row_factory = sqlite3.Row + + self.conn.execute('PRAGMA foreign_keys = ON') + + cursor = self.conn.cursor() + + cursor.execute(''' + CREATE TABLE IF NOT EXISTS groups ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT UNIQUE NOT NULL, + created_at TEXT + ) + ''') + + cursor.execute(''' + CREATE TABLE IF NOT EXISTS accounts ( + username TEXT PRIMARY KEY COLLATE NOCASE, + nick TEXT, + followers INTEGER, + following INTEGER, + posts INTEGER, + profile_image TEXT, + fetch_timestamp TEXT, + media_type TEXT, + fetch_mode TEXT, + timeline_type TEXT DEFAULT 'media', + group_id INTEGER, + FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE SET NULL + ) + ''') + + try: + cursor.execute("SELECT group_id FROM accounts LIMIT 1") + except sqlite3.OperationalError: + cursor.execute("ALTER TABLE accounts ADD COLUMN group_id INTEGER") + self.conn.commit() + + cursor.execute(''' + CREATE TABLE IF NOT EXISTS media ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT COLLATE NOCASE, + tweet_id TEXT, + url TEXT, + date TEXT, + type TEXT, + FOREIGN KEY (username) REFERENCES accounts(username) ON DELETE CASCADE, + UNIQUE(username, url) + ) + ''') + + cursor.execute(''' + CREATE TABLE IF NOT EXISTS downloads ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + url TEXT, + filepath TEXT, + filepath_normalized TEXT COLLATE NOCASE, + username TEXT COLLATE NOCASE, + tweet_id TEXT, + download_date TEXT, + file_size INTEGER, + status TEXT, + UNIQUE(url, filepath_normalized) + ) + ''') + + cursor.execute('CREATE INDEX IF NOT EXISTS idx_filepath_normalized ON downloads(filepath_normalized)') + cursor.execute('CREATE INDEX IF NOT EXISTS idx_url ON downloads(url)') + cursor.execute('CREATE INDEX IF NOT EXISTS idx_username ON downloads(username)') + cursor.execute('CREATE INDEX IF NOT EXISTS idx_media_username ON media(username)') + + self.conn.commit() + + def save_account(self, username: str, nick: str, followers: int, following: int, + posts: int, media_type: str, profile_image: str = None, + fetch_mode: str = 'all', timeline_type: str = 'media', + fetch_timestamp: str = None, group_id: int = None): + with self._lock: + cursor = self.conn.cursor() + + if fetch_timestamp is None: + fetch_timestamp = datetime.now().isoformat() + + cursor.execute(''' + INSERT OR REPLACE INTO accounts + (username, nick, followers, following, posts, profile_image, + fetch_timestamp, media_type, fetch_mode, timeline_type, group_id) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ''', (username, nick, followers, following, posts, profile_image, + fetch_timestamp, media_type, fetch_mode, timeline_type, group_id)) + + self.conn.commit() + + def save_media_list(self, username: str, media_list: List[Dict]): + with self._lock: + cursor = self.conn.cursor() + + cursor.execute('DELETE FROM media WHERE username = ?', (username,)) + + for item in media_list: + cursor.execute(''' + INSERT OR IGNORE INTO media (username, tweet_id, url, date, type) + VALUES (?, ?, ?, ?, ?) + ''', (username, item.get('tweet_id', ''), item['url'], + item['date'], item.get('type', 'image'))) + + self.conn.commit() + + def get_account(self, username: str, media_type: str = 'all') -> Optional[Dict]: + cursor = self.conn.cursor() + cursor.execute(''' + SELECT * FROM accounts + WHERE username = ? AND media_type = ? + ''', (username, media_type)) + + row = cursor.fetchone() + if row: + return dict(row) + return None + + def get_media_list(self, username: str) -> List[Dict]: + cursor = self.conn.cursor() + cursor.execute(''' + SELECT tweet_id, url, date, type + FROM media + WHERE username = ? + ORDER BY date DESC + ''', (username,)) + + return [dict(row) for row in cursor.fetchall()] + + def get_all_accounts(self) -> List[Dict]: + cursor = self.conn.cursor() + cursor.execute('SELECT * FROM accounts ORDER BY fetch_timestamp DESC') + return [dict(row) for row in cursor.fetchall()] + + def delete_account(self, username: str, media_type: str = None): + with self._lock: + cursor = self.conn.cursor() + + if media_type: + cursor.execute('DELETE FROM accounts WHERE username = ? AND media_type = ?', + (username, media_type)) + else: + cursor.execute('DELETE FROM accounts WHERE username = ?', (username,)) + + self.conn.commit() + + def is_file_downloaded(self, filepath: str) -> bool: + cursor = self.conn.cursor() + filepath_normalized = filepath.lower() + + cursor.execute(''' + SELECT 1 FROM downloads + WHERE filepath_normalized = ? AND status = 'downloaded' + LIMIT 1 + ''', (filepath_normalized,)) + + return cursor.fetchone() is not None + + def is_url_downloaded(self, url: str) -> bool: + cursor = self.conn.cursor() + + cursor.execute(''' + SELECT 1 FROM downloads + WHERE url = ? AND status = 'downloaded' + LIMIT 1 + ''', (url,)) + + return cursor.fetchone() is not None + + def record_download(self, url: str, filepath: str, username: str, + tweet_id: str, status: str = 'downloaded', file_size: int = 0): + with self._lock: + cursor = self.conn.cursor() + filepath_normalized = filepath.lower() + download_date = datetime.now().isoformat() + + cursor.execute(''' + INSERT OR REPLACE INTO downloads + (url, filepath, filepath_normalized, username, tweet_id, + download_date, file_size, status) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + ''', (url, filepath, filepath_normalized, username, tweet_id, + download_date, file_size, status)) + + self.conn.commit() + + def get_download_stats(self, username: str = None) -> Dict: + cursor = self.conn.cursor() + + if username: + cursor.execute(''' + SELECT + COUNT(*) as total, + SUM(CASE WHEN status = 'downloaded' THEN 1 ELSE 0 END) as downloaded, + SUM(CASE WHEN status = 'skipped' THEN 1 ELSE 0 END) as skipped, + SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed, + SUM(file_size) as total_size + FROM downloads + WHERE username = ? + ''', (username,)) + else: + cursor.execute(''' + SELECT + COUNT(*) as total, + SUM(CASE WHEN status = 'downloaded' THEN 1 ELSE 0 END) as downloaded, + SUM(CASE WHEN status = 'skipped' THEN 1 ELSE 0 END) as skipped, + SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed, + SUM(file_size) as total_size + FROM downloads + ''') + + row = cursor.fetchone() + return dict(row) if row else {} + + def migrate_from_json(self, json_dir: str) -> Tuple[int, int]: + if not os.path.exists(json_dir): + return 0, 0 + + json_files = [f for f in os.listdir(json_dir) if f.endswith('.json')] + accounts_migrated = 0 + media_migrated = 0 + + for json_file in json_files: + try: + json_path = os.path.join(json_dir, json_file) + + with open(json_path, 'r', encoding='utf-8') as f: + data = json.load(f) + + media_type = data.get('media_type', 'all') + + if media_type not in ['all', 'image', 'video', 'gif']: + filename_parts = json_file.replace('.json', '').split('_') + if len(filename_parts) >= 1: + last_part = filename_parts[-1] + if last_part in ['all', 'image', 'video', 'gif']: + media_type = last_part + else: + media_type = 'all' + else: + media_type = 'all' + + filename_parts = json_file.replace('.json', '').split('_') + if len(filename_parts) >= 2: + second_last = filename_parts[-2] + if second_last in ['media', 'timeline', 'tweets', 'with']: + timeline_type = second_last + else: + timeline_type = 'media' + else: + timeline_type = 'media' + + account_info = data.get('account_info', {}) + timeline = data.get('timeline', []) + + username = data.get('username') or account_info.get('name', filename_parts[0] if filename_parts else 'unknown') + + nick = data.get('nick') or account_info.get('nick') or account_info.get('name', username) + followers = data.get('followers') or account_info.get('followers_count', 0) + following = data.get('following') or account_info.get('friends_count', 0) + posts = data.get('posts') or account_info.get('statuses_count', 0) + profile_image = data.get('profile_image') or account_info.get('profile_image') + + self.save_account( + username=username, + nick=nick, + followers=followers, + following=following, + posts=posts, + media_type=media_type, + profile_image=profile_image, + fetch_mode=data.get('fetch_mode', 'all'), + timeline_type=timeline_type + ) + accounts_migrated += 1 + + media_list = timeline if timeline else data.get('media_list', []) + if media_list: + self.save_media_list(username, media_list) + media_migrated += len(media_list) + + backup_path = json_path + '.backup' + if not os.path.exists(backup_path): + os.rename(json_path, backup_path) + + except Exception as e: + print(f"Error migrating {json_file}: {e}") + continue + + return accounts_migrated, media_migrated + + def export_to_json(self, username: str, media_type: str, output_path: str): + account = self.get_account(username, media_type) + if not account: + return False + + media_list = self.get_media_list(username) + + export_data = { + 'username': account['username'], + 'nick': account['nick'], + 'followers': account['followers'], + 'following': account['following'], + 'posts': account['posts'], + 'profile_image': account['profile_image'], + 'fetch_timestamp': account['fetch_timestamp'], + 'media_type': account['media_type'], + 'fetch_mode': account['fetch_mode'], + 'media_list': media_list + } + + with open(output_path, 'w', encoding='utf-8') as f: + json.dump(export_data, f, ensure_ascii=False, indent=2) + + return True + + def import_from_json(self, json_path: str) -> bool: + try: + with open(json_path, 'r', encoding='utf-8') as f: + data = json.load(f) + + username = data.get('username') + if not username: + return False + + self.save_account( + username=username, + nick=data.get('nick', ''), + followers=data.get('followers', 0), + following=data.get('following', 0), + posts=data.get('posts', 0), + media_type=data.get('media_type', 'all'), + profile_image=data.get('profile_image'), + fetch_mode=data.get('fetch_mode', 'all') + ) + + media_list = data.get('media_list', []) + if media_list: + self.save_media_list(username, media_list) + + return True + + except Exception as e: + print(f"Error importing JSON: {e}") + return False + + def close(self): + if self.conn: + self.conn.close() + + def create_group(self, name: str) -> Optional[int]: + with self._lock: + try: + cursor = self.conn.cursor() + created_at = datetime.now().isoformat() + cursor.execute('INSERT INTO groups (name, created_at) VALUES (?, ?)', (name, created_at)) + self.conn.commit() + return cursor.lastrowid + except sqlite3.IntegrityError: + return None + + def get_all_groups(self) -> List[Dict]: + cursor = self.conn.cursor() + cursor.execute('SELECT * FROM groups ORDER BY name') + return [dict(row) for row in cursor.fetchall()] + + def get_group(self, group_id: int) -> Optional[Dict]: + cursor = self.conn.cursor() + cursor.execute('SELECT * FROM groups WHERE id = ?', (group_id,)) + row = cursor.fetchone() + return dict(row) if row else None + + def update_group(self, group_id: int, name: str) -> bool: + with self._lock: + try: + cursor = self.conn.cursor() + cursor.execute('UPDATE groups SET name = ? WHERE id = ?', (name, group_id)) + self.conn.commit() + return True + except sqlite3.IntegrityError: + return False + + def delete_group(self, group_id: int): + with self._lock: + cursor = self.conn.cursor() + cursor.execute('UPDATE accounts SET group_id = NULL WHERE group_id = ?', (group_id,)) + cursor.execute('DELETE FROM groups WHERE id = ?', (group_id,)) + self.conn.commit() + + def assign_account_to_group(self, username: str, group_id: Optional[int]): + with self._lock: + cursor = self.conn.cursor() + cursor.execute('UPDATE accounts SET group_id = ? WHERE username = ?', (group_id, username)) + self.conn.commit() + + def get_accounts_by_group(self, group_id: Optional[int]) -> List[Dict]: + cursor = self.conn.cursor() + if group_id is None: + cursor.execute('SELECT * FROM accounts WHERE group_id IS NULL ORDER BY fetch_timestamp DESC') + else: + cursor.execute('SELECT * FROM accounts WHERE group_id = ? ORDER BY fetch_timestamp DESC', (group_id,)) + return [dict(row) for row in cursor.fetchall()] diff --git a/core/downloadWorker.py b/core/downloadWorker.py new file mode 100644 index 0000000..3cb5212 --- /dev/null +++ b/core/downloadWorker.py @@ -0,0 +1,307 @@ +import sys +import os +import asyncio +import aiohttp +import subprocess +import imageio_ffmpeg +from datetime import datetime +from PyQt6.QtCore import QThread, pyqtSignal + +DEFAULT_BATCH_SIZE = 25 +TIMEOUT_SECONDS_MAC = 60 +TIMEOUT_SECONDS_DEFAULT = 30 +BATCH_SLEEP_INTERVAL = 0.1 +PAUSE_CHECK_INTERVAL = 0.1 + +class DownloadWorker(QThread): + finished = pyqtSignal(bool, str) + progress = pyqtSignal(str, int) + conversion_progress = pyqtSignal(str, int) + download_progress = pyqtSignal(str, int) + + def __init__(self, accounts, outpath, auth_token, filename_format='username_date', + download_batch_size=DEFAULT_BATCH_SIZE, convert_gif=False, gif_resolution='original', gif_conversion_mode='better', db_manager=None): + super().__init__() + self.accounts = accounts + self.outpath = outpath + self.auth_token = auth_token + self.filename_format = filename_format + self.download_batch_size = download_batch_size + self.convert_gif = convert_gif + self.gif_resolution = gif_resolution + self.gif_conversion_mode = gif_conversion_mode + self.is_paused = False + self.is_stopped = False + self.filepath_map = [] + self.db_manager = db_manager + + async def download_file(self, session, url, filepath, username, tweet_id): + try: + if self.db_manager and self.db_manager.is_url_downloaded(url): + return True, True + + if os.path.exists(filepath): + if self.db_manager: + file_size = os.path.getsize(filepath) + self.db_manager.record_download(url, filepath, username, tweet_id, 'downloaded', file_size) + return True, True + + if self.is_stopped: + return False, False + + try: + os.makedirs(os.path.dirname(filepath), exist_ok=True) + except OSError as e: + return False, False + + async with session.get(url) as response: + if response.status == 200: + content = await response.read() + + try: + with open(filepath, 'wb') as f: + f.write(content) + except (IOError, OSError) as e: + if self.db_manager: + self.db_manager.record_download(url, filepath, username, tweet_id, 'failed', 0) + return False, False + + if self.db_manager: + self.db_manager.record_download(url, filepath, username, tweet_id, 'downloaded', len(content)) + + return True, False + return False, False + except asyncio.TimeoutError: + if self.db_manager: + self.db_manager.record_download(url, filepath, username, tweet_id, 'failed', 0) + return False, False + except aiohttp.ClientError as e: + if self.db_manager: + self.db_manager.record_download(url, filepath, username, tweet_id, 'failed', 0) + return False, False + except Exception as e: + if self.db_manager: + self.db_manager.record_download(url, filepath, username, tweet_id, 'failed', 0) + return False, False + + async def download_account_media(self, account): + if not account.media_list: + return 0, 0, 0 + + safe_username = os.path.basename(account.username.replace('/', '_').replace('\\', '_')) + account_output_dir = os.path.join(self.outpath, safe_username) + os.makedirs(account_output_dir, exist_ok=True) + + timeout_seconds = TIMEOUT_SECONDS_MAC if sys.platform == 'darwin' else TIMEOUT_SECONDS_DEFAULT + timeout = aiohttp.ClientTimeout(total=timeout_seconds) + connector = aiohttp.TCPConnector(limit=self.download_batch_size) + + async with aiohttp.ClientSession(timeout=timeout, connector=connector) as session: + total = len(account.media_list) + completed = 0 + skipped = 0 + failed = 0 + + used_filenames = set() + + for i in range(0, total, self.download_batch_size): + if self.is_stopped: + break + + while self.is_paused: + if self.is_stopped: + return completed, skipped, failed + await asyncio.sleep(PAUSE_CHECK_INTERVAL) + + batch = account.media_list[i:i + self.download_batch_size] + tasks = [] + + for item in batch: + url = item['url'] + 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', '')) + + item_type = item.get('type', '') + if item_type == 'animated_gif': + media_type_folder = 'gif' + extension = 'mp4' + elif item_type == 'video' or 'video.twimg.com' in url: + media_type_folder = 'video' + extension = 'mp4' + else: + media_type_folder = 'image' + extension = 'jpg' + media_output_dir = os.path.join(account_output_dir, media_type_folder) + + if self.filename_format == "username_date": + base_filename = f"{safe_username}_{formatted_date}_{tweet_id}" + else: + base_filename = f"{formatted_date}_{safe_username}_{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(media_output_dir, filename) + self.filepath_map.append((item, filepath)) + task = asyncio.create_task(self.download_file(session, url, filepath, account.username, tweet_id)) + 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: + if was_skipped: + skipped += 1 + else: + completed += 1 + else: + failed += 1 + else: + failed += 1 + + progress_percent = int((completed + skipped + failed) / total * 100) + media_type_display = account.media_type if account.media_type != 'all' else 'media' + self.download_progress.emit(f"Downloading {account.username}'s {media_type_display}: {completed + skipped + failed:,}/{total:,}", progress_percent) + + await asyncio.sleep(BATCH_SLEEP_INTERVAL) + + return completed, skipped, failed + + def run(self): + try: + if sys.platform == 'darwin': + asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) + + total_accounts = len(self.accounts) + overall_completed = 0 + overall_skipped = 0 + overall_failed = 0 + + for i, account in enumerate(self.accounts): + if self.is_stopped: + break + + while self.is_paused: + if self.is_stopped: + return + self.msleep(100) + media_type_display = account.media_type if account.media_type != 'all' else 'media' + self.progress.emit(f"Downloading from account: {account.username} ({media_type_display}) - ({i+1}/{total_accounts})", + int((i) / total_accounts * 100)) + + completed, skipped, failed = asyncio.run(self.download_account_media(account)) + overall_completed += completed + overall_skipped += skipped + overall_failed += failed + self.progress.emit(f"Account {account.username} ({media_type_display}): {completed:,} downloaded, {skipped:,} skipped, {failed:,} failed", + int((i + 1) / total_accounts * 100)) + + if not self.is_stopped: + if self.convert_gif: + try: + ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe() + gif_items = [(item, fp) for item, fp in self.filepath_map if item.get('type') == 'animated_gif'] + total_gifs = len(gif_items) + if total_gifs > 0: + if sys.platform == 'win32': + creationflags = subprocess.CREATE_NO_WINDOW + else: + creationflags = 0 + + converted_count = 0 + skipped_count = 0 + self.progress.emit("Starting GIF conversion...", 0) + + for idx, (item, fp) in enumerate(gif_items, start=1): + if self.is_stopped: + break + + while self.is_paused: + if self.is_stopped: + break + self.msleep(100) + + if self.is_stopped: + break + + gif_fp = fp.rsplit('.', 1)[0] + '.gif' + + if os.path.exists(gif_fp): + try: + os.remove(fp) + skipped_count += 1 + conv_progress = int((idx / total_gifs) * 100) + quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" + self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display} - Skipped (exists)", conv_progress) + continue + except Exception: + pass + + + if self.gif_conversion_mode == 'fast': + ffmpeg_args = [ffmpeg_exe, '-i', fp, '-y', gif_fp] + else: + if self.gif_resolution == 'high': + ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=800:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=800:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '15', '-y', gif_fp] + elif self.gif_resolution == 'medium': + ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=600:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=600:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '10', '-y', gif_fp] + elif self.gif_resolution == 'low': + ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'scale=400:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=400:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a', '-r', '8', '-y', gif_fp] + else: + ffmpeg_args = [ffmpeg_exe, '-i', fp, '-lavfi', 'palettegen=stats_mode=full[palette];[0:v][palette]paletteuse=dither=sierra2_4a', '-y', gif_fp] + + try: + if sys.platform == 'win32': + result = subprocess.run(ffmpeg_args, capture_output=True, creationflags=creationflags) + else: + result = subprocess.run(ffmpeg_args, capture_output=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) + + if result.returncode == 0 and os.path.exists(gif_fp): + try: + os.remove(fp) + converted_count += 1 + except Exception: + pass + elif result.returncode != 0: + error_msg = result.stderr.decode('utf-8', errors='ignore') if result.stderr else 'Unknown error' + self.progress.emit(f"GIF conversion warning for file {idx}: {error_msg[:100]}", 0) + except Exception as e: + self.progress.emit(f"GIF conversion error for file {idx}: {str(e)[:100]}", 0) + + conv_progress = int((idx / total_gifs) * 100) + quality_display = f"({self.gif_conversion_mode}) - ({self.gif_resolution})" + self.conversion_progress.emit(f"Converting GIF {idx:,}/{total_gifs:,} {quality_display}", conv_progress) + if converted_count > 0 or skipped_count > 0: + completion_msg = f"GIF conversion completed: {converted_count:,} converted" + if skipped_count > 0: + completion_msg += f", {skipped_count:,} skipped (already exists)" + self.progress.emit(completion_msg, 100) + else: + self.progress.emit("GIF conversion completed", 100) + except Exception as conv_e: + self.progress.emit(f"GIF conversion error: {conv_e}", 0) + + success_message = f"Download completed! {overall_completed:,} files downloaded" + if overall_skipped > 0: + success_message += f", {overall_skipped:,} skipped" + if overall_failed > 0: + success_message += f", {overall_failed:,} failed" + self.finished.emit(True, success_message) + + except Exception as e: + self.finished.emit(False, str(e)) + + def pause(self): + self.is_paused = True + + def resume(self): + self.is_paused = False + + def stop(self): + self.is_stopped = True + self.is_paused = False \ No newline at end of file diff --git a/getMetadata.py b/core/metadataFetcher.py similarity index 55% rename from getMetadata.py rename to core/metadataFetcher.py index e41ab09..ec6ce81 100644 --- a/getMetadata.py +++ b/core/metadataFetcher.py @@ -3,7 +3,143 @@ import json from datetime import datetime from gallery_dl.extractor import twitter -def get_metadata(username, auth_token, timeline_type="timeline", batch_size=0, page=0, media_type="all"): +def get_metadata_by_date(username, auth_token, date_start, date_end, media_filter="filter:media", output_file=None): + query = f"from:{username} since:{date_start} until:{date_end}" + if media_filter: + query += f" {media_filter}" + + url = f"https://x.com/search?q={query}" + + extractor_class = twitter.TwitterSearchExtractor + match = re.match(extractor_class.pattern, url) + + if not match: + raise ValueError(f"Invalid search URL: {url}") + + extractor = extractor_class(match) + + config_dict = { + "cookies": { + "auth_token": auth_token + }, + "retweets": False + } + + extractor.config = lambda key, default=None: config_dict.get(key, default) + + try: + extractor.initialize() + + api = twitter.TwitterAPI(extractor) + + try: + user = api.user_by_screen_name(username) + + if "legacy" in user and user["legacy"].get("withheld_scope"): + raise ValueError("withheld") + + except Exception as e: + error_msg = str(e).lower() + if "withheld" in error_msg or (hasattr(e, "response") and "withheld" in str(e.response.text).lower()): + raise ValueError("withheld") + raise + + user_data = extractor._transform_user(user) + + structured_output = { + 'account_info': { + 'name': user_data.get('name', ''), + 'nick': user_data.get('nick', ''), + 'date': user_data.get('date', ''), + 'followers_count': user_data.get('followers_count', 0), + 'friends_count': user_data.get('friends_count', 0), + 'profile_image': user_data.get('profile_image', ''), + 'statuses_count': user_data.get('statuses_count', 0) + }, + 'total_urls': 0, + 'timeline': [], + 'search_query': query, + 'date_filter': { + 'start': date_start, + 'end': date_end, + 'method': 'search_api' + } + } + + if isinstance(structured_output['account_info']['date'], datetime): + structured_output['account_info']['date'] = structured_output['account_info']['date'].strftime("%Y-%m-%d %H:%M:%S") + + new_timeline_entries = [] + + try: + iterator = iter(extractor) + + while True: + try: + item = next(iterator) + + if isinstance(item, tuple) and len(item) >= 3: + media_url = item[1] + tweet_data = item[2] + + if 'pbs.twimg.com' in media_url or 'video.twimg.com' in media_url: + tweet_date = tweet_data.get('date', datetime.now()) + if isinstance(tweet_date, datetime): + tweet_date = tweet_date.strftime("%Y-%m-%d %H:%M:%S") + + timeline_entry = { + 'url': media_url, + 'date': tweet_date, + 'tweet_id': tweet_data.get('tweet_id', 0), + } + + if 'type' in tweet_data: + timeline_entry['type'] = tweet_data['type'] + + if 'retweet_id' in tweet_data and tweet_data['retweet_id']: + timeline_entry['retweet_id'] = tweet_data['retweet_id'] + timeline_entry['is_retweet'] = True + else: + timeline_entry['is_retweet'] = False + + new_timeline_entries.append(timeline_entry) + structured_output['total_urls'] += 1 + + except StopIteration: + break + + except Exception as e: + pass + + structured_output['timeline'] = new_timeline_entries + + structured_output['metadata'] = { + "new_entries": len(new_timeline_entries), + "method": "search_api", + "date_range": f"{date_start} to {date_end}" + } + + if output_file: + try: + with open(output_file, 'w', encoding='utf-8') as f: + json.dump(structured_output, f, ensure_ascii=False, indent=2) + except Exception as e: + pass + + return structured_output + + except Exception as e: + error_msg = str(e).lower() + if "withheld" in error_msg or e.__class__.__name__ == "ValueError" and str(e) == "withheld": + return {"error": "To download withheld accounts, use this userscript version: https://www.patreon.com/exyezed"} + else: + error_str = str(e) + if error_str == "None": + return {"error": "Failed to authenticate. Please verify your auth token is valid and not expired."} + else: + return {"error": error_str} + +def get_metadata(username, auth_token, timeline_type="timeline", batch_size=0, page=0, media_type="all", retweets=False): url = f"https://x.com/{username}/{timeline_type}" if timeline_type == "media": @@ -24,7 +160,8 @@ def get_metadata(username, auth_token, timeline_type="timeline", batch_size=0, p config_dict = { "cookies": { "auth_token": auth_token - } + }, + "retweets": retweets } if batch_size > 0: @@ -117,6 +254,12 @@ def get_metadata(username, auth_token, timeline_type="timeline", batch_size=0, p if 'type' in tweet_data: timeline_entry['type'] = tweet_data['type'] + if 'retweet_id' in tweet_data and tweet_data['retweet_id']: + timeline_entry['retweet_id'] = tweet_data['retweet_id'] + timeline_entry['is_retweet'] = True + else: + timeline_entry['is_retweet'] = False + if media_type == 'all' or ( (media_type == 'image' and 'pbs.twimg.com' in media_url and tweet_data.get('type') == 'photo') or (media_type == 'video' and 'video.twimg.com' in media_url and tweet_data.get('type') == 'video') or diff --git a/core/metadataWorker.py b/core/metadataWorker.py new file mode 100644 index 0000000..4b2de1b --- /dev/null +++ b/core/metadataWorker.py @@ -0,0 +1,60 @@ +from PyQt6.QtCore import QThread, pyqtSignal +from core.metadataFetcher import get_metadata, get_metadata_by_date + +class MetadataFetchWorker(QThread): + finished = pyqtSignal(dict) + error = pyqtSignal(str) + + def __init__(self, username, media_type='all', batch_mode=False, batch_size=0, page=0, timeline_type='media', include_retweets=False, use_date_range=False, date_start=None, date_end=None): + super().__init__() + self.username = username + self.media_type = media_type + self.batch_mode = batch_mode + self.batch_size = batch_size + self.page = page + self.timeline_type = timeline_type + self.include_retweets = include_retweets + self.use_date_range = use_date_range + self.date_start = date_start + self.date_end = date_end + self.auth_token = None + + 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 + + def run(self): + try: + normalized = self.normalize_url(self.username) + if self.use_date_range and self.date_start and self.date_end: + data = get_metadata_by_date( + username=normalized, + auth_token=self.auth_token, + date_start=self.date_start, + date_end=self.date_end, + media_filter="filter:media" if self.media_type == 'all' else f"filter:{self.media_type}" + ) + else: + data = get_metadata( + username=normalized, + auth_token=self.auth_token, + timeline_type=self.timeline_type, + batch_size=self.batch_size if self.batch_mode else 0, + page=self.page, + media_type=self.media_type, + retweets=self.include_retweets + ) + self.finished.emit(data) + except Exception as e: + self.error.emit(str(e)) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 58c6c56..3271b9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,8 @@ -PyQt6 +PyQt6==6.9.1 requests aiohttp imageio-ffmpeg packaging -gallery-dl \ No newline at end of file +gallery-dl +pyqtdarktheme +pyinstaller \ No newline at end of file diff --git a/ui/__init__.py b/ui/__init__.py new file mode 100644 index 0000000..9c2fde2 --- /dev/null +++ b/ui/__init__.py @@ -0,0 +1,7 @@ +from ui.mainWindow import TwitterXMediaBatchDownloaderGUI +from ui.dialogs import UpdateDialog + +__all__ = [ + 'TwitterXMediaBatchDownloaderGUI', + 'UpdateDialog' +] diff --git a/ui/dialogs.py b/ui/dialogs.py new file mode 100644 index 0000000..2bd6f45 --- /dev/null +++ b/ui/dialogs.py @@ -0,0 +1,33 @@ +from PyQt6.QtWidgets import ( + QDialog, QVBoxLayout, QLabel, QPushButton, QDialogButtonBox +) +from PyQt6.QtCore import Qt + +class UpdateDialog(QDialog): + def __init__(self, current_version, new_version, parent=None): + super().__init__(parent) + self.setWindowTitle("Update Now") + self.setFixedWidth(400) + self.setModal(True) + + layout = QVBoxLayout() + + message = QLabel(f"Twitter/X Media Batch Downloader v{new_version} Available!") + message.setWordWrap(True) + layout.addWidget(message) + + button_box = QDialogButtonBox() + self.update_button = QPushButton("Check") + self.update_button.setCursor(Qt.CursorShape.PointingHandCursor) + self.cancel_button = QPushButton("Later") + self.cancel_button.setCursor(Qt.CursorShape.PointingHandCursor) + + button_box.addButton(self.update_button, QDialogButtonBox.ButtonRole.AcceptRole) + button_box.addButton(self.cancel_button, QDialogButtonBox.ButtonRole.RejectRole) + + layout.addWidget(button_box) + + self.setLayout(layout) + + self.update_button.clicked.connect(self.accept) + self.cancel_button.clicked.connect(self.reject) diff --git a/ui/icons/cancel.svg b/ui/icons/cancel.svg new file mode 100644 index 0000000..44512a6 --- /dev/null +++ b/ui/icons/cancel.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/icons/database-export.svg b/ui/icons/database-export.svg similarity index 100% rename from icons/database-export.svg rename to ui/icons/database-export.svg diff --git a/icons/database-import.svg b/ui/icons/database-import.svg similarity index 100% rename from icons/database-import.svg rename to ui/icons/database-import.svg diff --git a/ui/icons/device-floppy.svg b/ui/icons/device-floppy.svg new file mode 100644 index 0000000..642df27 --- /dev/null +++ b/ui/icons/device-floppy.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/icons/download.svg b/ui/icons/download.svg similarity index 100% rename from icons/download.svg rename to ui/icons/download.svg diff --git a/ui/icons/edit.svg b/ui/icons/edit.svg new file mode 100644 index 0000000..5ec6d50 --- /dev/null +++ b/ui/icons/edit.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/icons/icon.icns b/ui/icons/icon.icns similarity index 100% rename from icons/icon.icns rename to ui/icons/icon.icns diff --git a/icons/icon.ico b/ui/icons/icon.ico similarity index 100% rename from icons/icon.ico rename to ui/icons/icon.ico diff --git a/icons/icon.svg b/ui/icons/icon.svg similarity index 100% rename from icons/icon.svg rename to ui/icons/icon.svg diff --git a/ui/icons/player-pause.svg b/ui/icons/player-pause.svg new file mode 100644 index 0000000..126df9e --- /dev/null +++ b/ui/icons/player-pause.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/icons/player-play.svg b/ui/icons/player-play.svg new file mode 100644 index 0000000..a876c03 --- /dev/null +++ b/ui/icons/player-play.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/icons/reload.svg b/ui/icons/reload.svg similarity index 100% rename from icons/reload.svg rename to ui/icons/reload.svg diff --git a/icons/trash.svg b/ui/icons/trash.svg similarity index 100% rename from icons/trash.svg rename to ui/icons/trash.svg diff --git a/ui/mainWindow.py b/ui/mainWindow.py new file mode 100644 index 0000000..bd9c94c --- /dev/null +++ b/ui/mainWindow.py @@ -0,0 +1,3168 @@ +import sys +import os +import requests +import json +from datetime import datetime +from pathlib import Path +from packaging import version +import qdarktheme +from PyQt6.QtWidgets import ( + QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, + QLabel, QFileDialog, QListWidget, QTextEdit, QTabWidget, QAbstractItemView, QProgressBar, QCheckBox, QDialog, + QDialogButtonBox, QComboBox, QListWidgetItem, QMessageBox, QDateEdit, QInputDialog +) +from PyQt6.QtCore import Qt, QThread, pyqtSignal, QUrl, QTimer, QTime, QSettings, QSize, QDate +from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest +from PyQt6.QtGui import QIcon, QTextCursor, QDesktopServices, QPixmap, QPainter, QPainterPath +from PyQt6.QtSvg import QSvgRenderer + +from core.databaseManager import DatabaseManager +from core.metadataWorker import MetadataFetchWorker +from core.downloadWorker import DownloadWorker +from core.accountModel import Account +from ui.dialogs import UpdateDialog + +ICON_DIR = os.path.join(os.path.dirname(__file__), 'icons') + +class TwitterXMediaBatchDownloaderGUI(QWidget): + def __init__(self): + super().__init__() + self.current_version = "3.8" + self.accounts = [] + self.temp_dir = os.path.join(Path.home(), ".twitterxmediabatchdownloader") + os.makedirs(self.temp_dir, exist_ok=True) + + db_path = os.path.join(self.temp_dir, "twitterxmediabatchdownloader.db") + self.db_manager = DatabaseManager(db_path) + + self.migrate_json_to_sqlite() + + self.reset_state() + + self.settings = QSettings('TwitterXMediaBatchDownloader', 'Settings') + self.last_output_path = self.settings.value('output_path', str(Path.home() / "Pictures")) + self.last_url = self.settings.value('twitter_url', '') + self.last_auth_token = self.settings.value('auth_token', '') + self.filename_format = self.settings.value('filename_format', 'username_date') + self.download_batch_size = self.settings.value('download_batch_size', 25, type=int) + self.batch_mode = self.settings.value('batch_mode', False, type=bool) + self.batch_size = self.settings.value('batch_size', 100, type=int) + self.timeline_type = self.settings.value('timeline_type', 'timeline') + self.media_type = self.settings.value('media_type', 'all') + self.include_retweets = self.settings.value('include_retweets', False, type=bool) + self.convert_gif = self.settings.value('convert_gif', False, type=bool) + self.gif_resolution = self.settings.value('gif_resolution', 'original') + self.gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') + self.check_for_updates = self.settings.value('check_for_updates', True, type=bool) + self.current_theme_color = self.settings.value('theme_color', '#2196F3') + self.last_group_filter = self.settings.value('group_filter', None) + + self.is_auto_fetching = False + self.current_fetch_username = None + self.current_fetch_media_type = None + self.current_fetch_metadata = None + self.is_multiple_user_mode = False + self.is_initializing = True + + self.profile_image_cache = {} + self.pending_downloads = {} + self.network_manager = QNetworkAccessManager() + + self.elapsed_time = QTime(0, 0, 0) + self.timer = QTimer(self) + self.timer.timeout.connect(self.update_timer) + self.initUI() + self.load_settings() + self.load_all_cached_accounts() + self.restore_filter_preferences() + self.is_initializing = False + + if self.check_for_updates: + QTimer.singleShot(0, self.check_updates) + + def get_time_ago(self, timestamp_str): + if not timestamp_str: + return "Unknown" + + try: + fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) + now = datetime.now(fetch_time.tzinfo) if fetch_time.tzinfo else datetime.now() + + diff = now - fetch_time + days = diff.days + hours = diff.seconds // 3600 + minutes = (diff.seconds % 3600) // 60 + + if days > 0: + return f"{days} day{'s' if days != 1 else ''} ago" + elif hours > 0: + return f"{hours} hour{'s' if hours != 1 else ''} ago" + elif minutes > 0: + return f"{minutes} minute{'s' if minutes != 1 else ''} ago" + else: + return "Just now" + except (ValueError, TypeError, AttributeError): + return "Unknown" + + def format_fetch_info(self, timestamp_str): + if not timestamp_str: + return "Fetched: Unknown" + + try: + fetch_time = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) + formatted_datetime = fetch_time.strftime("%Y/%m/%d • %H:%M") + age = self.get_time_ago(timestamp_str) + return f"Fetched: {formatted_datetime} ({age})" + except (ValueError, TypeError, AttributeError): + return "Fetched: Unknown" + + def check_updates(self): + try: + response = requests.get("https://raw.githubusercontent.com/afkarxyz/Twitter-X-Media-Batch-Downloader/refs/heads/main/version.json") + if response.status_code == 200: + data = response.json() + new_version = data.get("version") + + if new_version and version.parse(new_version) > version.parse(self.current_version): + dialog = UpdateDialog(self.current_version, new_version, self) + result = dialog.exec() + + if result == QDialog.DialogCode.Accepted: + QDesktopServices.openUrl(QUrl("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases")) + + except Exception as e: + pass + + def reset_state(self): + self.accounts.clear() + + def reset_ui(self): + self.account_list.clear() + self.log_output.clear() + self.progress_bar.setValue(0) + self.progress_bar.hide() + self.stop_btn.hide() + self.pause_resume_btn.hide() + self.pause_resume_btn.setText(' Pause') + icon_dir = ICON_DIR + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), self.current_theme_color)) + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + self.is_auto_fetching = False + self.is_multiple_user_mode = False + self.enable_batch_buttons() + self.hide_account_buttons() + + def initUI(self): + self.setWindowTitle('Twitter/X Media Batch Downloader') + self.setFixedWidth(650) + self.setMinimumHeight(350) + + icon_path = os.path.join(ICON_DIR, "icon.svg") + if os.path.exists(icon_path): + self.setWindowIcon(QIcon(icon_path)) + + self.main_layout = QVBoxLayout() + + self.setup_twitter_section() + self.setup_tabs() + + self.setLayout(self.main_layout) + + def setup_twitter_section(self): + twitter_layout = QHBoxLayout() + twitter_label = QLabel('Username/URL:') + twitter_label.setFixedWidth(100) + + self.twitter_url = QLineEdit() + self.twitter_url.setPlaceholderText("e.g. Takomayuyi or https://x.com/Takomayuyi or user1,user2,user3") + self.twitter_url.setClearButtonEnabled(True) + self.twitter_url.setText(self.last_url) + self.twitter_url.textChanged.connect(self.save_url) + self.fetch_btn = QPushButton('Fetch') + self.fetch_btn.setFixedWidth(80) + self.fetch_btn.setCursor(Qt.CursorShape.PointingHandCursor) + self.fetch_btn.clicked.connect(self.fetch_account) + + twitter_layout.addWidget(twitter_label) + twitter_layout.addWidget(self.twitter_url) + twitter_layout.addWidget(self.fetch_btn) + self.main_layout.addLayout(twitter_layout) + + date_range_layout = QHBoxLayout() + date_range_label = QLabel('Date Range:') + date_range_label.setFixedWidth(100) + + start_date_label = QLabel('From:') + self.start_date_edit = QDateEdit() + self.start_date_edit.setCalendarPopup(True) + self.start_date_edit.setDisplayFormat("yyyy-MM-dd") + self.start_date_edit.setDate(QDate.currentDate().addMonths(-1)) + self.start_date_edit.setMinimumWidth(120) + + end_date_label = QLabel('To:') + self.end_date_edit = QDateEdit() + self.end_date_edit.setCalendarPopup(True) + self.end_date_edit.setDisplayFormat("yyyy-MM-dd") + self.end_date_edit.setDate(QDate.currentDate()) + self.end_date_edit.setMinimumWidth(120) + + self.use_date_range_checkbox = QCheckBox('Use Date Range') + self.use_date_range_checkbox.setChecked(False) + self.use_date_range_checkbox.toggled.connect(self.toggle_date_range) + + date_range_layout.addWidget(date_range_label) + date_range_layout.addWidget(self.use_date_range_checkbox) + date_range_layout.addWidget(start_date_label) + date_range_layout.addWidget(self.start_date_edit) + date_range_layout.addWidget(end_date_label) + date_range_layout.addWidget(self.end_date_edit) + date_range_layout.addStretch() + self.main_layout.addLayout(date_range_layout) + + self.start_date_edit.setEnabled(False) + self.end_date_edit.setEnabled(False) + + def setup_tabs(self): + self.tab_widget = QTabWidget() + self.tab_widget.currentChanged.connect(self.on_tab_changed) + self.main_layout.addWidget(self.tab_widget) + + self.setup_dashboard_tab() + self.setup_group_tab() + self.setup_process_tab() + self.setup_settings_tab() + self.setup_theme_tab() + self.setup_about_tab() + + def setup_dashboard_tab(self): + dashboard_tab = QWidget() + dashboard_layout = QVBoxLayout() + + search_sort_layout = QHBoxLayout() + + self.search_input = QLineEdit() + self.search_input.setPlaceholderText("Search accounts...") + self.search_input.textChanged.connect(self.filter_accounts) + self.search_input.setClearButtonEnabled(True) + search_sort_layout.addWidget(self.search_input, 3) + + group_label = QLabel("Group:") + search_sort_layout.addWidget(group_label) + + self.group_filter_combo = QComboBox() + self.group_filter_combo.addItem("All Groups", None) + self.group_filter_combo.addItem("No Group", -1) + self.group_filter_combo.currentIndexChanged.connect(self.on_group_filter_changed) + self.group_filter_combo.setCursor(Qt.CursorShape.PointingHandCursor) + search_sort_layout.addWidget(self.group_filter_combo, 1) + + sort_label = QLabel("Sort by:") + search_sort_layout.addWidget(sort_label) + + self.sort_combo = QComboBox() + self.sort_combo.addItem("Latest Fetch", "fetch_timestamp_desc") + self.sort_combo.addItem("Oldest Fetch", "fetch_timestamp_asc") + self.sort_combo.addItem("Username (A-Z)", "username_asc") + self.sort_combo.addItem("Username (Z-A)", "username_desc") + self.sort_combo.addItem("Followers (High-Low)", "followers_desc") + self.sort_combo.addItem("Followers (Low-High)", "followers_asc") + self.sort_combo.addItem("Posts (High-Low)", "posts_desc") + self.sort_combo.addItem("Posts (Low-High)", "posts_asc") + self.sort_combo.addItem("Media Count (High-Low)", "media_count_desc") + self.sort_combo.addItem("Media Count (Low-High)", "media_count_asc") + self.sort_combo.currentIndexChanged.connect(self.on_sort_changed) + self.sort_combo.setCursor(Qt.CursorShape.PointingHandCursor) + search_sort_layout.addWidget(self.sort_combo, 2) + + dashboard_layout.addLayout(search_sort_layout) + + self.account_list = QListWidget() + self.account_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) + self.account_list.itemSelectionChanged.connect(self.update_button_states) + self.account_list.setIconSize(QSize(48, 48)) + self.account_list.setStyleSheet(""" + QListWidget { + padding: 4px; + outline: none; + } + QListWidget::item { + padding: 3px 6px; + margin: 0px 0px; + border: none; + outline: none; + } + QListWidget::item:selected { + border: none; + outline: none; + } + QListWidget::item:focus { + border: none; + outline: none; + } + """) + + dashboard_layout.addWidget(self.account_list) + + self.setup_account_buttons() + dashboard_layout.addLayout(self.btn_layout) + dashboard_tab.setLayout(dashboard_layout) + self.tab_widget.addTab(dashboard_tab, "Dashboard") + + self.hide_account_buttons() + + def setup_account_buttons(self): + self.btn_layout = QHBoxLayout() + self.import_btn = QPushButton(' Import') + self.export_btn = QPushButton(' Export') + self.download_selected_btn = QPushButton(' Download') + self.update_selected_btn = QPushButton(' Update') + self.delete_btn = QPushButton(' Delete') + + icon_dir = ICON_DIR + accent_color = self.current_theme_color + + self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), accent_color)) + self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), accent_color)) + self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), accent_color)) + self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), accent_color)) + self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), accent_color)) + + for btn in [self.import_btn, self.export_btn, self.download_selected_btn, + self.update_selected_btn, self.delete_btn]: + btn.setMinimumWidth(100) + btn.setCursor(Qt.CursorShape.PointingHandCursor) + btn.setIconSize(QSize(18, 18)) + + self.import_btn.clicked.connect(self.import_accounts) + self.export_btn.clicked.connect(self.export_accounts) + self.download_selected_btn.clicked.connect(self.download_selected) + self.update_selected_btn.clicked.connect(self.update_selected) + self.delete_btn.clicked.connect(self.delete_accounts) + + self.btn_layout.addStretch() + for btn in [self.import_btn, self.export_btn, self.download_selected_btn, + self.update_selected_btn, self.delete_btn]: + self.btn_layout.addWidget(btn, 1) + self.btn_layout.addStretch() + + def update_button_icons(self): + if not hasattr(self, 'import_btn'): + return + + icon_dir = ICON_DIR + accent_color = self.current_theme_color + + self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'), accent_color)) + self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'), accent_color)) + self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'), accent_color)) + self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'), accent_color)) + self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), accent_color)) + + if hasattr(self, 'rename_group_btn'): + self.rename_group_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'edit.svg'), accent_color)) + if hasattr(self, 'delete_group_btn'): + self.delete_group_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), accent_color)) + if hasattr(self, 'assign_to_group_btn'): + self.assign_to_group_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'category-plus.svg'), accent_color)) + if hasattr(self, 'save_assignment_btn'): + self.save_assignment_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'device-floppy.svg'), accent_color)) + + if hasattr(self, 'stop_btn'): + self.stop_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'cancel.svg'), accent_color)) + if hasattr(self, 'pause_resume_btn'): + if self.pause_resume_btn.text() == ' Resume': + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'), accent_color)) + else: + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), accent_color)) + if hasattr(self, 'stop_fetch_btn'): + self.stop_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'cancel.svg'), accent_color)) + if hasattr(self, 'pause_fetch_btn'): + if self.pause_fetch_btn.text() == ' Resume': + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'), accent_color)) + else: + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), accent_color)) + + def setup_group_tab(self): + group_tab = QWidget() + main_layout = QVBoxLayout() + main_layout.setSpacing(5) + main_layout.setContentsMargins(9, 9, 9, 9) + + content_layout = QHBoxLayout() + + left_panel = QWidget() + left_layout = QVBoxLayout(left_panel) + left_layout.setContentsMargins(0, 0, 0, 0) + left_layout.setSpacing(5) + + group_label = QLabel("Groups:") + left_layout.addWidget(group_label) + + create_group_layout = QHBoxLayout() + self.group_name_input = QLineEdit() + self.group_name_input.setPlaceholderText("Enter group name...") + self.create_group_btn = QPushButton("Create") + self.create_group_btn.setFixedWidth(80) + self.create_group_btn.setCursor(Qt.CursorShape.PointingHandCursor) + self.create_group_btn.clicked.connect(self.create_group) + create_group_layout.addWidget(self.group_name_input) + create_group_layout.addWidget(self.create_group_btn) + left_layout.addLayout(create_group_layout) + + self.group_list = QListWidget() + self.group_list.itemSelectionChanged.connect(self.on_group_selected) + left_layout.addWidget(self.group_list, 1) + + group_btn_layout = QHBoxLayout() + group_btn_layout.setSpacing(5) + + self.rename_group_btn = QPushButton(" Rename") + self.delete_group_btn = QPushButton(" Delete") + + icon_dir = ICON_DIR + accent_color = self.current_theme_color + + self.rename_group_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'edit.svg'), accent_color)) + self.delete_group_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'), accent_color)) + + for btn in [self.rename_group_btn, self.delete_group_btn]: + btn.setCursor(Qt.CursorShape.PointingHandCursor) + btn.setIconSize(QSize(18, 18)) + btn.setEnabled(False) + + self.rename_group_btn.clicked.connect(self.rename_group) + self.delete_group_btn.clicked.connect(self.delete_group) + + group_btn_layout.addWidget(self.rename_group_btn) + group_btn_layout.addWidget(self.delete_group_btn) + left_layout.addLayout(group_btn_layout) + + content_layout.addWidget(left_panel, 1) + + right_panel = QWidget() + right_layout = QVBoxLayout(right_panel) + right_layout.setContentsMargins(0, 0, 0, 0) + right_layout.setSpacing(5) + + assign_label = QLabel("Assign Accounts:") + right_layout.addWidget(assign_label) + + self.assign_search_input = QLineEdit() + self.assign_search_input.setPlaceholderText("Search accounts...") + self.assign_search_input.setClearButtonEnabled(True) + self.assign_search_input.textChanged.connect(self.filter_assign_accounts) + right_layout.addWidget(self.assign_search_input) + + self.assign_account_list = QListWidget() + self.assign_account_list.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection) + self.assign_account_list.itemChanged.connect(self.on_assign_item_changed) + right_layout.addWidget(self.assign_account_list, 1) + + assign_btn_layout = QHBoxLayout() + self.save_assignment_btn = QPushButton(" Save Assignment") + self.save_assignment_btn.setCursor(Qt.CursorShape.PointingHandCursor) + self.save_assignment_btn.setEnabled(False) + self.save_assignment_btn.clicked.connect(self.save_account_assignment) + + icon_dir = ICON_DIR + self.save_assignment_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'device-floppy.svg'), accent_color)) + self.save_assignment_btn.setIconSize(QSize(18, 18)) + + assign_btn_layout.addStretch() + assign_btn_layout.addWidget(self.save_assignment_btn) + assign_btn_layout.addStretch() + right_layout.addLayout(assign_btn_layout) + + content_layout.addWidget(right_panel, 1) + + main_layout.addLayout(content_layout) + + group_tab.setLayout(main_layout) + self.tab_widget.addTab(group_tab, "Group") + + self.load_groups() + + def setup_process_tab(self): + self.process_tab = QWidget() + process_layout = QVBoxLayout() + process_layout.setSpacing(5) + + self.log_output = QTextEdit() + self.log_output.setReadOnly(True) + process_layout.addWidget(self.log_output) + + progress_time_layout = QVBoxLayout() + progress_time_layout.setSpacing(2) + + self.progress_bar = QProgressBar() + progress_time_layout.addWidget(self.progress_bar) + + self.time_label = QLabel("00:00:00") + self.time_label.setAlignment(Qt.AlignmentFlag.AlignCenter) + progress_time_layout.addWidget(self.time_label) + + process_layout.addLayout(progress_time_layout) + + download_control_layout = QHBoxLayout() + self.stop_btn = QPushButton(' Stop') + self.pause_resume_btn = QPushButton(' Pause') + + icon_dir = ICON_DIR + accent_color = self.current_theme_color + + self.stop_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'cancel.svg'), accent_color)) + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), accent_color)) + + self.stop_btn.setFixedWidth(120) + self.pause_resume_btn.setFixedWidth(120) + self.stop_btn.setCursor(Qt.CursorShape.PointingHandCursor) + self.pause_resume_btn.setCursor(Qt.CursorShape.PointingHandCursor) + self.stop_btn.setIconSize(QSize(18, 18)) + self.pause_resume_btn.setIconSize(QSize(18, 18)) + + self.stop_btn.clicked.connect(self.stop_download) + self.pause_resume_btn.clicked.connect(self.toggle_pause_resume) + + download_control_layout.addStretch() + download_control_layout.addWidget(self.stop_btn) + download_control_layout.addWidget(self.pause_resume_btn) + download_control_layout.addStretch() + process_layout.addLayout(download_control_layout) + + batch_control_layout = QHBoxLayout() + self.pause_fetch_btn = QPushButton(' Pause') + self.stop_fetch_btn = QPushButton(' Stop') + + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), accent_color)) + self.stop_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'cancel.svg'), accent_color)) + + for btn in [self.pause_fetch_btn, self.stop_fetch_btn]: + btn.setFixedWidth(120) + btn.setCursor(Qt.CursorShape.PointingHandCursor) + btn.setIconSize(QSize(18, 18)) + + self.pause_fetch_btn.setToolTip("Pause batch fetching (can resume)") + self.stop_fetch_btn.setToolTip("Stop batch fetching completely") + + self.pause_fetch_btn.clicked.connect(self.pause_batch_fetch) + self.stop_fetch_btn.clicked.connect(self.stop_batch_fetch) + + batch_control_layout.addStretch() + batch_control_layout.addWidget(self.pause_fetch_btn) + batch_control_layout.addWidget(self.stop_fetch_btn) + batch_control_layout.addStretch() + process_layout.addLayout(batch_control_layout) + + self.process_tab.setLayout(process_layout) + + self.tab_widget.addTab(self.process_tab, "Process") + + self.progress_bar.hide() + self.time_label.hide() + self.stop_btn.hide() + self.pause_resume_btn.hide() + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + def setup_settings_tab(self): + settings_tab = QWidget() + settings_layout = QVBoxLayout() + settings_layout.setSpacing(0) + settings_layout.setContentsMargins(9, 9, 9, 9) + + output_group = QWidget() + output_layout = QVBoxLayout(output_group) + output_layout.setSpacing(5) + + output_label = QLabel('Output Directory') + output_label.setStyleSheet("font-weight: bold;") + output_layout.addWidget(output_label) + + output_dir_layout = QHBoxLayout() + self.output_dir = QLineEdit() + self.output_dir.setText(self.last_output_path) + self.output_dir.textChanged.connect(self.save_settings) + self.output_browse = QPushButton('Browse') + self.output_browse.setFixedWidth(80) + self.output_browse.setCursor(Qt.CursorShape.PointingHandCursor) + self.output_browse.clicked.connect(self.browse_output) + + output_dir_layout.addWidget(self.output_dir) + output_dir_layout.addWidget(self.output_browse) + output_layout.addLayout(output_dir_layout) + + settings_layout.addWidget(output_group) + + auth_group = QWidget() + auth_layout = QVBoxLayout(auth_group) + auth_layout.setSpacing(5) + + auth_label = QLabel('Authentication') + auth_label.setStyleSheet("font-weight: bold;") + auth_layout.addWidget(auth_label) + + auth_token_layout = QHBoxLayout() + auth_token_label = QLabel('Auth Token:') + + self.auth_token_input = QLineEdit() + self.auth_token_input.setPlaceholderText("Enter Auth Token") + self.auth_token_input.setText(self.last_auth_token) + self.auth_token_input.textChanged.connect(self.save_settings) + self.auth_token_input.setClearButtonEnabled(True) + + auth_token_layout.addWidget(auth_token_label) + auth_token_layout.addWidget(self.auth_token_input) + auth_layout.addLayout(auth_token_layout) + + settings_layout.addWidget(auth_group) + + gallery_dl_group = QWidget() + gallery_dl_layout = QVBoxLayout(gallery_dl_group) + gallery_dl_layout.setSpacing(5) + + gallery_dl_label = QLabel('gallery-dl Settings') + gallery_dl_label.setStyleSheet("font-weight: bold;") + gallery_dl_layout.addWidget(gallery_dl_label) + + first_row_layout = QHBoxLayout() + first_row_layout.setSpacing(5) + + timeline_label = QLabel("Timeline:") + self.timeline_type_combo = QComboBox() + self.timeline_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) + self.timeline_type_combo.setFixedWidth(75) + timeline_types = [ + ('media', 'Media'), + ('timeline', 'Post'), + ('tweets', 'Tweets'), + ('with_replies', 'Replies') + ] + for value, display in timeline_types: + self.timeline_type_combo.addItem(display, value) + self.timeline_type_combo.currentTextChanged.connect(self.save_settings) + first_row_layout.addWidget(timeline_label) + first_row_layout.addWidget(self.timeline_type_combo) + + first_row_layout.addSpacing(5) + + media_label = QLabel("Media:") + self.media_type_combo = QComboBox() + self.media_type_combo.setCursor(Qt.CursorShape.PointingHandCursor) + self.media_type_combo.setFixedWidth(65) + media_types = [('all', 'All'), ('image', 'Image'), ('video', 'Video'), ('gif', 'GIF')] + for value, display in media_types: + self.media_type_combo.addItem(display, value) + self.media_type_combo.currentTextChanged.connect(self.save_settings) + first_row_layout.addWidget(media_label) + first_row_layout.addWidget(self.media_type_combo) + + first_row_layout.addSpacing(5) + + self.retweets_checkbox = QCheckBox("Include Retweets") + self.retweets_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) + self.retweets_checkbox.setToolTip("Include retweets (not available for Media timeline)") + self.retweets_checkbox.stateChanged.connect(self.handle_retweets_checkbox) + first_row_layout.addWidget(self.retweets_checkbox) + + first_row_layout.addSpacing(5) + + self.batch_checkbox = QCheckBox("Fetch Batch") + self.batch_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) + self.batch_checkbox.setToolTip("Enable for accounts with thousands of media") + self.batch_checkbox.stateChanged.connect(self.handle_batch_checkbox) + self.batch_checkbox.setChecked(self.batch_mode) + first_row_layout.addWidget(self.batch_checkbox) + + self.size_label = QLabel("Size:") + first_row_layout.addWidget(self.size_label) + self.batch_size_combo = QComboBox() + self.batch_size_combo.setCursor(Qt.CursorShape.PointingHandCursor) + self.batch_size_combo.setFixedWidth(60) + for size in [50, 100, 150, 200]: + self.batch_size_combo.addItem(str(size)) + self.batch_size_combo.setCurrentIndex(1) + self.batch_size_combo.currentTextChanged.connect(self.save_batch_size) + first_row_layout.addWidget(self.batch_size_combo) + + self.size_label.hide() + self.batch_size_combo.hide() + + first_row_layout.addStretch() + gallery_dl_layout.addLayout(first_row_layout) + + settings_layout.addWidget(gallery_dl_group) + + download_conversion_group = QWidget() + download_conversion_layout = QVBoxLayout(download_conversion_group) + download_conversion_layout.setSpacing(5) + + labels_layout = QHBoxLayout() + + download_label_container = QWidget() + download_label_layout = QHBoxLayout(download_label_container) + download_label_layout.setContentsMargins(0, 0, 0, 0) + download_label = QLabel('Download Settings') + download_label.setStyleSheet("font-weight: bold;") + download_label_layout.addWidget(download_label) + download_label_layout.addStretch() + download_label_container.setFixedWidth(180) + labels_layout.addWidget(download_label_container) + + conversion_label = QLabel('Conversion Settings') + conversion_label.setStyleSheet("font-weight: bold;") + labels_layout.addWidget(conversion_label) + labels_layout.addStretch() + download_conversion_layout.addLayout(labels_layout) + + controls_layout = QHBoxLayout() + + download_controls_container = QWidget() + download_controls_layout = QHBoxLayout(download_controls_container) + download_controls_layout.setContentsMargins(0, 0, 0, 0) + + batch_label = QLabel('Concurrents:') + download_controls_layout.addWidget(batch_label) + + self.download_batch_combo = QComboBox() + self.download_batch_combo.setCursor(Qt.CursorShape.PointingHandCursor) + self.download_batch_combo.setFixedWidth(80) + self.download_batch_combo.addItem("1") + for size in range(5, 101, 5): + self.download_batch_combo.addItem(str(size)) + self.download_batch_combo.setCurrentText(str(self.download_batch_size)) + self.download_batch_combo.currentTextChanged.connect(self.save_settings) + download_controls_layout.addWidget(self.download_batch_combo) + download_controls_layout.addStretch() + + download_controls_container.setFixedWidth(180) + controls_layout.addWidget(download_controls_container) + + self.convert_gif_checkbox = QCheckBox("Convert GIF") + self.convert_gif_checkbox.setCursor(Qt.CursorShape.PointingHandCursor) + self.convert_gif_checkbox.setChecked(self.convert_gif) + self.convert_gif_checkbox.toggled.connect(self.handle_conversion_checkbox) + controls_layout.addWidget(self.convert_gif_checkbox) + + controls_layout.addSpacing(5) + + mode_label = QLabel("Mode:") + self.conversion_mode_combo = QComboBox() + self.conversion_mode_combo.setCursor(Qt.CursorShape.PointingHandCursor) + self.conversion_mode_combo.setFixedWidth(80) + conversion_modes = [('better', 'Better'), ('fast', 'Fast')] + for value, display in conversion_modes: + self.conversion_mode_combo.addItem(display, value) + self.conversion_mode_combo.currentTextChanged.connect(self.save_settings) + controls_layout.addWidget(mode_label) + controls_layout.addWidget(self.conversion_mode_combo) + + controls_layout.addSpacing(5) + + quality_label = QLabel("Quality:") + self.conversion_quality_combo = QComboBox() + self.conversion_quality_combo.setCursor(Qt.CursorShape.PointingHandCursor) + self.conversion_quality_combo.setFixedWidth(85) + gif_qualities = [ + ('original', 'Original'), + ('high', 'High'), + ('medium', 'Medium'), + ('low', 'Low') + ] + for value, display in gif_qualities: + self.conversion_quality_combo.addItem(display, value) + self.conversion_quality_combo.currentTextChanged.connect(self.save_settings) + controls_layout.addWidget(quality_label) + controls_layout.addWidget(self.conversion_quality_combo) + + self.mode_label = mode_label + self.quality_label = quality_label + + if not self.convert_gif: + mode_label.hide() + self.conversion_mode_combo.hide() + quality_label.hide() + self.conversion_quality_combo.hide() + + controls_layout.addStretch() + download_conversion_layout.addLayout(controls_layout) + + settings_layout.addWidget(download_conversion_group) + settings_layout.addStretch() + + settings_tab.setLayout(settings_layout) + self.tab_widget.addTab(settings_tab, "Settings") + + def setup_theme_tab(self): + theme_tab = QWidget() + theme_layout = QVBoxLayout() + theme_layout.setSpacing(8) + theme_layout.setContentsMargins(8, 15, 15, 15) + + grid_layout = QVBoxLayout() + + self.color_buttons = {} + + first_row_palettes = [ + ("Red", [ + ("#FFCDD2", "100"), ("#EF9A9A", "200"), ("#E57373", "300"), ("#EF5350", "400"), ("#F44336", "500"), ("#E53935", "600"), ("#D32F2F", "700"), ("#C62828", "800"), ("#B71C1C", "900"), ("#FF8A80", "A100"), ("#FF5252", "A200"), ("#FF1744", "A400"), ("#D50000", "A700") + ]), + ("Pink", [ + ("#F8BBD0", "100"), ("#F48FB1", "200"), ("#F06292", "300"), ("#EC407A", "400"), ("#E91E63", "500"), ("#D81B60", "600"), ("#C2185B", "700"), ("#AD1457", "800"), ("#880E4F", "900"), ("#FF80AB", "A100"), ("#FF4081", "A200"), ("#F50057", "A400"), ("#C51162", "A700") + ]), + ("Purple", [ + ("#E1BEE7", "100"), ("#CE93D8", "200"), ("#BA68C8", "300"), ("#AB47BC", "400"), ("#9C27B0", "500"), ("#8E24AA", "600"), ("#7B1FA2", "700"), ("#6A1B9A", "800"), ("#4A148C", "900"), ("#EA80FC", "A100"), ("#E040FB", "A200"), ("#D500F9", "A400"), ("#AA00FF", "A700") + ]) + ] + + second_row_palettes = [ + ("Deep Purple", [ + ("#D1C4E9", "100"), ("#B39DDB", "200"), ("#9575CD", "300"), ("#7E57C2", "400"), ("#673AB7", "500"), ("#5E35B1", "600"), ("#512DA8", "700"), ("#4527A0", "800"), ("#311B92", "900"), ("#B388FF", "A100"), ("#7C4DFF", "A200"), ("#651FFF", "A400"), ("#6200EA", "A700") + ]), + ("Indigo", [ + ("#C5CAE9", "100"), ("#9FA8DA", "200"), ("#7986CB", "300"), ("#5C6BC0", "400"), ("#3F51B5", "500"), ("#3949AB", "600"), ("#303F9F", "700"), ("#283593", "800"), ("#1A237E", "900"), ("#8C9EFF", "A100"), ("#536DFE", "A200"), ("#3D5AFE", "A400"), ("#304FFE", "A700") + ]), + ("Blue", [ + ("#BBDEFB", "100"), ("#90CAF9", "200"), ("#64B5F6", "300"), ("#42A5F5", "400"), ("#2196F3", "500"), ("#1E88E5", "600"), ("#1976D2", "700"), ("#1565C0", "800"), ("#0D47A1", "900"), ("#82B1FF", "A100"), ("#448AFF", "A200"), ("#2979FF", "A400"), ("#2962FF", "A700") + ]) + ] + + third_row_palettes = [ + ("Light Blue", [ + ("#B3E5FC", "100"), ("#81D4FA", "200"), ("#4FC3F7", "300"), ("#29B6F6", "400"), ("#03A9F4", "500"), ("#039BE5", "600"), ("#0288D1", "700"), ("#0277BD", "800"), ("#01579B", "900"), ("#80D8FF", "A100"), ("#40C4FF", "A200"), ("#00B0FF", "A400"), ("#0091EA", "A700") + ]), + ("Cyan", [ + ("#B2EBF2", "100"), ("#80DEEA", "200"), ("#4DD0E1", "300"), ("#26C6DA", "400"), ("#00BCD4", "500"), ("#00ACC1", "600"), ("#0097A7", "700"), ("#00838F", "800"), ("#006064", "900"), ("#84FFFF", "A100"), ("#18FFFF", "A200"), ("#00E5FF", "A400"), ("#00B8D4", "A700") + ]), + ("Teal", [ + ("#B2DFDB", "100"), ("#80CBC4", "200"), ("#4DB6AC", "300"), ("#26A69A", "400"), ("#009688", "500"), ("#00897B", "600"), ("#00796B", "700"), ("#00695C", "800"), ("#004D40", "900"), ("#A7FFEB", "A100"), ("#64FFDA", "A200"), ("#1DE9B6", "A400"), ("#00BFA5", "A700") + ]) + ] + + fourth_row_palettes = [ + ("Green", [ + ("#C8E6C9", "100"), ("#A5D6A7", "200"), ("#81C784", "300"), ("#66BB6A", "400"), ("#4CAF50", "500"), ("#43A047", "600"), ("#388E3C", "700"), ("#2E7D32", "800"), ("#1B5E20", "900"), ("#B9F6CA", "A100"), ("#69F0AE", "A200"), ("#00E676", "A400"), ("#00C853", "A700") + ]), + ("Light Green", [ + ("#DCEDC8", "100"), ("#C5E1A5", "200"), ("#AED581", "300"), ("#9CCC65", "400"), ("#8BC34A", "500"), ("#7CB342", "600"), ("#689F38", "700"), ("#558B2F", "800"), ("#33691E", "900"), ("#CCFF90", "A100"), ("#B2FF59", "A200"), ("#76FF03", "A400"), ("#64DD17", "A700") + ]), + ("Lime", [ + ("#F0F4C3", "100"), ("#E6EE9C", "200"), ("#DCE775", "300"), ("#D4E157", "400"), ("#CDDC39", "500"), ("#C0CA33", "600"), ("#AFB42B", "700"), ("#9E9D24", "800"), ("#827717", "900"), ("#F4FF81", "A100"), ("#EEFF41", "A200"), ("#C6FF00", "A400"), ("#AEEA00", "A700") + ]) + ] + + fifth_row_palettes = [ + ("Yellow", [ + ("#FFF9C4", "100"), ("#FFF59D", "200"), ("#FFF176", "300"), ("#FFEE58", "400"), ("#FFEB3B", "500"), ("#FDD835", "600"), ("#FBC02D", "700"), ("#F9A825", "800"), ("#F57F17", "900"), ("#FFFF8D", "A100"), ("#FFFF00", "A200"), ("#FFEA00", "A400"), ("#FFD600", "A700") + ]), + ("Amber", [ + ("#FFECB3", "100"), ("#FFE082", "200"), ("#FFD54F", "300"), ("#FFCA28", "400"), ("#FFC107", "500"), ("#FFB300", "600"), ("#FFA000", "700"), ("#FF8F00", "800"), ("#FF6F00", "900"), ("#FFE57F", "A100"), ("#FFD740", "A200"), ("#FFC400", "A400"), ("#FFAB00", "A700") + ]), + ("Orange", [ + ("#FFE0B2", "100"), ("#FFCC80", "200"), ("#FFB74D", "300"), ("#FFA726", "400"), ("#FF9800", "500"), ("#FB8C00", "600"), ("#F57C00", "700"), ("#EF6C00", "800"), ("#E65100", "900"), ("#FFD180", "A100"), ("#FFAB40", "A200"), ("#FF9100", "A400"), ("#FF6D00", "A700") + ]) + ] + + for row_palettes in [first_row_palettes, second_row_palettes, third_row_palettes, fourth_row_palettes, fifth_row_palettes]: + row_layout = QHBoxLayout() + row_layout.setSpacing(15) + + for palette_name, colors in row_palettes: + column_layout = QVBoxLayout() + column_layout.setSpacing(3) + + palette_label = QLabel(palette_name) + palette_label.setStyleSheet("margin-bottom: 2px;") + palette_label.setAlignment(Qt.AlignmentFlag.AlignCenter) + column_layout.addWidget(palette_label) + + color_buttons_layout = QHBoxLayout() + color_buttons_layout.setSpacing(3) + + for color_hex, color_name in colors: + color_btn = QPushButton() + color_btn.setFixedSize(18, 18) + + is_current = color_hex == self.current_theme_color + border_style = "2px solid #fff" if is_current else "none" + + color_btn.setStyleSheet(f""" + QPushButton {{ + background-color: {color_hex}; + border: {border_style}; + border-radius: 9px; + }} + QPushButton:hover {{ + border: 2px solid #fff; + }} + QPushButton:pressed {{ + border: 2px solid #fff; + }} + """) + color_btn.setCursor(Qt.CursorShape.PointingHandCursor) + color_btn.setToolTip(f"{palette_name} {color_name}\n{color_hex}") + color_btn.clicked.connect(lambda checked, color=color_hex, btn=color_btn: self.change_theme_color(color, btn)) + + self.color_buttons[color_hex] = color_btn + + color_buttons_layout.addWidget(color_btn) + + column_layout.addLayout(color_buttons_layout) + row_layout.addLayout(column_layout) + + grid_layout.addLayout(row_layout) + + theme_layout.addLayout(grid_layout) + theme_layout.addStretch() + + theme_tab.setLayout(theme_layout) + self.tab_widget.addTab(theme_tab, "Theme") + + def change_theme_color(self, color, clicked_btn=None): + if hasattr(self, 'color_buttons'): + for color_hex, btn in self.color_buttons.items(): + if color_hex == self.current_theme_color: + btn.setStyleSheet(f""" + QPushButton {{ + background-color: {color_hex}; + border: none; + border-radius: 9px; + }} + QPushButton:hover {{ + border: 2px solid #fff; + }} + QPushButton:pressed {{ + border: 2px solid #fff; + }} + """) + break + + self.current_theme_color = color + self.settings.setValue('theme_color', color) + self.settings.sync() + + if clicked_btn: + clicked_btn.setStyleSheet(f""" + QPushButton {{ + background-color: {color}; + border: 2px solid #fff; + border-radius: 9px; + }} + QPushButton:hover {{ + border: 2px solid #fff; + }} + QPushButton:pressed {{ + border: 2px solid #fff; + }} + """) + + qdarktheme.setup_theme( + custom_colors={ + "[dark]": { + "primary": color, + } + } + ) + + self.update_button_icons() + + def setup_about_tab(self): + about_tab = QWidget() + about_layout = QVBoxLayout() + about_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) + about_layout.setSpacing(15) + + sections = [ + ("Check for Updates", "Check", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases"), + ("Report an Issue", "Report", "https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/issues"), + ("gallery-dl Repository", "Visit", "https://github.com/mikf/gallery-dl") + ] + + for title, button_text, url in sections: + section_widget = QWidget() + section_layout = QVBoxLayout(section_widget) + section_layout.setSpacing(10) + section_layout.setContentsMargins(0, 0, 0, 0) + + label = QLabel(title) + label.setStyleSheet("font-weight: bold;") + label.setAlignment(Qt.AlignmentFlag.AlignCenter) + section_layout.addWidget(label) + + button = QPushButton(button_text) + button.setFixedSize(100, 25) + button.setCursor(Qt.CursorShape.PointingHandCursor) + button.clicked.connect(lambda _, url=url: QDesktopServices.openUrl(QUrl(url if url.startswith(('http://', 'https://')) else f'https://{url}'))) + section_layout.addWidget(button, alignment=Qt.AlignmentFlag.AlignCenter) + + about_layout.addWidget(section_widget) + + footer_label = QLabel(f"v{self.current_version} | gallery-dl v1.30.10 | November 2025") + about_layout.addWidget(footer_label, alignment=Qt.AlignmentFlag.AlignCenter) + + about_tab.setLayout(about_layout) + self.tab_widget.addTab(about_tab, "About") + + def save_url(self): + self.settings.setValue('twitter_url', self.twitter_url.text().strip()) + self.settings.sync() + + def toggle_date_range(self, checked): + self.start_date_edit.setEnabled(checked) + self.end_date_edit.setEnabled(checked) + + def save_batch_size(self): + self.batch_size = int(self.batch_size_combo.currentText()) + self.settings.setValue('batch_size', self.batch_size) + self.settings.sync() + + def save_settings(self): + self.settings.setValue('output_path', self.output_dir.text().strip()) + self.settings.setValue('auth_token', self.auth_token_input.text().strip()) + self.settings.setValue('media_type', self.media_type_combo.currentData()) + self.settings.setValue('timeline_type', self.timeline_type_combo.currentData()) + self.settings.setValue('download_batch_size', int(self.download_batch_combo.currentText())) + + if hasattr(self, 'convert_gif_checkbox'): + self.settings.setValue('convert_gif', self.convert_gif_checkbox.isChecked()) + self.convert_gif = self.convert_gif_checkbox.isChecked() + + if hasattr(self, 'conversion_mode_combo'): + self.settings.setValue('gif_conversion_mode', self.conversion_mode_combo.currentData()) + self.gif_conversion_mode = self.conversion_mode_combo.currentData() + + if hasattr(self, 'conversion_quality_combo'): + self.settings.setValue('gif_resolution', self.conversion_quality_combo.currentData()) + self.gif_resolution = self.conversion_quality_combo.currentData() + + self.settings.sync() + + def load_settings(self): + try: + if not hasattr(self, 'batch_checkbox') or not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): + return + + include_retweets = self.settings.value('include_retweets', False, type=bool) + if hasattr(self, 'retweets_checkbox'): + self.retweets_checkbox.blockSignals(True) + self.retweets_checkbox.setChecked(include_retweets) + self.retweets_checkbox.blockSignals(False) + + if include_retweets: + for i in range(self.timeline_type_combo.count()): + if self.timeline_type_combo.itemData(i) == 'media': + model = self.timeline_type_combo.model() + item = model.item(i) + item.setEnabled(False) + break + + batch_mode = self.settings.value('batch_mode', False, type=bool) + + self.batch_checkbox.blockSignals(True) + self.batch_checkbox.setChecked(batch_mode) + self.batch_checkbox.blockSignals(False) + + if batch_mode: + self.size_label.show() + self.batch_size_combo.show() + self.fetch_btn.setText('Fetch Batch') + else: + self.size_label.hide() + self.batch_size_combo.hide() + self.fetch_btn.setText('Fetch') + + fetch_batch_size = str(self.settings.value('batch_size', 100)) + index = self.batch_size_combo.findText(fetch_batch_size) + if index >= 0: + self.batch_size_combo.setCurrentIndex(index) + else: + self.batch_size_combo.setCurrentIndex(1) + + download_batch_size = str(self.settings.value('download_batch_size', 25)) + index = self.download_batch_combo.findText(download_batch_size) + if index >= 0: + self.download_batch_combo.setCurrentIndex(index) + else: + self.download_batch_combo.setCurrentIndex(0) + media_type = self.settings.value('media_type', 'all') + for i in range(self.media_type_combo.count()): + if self.media_type_combo.itemData(i) == media_type: + self.media_type_combo.setCurrentIndex(i) + break + + timeline_type = self.settings.value('timeline_type', 'timeline') + for i in range(self.timeline_type_combo.count()): + if self.timeline_type_combo.itemData(i) == timeline_type: + self.timeline_type_combo.setCurrentIndex(i) + break + + convert_gif = self.settings.value('convert_gif', False, type=bool) + if hasattr(self, 'convert_gif_checkbox'): + self.convert_gif_checkbox.blockSignals(True) + self.convert_gif_checkbox.setChecked(convert_gif) + self.convert_gif_checkbox.blockSignals(False) + + if convert_gif: + if hasattr(self, 'mode_label'): + self.mode_label.show() + if hasattr(self, 'conversion_mode_combo'): + self.conversion_mode_combo.show() + if hasattr(self, 'quality_label'): + self.quality_label.show() + if hasattr(self, 'conversion_quality_combo'): + self.conversion_quality_combo.show() + else: + if hasattr(self, 'mode_label'): + self.mode_label.hide() + if hasattr(self, 'conversion_mode_combo'): + self.conversion_mode_combo.hide() + if hasattr(self, 'quality_label'): + self.quality_label.hide() + if hasattr(self, 'conversion_quality_combo'): + self.conversion_quality_combo.hide() + + gif_conversion_mode = self.settings.value('gif_conversion_mode', 'better') + if hasattr(self, 'conversion_mode_combo'): + for i in range(self.conversion_mode_combo.count()): + if self.conversion_mode_combo.itemData(i) == gif_conversion_mode: + self.conversion_mode_combo.setCurrentIndex(i) + break + + gif_resolution = self.settings.value('gif_resolution', 'original') + if hasattr(self, 'conversion_quality_combo'): + for i in range(self.conversion_quality_combo.count()): + if self.conversion_quality_combo.itemData(i) == gif_resolution: + self.conversion_quality_combo.setCurrentIndex(i) + break + + last_username_url = self.settings.value('twitter_url', '') + if last_username_url and hasattr(self, 'twitter_url'): + self.twitter_url.setText(last_username_url) + + if hasattr(self, 'sort_combo'): + sort_pref = self.settings.value('sort_preference', 'fetch_timestamp_desc') + for i in range(self.sort_combo.count()): + if self.sort_combo.itemData(i) == sort_pref: + self.sort_combo.blockSignals(True) + self.sort_combo.setCurrentIndex(i) + self.sort_combo.blockSignals(False) + break + + except Exception as e: + pass + + def restore_filter_preferences(self): + try: + if hasattr(self, 'group_filter_combo') and self.group_filter_combo.count() > 0: + saved_filter = self.settings.value('group_filter') + + if saved_filter is None or saved_filter == '' or saved_filter == 'None': + group_filter = None + elif saved_filter == '-1' or saved_filter == -1: + group_filter = -1 + else: + try: + group_filter = int(saved_filter) + except (ValueError, TypeError): + group_filter = None + + for i in range(self.group_filter_combo.count()): + item_data = self.group_filter_combo.itemData(i) + if item_data == group_filter: + self.group_filter_combo.blockSignals(True) + self.group_filter_combo.setCurrentIndex(i) + self.group_filter_combo.blockSignals(False) + if group_filter is not None: + self.filter_accounts() + break + except Exception as e: + pass + + def handle_retweets_checkbox(self, state): + self.include_retweets = self.retweets_checkbox.isChecked() + self.settings.setValue('include_retweets', self.include_retweets) + + if self.include_retweets: + current_timeline = self.timeline_type_combo.currentData() + if current_timeline == 'media': + for i in range(self.timeline_type_combo.count()): + if self.timeline_type_combo.itemData(i) == 'timeline': + self.timeline_type_combo.setCurrentIndex(i) + self.settings.setValue('timeline_type', 'timeline') + break + for i in range(self.timeline_type_combo.count()): + if self.timeline_type_combo.itemData(i) == 'media': + model = self.timeline_type_combo.model() + item = model.item(i) + item.setEnabled(False) + break + else: + for i in range(self.timeline_type_combo.count()): + if self.timeline_type_combo.itemData(i) == 'media': + model = self.timeline_type_combo.model() + item = model.item(i) + item.setEnabled(True) + break + + self.settings.sync() + + def handle_batch_checkbox(self, state): + if not hasattr(self, 'size_label') or not hasattr(self, 'batch_size_combo'): + return + + self.batch_mode = self.batch_checkbox.isChecked() + self.settings.setValue('batch_mode', self.batch_mode) + + if self.batch_checkbox.isChecked(): + self.size_label.show() + self.batch_size_combo.show() + self.fetch_btn.setText('Fetch Batch') + else: + self.size_label.hide() + self.batch_size_combo.hide() + self.fetch_btn.setText('Fetch') + self.update_button_states() + + def handle_conversion_checkbox(self, checked): + self.convert_gif = checked + self.save_settings() + + if checked: + if hasattr(self, 'mode_label'): + self.mode_label.show() + if hasattr(self, 'conversion_mode_combo'): + self.conversion_mode_combo.show() + if hasattr(self, 'quality_label'): + self.quality_label.show() + if hasattr(self, 'conversion_quality_combo'): + self.conversion_quality_combo.show() + else: + if hasattr(self, 'mode_label'): + self.mode_label.hide() + if hasattr(self, 'conversion_mode_combo'): + self.conversion_mode_combo.hide() + if hasattr(self, 'quality_label'): + self.quality_label.hide() + if hasattr(self, 'conversion_quality_combo'): + self.conversion_quality_combo.hide() + + def browse_output(self): + directory = QFileDialog.getExistingDirectory(self, "Select Output Directory") + if directory: + self.output_dir.setText(directory) + self.save_settings() + + def migrate_json_to_sqlite(self): + try: + accounts_migrated, media_migrated = self.db_manager.migrate_from_json(self.temp_dir) + if accounts_migrated > 0: + print(f"Migrated {accounts_migrated} accounts and {media_migrated} media items from JSON to SQLite") + except Exception as e: + print(f"Migration error: {e}") + + def get_cache_file_path(self, username, media_type, is_batch=None): + timeline_type = self.timeline_type_combo.currentData() or 'timeline' + filename = f"{username}_{timeline_type}_{media_type}.json" + return os.path.join(self.temp_dir, filename) + + def load_cached_data(self, username, media_type, is_batch=None): + try: + account = self.db_manager.get_account(username, media_type) + if account: + media_list = self.db_manager.get_media_list(username) + return { + 'username': account['username'], + 'nick': account['nick'], + 'followers': account['followers'], + 'following': account['following'], + 'posts': account['posts'], + 'profile_image': account['profile_image'], + 'fetch_timestamp': account['fetch_timestamp'], + 'media_type': account['media_type'], + 'fetch_mode': account['fetch_mode'], + 'media_list': media_list + } + except Exception as e: + print(f"Error loading cached data: {e}") + return None + + def save_cached_data(self, username, media_type, data, is_batch=None): + try: + account_info = data.get('account_info', {}) + + nick = account_info.get('nick', account_info.get('name', data.get('nick', ''))) + followers = account_info.get('followers_count', data.get('followers', 0)) + following = account_info.get('friends_count', data.get('following', 0)) + posts = account_info.get('statuses_count', data.get('posts', 0)) + profile_image = account_info.get('profile_image', data.get('profile_image')) + + existing_account = None + for acc in self.accounts: + if acc.username == username and acc.media_type == media_type: + existing_account = acc + break + + group_id = existing_account.group_id if existing_account else data.get('group_id') + + self.db_manager.save_account( + username=username, + nick=nick, + followers=followers, + following=following, + posts=posts, + media_type=media_type, + profile_image=profile_image, + fetch_mode=data.get('fetch_mode', 'batch' if is_batch else 'all'), + fetch_timestamp=data.get('fetch_timestamp'), + group_id=group_id + ) + + media_list = data.get('timeline', data.get('media_list', [])) + if media_list: + self.db_manager.save_media_list(username, media_list) + except Exception as e: + print(f"Error saving cached data: {e}") + import traceback + traceback.print_exc() + + def load_all_cached_accounts(self): + try: + db_accounts = self.db_manager.get_all_accounts() + + for db_account in db_accounts: + username = db_account['username'] + media_type = db_account['media_type'] + + already_exists = any( + acc.username == username and acc.media_type == media_type + for acc in self.accounts + ) + + if not already_exists: + media_list = self.db_manager.get_media_list(username) + + if media_list: + account = Account( + username=username, + nick=db_account['nick'], + followers=db_account['followers'], + following=db_account['following'], + posts=db_account['posts'], + media_type=media_type, + profile_image=db_account['profile_image'], + media_list=media_list, + fetch_mode=db_account.get('fetch_mode', 'all'), + fetch_timestamp=db_account.get('fetch_timestamp'), + group_id=db_account.get('group_id') + ) + + self.accounts.append(account) + + if self.accounts: + if hasattr(self, 'sort_combo'): + self.sort_accounts() + self.update_account_list() + if hasattr(self, 'group_filter_combo') and self.group_filter_combo.currentData() is not None: + self.filter_accounts() + + except Exception as e: + pass + + def fetch_account(self): + url_input = self.twitter_url.text().strip() + + if not url_input: + self.log_output.append('Warning: Please enter a Twitter username/URL.') + return + + if not self.auth_token_input.text().strip(): + self.log_output.append('Warning: Please enter your auth token.') + return + + urls = [url.strip() for url in url_input.split(',') if url.strip()] + + if len(urls) > 1: + + self.log_output.append(f'Processing {len(urls)} accounts: {", ".join(urls)}') + self.tab_widget.setCurrentWidget(self.process_tab) + + self.accounts_to_fetch = [] + media_type = self.media_type_combo.currentData() + + for url in urls: + username = self.normalize_url_to_username(url) + if username: + self.accounts_to_fetch.append((username, media_type)) + + if self.accounts_to_fetch: + self.current_fetch_index = 0 + self.is_multiple_user_mode = True + self.is_initial_fetch = True + + if self.batch_mode: + self.is_auto_fetching = True + self.pause_fetch_btn.hide() + self.stop_fetch_btn.show() + self.log_output.append('Multiple user batch mode: Auto-fetching all batches for all users...') + else: + self.is_auto_fetching = True + self.pause_fetch_btn.hide() + self.stop_fetch_btn.show() + self.log_output.append('Multiple user mode: Auto-fetching all users...') + + self.fetch_next_account_in_batch() + return + + url = urls[0] + username = self.normalize_url_to_username(url) + if not username: + self.log_output.append('Warning: Invalid username/URL format.') + return + + media_type = self.media_type_combo.currentData() + + existing_account = None + for account in self.accounts: + if account.username == username and account.media_type == media_type: + existing_account = account + break + + if existing_account: + self.accounts.remove(existing_account) + + for is_batch in [True, False]: + cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) + try: + if os.path.exists(cache_file): + os.remove(cache_file) + self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') + except Exception as e: + self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') + + self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') + self.update_account_list() + + if not existing_account: + cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) + if cached_data: + try: + account_info = cached_data.get('account_info', {}) + timeline = cached_data.get('timeline', []) + if account_info and timeline: + followers = account_info.get('followers_count', 0) + following = account_info.get('friends_count', 0) + posts = account_info.get('statuses_count', 0) + nick = account_info.get('nick', account_info.get('name', '')) + + account = Account( + username=username, + nick=nick, + followers=followers, + following=following, + posts=posts, + media_type=media_type, + media_list=timeline, + fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', + fetch_timestamp=cached_data.get('fetch_timestamp') + ) + + self.accounts.append(account) + self.update_account_list() + self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') + self.twitter_url.clear() + return + except (KeyError, AttributeError, TypeError) as e: + pass + + try: + self.reset_ui() + + self.is_auto_fetching = False + self.is_multiple_user_mode = False + self.current_fetch_username = username + self.current_fetch_media_type = media_type + + + self.log_output.append(f'Fetching metadata for {username}...') + self.tab_widget.setCurrentWidget(self.process_tab) + + + use_date_range = self.use_date_range_checkbox.isChecked() + date_start = None + date_end = None + if use_date_range: + date_start = self.start_date_edit.date().toString("yyyy-MM-dd") + date_end = self.end_date_edit.date().toString("yyyy-MM-dd") + self.log_output.append(f'Using date range: {date_start} to {date_end}') + + self.metadata_worker = MetadataFetchWorker( + username, + media_type, + batch_mode=self.batch_mode, + batch_size=self.batch_size if self.batch_mode else 0, + page=0, + timeline_type=self.timeline_type_combo.currentData() or 'timeline', + include_retweets=self.include_retweets, + use_date_range=use_date_range, + date_start=date_start, + date_end=date_end + ) + self.metadata_worker.auth_token = self.auth_token_input.text().strip() + self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) + self.metadata_worker.error.connect(self.on_metadata_error) + self.metadata_worker.start() + + except Exception as e: + self.log_output.append(f'Error: Failed to start metadata fetch: {str(e)}') + self.update_account_list() + + def normalize_url_to_username(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 if username else None + + def fetch_next_account_in_batch(self): + + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: + self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') + + if hasattr(self, 'accounts_to_fetch'): + delattr(self, 'accounts_to_fetch') + if hasattr(self, 'current_fetch_index'): + delattr(self, 'current_fetch_index') + if hasattr(self, 'is_multiple_user_mode'): + delattr(self, 'is_multiple_user_mode') + if hasattr(self, 'is_initial_fetch'): + delattr(self, 'is_initial_fetch') + self.twitter_url.clear() + self.tab_widget.setCurrentIndex(0) + return + + if not hasattr(self, 'accounts_to_fetch') or self.current_fetch_index >= len(self.accounts_to_fetch): + total_accounts = len(self.accounts_to_fetch) if hasattr(self, 'accounts_to_fetch') else 0 + self.log_output.append(f'Batch processing completed! Processed {total_accounts} accounts.') + self.twitter_url.clear() + + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + self.stop_fetch_btn.hide() + self.is_auto_fetching = False + + if hasattr(self, 'accounts_to_fetch'): + delattr(self, 'accounts_to_fetch') + if hasattr(self, 'current_fetch_index'): + delattr(self, 'current_fetch_index') + if hasattr(self, 'is_multiple_user_mode'): + delattr(self, 'is_multiple_user_mode') + if hasattr(self, 'is_initial_fetch'): + delattr(self, 'is_initial_fetch') + self.tab_widget.setCurrentIndex(0) + return + + username, media_type = self.accounts_to_fetch[self.current_fetch_index] + self.log_output.append(f'Processing account {self.current_fetch_index + 1}/{len(self.accounts_to_fetch)}: {username} ({media_type})') + + existing_account = None + for account in self.accounts: + if account.username == username and account.media_type == media_type: + existing_account = account + break + + if existing_account and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not getattr(self, 'is_initial_fetch', True): + self.log_output.append(f'Resuming from existing progress for {username} ({media_type}) - {len(existing_account.media_list):,} items already fetched') + + cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) + if cached_data and cached_data.get('metadata', {}).get('has_more', False): + current_page = cached_data.get('metadata', {}).get('page', 0) + next_page = current_page + 1 + self.log_output.append(f'Continuing from batch {next_page + 1} for {username}...') + self.fetch_next_batch_for_current_user(username, media_type, next_page, cached_data.get('metadata', {})) + return + else: + self.log_output.append(f'Account {username} already completed. Moving to next account.') + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + return + elif existing_account: + self.accounts.remove(existing_account) + + for is_batch in [True, False]: + cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) + try: + if os.path.exists(cache_file): + os.remove(cache_file) + self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') + except Exception as e: + self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') + + self.log_output.append(f'Preparing new batch for {username} ({media_type}) - cache cleared') + self.update_account_list() + + if not existing_account: + cached_data = self.load_cached_data(username, media_type, is_batch=self.batch_mode) + if cached_data: + try: + account_info = cached_data.get('account_info', {}) + timeline = cached_data.get('timeline', []) + if account_info and timeline: + followers = account_info.get('followers_count', 0) + following = account_info.get('friends_count', 0) + posts = account_info.get('statuses_count', 0) + nick = account_info.get('nick', account_info.get('name', '')) + + account = Account( + username=username, + nick=nick, + followers=followers, + following=following, + posts=posts, + media_type=media_type, + media_list=timeline, + fetch_mode='batch' if cached_data.get('is_batch', False) else 'all', + fetch_timestamp=cached_data.get('fetch_timestamp') + ) + + self.accounts.append(account) + self.update_account_list() + self.log_output.append(f'Loaded from cache: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') + + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + return + except (KeyError, AttributeError, TypeError) as e: + pass + + try: + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + if self.batch_mode and not self.is_auto_fetching: + self.is_auto_fetching = True + self.pause_fetch_btn.hide() + self.stop_fetch_btn.show() + else: + self.is_auto_fetching = False + + self.current_fetch_username = username + self.current_fetch_media_type = media_type + + + self.metadata_worker = MetadataFetchWorker( + username, + media_type, + batch_mode=self.batch_mode, + batch_size=self.batch_size if self.batch_mode else 0, + page=0, + timeline_type=self.timeline_type_combo.currentData() or 'timeline', + include_retweets=self.include_retweets + ) + self.metadata_worker.auth_token = self.auth_token_input.text().strip() + self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) + self.metadata_worker.error.connect(self.on_batch_metadata_error) + self.metadata_worker.start() + + except Exception as e: + self.log_output.append(f'Error: Failed to start metadata fetch for {username}: {str(e)}') + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + + def on_batch_metadata_fetched(self, data, username, media_type): + try: + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: + self.log_output.append(f'Ignoring result for {username} - auto-fetch was stopped.') + return + + if 'error' in data: + self.log_output.append(f'Error fetching {username}: {data["error"]}') + if self.is_auto_fetching: + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + return + + account_info = data.get('account_info', {}) + timeline = data.get('timeline', []) + metadata = data.get('metadata', {}) + + if not account_info: + self.log_output.append(f'Error: Invalid account data received for {username}') + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + return + + if not timeline: + media_type_display = media_type if media_type != 'all' else 'media' + self.log_output.append(f'Warning: No {media_type_display} found for account {username}') + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + return + + existing_account = None + for account in self.accounts: + if account.username == username and account.media_type == media_type: + existing_account = account + break + + if existing_account: + existing_account.media_list.extend(timeline) + new_items = len(timeline) + total_items = len(existing_account.media_list) + self.log_output.append(f'Added {new_items:,} items to {username}. Total: {total_items:,} media items') + + updated_data = { + 'account_info': account_info, + 'timeline': existing_account.media_list, + 'metadata': metadata, + 'is_batch': self.batch_mode, + 'fetch_timestamp': existing_account.fetch_timestamp + } + self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) + else: + followers = account_info.get('followers_count', 0) + following = account_info.get('friends_count', 0) + posts = account_info.get('statuses_count', 0) + nick = account_info.get('nick', account_info.get('name', '')) + profile_image_url = account_info.get('profile_image', '') + + account = Account( + username=username, + nick=nick, + followers=followers, + following=following, + posts=posts, + media_type=media_type, + profile_image=profile_image_url, + media_list=timeline, + fetch_mode='batch' if self.batch_mode else 'all', + fetch_timestamp=datetime.now().isoformat() + ) + + self.accounts.append(account) + existing_account = account + self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') + + updated_data = { + 'account_info': account_info, + 'timeline': timeline, + 'metadata': metadata, + 'is_batch': self.batch_mode, + 'fetch_timestamp': account.fetch_timestamp + } + self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) + + self.update_account_list() + + if self.batch_mode and metadata.get('has_more', False): + current_page = metadata.get('page', 0) + next_page = current_page + 1 + + self.is_auto_fetching = True + self.log_output.append(f'Batch {current_page + 1} completed for {username}. Auto-fetching batch {next_page + 1}...') + self.pause_fetch_btn.show() + self.stop_fetch_btn.show() + self.fetch_next_batch_for_current_user(username, media_type, next_page, metadata) + else: + if self.batch_mode: + total_items = len(existing_account.media_list) + self.log_output.append(f'All batches completed for {username}! Total: {total_items:,} media items') + + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + if self.is_auto_fetching: + self.pause_fetch_btn.show() + self.stop_fetch_btn.show() + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + else: + self.log_output.append('Auto-fetch stopped by user. Multiple user processing halted.') + return + else: + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + + except Exception as e: + self.log_output.append(f'Error processing {username}: {str(e)}') + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + + def fetch_next_batch_for_current_user(self, username, media_type, page, metadata): + try: + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: + self.log_output.append('Auto-fetch stopped by user. Batch processing halted.') + return + + if self.is_auto_fetching: + self.pause_fetch_btn.show() + self.stop_fetch_btn.show() + + self.metadata_worker = MetadataFetchWorker( + username, + media_type, + batch_mode=True, + batch_size=self.batch_size, + page=page, + timeline_type=self.timeline_type_combo.currentData() or 'timeline', + include_retweets=self.include_retweets + ) + self.metadata_worker.auth_token = self.auth_token_input.text().strip() + self.metadata_worker.finished.connect(lambda data: self.on_batch_metadata_fetched(data, username, media_type)) + self.metadata_worker.error.connect(self.on_batch_metadata_error) + self.metadata_worker.start() + + except Exception as e: + self.log_output.append(f'Error: Failed to start next batch fetch for {username}: {str(e)}') + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: + return + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + + def on_batch_metadata_error(self, error_message): + if hasattr(self, 'accounts_to_fetch') and hasattr(self, 'current_fetch_index'): + username, media_type = self.accounts_to_fetch[self.current_fetch_index] + self.log_output.append(f'Error fetching {username}: {error_message}') + + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode and not self.is_auto_fetching: + self.log_output.append('Auto-fetch stopped by user. Error handling halted.') + return + + if self.is_auto_fetching and hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + self.pause_fetch_btn.show() + self.stop_fetch_btn.show() + + self.current_fetch_index += 1 + self.fetch_next_account_in_batch() + else: + self.on_metadata_error(error_message) + + def on_metadata_fetched(self, data, username, media_type): + try: + if 'error' in data: + self.log_output.append(f'Error: {data["error"]}') + self.update_account_list() + return + + account_info = data.get('account_info', {}) + timeline = data.get('timeline', []) + metadata = data.get('metadata', {}) + if not account_info: + self.log_output.append('Error: Invalid account data received') + self.update_account_list() + return + + if not timeline: + media_type_display = media_type if media_type != 'all' else 'media' + self.log_output.append(f'Error: No {media_type_display} found for account {username}') + self.update_account_list() + return + existing_account = None + for account in self.accounts: + if account.username == username and account.media_type == media_type: + existing_account = account + break + + if existing_account: + existing_account.media_list.extend(timeline) + new_items = len(timeline) + total_items = len(existing_account.media_list) + self.log_output.append(f'Added {new_items:,} items. Total: {total_items:,} media items for {username}') + else: + followers = account_info.get('followers_count', 0) + following = account_info.get('friends_count', 0) + posts = account_info.get('statuses_count', 0) + nick = account_info.get('nick', account_info.get('name', '')) + profile_image_url = account_info.get('profile_image', '') + + account = Account( + username=username, + nick=nick, + followers=followers, + following=following, + posts=posts, + media_type=media_type, + profile_image=profile_image_url, + media_list=timeline, + fetch_mode='batch' if self.batch_mode else 'all', + fetch_timestamp=datetime.now().isoformat() + ) + + self.accounts.append(account) + self.log_output.append(f'Successfully fetched: {username} - Followers: {followers:,} - Posts: {posts:,} • {media_type.title()}') + existing_account = account + + updated_data = { + 'account_info': account_info, + 'timeline': existing_account.media_list, + 'metadata': metadata, + 'is_batch': self.batch_mode, + 'fetch_timestamp': existing_account.fetch_timestamp + } + self.save_cached_data(username, media_type, updated_data, is_batch=self.batch_mode) + + self.update_account_list() + + self.current_fetch_username = username + self.current_fetch_media_type = media_type + self.current_fetch_metadata = metadata + + if self.batch_mode and metadata.get('has_more', False): + current_page = metadata.get('page', 0) + next_page = current_page + 1 + self.log_output.append(f'Batch {current_page + 1} completed. Auto-fetching batch {next_page + 1}...') + + self.is_auto_fetching = True + self.pause_fetch_btn.show() + self.stop_fetch_btn.show() + self.fetch_next_batch_internal() + else: + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + self.is_auto_fetching = False + + if self.batch_mode: + total_items = len(existing_account.media_list) + self.log_output.append(f'All batches complete! Total: {total_items:,} media items for {username}') + + self.twitter_url.clear() + + if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): + self.current_update_index += 1 + self.update_next_account() + else: + self.tab_widget.setCurrentIndex(0) + except Exception as e: + self.log_output.append(f'Error: {str(e)}') + self.update_account_list() + + def on_metadata_error(self, error_message): + self.log_output.append(f'Error: {error_message}') + + if self.is_auto_fetching: + self.stop_auto_fetch() + + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + self.enable_batch_buttons() + + if hasattr(self, 'accounts_to_update') and hasattr(self, 'current_update_index'): + self.current_update_index += 1 + self.update_next_account() + + self.update_account_list() + + def update_account_list(self): + self.sort_accounts() + + self.account_list.clear() + for i, account in enumerate(self.accounts, 1): + media_count = len(account.media_list) if account.media_list else 0 + + if account.media_type.lower() == "gif": + media_type_display = "GIF" + elif account.media_type.lower() == "all": + media_type_display = "All" + elif account.media_type.lower() == "image": + media_type_display = "Image" + elif account.media_type.lower() == "video": + media_type_display = "Video" + else: + media_type_display = account.media_type.title() + + line1 = f"{i}. {account.username} ({account.nick})" + line2 = f"Followers: {account.followers:,} • Following: {account.following:,} • Posts: {account.posts:,} • {media_type_display}: {media_count:,}" + line3 = self.format_fetch_info(account.fetch_timestamp) + display_text = f"{line1}\n{line2}\n{line3}" + item = QListWidgetItem() + item.setText(display_text) + item.setSizeHint(QSize(0, 70)) + + if account.profile_image: + if account.profile_image in self.profile_image_cache: + item.setIcon(QIcon(self.profile_image_cache[account.profile_image])) + else: + self.download_profile_image(account.profile_image) + placeholder = self.create_placeholder_icon(48) + if placeholder: + item.setIcon(QIcon(placeholder)) + else: + placeholder = self.create_placeholder_icon(48) + if placeholder: + item.setIcon(QIcon(placeholder)) + + self.account_list.addItem(item) + + if hasattr(self, 'search_input'): + search_text = self.search_input.text() + has_group_filter = hasattr(self, 'group_filter_combo') and self.group_filter_combo.currentData() is not None + if search_text or has_group_filter: + self.filter_accounts() + + self.update_button_states() + + def create_colored_icon(self, svg_path, color): + with open(svg_path, 'r', encoding='utf-8') as f: + svg_content = f.read() + + svg_content = svg_content.replace('stroke="currentColor"', f'stroke="{color}"') + svg_content = svg_content.replace('fill="currentColor"', f'fill="{color}"') + + pixmap = QPixmap(24, 24) + pixmap.fill(Qt.GlobalColor.transparent) + + from PyQt6.QtSvg import QSvgRenderer + renderer = QSvgRenderer(svg_content.encode('utf-8')) + painter = QPainter(pixmap) + renderer.render(painter) + painter.end() + + return QIcon(pixmap) + + def create_placeholder_icon(self, size=48): + pixmap = QPixmap(size, size) + pixmap.fill(Qt.GlobalColor.transparent) + + painter = QPainter(pixmap) + painter.setRenderHint(QPainter.RenderHint.Antialiasing) + + painter.setBrush(Qt.GlobalColor.gray) + painter.setPen(Qt.PenStyle.NoPen) + painter.drawRoundedRect(0, 0, size, size, 8, 8) + + painter.setPen(Qt.GlobalColor.white) + font_size = int(size * 0.4) + painter.setFont(painter.font()) + painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "👤") + + painter.end() + return pixmap + + def create_square_pixmap(self, original_pixmap, size=48): + if original_pixmap.isNull(): + return self.create_placeholder_icon(size) + + square_pixmap = QPixmap(size, size) + square_pixmap.fill(Qt.GlobalColor.transparent) + + painter = QPainter(square_pixmap) + painter.setRenderHint(QPainter.RenderHint.Antialiasing) + + path = QPainterPath() + path.addRoundedRect(0, 0, size, size, 8, 8) + painter.setClipPath(path) + + scaled_pixmap = original_pixmap.scaled( + size, size, + Qt.AspectRatioMode.KeepAspectRatioByExpanding, + Qt.TransformationMode.SmoothTransformation + ) + + x = (size - scaled_pixmap.width()) // 2 + y = (size - scaled_pixmap.height()) // 2 + painter.drawPixmap(x, y, scaled_pixmap) + + painter.end() + return square_pixmap + + def download_profile_image(self, url): + if not url or url in self.profile_image_cache or url in self.pending_downloads: + return + try: + request = QNetworkRequest(QUrl(url)) + request.setHeader(QNetworkRequest.KnownHeaders.UserAgentHeader, + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36") + + reply = self.network_manager.get(request) + self.pending_downloads[url] = reply + reply.finished.connect(lambda: self.on_profile_image_downloaded(reply, url)) + except Exception as e: + pass + + def on_profile_image_downloaded(self, reply, image_url): + try: + if reply.error() == reply.NetworkError.NoError: + data = reply.readAll() + pixmap = QPixmap() + if pixmap.loadFromData(data): + square_pixmap = self.create_square_pixmap(pixmap, 48) + self.profile_image_cache[image_url] = square_pixmap + + self.update_account_list() + + if image_url in self.pending_downloads: + del self.pending_downloads[image_url] + + except Exception as e: + pass + finally: + reply.deleteLater() + + def filter_accounts(self): + search_text = self.search_input.text().lower().strip() + + for i in range(self.account_list.count()): + item = self.account_list.item(i) + if search_text: + item_text = item.text().lower() + item.setHidden(search_text not in item_text) + else: + item.setHidden(False) + + def on_sort_changed(self): + self.sort_accounts() + self.update_account_list() + self.filter_accounts() + if hasattr(self, 'is_initializing') and not self.is_initializing: + if hasattr(self, 'settings'): + self.settings.setValue('sort_preference', self.sort_combo.currentData()) + self.settings.sync() + + def sort_accounts(self): + if not hasattr(self, 'sort_combo'): + return + + sort_by = self.sort_combo.currentData() + + if sort_by == "username_asc": + self.accounts.sort(key=lambda x: x.username.lower()) + elif sort_by == "username_desc": + self.accounts.sort(key=lambda x: x.username.lower(), reverse=True) + elif sort_by == "followers_desc": + self.accounts.sort(key=lambda x: x.followers, reverse=True) + elif sort_by == "followers_asc": + self.accounts.sort(key=lambda x: x.followers) + elif sort_by == "posts_desc": + self.accounts.sort(key=lambda x: x.posts, reverse=True) + elif sort_by == "posts_asc": + self.accounts.sort(key=lambda x: x.posts) + elif sort_by == "media_count_desc": + self.accounts.sort(key=lambda x: len(x.media_list) if x.media_list else 0, reverse=True) + elif sort_by == "media_count_asc": + self.accounts.sort(key=lambda x: len(x.media_list) if x.media_list else 0) + elif sort_by == "fetch_timestamp_asc": + self.accounts.sort(key=lambda x: (x.fetch_timestamp is None or x.fetch_timestamp == "", x.fetch_timestamp or "9999")) + elif sort_by == "fetch_timestamp_desc": + self.accounts.sort(key=lambda x: (x.fetch_timestamp is None or x.fetch_timestamp == "", x.fetch_timestamp or ""), reverse=True) + + + def update_button_states(self): + has_accounts = len(self.accounts) > 0 + selected_items = self.account_list.selectedItems() + has_selected = len(selected_items) > 0 + + self.import_btn.setEnabled(True) + self.export_btn.setEnabled(has_accounts) + self.download_selected_btn.setEnabled(has_accounts) + self.update_selected_btn.setEnabled(has_accounts) + self.delete_btn.setEnabled(has_accounts) + + self.import_btn.show() + self.export_btn.show() + self.download_selected_btn.show() + self.update_selected_btn.show() + self.delete_btn.show() + + if len(selected_items) == 1: + item = selected_items[0] + for account in self.accounts: + item_text = item.text() + if account.username in item_text: + self.twitter_url.setText(account.username) + break + + def hide_account_buttons(self): + self.import_btn.setEnabled(True) + self.export_btn.setEnabled(False) + self.download_selected_btn.setEnabled(False) + self.update_selected_btn.setEnabled(False) + self.delete_btn.setEnabled(False) + + self.import_btn.show() + self.export_btn.show() + self.download_selected_btn.show() + self.update_selected_btn.show() + self.delete_btn.show() + + def download_selected(self): + selected_items = self.account_list.selectedItems() + + if not selected_items: + visible_indices = [] + for i in range(self.account_list.count()): + item = self.account_list.item(i) + if not item.isHidden(): + visible_indices.append(i) + + visible_count = len(visible_indices) + reply = QMessageBox.question( + self, + 'Confirm Download All', + f'No accounts selected. Download all {visible_count} visible account(s)?', + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No + ) + if reply == QMessageBox.StandardButton.Yes: + self.download_accounts(visible_indices) + return + + self.download_accounts([self.account_list.row(item) for item in selected_items]) + + def update_selected(self): + selected_items = self.account_list.selectedItems() + + if not selected_items: + visible_accounts = [] + for i in range(self.account_list.count()): + item = self.account_list.item(i) + if not item.isHidden() and i < len(self.accounts): + visible_accounts.append(self.accounts[i]) + + visible_count = len(visible_accounts) + reply = QMessageBox.question( + self, + 'Confirm Update All', + f'No accounts selected. Update all {visible_count} visible account(s)?', + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No + ) + if reply == QMessageBox.StandardButton.No: + return + selected_accounts = visible_accounts + else: + selected_accounts = [] + for item in selected_items: + index = self.account_list.row(item) + if index < len(self.accounts): + selected_accounts.append(self.accounts[index]) + + if not selected_accounts: + return + + self.tab_widget.setCurrentWidget(self.process_tab) + + self.log_output.clear() + self.log_output.append(f'Starting update for {len(selected_accounts)} account(s)...') + + self.accounts_to_update = selected_accounts.copy() + self.current_update_index = 0 + + self.update_next_account() + + def update_next_account(self): + if not hasattr(self, 'accounts_to_update') or self.current_update_index >= len(self.accounts_to_update): + self.log_output.append('All selected accounts have been updated!') + if hasattr(self, 'accounts_to_update'): + delattr(self, 'accounts_to_update') + if hasattr(self, 'current_update_index'): + delattr(self, 'current_update_index') + self.tab_widget.setCurrentIndex(0) + return + + account = self.accounts_to_update[self.current_update_index] + self.log_output.append(f'Updating account {self.current_update_index + 1}/{len(self.accounts_to_update)}: {account.username} ({account.media_type})') + self.update_account(account) + + def update_account(self, account): + original_batch_mode = self.batch_mode + original_batch_size = self.batch_size + original_media_type = self.media_type_combo.currentData() + + self.batch_mode = (account.fetch_mode == 'batch') + if self.batch_mode: + self.batch_size = self.settings.value('batch_size', 100, type=int) + + for i in range(self.media_type_combo.count()): + if self.media_type_combo.itemData(i) == account.media_type: + self.media_type_combo.setCurrentIndex(i) + break + + self.twitter_url.setText(account.username) + + self.tab_widget.setCurrentWidget(self.process_tab) + + self.log_output.clear() + self.log_output.append(f'Updating {account.username} ({account.media_type}) using {account.fetch_mode} mode...') + + username = account.username + media_type = account.media_type + + for is_batch in [True, False]: + cache_file = self.get_cache_file_path(username, media_type, is_batch=is_batch) + try: + if os.path.exists(cache_file): + os.remove(cache_file) + self.log_output.append(f'Removed existing cache: {os.path.basename(cache_file)}') + except Exception as e: + self.log_output.append(f'Warning: Could not remove cache {os.path.basename(cache_file)}: {str(e)}') + + for i, existing_account in enumerate(self.accounts): + if existing_account.username == account.username and existing_account.media_type == account.media_type: + self.accounts.pop(i) + break + + self.update_account_list() + + self.start_fetch_process(username, media_type) + + self.batch_mode = original_batch_mode + self.batch_size = original_batch_size + + for i in range(self.media_type_combo.count()): + if self.media_type_combo.itemData(i) == original_media_type: + self.media_type_combo.setCurrentIndex(i) + break + + def start_fetch_process(self, username, media_type): + if not self.auth_token_input.text().strip(): + self.log_output.append("Error: Please enter your auth token") + return + + + self.worker = MetadataFetchWorker( + username, + media_type, + batch_mode=self.batch_mode, + batch_size=self.batch_size if self.batch_mode else 0, + page=0, + timeline_type=self.timeline_type_combo.currentData() or 'timeline', + include_retweets=self.include_retweets + ) + self.worker.auth_token = self.auth_token_input.text().strip() + self.worker.finished.connect(lambda data: self.on_metadata_fetched(data, username, media_type)) + self.worker.error.connect(self.on_metadata_error) + self.worker.start() + + def download_accounts(self, indices): + self.log_output.clear() + outpath = self.output_dir.text() + if not os.path.exists(outpath): + self.log_output.append('Warning: Invalid output directory.') + return + + if not self.auth_token_input.text().strip(): + self.log_output.append("Error: Please enter your auth token") + return + + accounts_to_download = [self.accounts[i] for i in indices] + + try: + self.start_download_worker(accounts_to_download, outpath) + except Exception as e: + self.log_output.append(f"Error: An error occurred while starting the download: {str(e)}") + + def start_download_worker(self, accounts_to_download, outpath): + self.worker = DownloadWorker( + accounts_to_download, + outpath, + self.auth_token_input.text().strip(), + self.filename_format, + self.download_batch_size, + self.convert_gif, + self.gif_resolution, + self.gif_conversion_mode, + self.db_manager + ) + self.worker.finished.connect(self.on_download_finished) + self.worker.progress.connect(self.update_progress) + self.worker.conversion_progress.connect(self.update_conversion_progress) + self.worker.download_progress.connect(self.update_download_progress) + self.worker.start() + self.start_timer() + self.update_ui_for_download_start(len(accounts_to_download)) + + def update_ui_for_download_start(self, account_count): + self.download_selected_btn.setEnabled(False) + self.update_selected_btn.setEnabled(False) + self.stop_btn.show() + self.pause_resume_btn.show() + + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + self.progress_bar.show() + self.progress_bar.setValue(0) + + self.tab_widget.setCurrentWidget(self.process_tab) + + def update_progress(self, message, percentage): + cursor = self.log_output.textCursor() + cursor.movePosition(QTextCursor.MoveOperation.End) + cursor.select(QTextCursor.SelectionType.LineUnderCursor) + current_text = cursor.selectedText() + progress_indicators = [ + "Downloading", + "Account", + "files downloaded" + ] + + should_replace = False + for indicator in progress_indicators: + if indicator in current_text and indicator in message: + should_replace = True + break + if should_replace and current_text.strip(): + cursor.removeSelectedText() + cursor.deletePreviousChar() + + self.log_output.append(message) + self.log_output.moveCursor(QTextCursor.MoveOperation.End) + + if percentage > 0: + self.progress_bar.setValue(percentage) + + def update_conversion_progress(self, message, percentage): + cursor = self.log_output.textCursor() + cursor.movePosition(QTextCursor.MoveOperation.End) + cursor.select(QTextCursor.SelectionType.LineUnderCursor) + current_text = cursor.selectedText() + if "Converting GIF" in current_text: + cursor.removeSelectedText() + cursor.deletePreviousChar() + + self.log_output.append(message) + self.log_output.moveCursor(QTextCursor.MoveOperation.End) + + if percentage > 0: + self.progress_bar.setValue(percentage) + + def update_download_progress(self, message, percentage): + cursor = self.log_output.textCursor() + cursor.movePosition(QTextCursor.MoveOperation.End) + cursor.select(QTextCursor.SelectionType.LineUnderCursor) + + current_text = cursor.selectedText() + if "Downloading" in current_text and ":" in current_text and "/" in current_text: + cursor.removeSelectedText() + cursor.deletePreviousChar() + + self.log_output.append(message) + self.log_output.moveCursor(QTextCursor.MoveOperation.End) + + if percentage > 0: + self.progress_bar.setValue(percentage) + + def stop_download(self): + if hasattr(self, 'worker'): + self.worker.stop() + self.stop_timer() + self.on_download_finished(True, "Download stopped by user.") + + def on_download_finished(self, success, message): + self.progress_bar.hide() + self.stop_btn.hide() + self.pause_resume_btn.hide() + self.pause_resume_btn.setText(' Pause') + icon_dir = ICON_DIR + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), self.current_theme_color)) + self.stop_timer() + + self.download_selected_btn.setEnabled(True) + self.update_selected_btn.setEnabled(True) + + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + if success: + self.log_output.append(f"\nStatus: {message}") + else: + self.log_output.append(f"Error: {message}") + + self.tab_widget.setCurrentWidget(self.process_tab) + + def toggle_pause_resume(self): + if hasattr(self, 'worker'): + icon_dir = ICON_DIR + accent_color = self.current_theme_color + + if self.worker.is_paused: + self.worker.resume() + self.pause_resume_btn.setText(' Pause') + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), accent_color)) + self.timer.start(1000) + else: + self.worker.pause() + self.pause_resume_btn.setText(' Resume') + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'), accent_color)) + + def delete_accounts(self): + selected_items = self.account_list.selectedItems() + + if not selected_items: + visible_accounts = [] + visible_indices = [] + for i in range(self.account_list.count()): + item = self.account_list.item(i) + if not item.isHidden() and i < len(self.accounts): + visible_accounts.append(self.accounts[i]) + visible_indices.append(i) + + visible_count = len(visible_accounts) + reply = QMessageBox.question( + self, + 'Confirm Delete All', + f'No accounts selected. Delete all {visible_count} visible account(s) from database?', + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No + ) + if reply == QMessageBox.StandardButton.No: + return + + for account in visible_accounts: + username = account.username + if username: + try: + self.db_manager.delete_account(username, account.media_type) + self.log_output.append(f'Removed from database: {username} ({account.media_type})') + except Exception as e: + self.log_output.append(f'Warning: Could not remove {username}: {str(e)}') + + for index in sorted(visible_indices, reverse=True): + self.accounts.pop(index) + + self.update_account_list() + self.update_button_states() + else: + reply = QMessageBox.question( + self, + 'Confirm Delete', + f'Delete {len(selected_items)} selected account(s) from database?', + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No + ) + if reply == QMessageBox.StandardButton.No: + return + + selected_indices = sorted([self.account_list.row(item) for item in selected_items], reverse=True) + + for index in selected_indices: + account = self.accounts[index] + username = account.username + if username: + try: + self.db_manager.delete_account(username, account.media_type) + self.log_output.append(f'Removed from database: {username} ({account.media_type})') + except Exception as e: + self.log_output.append(f'Warning: Could not remove {username}: {str(e)}') + + self.accounts.pop(index) + + self.update_account_list() + self.update_button_states() + + def export_accounts(self): + self.tab_widget.setCurrentWidget(self.process_tab) + + selected_items = self.account_list.selectedItems() + + if not selected_items: + reply = QMessageBox.question( + self, + 'Confirm Export All', + f'No accounts selected. Export all {len(self.accounts)} account(s)?', + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No + ) + if reply == QMessageBox.StandardButton.No: + self.log_output.append('Export cancelled') + return + selected_accounts = self.accounts.copy() + else: + selected_accounts = [] + for item in selected_items: + row = self.account_list.row(item) + if 0 <= row < len(self.accounts): + selected_accounts.append(self.accounts[row]) + + if not selected_accounts: + self.log_output.append('No valid accounts found for export') + return + + now = datetime.now() + date_str = now.strftime("%Y%m%d_%H%M%S") + default_filename = f"twitterxmediabatchdownloader_{date_str}.db" + + file_path, _ = QFileDialog.getSaveFileName( + self, + "Export Database", + os.path.join(self.last_output_path, default_filename), + "Database Files (*.db)" + ) + + if not file_path: + self.log_output.append('Export cancelled') + return + + self.log_output.append(f'Starting export of {len(selected_accounts)} account(s) to database...') + + try: + export_db_manager = DatabaseManager(file_path) + exported_count = 0 + + for account in selected_accounts: + try: + export_db_manager.save_account( + username=account.username, + nick=account.nick, + followers=account.followers, + following=account.following, + posts=account.posts, + media_type=account.media_type, + profile_image=account.profile_image, + fetch_mode=account.fetch_mode, + fetch_timestamp=account.fetch_timestamp + ) + + if account.media_list: + export_db_manager.save_media_list(account.username, account.media_list) + + exported_count += 1 + media_count = len(account.media_list) if account.media_list else 0 + self.log_output.append(f'Exported: {account.username} ({media_count:,} media)') + + except Exception as e: + self.log_output.append(f'Error exporting {account.username}: {str(e)}') + + export_db_manager.close() + self.log_output.append(f'Export completed: {exported_count}/{len(selected_accounts)} account(s) exported') + self.log_output.append(f'Export location: {file_path}') + + except Exception as e: + self.log_output.append(f'Export error: {str(e)}') + + def import_accounts(self): + self.tab_widget.setCurrentWidget(self.process_tab) + + file_paths, selected_filter = QFileDialog.getOpenFileNames( + self, + "Select File(s) to Import (Multiple Selection Allowed)", + self.last_output_path, + "All Supported Files (*.db *.json);;Database Files (*.db);;JSON Files (*.json)" + ) + + if not file_paths: + self.log_output.append('Import cancelled - No files selected') + return + + self.log_output.append(f'Starting import of {len(file_paths)} file(s)...') + + total_imported = 0 + total_skipped = 0 + total_errors = 0 + + for file_path in file_paths: + self.log_output.append(f'\n--- Processing: {os.path.basename(file_path)} ---') + + imported_count = 0 + skipped_count = 0 + error_count = 0 + + if file_path.endswith('.db'): + self.log_output.append(f'Importing from database: {os.path.basename(file_path)}') + + try: + import_db_manager = DatabaseManager(file_path) + db_accounts = import_db_manager.get_all_accounts() + + for db_account in db_accounts: + try: + username = db_account['username'] + media_type = db_account['media_type'] + + existing_account = next( + (acc for acc in self.accounts + if acc.username == username and acc.media_type == media_type), + None + ) + + if existing_account: + self.log_output.append(f'Skipped {username} ({media_type}): Already exists in dashboard') + skipped_count += 1 + continue + + media_list = import_db_manager.get_media_list(username) + + account = Account( + username=username, + nick=db_account['nick'], + followers=db_account['followers'], + following=db_account['following'], + posts=db_account['posts'], + media_type=media_type, + profile_image=db_account['profile_image'], + media_list=media_list, + fetch_mode=db_account.get('fetch_mode', 'all'), + fetch_timestamp=db_account.get('fetch_timestamp') + ) + + self.accounts.append(account) + + self.db_manager.save_account( + username=username, + nick=db_account['nick'], + followers=db_account['followers'], + following=db_account['following'], + posts=db_account['posts'], + media_type=media_type, + profile_image=db_account['profile_image'], + fetch_mode=db_account.get('fetch_mode', 'all'), + fetch_timestamp=db_account.get('fetch_timestamp') + ) + + if media_list: + self.db_manager.save_media_list(username, media_list) + + imported_count += 1 + media_count = len(media_list) + self.log_output.append(f'Imported: {username} ({media_type}) - {media_count:,} media') + + except Exception as e: + self.log_output.append(f'Error importing account: {str(e)}') + error_count += 1 + + import_db_manager.close() + + except Exception as e: + self.log_output.append(f'Error opening database: {str(e)}') + error_count += 1 + + elif file_path.endswith('.json'): + self.log_output.append(f'Importing from JSON: {os.path.basename(file_path)}') + + try: + with open(file_path, 'r', encoding='utf-8') as f: + data = json.load(f) + + username = data.get('username') + if not username: + self.log_output.append(f'Error: No username found in JSON file') + error_count += 1 + else: + media_type = data.get('media_type', 'all') + + existing_account = next( + (acc for acc in self.accounts + if acc.username == username and acc.media_type == media_type), + None + ) + + if existing_account: + self.log_output.append(f'Skipped {username} ({media_type}): Already exists in dashboard') + skipped_count += 1 + else: + account = Account( + username=username, + nick=data.get('nick', ''), + followers=data.get('followers', 0), + following=data.get('following', 0), + posts=data.get('posts', 0), + media_type=media_type, + profile_image=data.get('profile_image'), + media_list=data.get('media_list', []), + fetch_mode=data.get('fetch_mode', 'all'), + fetch_timestamp=data.get('fetch_timestamp') + ) + + self.accounts.append(account) + + self.db_manager.save_account( + username=username, + nick=account.nick, + followers=account.followers, + following=account.following, + posts=account.posts, + media_type=media_type, + profile_image=account.profile_image, + fetch_mode=account.fetch_mode, + fetch_timestamp=account.fetch_timestamp + ) + + if account.media_list: + self.db_manager.save_media_list(username, account.media_list) + + imported_count += 1 + media_count = len(account.media_list) if account.media_list else 0 + self.log_output.append(f'Imported: {username} ({media_type}) - {media_count:,} media') + + except Exception as e: + self.log_output.append(f'Error importing JSON: {str(e)}') + error_count += 1 + + else: + self.log_output.append('Error: Unsupported file format') + error_count += 1 + + self.log_output.append(f'File result: {imported_count} imported, {skipped_count} skipped, {error_count} errors') + total_imported += imported_count + total_skipped += skipped_count + total_errors += error_count + + self.update_account_list() + self.update_button_states() + + self.log_output.append(f'\n=== TOTAL IMPORT SUMMARY ===') + self.log_output.append(f'Files processed: {len(file_paths)}') + self.log_output.append(f'Total imported: {total_imported}') + self.log_output.append(f'Total skipped: {total_skipped}') + self.log_output.append(f'Total errors: {total_errors}') + + self.tab_widget.setCurrentIndex(0) + + def update_timer(self): + self.elapsed_time = self.elapsed_time.addSecs(1) + self.time_label.setText(self.elapsed_time.toString("hh:mm:ss")) + + def start_timer(self): + self.elapsed_time = QTime(0, 0, 0) + self.time_label.setText("00:00:00") + self.time_label.show() + self.timer.start(1000) + + def stop_timer(self): + self.timer.stop() + self.time_label.hide() + + def fetch_next_batch(self): + if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): + self.log_output.append('No more batches available.') + return + + self.fetch_next_batch_internal() + + def start_auto_batch(self): + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + self.is_auto_fetching = True + self.is_initial_fetch = False + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + self.stop_fetch_btn.show() + + self.log_output.append('Resuming multiple user batch processing...') + + self.fetch_next_account_in_batch() + return + + if not self.current_fetch_metadata or not self.current_fetch_metadata.get('has_more', False): + self.log_output.append('No more batches available.') + return + + self.is_auto_fetching = True + self.pause_fetch_btn.show() + self.stop_fetch_btn.show() + + self.log_output.append('Auto batch mode enabled.') + self.fetch_next_batch_internal() + + def pause_batch_fetch(self): + if not self.is_auto_fetching: + return + + self.is_auto_fetching = False + + if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): + self.metadata_worker.terminate() + self.metadata_worker.wait() + + self.pause_fetch_btn.setText(' Resume') + icon_dir = ICON_DIR + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'), self.current_theme_color)) + self.pause_fetch_btn.clicked.disconnect() + self.pause_fetch_btn.clicked.connect(self.resume_batch_fetch) + self.log_output.append('Batch fetching paused.') + + def resume_batch_fetch(self): + self.is_auto_fetching = True + self.pause_fetch_btn.setText(' Pause') + icon_dir = ICON_DIR + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), self.current_theme_color)) + self.pause_fetch_btn.clicked.disconnect() + self.pause_fetch_btn.clicked.connect(self.pause_batch_fetch) + self.log_output.append('Resuming batch fetching...') + + if hasattr(self, 'current_fetch_metadata') and self.current_fetch_metadata: + if self.current_fetch_metadata.get('has_more', False): + self.fetch_next_batch_internal() + elif hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + self.fetch_next_account_in_batch() + + def stop_batch_fetch(self): + self.is_auto_fetching = False + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): + self.metadata_worker.terminate() + self.metadata_worker.wait() + + if self.pause_fetch_btn.text() == ' Resume': + self.pause_fetch_btn.setText(' Pause') + icon_dir = ICON_DIR + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'), self.current_theme_color)) + self.pause_fetch_btn.clicked.disconnect() + self.pause_fetch_btn.clicked.connect(self.pause_batch_fetch) + + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + self.log_output.append('Batch processing stopped.') + if hasattr(self, 'accounts_to_fetch'): + delattr(self, 'accounts_to_fetch') + if hasattr(self, 'current_fetch_index'): + delattr(self, 'current_fetch_index') + self.is_multiple_user_mode = False + else: + self.log_output.append('Batch fetching stopped.') + + def stop_auto_fetch(self): + self.stop_batch_fetch() + + def cancel_multiple_user_fetch(self): + self.stop_batch_fetch() + + def disable_batch_buttons(self): + self.pause_fetch_btn.setEnabled(False) + self.stop_fetch_btn.setEnabled(False) + + def enable_batch_buttons(self): + self.pause_fetch_btn.setEnabled(True) + self.stop_fetch_btn.setEnabled(True) + + def fetch_next_batch_internal(self): + if not self.current_fetch_username or not self.current_fetch_media_type or not self.current_fetch_metadata: + return + + current_page = self.current_fetch_metadata.get('page', 0) + next_page = current_page + 1 + + self.log_output.append(f'Fetching batch {next_page + 1} for {self.current_fetch_username}...') + + self.metadata_worker = MetadataFetchWorker( + self.current_fetch_username, + self.current_fetch_media_type, + batch_mode=True, + batch_size=self.batch_size, + page=next_page, + timeline_type=self.timeline_type_combo.currentData() or 'timeline', + include_retweets=self.include_retweets + ) + self.metadata_worker.auth_token = self.auth_token_input.text().strip() + self.metadata_worker.finished.connect(lambda data: self.on_metadata_fetched(data, self.current_fetch_username, self.current_fetch_media_type)) + self.metadata_worker.error.connect(self.on_metadata_error) + self.metadata_worker.start() + + def on_tab_changed(self, index): + if index == 0: + self.update_button_states() + + def load_groups(self): + self.group_list.clear() + self.group_filter_combo.clear() + self.group_filter_combo.addItem("All Groups", None) + self.group_filter_combo.addItem("No Group", -1) + + groups = self.db_manager.get_all_groups() + for group in groups: + accounts_in_group = self.db_manager.get_accounts_by_group(group['id']) + account_count = len(accounts_in_group) + item = QListWidgetItem(f"{group['name']} ({account_count})") + item.setData(Qt.ItemDataRole.UserRole, group['id']) + self.group_list.addItem(item) + self.group_filter_combo.addItem(group['name'], group['id']) + + def create_group(self): + name = self.group_name_input.text().strip() + if not name: + QMessageBox.warning(self, "Warning", "Please enter a group name.") + return + + group_id = self.db_manager.create_group(name) + if group_id: + self.group_name_input.clear() + self.load_groups() + QMessageBox.information(self, "Success", f"Group '{name}' created successfully!") + else: + QMessageBox.warning(self, "Error", f"Group '{name}' already exists.") + + def on_group_selected(self): + selected = len(self.group_list.selectedItems()) > 0 + self.rename_group_btn.setEnabled(selected) + self.delete_group_btn.setEnabled(selected) + + if selected: + self.load_assign_accounts() + else: + self.assign_account_list.clear() + self.save_assignment_btn.setEnabled(False) + + def load_assign_accounts(self): + selected_items = self.group_list.selectedItems() + if not selected_items: + return + + group_id = selected_items[0].data(Qt.ItemDataRole.UserRole) + + self.assign_account_list.blockSignals(True) + self.assign_account_list.clear() + + for account in self.accounts: + if account.group_id is not None and account.group_id != group_id: + continue + + item_text = f"{account.username} ({account.media_type})" + item = QListWidgetItem(item_text) + item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable) + item.setCheckState(Qt.CheckState.Checked if account.group_id == group_id else Qt.CheckState.Unchecked) + item.setData(Qt.ItemDataRole.UserRole, (account.username, account.media_type)) + self.assign_account_list.addItem(item) + + self.assign_account_list.blockSignals(False) + self.save_assignment_btn.setEnabled(True) + + def filter_assign_accounts(self): + search_text = self.assign_search_input.text().lower().strip() + for i in range(self.assign_account_list.count()): + item = self.assign_account_list.item(i) + if search_text: + item_text = item.text().lower() + item.setHidden(search_text not in item_text) + else: + item.setHidden(False) + + def on_assign_item_changed(self, item): + pass + + def save_account_assignment(self): + selected_group_items = self.group_list.selectedItems() + if not selected_group_items: + return + + group_id = selected_group_items[0].data(Qt.ItemDataRole.UserRole) + group = self.db_manager.get_group(group_id) + + if not group: + return + + checked_usernames = [] + unchecked_usernames = [] + + for i in range(self.assign_account_list.count()): + item = self.assign_account_list.item(i) + username, media_type = item.data(Qt.ItemDataRole.UserRole) + if item.checkState() == Qt.CheckState.Checked: + checked_usernames.append(username) + else: + unchecked_usernames.append(username) + + for account in self.accounts: + if account.username in checked_usernames: + account.group_id = group_id + self.db_manager.assign_account_to_group(account.username, group_id) + elif account.username in unchecked_usernames and account.group_id == group_id: + account.group_id = None + self.db_manager.assign_account_to_group(account.username, None) + + self.load_groups() + self.load_assign_accounts() + self.update_account_list() + QMessageBox.information(self, "Success", f"Accounts assigned to '{group['name']}'!") + + def rename_group(self): + selected_items = self.group_list.selectedItems() + if not selected_items: + return + + item = selected_items[0] + group_id = item.data(Qt.ItemDataRole.UserRole) + group = self.db_manager.get_group(group_id) + + if not group: + return + + new_name, ok = QInputDialog.getText(self, "Rename Group", "Enter new name:", text=group['name']) + + if ok and new_name.strip(): + if self.db_manager.update_group(group_id, new_name.strip()): + self.load_groups() + self.update_account_list() + QMessageBox.information(self, "Success", f"Group renamed to '{new_name}'!") + else: + QMessageBox.warning(self, "Error", f"Group name '{new_name}' already exists.") + + def delete_group(self): + selected_items = self.group_list.selectedItems() + if not selected_items: + return + + item = selected_items[0] + group_id = item.data(Qt.ItemDataRole.UserRole) + group = self.db_manager.get_group(group_id) + + if not group: + return + + reply = QMessageBox.question( + self, + 'Confirm Delete', + f"Delete group '{group['name']}'?\nAccounts in this group will not be deleted.", + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No + ) + + if reply == QMessageBox.StandardButton.Yes: + self.db_manager.delete_group(group_id) + self.assign_account_list.clear() + self.save_assignment_btn.setEnabled(False) + + self.group_filter_combo.blockSignals(True) + self.load_groups() + self.group_filter_combo.setCurrentIndex(0) + self.group_filter_combo.blockSignals(False) + + self.accounts.clear() + self.load_all_cached_accounts() + self.update_account_list() + + QMessageBox.information(self, "Success", f"Group '{group['name']}' deleted!") + + + + def on_group_filter_changed(self): + self.filter_accounts() + if hasattr(self, 'is_initializing') and not self.is_initializing: + if hasattr(self, 'settings') and hasattr(self, 'group_filter_combo'): + group_filter = self.group_filter_combo.currentData() + if group_filter is None: + self.settings.setValue('group_filter', 'None') + else: + self.settings.setValue('group_filter', int(group_filter)) + self.settings.sync() + + def filter_accounts(self): + search_text = self.search_input.text().lower().strip() + selected_group_id = self.group_filter_combo.currentData() + + for i in range(self.account_list.count()): + item = self.account_list.item(i) + account = self.accounts[i] if i < len(self.accounts) else None + + if not account: + continue + + show_item = True + + if search_text: + item_text = item.text().lower() + if search_text not in item_text: + show_item = False + + if show_item and selected_group_id is not None: + if selected_group_id == -1: + if account.group_id is not None: + show_item = False + else: + if account.group_id != selected_group_id: + show_item = False + + item.setHidden(not show_item) diff --git a/ui/mainWindow_Unix.py b/ui/mainWindow_Unix.py new file mode 100644 index 0000000..aa79214 --- /dev/null +++ b/ui/mainWindow_Unix.py @@ -0,0 +1,267 @@ +import os +from PyQt6.QtWidgets import QApplication, QLabel +from PyQt6.QtCore import Qt, QSize, QEvent +from PyQt6.QtGui import QIcon, QPalette, QColor +from ui.mainWindow import TwitterXMediaBatchDownloaderGUI + +class TwitterXMediaBatchDownloaderGUI_Unix(TwitterXMediaBatchDownloaderGUI): + def __init__(self): + super().__init__() + self.setup_unix_specific() + + QApplication.instance().installEventFilter(self) + + def eventFilter(self, obj, event): + if event.type() == QEvent.Type.ApplicationPaletteChange: + self.update_button_icons_unix() + self.update_ui_colors() + return super().eventFilter(obj, event) + + def update_ui_colors(self): + from PyQt6.QtWidgets import QLineEdit, QTextEdit, QComboBox, QCheckBox, QPushButton, QProgressBar, QDateEdit, QListWidget, QTabWidget + + new_palette = QApplication.palette() + + self.account_list.setStyleSheet(""" + QListWidget { + background-color: palette(base); + padding: 4px; + outline: none; + } + QListWidget::item { + padding: 3px 6px; + margin: 0px 0px; + border: none; + outline: none; + } + QListWidget::item:selected { + background-color: palette(highlight); + border: none; + outline: none; + } + QListWidget::item:focus { + border: none; + outline: none; + } + QListWidget::item:hover { + background-color: palette(highlight); + } + """) + + widget_types = [QLabel, QLineEdit, QTextEdit, QComboBox, QCheckBox, QPushButton, QProgressBar, QDateEdit, QListWidget, QTabWidget] + + for widget_type in widget_types: + for widget in self.findChildren(widget_type): + widget.setPalette(new_palette) + widget.update() + + for i in range(self.account_list.count()): + item = self.account_list.item(i) + if item: + self.account_list.update(self.account_list.indexFromItem(item)) + + self.setPalette(new_palette) + self.update() + + def setup_unix_specific(self): + self.setFixedWidth(670) + + if hasattr(self, 'fetch_btn'): + self.fetch_btn.setFixedWidth(100) + + if hasattr(self, 'start_date_edit'): + self.start_date_edit.setMinimumWidth(130) + + if hasattr(self, 'end_date_edit'): + self.end_date_edit.setMinimumWidth(130) + + if hasattr(self, 'output_browse'): + self.output_browse.setFixedWidth(100) + + self.account_list.setStyleSheet(""" + QListWidget { + background-color: palette(base); + padding: 4px; + outline: none; + } + QListWidget::item { + padding: 3px 6px; + margin: 0px 0px; + border: none; + outline: none; + } + QListWidget::item:selected { + background-color: palette(highlight); + border: none; + outline: none; + } + QListWidget::item:focus { + border: none; + outline: none; + } + QListWidget::item:hover { + background-color: palette(highlight); + } + """) + + self.update_button_icons_unix() + + def create_colored_icon(self, icon_path, color=None): + if not os.path.exists(icon_path): + return QIcon() + + palette = QApplication.palette() + text_color = palette.color(QPalette.ColorRole.Text) + color_hex = text_color.name() + + from PyQt6.QtSvg import QSvgRenderer + from PyQt6.QtGui import QPixmap, QPainter + + renderer = QSvgRenderer(icon_path) + pixmap = QPixmap(24, 24) + pixmap.fill(Qt.GlobalColor.transparent) + + painter = QPainter(pixmap) + renderer.render(painter) + painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn) + painter.fillRect(pixmap.rect(), QColor(color_hex)) + painter.end() + + return QIcon(pixmap) + + def update_button_icons(self): + self.update_button_icons_unix() + + def update_button_icons_unix_dashboard(self): + if not hasattr(self, 'import_btn'): + return + + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + + self.import_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-import.svg'))) + self.export_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'database-export.svg'))) + self.download_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'download.svg'))) + self.update_selected_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'reload.svg'))) + self.delete_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'trash.svg'))) + + def update_button_icons_unix(self): + self.update_button_icons_unix_dashboard() + + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + + if hasattr(self, 'stop_btn'): + self.stop_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'cancel.svg'))) + if hasattr(self, 'pause_resume_btn'): + if self.pause_resume_btn.text() == ' Resume': + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'))) + else: + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + if hasattr(self, 'stop_fetch_btn'): + self.stop_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'cancel.svg'))) + if hasattr(self, 'pause_fetch_btn'): + if self.pause_fetch_btn.text() == ' Resume': + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'))) + else: + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + + def setup_theme_tab(self): + pass + + def change_theme_color(self, color, clicked_btn=None): + pass + + def toggle_pause_resume(self): + if hasattr(self, 'worker'): + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + + if self.worker.is_paused: + self.worker.resume() + self.pause_resume_btn.setText(' Pause') + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + self.timer.start(1000) + else: + self.worker.pause() + self.pause_resume_btn.setText(' Resume') + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'))) + + def pause_batch_fetch(self): + self.is_auto_fetching = False + + if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): + self.metadata_worker.terminate() + self.metadata_worker.wait() + + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + self.pause_fetch_btn.setText(' Resume') + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-play.svg'))) + self.pause_fetch_btn.clicked.disconnect() + self.pause_fetch_btn.clicked.connect(self.resume_batch_fetch) + self.log_output.append('Batch fetching paused.') + + def resume_batch_fetch(self): + self.is_auto_fetching = True + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + self.pause_fetch_btn.setText(' Pause') + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + self.pause_fetch_btn.clicked.disconnect() + self.pause_fetch_btn.clicked.connect(self.pause_batch_fetch) + self.log_output.append('Resuming batch fetching...') + + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + self.fetch_next_account_in_batch() + else: + self.fetch_next_batch_internal() + + def stop_batch_fetch(self): + self.is_auto_fetching = False + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + if hasattr(self, 'metadata_worker') and self.metadata_worker.isRunning(): + self.metadata_worker.terminate() + self.metadata_worker.wait() + + if self.pause_fetch_btn.text() == ' Resume': + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + self.pause_fetch_btn.setText(' Pause') + self.pause_fetch_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + self.pause_fetch_btn.clicked.disconnect() + self.pause_fetch_btn.clicked.connect(self.pause_batch_fetch) + + if hasattr(self, 'is_multiple_user_mode') and self.is_multiple_user_mode: + self.log_output.append('Batch processing stopped.') + self.is_multiple_user_mode = False + if hasattr(self, 'accounts_to_fetch'): + del self.accounts_to_fetch + else: + self.log_output.append('Auto batch mode stopped.') + + self.enable_batch_buttons() + + def reset_ui(self): + super().reset_ui() + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + if hasattr(self, 'pause_resume_btn'): + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + + def on_download_finished(self, success, message): + self.progress_bar.hide() + self.stop_btn.hide() + self.pause_resume_btn.hide() + self.pause_resume_btn.setText(' Pause') + icon_dir = os.path.join(os.path.dirname(__file__), 'icons') + self.pause_resume_btn.setIcon(self.create_colored_icon(os.path.join(icon_dir, 'player-pause.svg'))) + self.stop_timer() + + self.download_selected_btn.setEnabled(True) + self.update_selected_btn.setEnabled(True) + + self.pause_fetch_btn.hide() + self.stop_fetch_btn.hide() + + if success: + self.log_output.append(f"\nStatus: {message}") + else: + self.log_output.append(f"Error: {message}") + + self.tab_widget.setCurrentWidget(self.process_tab) \ No newline at end of file