mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-11-21 13:53:13 +01:00
v4.0
This commit is contained in:
1 parent
8e5f18c6cc
commit
280133649d
92 files changed
+15747
-4673
No files matched your search
+62
@@ -0,0 +1,62 @@
|
||||
# Wails Build
|
||||
build/
|
||||
backend/bin/
|
||||
*.exe
|
||||
*.dll
|
||||
*.dylib
|
||||
*.so
|
||||
|
||||
# Wails Generated Files
|
||||
frontend/wailsjs/
|
||||
|
||||
# Go
|
||||
*.test
|
||||
*.out
|
||||
go.work
|
||||
go.work.sum
|
||||
|
||||
# Node / Frontend
|
||||
node_modules/
|
||||
frontend/node_modules/
|
||||
frontend/dist/
|
||||
frontend/.vite/
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
.pnpm-store/
|
||||
.npm
|
||||
.yarn
|
||||
*.tsbuildinfo
|
||||
|
||||
# IDE / Editors
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
.DS_Store
|
||||
|
||||
# OS
|
||||
Thumbs.db
|
||||
desktop.ini
|
||||
|
||||
# Environment
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
logs/
|
||||
|
||||
# Temporary files
|
||||
tmp/
|
||||
temp/
|
||||
*.tmp
|
||||
*.bak
|
||||
*.old
|
||||
|
||||
# Build notes (optional - uncomment if you don't want to commit)
|
||||
# BUILD_NOTES.md
|
||||
build.txt
|
||||
@@ -3,47 +3,23 @@
|
||||

|
||||
|
||||
<div align="center">
|
||||
<b>Twitter/X Media Batch Downloader</b> is a GUI tool that allows users to download all media, including images and videos, in their original quality from Twitter/X accounts using <code>gallery-dl</code>
|
||||
|
||||
A GUI tool to download original-quality images and videos from Twitter/X accounts, powered by gallery-dl by @mikf
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
</div>
|
||||
|
||||
## 📥 Download
|
||||
### [Download](https://github.com/afkarxyz/SpotiFLAC/releases)
|
||||
|
||||
- [**Windows**](https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/latest/download/TwitterXMediaBatchDownloader_Windows.exe)
|
||||
- [**Linux**](https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/latest/download/TwitterXMediaBatchDownloader_Linux)
|
||||
- [**Mac**](https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/latest/download/TwitterXMediaBatchDownloader_Mac.zip)
|
||||
- If you're familiar with **userscripts**, please install it [**here**](https://greasyfork.org/en/scripts/523157)
|
||||
- The **Chrome extension** version is now available, please install it [**here**](https://www.patreon.com/posts/chrome-extension-139296789)
|
||||
|
||||
## ✨ Features
|
||||
|
||||
- Powered by `gallery-dl`, similar to `yt-dlp`
|
||||
- **Bulk fetch**: Fetch multiple accounts at once
|
||||
- **Bulk download**: Download multiple accounts at once
|
||||
- Media-type selection: All (Images + GIFs + Videos) or specific (Images / GIFs / Videos)
|
||||
- **MP4 → GIF conversion:** Convert downloaded MP4 videos into GIF format
|
||||
- Downloads at original quality
|
||||
|
||||
## 🖼️ Screenshot
|
||||
## Screenshot
|
||||
|
||||

|
||||
|
||||
### The downloaded application won't run❓
|
||||
|
||||
🐧 **Solution (Ubuntu/Linux)**: Open your terminal and run:
|
||||
|
||||
```bash
|
||||
chmod +x TwitterXMediaBatchDownloader_Linux
|
||||
```
|
||||

|
||||
|
||||
🍎 **Solution (macOS)**: Open your terminal and run:
|
||||
|
||||
```bash
|
||||
xattr -rd com.apple.quarantine ~/Downloads/TwitterXMediaBatchDownloader_Mac.app
|
||||
```
|
||||

|
||||
|
||||
## 🔑 How to Obtain Auth Token
|
||||
## How to Obtain Auth Token
|
||||
|
||||
> [!Warning]
|
||||
> - I suggest not using the **main account** to obtain the token.
|
||||
@@ -55,7 +31,7 @@ xattr -rd com.apple.quarantine ~/Downloads/TwitterXMediaBatchDownloader_Mac.app
|
||||
|
||||
1. Go to [Twitter's website](https://www.x.com/)
|
||||
2. Log into your account
|
||||
3. Open the Developer Tools by pressing `F12`
|
||||
3. Open the Developer Tools by pressing <kbd>F12</kbd>
|
||||
4. Navigate to the **Application** tab, then select **Storage → Cookies**
|
||||
5. Find and copy the `auth_token` value or use the browser extension [X.com Auth Token Grabber](https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/download/v1.0/X.com.Auth.Token.Grabber.zip)
|
||||
6. Do not log out of your account, as a new `auth_token` will be generated by Twitter
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import sys
|
||||
import os
|
||||
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 = TwitterXMediaBatchDownloaderGUI_Unix()
|
||||
window.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,18 +0,0 @@
|
||||
import sys
|
||||
import os
|
||||
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 = TwitterXMediaBatchDownloaderGUI_Unix()
|
||||
window.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,29 +0,0 @@
|
||||
import sys
|
||||
import os
|
||||
import qdarktheme
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
from PyQt6.QtCore import QSettings
|
||||
from ui.mainWindow import TwitterXMediaBatchDownloaderGUI
|
||||
|
||||
def main():
|
||||
if getattr(sys, 'frozen', False):
|
||||
os.chdir(os.path.dirname(sys.executable))
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
settings = QSettings('TwitterXMediaBatchDownloader', 'Settings')
|
||||
theme_color = settings.value('theme_color', '#2196F3')
|
||||
|
||||
qdarktheme.setup_theme(
|
||||
custom_colors={
|
||||
"[dark]": {
|
||||
"primary": theme_color,
|
||||
}
|
||||
}
|
||||
)
|
||||
window = TwitterXMediaBatchDownloaderGUI()
|
||||
window.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,490 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"twitterxmediabatchdownloader/backend"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// App struct
|
||||
type App struct {
|
||||
ctx context.Context
|
||||
downloadCtx context.Context
|
||||
downloadCancel context.CancelFunc
|
||||
}
|
||||
|
||||
// NewApp creates a new App application struct
|
||||
func NewApp() *App {
|
||||
return &App{}
|
||||
}
|
||||
|
||||
// startup is called when the app starts. The context is saved
|
||||
// so we can call the runtime methods
|
||||
func (a *App) startup(ctx context.Context) {
|
||||
a.ctx = ctx
|
||||
// Initialize database
|
||||
backend.InitDB()
|
||||
// Kill any leftover extractor processes from previous session
|
||||
backend.KillAllExtractorProcesses()
|
||||
}
|
||||
|
||||
// shutdown is called when the app is closing
|
||||
func (a *App) shutdown(ctx context.Context) {
|
||||
backend.CloseDB()
|
||||
// Kill any running extractor processes
|
||||
backend.KillAllExtractorProcesses()
|
||||
}
|
||||
|
||||
// CleanupExtractorProcesses kills all running extractor processes
|
||||
// Can be called from frontend when user wants to stop/reset
|
||||
func (a *App) CleanupExtractorProcesses() {
|
||||
backend.KillAllExtractorProcesses()
|
||||
}
|
||||
|
||||
// TimelineRequest represents the request structure for timeline extraction
|
||||
type TimelineRequest struct {
|
||||
Username string `json:"username"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
TimelineType string `json:"timeline_type"`
|
||||
BatchSize int `json:"batch_size"`
|
||||
Page int `json:"page"`
|
||||
MediaType string `json:"media_type"`
|
||||
Retweets bool `json:"retweets"`
|
||||
Cursor string `json:"cursor,omitempty"` // Resume from this cursor position
|
||||
}
|
||||
|
||||
// DateRangeRequest represents the request structure for date range extraction
|
||||
type DateRangeRequest struct {
|
||||
Username string `json:"username"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
StartDate string `json:"start_date"`
|
||||
EndDate string `json:"end_date"`
|
||||
MediaFilter string `json:"media_filter"`
|
||||
Retweets bool `json:"retweets"`
|
||||
}
|
||||
|
||||
// ExtractTimeline extracts media from user timeline
|
||||
func (a *App) ExtractTimeline(req TimelineRequest) (string, error) {
|
||||
// Username not required for bookmarks only
|
||||
if req.Username == "" && req.TimelineType != "bookmarks" {
|
||||
return "", fmt.Errorf("username is required")
|
||||
}
|
||||
if req.AuthToken == "" {
|
||||
return "", fmt.Errorf("auth token is required")
|
||||
}
|
||||
|
||||
backendReq := backend.TimelineRequest{
|
||||
Username: req.Username,
|
||||
AuthToken: req.AuthToken,
|
||||
TimelineType: req.TimelineType,
|
||||
BatchSize: req.BatchSize,
|
||||
Page: req.Page,
|
||||
MediaType: req.MediaType,
|
||||
Retweets: req.Retweets,
|
||||
Cursor: req.Cursor,
|
||||
}
|
||||
|
||||
response, err := backend.ExtractTimeline(backendReq)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to extract timeline: %v", err)
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(response, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to encode response: %v", err)
|
||||
}
|
||||
|
||||
return string(jsonData), nil
|
||||
}
|
||||
|
||||
// ExtractDateRange extracts media based on date range
|
||||
func (a *App) ExtractDateRange(req DateRangeRequest) (string, error) {
|
||||
if req.Username == "" {
|
||||
return "", fmt.Errorf("username is required")
|
||||
}
|
||||
if req.AuthToken == "" {
|
||||
return "", fmt.Errorf("auth token is required")
|
||||
}
|
||||
if req.StartDate == "" {
|
||||
return "", fmt.Errorf("start date is required")
|
||||
}
|
||||
if req.EndDate == "" {
|
||||
return "", fmt.Errorf("end date is required")
|
||||
}
|
||||
|
||||
backendReq := backend.DateRangeRequest{
|
||||
Username: req.Username,
|
||||
AuthToken: req.AuthToken,
|
||||
StartDate: req.StartDate,
|
||||
EndDate: req.EndDate,
|
||||
MediaFilter: req.MediaFilter,
|
||||
Retweets: req.Retweets,
|
||||
}
|
||||
|
||||
response, err := backend.ExtractDateRange(backendReq)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to extract date range: %v", err)
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(response, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to encode response: %v", err)
|
||||
}
|
||||
|
||||
return string(jsonData), nil
|
||||
}
|
||||
|
||||
// OpenFolder opens a folder in the file explorer
|
||||
func (a *App) OpenFolder(path string) error {
|
||||
if path == "" {
|
||||
return fmt.Errorf("path is required")
|
||||
}
|
||||
|
||||
// Clean the path to use correct separators for the OS
|
||||
cleanPath := filepath.Clean(path)
|
||||
|
||||
err := backend.OpenFolderInExplorer(cleanPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open folder: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SelectFolder opens a folder selection dialog and returns the selected path
|
||||
func (a *App) SelectFolder(defaultPath string) (string, error) {
|
||||
return backend.SelectFolderDialog(a.ctx, defaultPath)
|
||||
}
|
||||
|
||||
// GetDefaults returns the default configuration
|
||||
func (a *App) GetDefaults() map[string]string {
|
||||
return map[string]string{
|
||||
"downloadPath": backend.GetDefaultDownloadPath(),
|
||||
}
|
||||
}
|
||||
|
||||
// Quit closes the application
|
||||
func (a *App) Quit() {
|
||||
panic("quit")
|
||||
}
|
||||
|
||||
// DownloadMediaRequest represents the request for downloading media (legacy)
|
||||
type DownloadMediaRequest struct {
|
||||
URLs []string `json:"urls"`
|
||||
OutputDir string `json:"output_dir"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// MediaItemRequest represents a media item with metadata
|
||||
type MediaItemRequest struct {
|
||||
URL string `json:"url"`
|
||||
Date string `json:"date"`
|
||||
TweetID backend.TweetIDString `json:"tweet_id"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content,omitempty"` // Tweet text content (for text-only tweets)
|
||||
}
|
||||
|
||||
// DownloadMediaWithMetadataRequest represents the request for downloading media with metadata
|
||||
type DownloadMediaWithMetadataRequest struct {
|
||||
Items []MediaItemRequest `json:"items"`
|
||||
OutputDir string `json:"output_dir"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// DownloadMediaResponse represents the response for download operation
|
||||
type DownloadMediaResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Downloaded int `json:"downloaded"`
|
||||
Failed int `json:"failed"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// DownloadMedia downloads media files from URLs (legacy)
|
||||
func (a *App) DownloadMedia(req DownloadMediaRequest) (DownloadMediaResponse, error) {
|
||||
if len(req.URLs) == 0 {
|
||||
return DownloadMediaResponse{
|
||||
Success: false,
|
||||
Message: "No URLs provided",
|
||||
}, fmt.Errorf("no URLs provided")
|
||||
}
|
||||
|
||||
outputDir := req.OutputDir
|
||||
if outputDir == "" {
|
||||
outputDir = backend.GetDefaultDownloadPath()
|
||||
}
|
||||
|
||||
// Create subfolder for username if provided
|
||||
if req.Username != "" {
|
||||
outputDir = filepath.Join(outputDir, req.Username)
|
||||
}
|
||||
|
||||
downloaded, failed, err := backend.DownloadMediaFiles(req.URLs, outputDir)
|
||||
if err != nil {
|
||||
return DownloadMediaResponse{
|
||||
Success: false,
|
||||
Downloaded: downloaded,
|
||||
Failed: failed,
|
||||
Message: err.Error(),
|
||||
}, err
|
||||
}
|
||||
|
||||
return DownloadMediaResponse{
|
||||
Success: true,
|
||||
Downloaded: downloaded,
|
||||
Failed: failed,
|
||||
Message: fmt.Sprintf("Downloaded %d files, %d failed", downloaded, failed),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DownloadProgress represents download progress event data
|
||||
type DownloadProgress struct {
|
||||
Current int `json:"current"`
|
||||
Total int `json:"total"`
|
||||
Percent int `json:"percent"`
|
||||
}
|
||||
|
||||
// DownloadItemStatus represents per-item download status event data
|
||||
type DownloadItemStatus struct {
|
||||
TweetID int64 `json:"tweet_id"`
|
||||
Index int `json:"index"`
|
||||
Status string `json:"status"` // "success", "failed", "skipped"
|
||||
}
|
||||
|
||||
// DownloadMediaWithMetadata downloads media files with proper naming and categorization
|
||||
func (a *App) DownloadMediaWithMetadata(req DownloadMediaWithMetadataRequest) (DownloadMediaResponse, error) {
|
||||
if len(req.Items) == 0 {
|
||||
return DownloadMediaResponse{
|
||||
Success: false,
|
||||
Message: "No items provided",
|
||||
}, fmt.Errorf("no items provided")
|
||||
}
|
||||
|
||||
outputDir := req.OutputDir
|
||||
if outputDir == "" {
|
||||
outputDir = backend.GetDefaultDownloadPath()
|
||||
}
|
||||
|
||||
// Convert request items to backend items
|
||||
items := make([]backend.MediaItem, len(req.Items))
|
||||
for i, item := range req.Items {
|
||||
items[i] = backend.MediaItem{
|
||||
URL: item.URL,
|
||||
Date: item.Date,
|
||||
TweetID: int64(item.TweetID),
|
||||
Type: item.Type,
|
||||
Username: req.Username,
|
||||
Content: item.Content,
|
||||
}
|
||||
}
|
||||
|
||||
// Create cancellable context
|
||||
a.downloadCtx, a.downloadCancel = context.WithCancel(context.Background())
|
||||
|
||||
// Progress callback
|
||||
progressCallback := func(current, total int) {
|
||||
percent := 0
|
||||
if total > 0 {
|
||||
percent = (current * 100) / total
|
||||
}
|
||||
runtime.EventsEmit(a.ctx, "download-progress", DownloadProgress{
|
||||
Current: current,
|
||||
Total: total,
|
||||
Percent: percent,
|
||||
})
|
||||
}
|
||||
|
||||
// Per-item status callback
|
||||
itemStatusCallback := func(tweetID int64, index int, status string) {
|
||||
runtime.EventsEmit(a.ctx, "download-item-status", DownloadItemStatus{
|
||||
TweetID: tweetID,
|
||||
Index: index,
|
||||
Status: status,
|
||||
})
|
||||
}
|
||||
|
||||
downloaded, failed, err := backend.DownloadMediaWithMetadataProgressAndStatus(items, outputDir, req.Username, progressCallback, itemStatusCallback, a.downloadCtx)
|
||||
if err != nil {
|
||||
return DownloadMediaResponse{
|
||||
Success: false,
|
||||
Downloaded: downloaded,
|
||||
Failed: failed,
|
||||
Message: err.Error(),
|
||||
}, err
|
||||
}
|
||||
|
||||
// Clear cancel function
|
||||
a.downloadCancel = nil
|
||||
|
||||
return DownloadMediaResponse{
|
||||
Success: true,
|
||||
Downloaded: downloaded,
|
||||
Failed: failed,
|
||||
Message: fmt.Sprintf("Downloaded %d files, %d failed", downloaded, failed),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// StopDownload cancels the current download operation
|
||||
func (a *App) StopDownload() bool {
|
||||
if a.downloadCancel != nil {
|
||||
a.downloadCancel()
|
||||
a.downloadCancel = nil
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Database functions
|
||||
|
||||
// SaveAccountToDB saves account data to database
|
||||
func (a *App) SaveAccountToDB(username, name, profileImage string, totalMedia int, responseJSON string, mediaType string) error {
|
||||
return backend.SaveAccount(username, name, profileImage, totalMedia, responseJSON, mediaType)
|
||||
}
|
||||
|
||||
// SaveAccountToDBWithStatus saves account data with cursor and completion status for resume capability
|
||||
func (a *App) SaveAccountToDBWithStatus(username, name, profileImage string, totalMedia int, responseJSON string, mediaType string, cursor string, completed bool) error {
|
||||
return backend.SaveAccountWithStatus(username, name, profileImage, totalMedia, responseJSON, mediaType, cursor, completed)
|
||||
}
|
||||
|
||||
// GetAllAccountsFromDB returns all saved accounts
|
||||
func (a *App) GetAllAccountsFromDB() ([]backend.AccountListItem, error) {
|
||||
return backend.GetAllAccounts()
|
||||
}
|
||||
|
||||
// GetAccountFromDB returns account data by ID
|
||||
func (a *App) GetAccountFromDB(id int64) (string, error) {
|
||||
acc, err := backend.GetAccountByID(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return acc.ResponseJSON, nil
|
||||
}
|
||||
|
||||
// DeleteAccountFromDB deletes an account from database
|
||||
func (a *App) DeleteAccountFromDB(id int64) error {
|
||||
return backend.DeleteAccount(id)
|
||||
}
|
||||
|
||||
// ClearAllAccountsFromDB deletes all accounts from database
|
||||
func (a *App) ClearAllAccountsFromDB() error {
|
||||
return backend.ClearAllAccounts()
|
||||
}
|
||||
|
||||
// ExportAccountJSON exports account to JSON file in specified directory
|
||||
func (a *App) ExportAccountJSON(id int64, outputDir string) (string, error) {
|
||||
return backend.ExportAccountToFile(id, outputDir)
|
||||
}
|
||||
|
||||
// UpdateAccountGroup updates the group for an account
|
||||
func (a *App) UpdateAccountGroup(id int64, groupName, groupColor string) error {
|
||||
return backend.UpdateAccountGroup(id, groupName, groupColor)
|
||||
}
|
||||
|
||||
// GetAllGroups returns all unique groups
|
||||
func (a *App) GetAllGroups() ([]map[string]string, error) {
|
||||
return backend.GetAllGroups()
|
||||
}
|
||||
|
||||
// FFmpeg functions
|
||||
|
||||
// IsFFmpegInstalled checks if ffmpeg is available
|
||||
func (a *App) IsFFmpegInstalled() bool {
|
||||
return backend.IsFFmpegInstalled()
|
||||
}
|
||||
|
||||
// DownloadFFmpeg downloads ffmpeg binary
|
||||
func (a *App) DownloadFFmpeg() error {
|
||||
return backend.DownloadFFmpeg(nil)
|
||||
}
|
||||
|
||||
// ConvertGIFsRequest represents request for converting GIFs
|
||||
type ConvertGIFsRequest struct {
|
||||
FolderPath string `json:"folder_path"`
|
||||
Quality string `json:"quality"` // "fast" or "better"
|
||||
Resolution string `json:"resolution"` // "original", "high", "medium", "low"
|
||||
DeleteOriginal bool `json:"delete_original"`
|
||||
}
|
||||
|
||||
// ConvertGIFsResponse represents response for GIF conversion
|
||||
type ConvertGIFsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Converted int `json:"converted"`
|
||||
Failed int `json:"failed"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// ConvertGIFs converts MP4 files in gifs folder to actual GIF format
|
||||
func (a *App) ConvertGIFs(req ConvertGIFsRequest) (ConvertGIFsResponse, error) {
|
||||
if !backend.IsFFmpegInstalled() {
|
||||
return ConvertGIFsResponse{
|
||||
Success: false,
|
||||
Message: "FFmpeg not installed. Please download it first.",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Default values if not provided
|
||||
quality := req.Quality
|
||||
if quality == "" {
|
||||
quality = "fast"
|
||||
}
|
||||
resolution := req.Resolution
|
||||
if resolution == "" {
|
||||
resolution = "high"
|
||||
}
|
||||
|
||||
converted, failed, err := backend.ConvertGIFsInFolder(req.FolderPath, quality, resolution, req.DeleteOriginal)
|
||||
if err != nil {
|
||||
return ConvertGIFsResponse{
|
||||
Success: false,
|
||||
Message: err.Error(),
|
||||
}, err
|
||||
}
|
||||
|
||||
return ConvertGIFsResponse{
|
||||
Success: true,
|
||||
Converted: converted,
|
||||
Failed: failed,
|
||||
Message: fmt.Sprintf("Converted %d GIFs, %d failed", converted, failed),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ImportAccountResponse represents the response for import operation
|
||||
type ImportAccountResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Username string `json:"username"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// ImportAccountFromJSON imports account from JSON file (supports both old and new format)
|
||||
func (a *App) ImportAccountFromJSON() (ImportAccountResponse, error) {
|
||||
// Open file dialog
|
||||
filePath, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
|
||||
Title: "Import Account JSON",
|
||||
Filters: []runtime.FileFilter{
|
||||
{DisplayName: "JSON Files", Pattern: "*.json"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return ImportAccountResponse{Success: false, Message: err.Error()}, err
|
||||
}
|
||||
|
||||
// User cancelled
|
||||
if filePath == "" {
|
||||
return ImportAccountResponse{Success: false, Message: "Cancelled"}, nil
|
||||
}
|
||||
|
||||
// Import the file
|
||||
username, err := backend.ImportAccountFromFile(filePath)
|
||||
if err != nil {
|
||||
return ImportAccountResponse{Success: false, Message: err.Error()}, err
|
||||
}
|
||||
|
||||
return ImportAccountResponse{
|
||||
Success: true,
|
||||
Username: username,
|
||||
Message: fmt.Sprintf("Successfully imported @%s", username),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
//go:build !windows
|
||||
|
||||
package backend
|
||||
|
||||
import "os/exec"
|
||||
|
||||
// hideWindow is a no-op on non-Windows platforms
|
||||
func hideWindow(cmd *exec.Cmd) {
|
||||
// No action needed on Unix-like systems
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build windows
|
||||
|
||||
package backend
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// hideWindow sets the command to run without showing a console window on Windows
|
||||
func hideWindow(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func GetDefaultDownloadPath() string {
|
||||
// Get user's home directory
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
// Fallback to Public Pictures if can't get home dir
|
||||
return "C:\\Users\\Public\\Pictures"
|
||||
}
|
||||
|
||||
// Return path to user's Pictures folder
|
||||
return filepath.Join(homeDir, "Pictures")
|
||||
}
|
||||
@@ -0,0 +1,499 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// AccountDB represents a saved account in the database
|
||||
type AccountDB struct {
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Name string `json:"name"`
|
||||
ProfileImage string `json:"profile_image"`
|
||||
TotalMedia int `json:"total_media"`
|
||||
LastFetched time.Time `json:"last_fetched"`
|
||||
ResponseJSON string `json:"response_json"`
|
||||
MediaType string `json:"media_type"`
|
||||
Cursor string `json:"cursor"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
// AccountListItem represents a simplified account for listing
|
||||
type AccountListItem struct {
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Name string `json:"name"`
|
||||
ProfileImage string `json:"profile_image"`
|
||||
TotalMedia int `json:"total_media"`
|
||||
LastFetched string `json:"last_fetched"`
|
||||
GroupName string `json:"group_name"`
|
||||
GroupColor string `json:"group_color"`
|
||||
MediaType string `json:"media_type"`
|
||||
Cursor string `json:"cursor"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
// GetDBPath returns the database file path
|
||||
func GetDBPath() string {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
homeDir = "."
|
||||
}
|
||||
return filepath.Join(homeDir, ".twitterxmediabatchdownloader", "accounts.db")
|
||||
}
|
||||
|
||||
// InitDB initializes the database connection
|
||||
func InitDB() error {
|
||||
dbPath := GetDBPath()
|
||||
|
||||
// Create directory if not exists
|
||||
dir := filepath.Dir(dbPath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create tables
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL,
|
||||
name TEXT,
|
||||
profile_image TEXT,
|
||||
total_media INTEGER DEFAULT 0,
|
||||
last_fetched DATETIME,
|
||||
response_json TEXT,
|
||||
group_name TEXT DEFAULT '',
|
||||
group_color TEXT DEFAULT '',
|
||||
media_type TEXT DEFAULT 'all',
|
||||
cursor TEXT DEFAULT '',
|
||||
completed INTEGER DEFAULT 1,
|
||||
UNIQUE(username, media_type)
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add group columns if they don't exist (migration for existing databases)
|
||||
db.Exec("ALTER TABLE accounts ADD COLUMN group_name TEXT DEFAULT ''")
|
||||
db.Exec("ALTER TABLE accounts ADD COLUMN group_color TEXT DEFAULT ''")
|
||||
db.Exec("ALTER TABLE accounts ADD COLUMN media_type TEXT DEFAULT 'all'")
|
||||
db.Exec("ALTER TABLE accounts ADD COLUMN cursor TEXT DEFAULT ''")
|
||||
db.Exec("ALTER TABLE accounts ADD COLUMN completed INTEGER DEFAULT 1")
|
||||
|
||||
// Migration: Update unique constraint for existing databases
|
||||
// This allows same username with different media types
|
||||
db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_username_media_type ON accounts(username, media_type)")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseDB closes the database connection
|
||||
func CloseDB() {
|
||||
if db != nil {
|
||||
db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// SaveAccount saves or updates an account in the database
|
||||
func SaveAccount(username, name, profileImage string, totalMedia int, responseJSON string, mediaType string) error {
|
||||
return SaveAccountWithStatus(username, name, profileImage, totalMedia, responseJSON, mediaType, "", true)
|
||||
}
|
||||
|
||||
// SaveAccountWithStatus saves or updates an account with cursor and completion status
|
||||
func SaveAccountWithStatus(username, name, profileImage string, totalMedia int, responseJSON string, mediaType string, cursor string, completed bool) error {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Default to "all" if not specified
|
||||
if mediaType == "" {
|
||||
mediaType = "all"
|
||||
}
|
||||
|
||||
completedInt := 0
|
||||
if completed {
|
||||
completedInt = 1
|
||||
}
|
||||
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO accounts (username, name, profile_image, total_media, last_fetched, response_json, media_type, cursor, completed)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(username, media_type) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
profile_image = excluded.profile_image,
|
||||
total_media = excluded.total_media,
|
||||
last_fetched = excluded.last_fetched,
|
||||
response_json = excluded.response_json,
|
||||
cursor = excluded.cursor,
|
||||
completed = excluded.completed
|
||||
`, username, name, profileImage, totalMedia, time.Now(), responseJSON, mediaType, cursor, completedInt)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetAllAccounts returns all saved accounts
|
||||
func GetAllAccounts() ([]AccountListItem, error) {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT id, username, name, profile_image, total_media, last_fetched,
|
||||
COALESCE(group_name, '') as group_name, COALESCE(group_color, '') as group_color,
|
||||
COALESCE(media_type, 'all') as media_type,
|
||||
COALESCE(cursor, '') as cursor, COALESCE(completed, 1) as completed
|
||||
FROM accounts
|
||||
ORDER BY group_name ASC, last_fetched DESC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var accounts []AccountListItem
|
||||
for rows.Next() {
|
||||
var acc AccountListItem
|
||||
var lastFetched time.Time
|
||||
var completedInt int
|
||||
if err := rows.Scan(&acc.ID, &acc.Username, &acc.Name, &acc.ProfileImage, &acc.TotalMedia, &lastFetched, &acc.GroupName, &acc.GroupColor, &acc.MediaType, &acc.Cursor, &completedInt); err != nil {
|
||||
continue
|
||||
}
|
||||
acc.LastFetched = lastFetched.Format("2006-01-02 15:04")
|
||||
acc.Completed = completedInt == 1
|
||||
accounts = append(accounts, acc)
|
||||
}
|
||||
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
// UpdateAccountGroup updates the group for an account
|
||||
func UpdateAccountGroup(id int64, groupName, groupColor string) error {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err := db.Exec("UPDATE accounts SET group_name = ?, group_color = ? WHERE id = ?", groupName, groupColor, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetAllGroups returns all unique groups
|
||||
func GetAllGroups() ([]map[string]string, error) {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT DISTINCT group_name, group_color
|
||||
FROM accounts
|
||||
WHERE group_name != ''
|
||||
ORDER BY group_name
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var groups []map[string]string
|
||||
for rows.Next() {
|
||||
var name, color string
|
||||
if err := rows.Scan(&name, &color); err != nil {
|
||||
continue
|
||||
}
|
||||
groups = append(groups, map[string]string{"name": name, "color": color})
|
||||
}
|
||||
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
// ClearAllAccounts deletes all accounts from the database
|
||||
func ClearAllAccounts() error {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err := db.Exec("DELETE FROM accounts")
|
||||
return err
|
||||
}
|
||||
|
||||
// GetAccountByUsername returns a specific account by username
|
||||
func GetAccountByUsername(username string) (*AccountDB, error) {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var acc AccountDB
|
||||
var lastFetched time.Time
|
||||
var completedInt int
|
||||
err := db.QueryRow(`
|
||||
SELECT id, username, name, profile_image, total_media, last_fetched, response_json,
|
||||
COALESCE(cursor, '') as cursor, COALESCE(completed, 1) as completed
|
||||
FROM accounts WHERE username = ?
|
||||
`, username).Scan(&acc.ID, &acc.Username, &acc.Name, &acc.ProfileImage, &acc.TotalMedia, &lastFetched, &acc.ResponseJSON, &acc.Cursor, &completedInt)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
acc.LastFetched = lastFetched
|
||||
acc.Completed = completedInt == 1
|
||||
|
||||
// Convert legacy format if needed
|
||||
if converted, err := ConvertLegacyToNewFormat(acc.ResponseJSON); err == nil {
|
||||
acc.ResponseJSON = converted
|
||||
}
|
||||
|
||||
return &acc, nil
|
||||
}
|
||||
|
||||
// GetAccountByID returns a specific account by ID
|
||||
func GetAccountByID(id int64) (*AccountDB, error) {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var acc AccountDB
|
||||
var lastFetched time.Time
|
||||
var completedInt int
|
||||
err := db.QueryRow(`
|
||||
SELECT id, username, name, profile_image, total_media, last_fetched, response_json,
|
||||
COALESCE(cursor, '') as cursor, COALESCE(completed, 1) as completed
|
||||
FROM accounts WHERE id = ?
|
||||
`, id).Scan(&acc.ID, &acc.Username, &acc.Name, &acc.ProfileImage, &acc.TotalMedia, &lastFetched, &acc.ResponseJSON, &acc.Cursor, &completedInt)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
acc.LastFetched = lastFetched
|
||||
acc.Completed = completedInt == 1
|
||||
|
||||
// Convert legacy format if needed
|
||||
if converted, err := ConvertLegacyToNewFormat(acc.ResponseJSON); err == nil {
|
||||
acc.ResponseJSON = converted
|
||||
}
|
||||
|
||||
return &acc, nil
|
||||
}
|
||||
|
||||
// DeleteAccount deletes an account from the database
|
||||
func DeleteAccount(id int64) error {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err := db.Exec("DELETE FROM accounts WHERE id = ?", id)
|
||||
return err
|
||||
}
|
||||
|
||||
// ParseResponseJSON parses the stored JSON response
|
||||
func ParseResponseJSON(jsonStr string) (map[string]interface{}, error) {
|
||||
var result map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonStr), &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
// LegacyMediaEntry represents media entry in old format
|
||||
type LegacyMediaEntry struct {
|
||||
TweetID string `json:"tweet_id"`
|
||||
URL string `json:"url"`
|
||||
Date string `json:"date"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// LegacyAccountFormat represents the old saved account format
|
||||
type LegacyAccountFormat struct {
|
||||
Username string `json:"username"`
|
||||
Nick string `json:"nick"`
|
||||
Followers int `json:"followers"`
|
||||
Following int `json:"following"`
|
||||
Posts int `json:"posts"`
|
||||
MediaType string `json:"media_type"`
|
||||
ProfileImage string `json:"profile_image"`
|
||||
FetchMode string `json:"fetch_mode"`
|
||||
FetchTimestamp string `json:"fetch_timestamp"`
|
||||
GroupID interface{} `json:"group_id"`
|
||||
MediaList []LegacyMediaEntry `json:"media_list"`
|
||||
}
|
||||
|
||||
// ConvertLegacyToNewFormat converts old format to new TwitterResponse format
|
||||
func ConvertLegacyToNewFormat(jsonStr string) (string, error) {
|
||||
// First check if it's already in new format (has account_info key)
|
||||
var check map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(jsonStr), &check); err != nil {
|
||||
return jsonStr, err
|
||||
}
|
||||
|
||||
// If already has account_info, it's new format - return as is
|
||||
if _, hasAccountInfo := check["account_info"]; hasAccountInfo {
|
||||
return jsonStr, nil
|
||||
}
|
||||
|
||||
// Check if it's legacy format (has username and media_list)
|
||||
if _, hasUsername := check["username"]; !hasUsername {
|
||||
return jsonStr, nil
|
||||
}
|
||||
if _, hasMediaList := check["media_list"]; !hasMediaList {
|
||||
return jsonStr, nil
|
||||
}
|
||||
|
||||
// Parse as legacy format
|
||||
var legacy LegacyAccountFormat
|
||||
if err := json.Unmarshal([]byte(jsonStr), &legacy); err != nil {
|
||||
return jsonStr, err
|
||||
}
|
||||
|
||||
// Convert timeline entries
|
||||
timeline := make([]map[string]interface{}, len(legacy.MediaList))
|
||||
for i, media := range legacy.MediaList {
|
||||
timeline[i] = map[string]interface{}{
|
||||
"url": media.URL,
|
||||
"date": media.Date,
|
||||
"tweet_id": media.TweetID,
|
||||
"type": media.Type,
|
||||
"is_retweet": false,
|
||||
}
|
||||
}
|
||||
|
||||
// Build new format
|
||||
newFormat := map[string]interface{}{
|
||||
"account_info": map[string]interface{}{
|
||||
"name": legacy.Username,
|
||||
"nick": legacy.Nick,
|
||||
"date": "",
|
||||
"followers_count": legacy.Followers,
|
||||
"friends_count": legacy.Following,
|
||||
"profile_image": legacy.ProfileImage,
|
||||
"statuses_count": legacy.Posts,
|
||||
},
|
||||
"total_urls": len(legacy.MediaList),
|
||||
"timeline": timeline,
|
||||
"metadata": map[string]interface{}{
|
||||
"new_entries": len(legacy.MediaList),
|
||||
"page": 0,
|
||||
"batch_size": 0,
|
||||
"has_more": false,
|
||||
},
|
||||
}
|
||||
|
||||
// Convert back to JSON
|
||||
newJSON, err := json.Marshal(newFormat)
|
||||
if err != nil {
|
||||
return jsonStr, err
|
||||
}
|
||||
|
||||
return string(newJSON), nil
|
||||
}
|
||||
|
||||
// ExportAccountToFile exports account JSON to a file
|
||||
func ExportAccountToFile(id int64, outputDir string) (string, error) {
|
||||
acc, err := GetAccountByID(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create export directory if not exists
|
||||
exportDir := filepath.Join(outputDir, "twitterxmediabatchdownloader_backups")
|
||||
if err := os.MkdirAll(exportDir, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Use username (nick) for filename
|
||||
filename := acc.Username
|
||||
if filename == "" {
|
||||
filename = acc.Name
|
||||
}
|
||||
|
||||
filePath := filepath.Join(exportDir, filename+".json")
|
||||
|
||||
if err := os.WriteFile(filePath, []byte(acc.ResponseJSON), 0644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
// ImportAccountFromFile imports account from JSON file (supports both old and new format)
|
||||
func ImportAccountFromFile(filePath string) (string, error) {
|
||||
if db == nil {
|
||||
if err := InitDB(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// Read file
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
jsonStr := string(data)
|
||||
|
||||
// Convert legacy format if needed
|
||||
convertedJSON, err := ConvertLegacyToNewFormat(jsonStr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Parse the converted JSON to extract account info
|
||||
var parsed map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(convertedJSON), &parsed); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Extract account info
|
||||
accountInfo, ok := parsed["account_info"].(map[string]interface{})
|
||||
if !ok {
|
||||
return "", fmt.Errorf("invalid JSON format: missing account_info")
|
||||
}
|
||||
|
||||
username, _ := accountInfo["name"].(string)
|
||||
name, _ := accountInfo["nick"].(string)
|
||||
profileImage, _ := accountInfo["profile_image"].(string)
|
||||
|
||||
totalURLs := 0
|
||||
if total, ok := parsed["total_urls"].(float64); ok {
|
||||
totalURLs = int(total)
|
||||
}
|
||||
|
||||
if username == "" {
|
||||
return "", fmt.Errorf("invalid JSON format: missing username")
|
||||
}
|
||||
|
||||
// Save to database with default media type "all" for imported files
|
||||
err = SaveAccount(username, name, profileImage, totalURLs, convertedJSON, "all")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return username, nil
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxConcurrentDownloads is the number of parallel downloads
|
||||
MaxConcurrentDownloads = 10
|
||||
)
|
||||
|
||||
// MediaItem represents a media item with metadata for download
|
||||
type MediaItem struct {
|
||||
URL string `json:"url"`
|
||||
Date string `json:"date"`
|
||||
TweetID int64 `json:"tweet_id"`
|
||||
Type string `json:"type"`
|
||||
Username string `json:"username"`
|
||||
Content string `json:"content,omitempty"` // Tweet text content (for text-only tweets)
|
||||
}
|
||||
|
||||
// DownloadMediaFiles downloads media files from URLs to the output directory (legacy)
|
||||
func DownloadMediaFiles(urls []string, outputDir string) (downloaded int, failed int, err error) {
|
||||
// Create output directory if it doesn't exist
|
||||
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
||||
return 0, len(urls), fmt.Errorf("failed to create output directory: %v", err)
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
for _, mediaURL := range urls {
|
||||
filename := extractFilename(mediaURL)
|
||||
outputPath := filepath.Join(outputDir, filename)
|
||||
|
||||
// Skip if file already exists
|
||||
if _, err := os.Stat(outputPath); err == nil {
|
||||
downloaded++
|
||||
continue
|
||||
}
|
||||
|
||||
if err := downloadFile(client, mediaURL, outputPath); err != nil {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
downloaded++
|
||||
}
|
||||
|
||||
return downloaded, failed, nil
|
||||
}
|
||||
|
||||
// ProgressCallback is a function type for progress updates
|
||||
type ProgressCallback func(current, total int)
|
||||
|
||||
// ItemStatusCallback is a function type for per-item status updates
|
||||
type ItemStatusCallback func(tweetID int64, index int, status string) // status: "success", "failed", "skipped"
|
||||
|
||||
// downloadTask represents a single download task
|
||||
type downloadTask struct {
|
||||
item MediaItem
|
||||
outputPath string
|
||||
index int
|
||||
}
|
||||
|
||||
// DownloadMediaWithMetadata downloads media files with proper naming and categorization
|
||||
func DownloadMediaWithMetadata(items []MediaItem, outputDir string, username string) (downloaded int, failed int, err error) {
|
||||
return DownloadMediaWithMetadataProgressAndStatus(items, outputDir, username, nil, nil, nil)
|
||||
}
|
||||
|
||||
// DownloadMediaWithMetadataProgress downloads media files with progress callback and cancellation support
|
||||
func DownloadMediaWithMetadataProgress(items []MediaItem, outputDir string, username string, progress ProgressCallback, ctx context.Context) (downloaded int, failed int, err error) {
|
||||
return DownloadMediaWithMetadataProgressAndStatus(items, outputDir, username, progress, nil, ctx)
|
||||
}
|
||||
|
||||
// DownloadMediaWithMetadataProgressAndStatus downloads media files with progress and per-item status callbacks
|
||||
func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir string, username string, progress ProgressCallback, itemStatus ItemStatusCallback, ctx context.Context) (downloaded int, failed int, err error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
// Create base output directory
|
||||
baseDir := filepath.Join(outputDir, username)
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
return 0, len(items), fmt.Errorf("failed to create output directory: %v", err)
|
||||
}
|
||||
|
||||
total := len(items)
|
||||
if total == 0 {
|
||||
return 0, 0, nil
|
||||
}
|
||||
|
||||
// Prepare all tasks first (sequential to handle tweet media count)
|
||||
tweetMediaCount := make(map[int64]int)
|
||||
tasks := make([]downloadTask, 0, total)
|
||||
|
||||
for i, item := range items {
|
||||
// Determine subfolder based on type
|
||||
var subfolder string
|
||||
switch item.Type {
|
||||
case "photo":
|
||||
subfolder = "images"
|
||||
case "video":
|
||||
subfolder = "videos"
|
||||
case "gif", "animated_gif":
|
||||
subfolder = "gifs"
|
||||
case "text":
|
||||
subfolder = "texts"
|
||||
default:
|
||||
subfolder = "other"
|
||||
}
|
||||
|
||||
// Create type subfolder
|
||||
typeDir := filepath.Join(baseDir, subfolder)
|
||||
if err := os.MkdirAll(typeDir, 0755); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Format timestamp from date
|
||||
timestamp := formatTimestamp(item.Date)
|
||||
|
||||
// Get file extension
|
||||
ext := getExtension(item.URL, item.Type)
|
||||
|
||||
// Increment counter for this tweet_id
|
||||
tweetMediaCount[item.TweetID]++
|
||||
mediaIndex := tweetMediaCount[item.TweetID]
|
||||
|
||||
// Create filename: {username}_{timestamp}_{tweet_id}_{index}.{ext}
|
||||
filename := fmt.Sprintf("%s_%s_%d_%02d%s", username, timestamp, item.TweetID, mediaIndex, ext)
|
||||
outputPath := filepath.Join(typeDir, filename)
|
||||
|
||||
tasks = append(tasks, downloadTask{
|
||||
item: item,
|
||||
outputPath: outputPath,
|
||||
index: i,
|
||||
})
|
||||
}
|
||||
|
||||
// Counters for parallel downloads
|
||||
var downloadedCount int64
|
||||
var failedCount int64
|
||||
var completedCount int64
|
||||
|
||||
// Create worker pool
|
||||
taskChan := make(chan downloadTask, len(tasks))
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Start workers
|
||||
numWorkers := MaxConcurrentDownloads
|
||||
if numWorkers > len(tasks) {
|
||||
numWorkers = len(tasks)
|
||||
}
|
||||
|
||||
for i := 0; i < numWorkers; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
client := &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
for task := range taskChan {
|
||||
// Check for cancellation
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
var status string
|
||||
// Skip if file already exists
|
||||
if _, err := os.Stat(task.outputPath); err == nil {
|
||||
atomic.AddInt64(&downloadedCount, 1)
|
||||
status = "skipped"
|
||||
} else if task.item.Type == "text" {
|
||||
// For text tweets, write content to file
|
||||
if err := os.WriteFile(task.outputPath, []byte(task.item.Content), 0644); err != nil {
|
||||
atomic.AddInt64(&failedCount, 1)
|
||||
status = "failed"
|
||||
} else {
|
||||
atomic.AddInt64(&downloadedCount, 1)
|
||||
status = "success"
|
||||
}
|
||||
} else if err := downloadFileWithContext(ctx, client, task.item.URL, task.outputPath); err != nil {
|
||||
atomic.AddInt64(&failedCount, 1)
|
||||
status = "failed"
|
||||
} else {
|
||||
atomic.AddInt64(&downloadedCount, 1)
|
||||
status = "success"
|
||||
}
|
||||
|
||||
// Emit per-item status
|
||||
if itemStatus != nil {
|
||||
itemStatus(task.item.TweetID, task.index, status)
|
||||
}
|
||||
|
||||
// Update progress
|
||||
completed := atomic.AddInt64(&completedCount, 1)
|
||||
if progress != nil {
|
||||
progress(int(completed), total)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Send tasks to workers
|
||||
for _, task := range tasks {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
close(taskChan)
|
||||
wg.Wait()
|
||||
return int(downloadedCount), int(failedCount) + (total - int(completedCount)), ctx.Err()
|
||||
case taskChan <- task:
|
||||
}
|
||||
}
|
||||
close(taskChan)
|
||||
|
||||
// Wait for all workers to finish
|
||||
wg.Wait()
|
||||
|
||||
return int(downloadedCount), int(failedCount), nil
|
||||
}
|
||||
|
||||
// downloadFileWithContext downloads a single file with context support for cancellation
|
||||
func downloadFileWithContext(ctx context.Context, client *http.Client, url, outputPath string) error {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("bad status: %s", resp.Status)
|
||||
}
|
||||
|
||||
out, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
// formatTimestamp converts date string to timestamp format
|
||||
func formatTimestamp(dateStr string) string {
|
||||
// Try parsing various date formats
|
||||
formats := []string{
|
||||
"2006-01-02T15:04:05", // ISO 8601 without timezone (from extractor)
|
||||
"2006-01-02T15:04:05+00:00", // ISO 8601 with timezone
|
||||
"2006-01-02T15:04:05-07:00", // ISO 8601 with timezone offset
|
||||
time.RFC3339, // Standard RFC3339
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
"2006-01-02T15:04:05Z",
|
||||
"2006-01-02 15:04:05",
|
||||
"Mon Jan 02 15:04:05 -0700 2006",
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
if t, err := time.Parse(format, dateStr); err == nil {
|
||||
return t.Format("20060102_150405")
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: use empty string to indicate parsing failed
|
||||
return "00000000_000000"
|
||||
}
|
||||
|
||||
// getExtension determines file extension from URL and type
|
||||
func getExtension(mediaURL string, mediaType string) string {
|
||||
parsedURL, err := url.Parse(mediaURL)
|
||||
if err != nil {
|
||||
return ".jpg"
|
||||
}
|
||||
|
||||
// Check format query param for Twitter images
|
||||
if format := parsedURL.Query().Get("format"); format != "" {
|
||||
return "." + format
|
||||
}
|
||||
|
||||
// Get extension from path
|
||||
path := parsedURL.Path
|
||||
ext := filepath.Ext(path)
|
||||
if ext != "" {
|
||||
return ext
|
||||
}
|
||||
|
||||
// Default based on type
|
||||
switch mediaType {
|
||||
case "video":
|
||||
return ".mp4"
|
||||
case "gif", "animated_gif":
|
||||
return ".mp4" // Twitter GIFs are actually MP4
|
||||
case "text":
|
||||
return ".txt"
|
||||
default:
|
||||
return ".jpg"
|
||||
}
|
||||
}
|
||||
|
||||
// extractFilename extracts filename from URL (legacy)
|
||||
func extractFilename(mediaURL string) string {
|
||||
parsedURL, err := url.Parse(mediaURL)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("media_%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// Get the path part
|
||||
path := parsedURL.Path
|
||||
|
||||
// Extract base filename
|
||||
base := filepath.Base(path)
|
||||
|
||||
// Handle Twitter image URLs: /media/XXX -> XXX.jpg
|
||||
if strings.Contains(mediaURL, "pbs.twimg.com/media/") {
|
||||
// Get format from query params
|
||||
format := parsedURL.Query().Get("format")
|
||||
if format == "" {
|
||||
format = "jpg"
|
||||
}
|
||||
|
||||
// Remove any existing extension
|
||||
if idx := strings.LastIndex(base, "."); idx > 0 {
|
||||
base = base[:idx]
|
||||
}
|
||||
|
||||
return base + "." + format
|
||||
}
|
||||
|
||||
// Handle Twitter video URLs
|
||||
if strings.Contains(mediaURL, "video.twimg.com") {
|
||||
return base
|
||||
}
|
||||
|
||||
// Default: use base name or generate one
|
||||
if base == "" || base == "." {
|
||||
return fmt.Sprintf("media_%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
|
||||
// downloadFile downloads a single file from URL
|
||||
func downloadFile(client *http.Client, url, outputPath string) error {
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("bad status: %s", resp.Status)
|
||||
}
|
||||
|
||||
out, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package backend
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed bin/extractor
|
||||
var extractorBin []byte
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build windows
|
||||
|
||||
package backend
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed bin/extractor.exe
|
||||
var extractorBin []byte
|
||||
@@ -0,0 +1,316 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/ulikunitz/xz"
|
||||
)
|
||||
|
||||
// FFmpeg download URLs
|
||||
const (
|
||||
ffmpegWindowsURL = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip"
|
||||
ffmpegLinuxURL = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz"
|
||||
ffmpegMacOSURL = "https://evermeet.cx/ffmpeg/getrelease/ffmpeg/zip"
|
||||
)
|
||||
|
||||
// GetFFmpegPath returns the path to ffmpeg binary
|
||||
func GetFFmpegPath() string {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
baseDir := filepath.Join(homeDir, ".twitterxmediabatchdownloader")
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
return filepath.Join(baseDir, "ffmpeg.exe")
|
||||
default:
|
||||
return filepath.Join(baseDir, "ffmpeg")
|
||||
}
|
||||
}
|
||||
|
||||
// IsFFmpegInstalled checks if ffmpeg is available
|
||||
func IsFFmpegInstalled() bool {
|
||||
ffmpegPath := GetFFmpegPath()
|
||||
if _, err := os.Stat(ffmpegPath); err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DownloadFFmpeg downloads ffmpeg binary for current platform
|
||||
func DownloadFFmpeg(progressCallback func(downloaded, total int64)) error {
|
||||
var downloadURL string
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
downloadURL = ffmpegWindowsURL
|
||||
case "linux":
|
||||
downloadURL = ffmpegLinuxURL
|
||||
case "darwin":
|
||||
downloadURL = ffmpegMacOSURL
|
||||
default:
|
||||
return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
|
||||
}
|
||||
|
||||
// Create temp file for download
|
||||
tempFile, err := os.CreateTemp("", "ffmpeg-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create temp file: %v", err)
|
||||
}
|
||||
tempPath := tempFile.Name()
|
||||
defer os.Remove(tempPath)
|
||||
defer tempFile.Close()
|
||||
|
||||
// Download file
|
||||
resp, err := http.Get(downloadURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to download ffmpeg: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to download ffmpeg: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Copy with progress
|
||||
total := resp.ContentLength
|
||||
var downloaded int64
|
||||
buf := make([]byte, 32*1024)
|
||||
|
||||
for {
|
||||
n, err := resp.Body.Read(buf)
|
||||
if n > 0 {
|
||||
_, writeErr := tempFile.Write(buf[:n])
|
||||
if writeErr != nil {
|
||||
return fmt.Errorf("failed to write temp file: %v", writeErr)
|
||||
}
|
||||
downloaded += int64(n)
|
||||
if progressCallback != nil {
|
||||
progressCallback(downloaded, total)
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to download: %v", err)
|
||||
}
|
||||
}
|
||||
tempFile.Close()
|
||||
|
||||
// Extract ffmpeg binary
|
||||
ffmpegPath := GetFFmpegPath()
|
||||
baseDir := filepath.Dir(ffmpegPath)
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %v", err)
|
||||
}
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows", "darwin":
|
||||
return extractFromZip(tempPath, ffmpegPath)
|
||||
case "linux":
|
||||
return extractFromTarXz(tempPath, ffmpegPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// extractFromZip extracts ffmpeg from zip archive
|
||||
func extractFromZip(zipPath, destPath string) error {
|
||||
r, err := zip.OpenReader(zipPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open zip: %v", err)
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
for _, f := range r.File {
|
||||
// Look for ffmpeg binary
|
||||
name := filepath.Base(f.Name)
|
||||
if name == "ffmpeg" || name == "ffmpeg.exe" {
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file in zip: %v", err)
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create output file: %v", err)
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if _, err := io.Copy(out, rc); err != nil {
|
||||
return fmt.Errorf("failed to extract file: %v", err)
|
||||
}
|
||||
|
||||
// Make executable on Unix
|
||||
if runtime.GOOS != "windows" {
|
||||
os.Chmod(destPath, 0755)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("ffmpeg binary not found in archive")
|
||||
}
|
||||
|
||||
// extractFromTarXz extracts ffmpeg from tar.xz archive
|
||||
func extractFromTarXz(tarXzPath, destPath string) error {
|
||||
file, err := os.Open(tarXzPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open tar.xz: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
xzReader, err := xz.NewReader(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create xz reader: %v", err)
|
||||
}
|
||||
|
||||
tarReader := tar.NewReader(xzReader)
|
||||
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read tar: %v", err)
|
||||
}
|
||||
|
||||
// Look for ffmpeg binary
|
||||
name := filepath.Base(header.Name)
|
||||
if name == "ffmpeg" && header.Typeflag == tar.TypeReg {
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create output file: %v", err)
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if _, err := io.Copy(out, tarReader); err != nil {
|
||||
return fmt.Errorf("failed to extract file: %v", err)
|
||||
}
|
||||
|
||||
os.Chmod(destPath, 0755)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("ffmpeg binary not found in archive")
|
||||
}
|
||||
|
||||
// ConvertMP4ToGIF converts an MP4 file to GIF using ffmpeg
|
||||
// quality: "fast" for simple conversion, "better" for optimized palette
|
||||
// resolution: "original", "high" (800px), "medium" (600px), "low" (400px)
|
||||
func ConvertMP4ToGIF(inputPath, outputPath, quality, resolution string) error {
|
||||
ffmpegPath := GetFFmpegPath()
|
||||
|
||||
if !IsFFmpegInstalled() {
|
||||
return fmt.Errorf("ffmpeg not installed")
|
||||
}
|
||||
|
||||
var args []string
|
||||
|
||||
if quality == "fast" {
|
||||
// Fast mode: simple conversion
|
||||
args = []string{
|
||||
"-i", inputPath,
|
||||
"-loop", "0",
|
||||
"-y",
|
||||
outputPath,
|
||||
}
|
||||
} else {
|
||||
// Better mode: optimized palette with dithering
|
||||
var filter string
|
||||
|
||||
switch resolution {
|
||||
case "high":
|
||||
filter = "scale=800:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=800:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a"
|
||||
case "medium":
|
||||
filter = "scale=600:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=600:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a"
|
||||
case "low":
|
||||
filter = "scale=400:-1:flags=lanczos,palettegen=stats_mode=full[palette];[0:v]scale=400:-1:flags=lanczos[scaled];[scaled][palette]paletteuse=dither=sierra2_4a"
|
||||
default: // original
|
||||
filter = "palettegen=stats_mode=full[palette];[0:v][palette]paletteuse=dither=sierra2_4a"
|
||||
}
|
||||
|
||||
// Set FPS based on resolution
|
||||
fps := "15"
|
||||
if resolution == "medium" {
|
||||
fps = "10"
|
||||
} else if resolution == "low" {
|
||||
fps = "8"
|
||||
}
|
||||
|
||||
args = []string{
|
||||
"-i", inputPath,
|
||||
"-lavfi", filter,
|
||||
"-r", fps,
|
||||
"-y",
|
||||
outputPath,
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(ffmpegPath, args...)
|
||||
hideWindow(cmd) // Hide console window on Windows
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ffmpeg error: %v, output: %s", err, string(output))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConvertGIFsInFolder converts all MP4 files in gifs folder to actual GIF format
|
||||
func ConvertGIFsInFolder(folderPath, quality, resolution string, deleteOriginal bool) (converted int, failed int, err error) {
|
||||
if !IsFFmpegInstalled() {
|
||||
return 0, 0, fmt.Errorf("ffmpeg not installed")
|
||||
}
|
||||
|
||||
// Clean the path to handle cross-platform path separators
|
||||
cleanPath := filepath.Clean(folderPath)
|
||||
gifsFolder := filepath.Join(cleanPath, "gifs")
|
||||
if _, err := os.Stat(gifsFolder); os.IsNotExist(err) {
|
||||
return 0, 0, fmt.Errorf("gifs folder not found: %s", gifsFolder)
|
||||
}
|
||||
|
||||
files, err := os.ReadDir(gifsFolder)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to read gifs folder: %v", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
name := file.Name()
|
||||
if !strings.HasSuffix(strings.ToLower(name), ".mp4") {
|
||||
continue
|
||||
}
|
||||
|
||||
inputPath := filepath.Join(gifsFolder, name)
|
||||
outputPath := filepath.Join(gifsFolder, strings.TrimSuffix(name, filepath.Ext(name))+".gif")
|
||||
|
||||
if err := ConvertMP4ToGIF(inputPath, outputPath, quality, resolution); err != nil {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
|
||||
if deleteOriginal {
|
||||
os.Remove(inputPath)
|
||||
}
|
||||
|
||||
converted++
|
||||
}
|
||||
|
||||
return converted, failed, nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
|
||||
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
func OpenFolderInExplorer(path string) error {
|
||||
var cmd *exec.Cmd
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
// Use cmd /c start to open explorer - more reliable
|
||||
cmd = exec.Command("cmd", "/c", "start", "", path)
|
||||
case "darwin": // macOS
|
||||
cmd = exec.Command("open", path)
|
||||
case "linux":
|
||||
cmd = exec.Command("xdg-open", path)
|
||||
default:
|
||||
cmd = exec.Command("xdg-open", path)
|
||||
}
|
||||
|
||||
hideWindow(cmd) // Hide console window on Windows
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func SelectFolderDialog(ctx context.Context, defaultPath string) (string, error) {
|
||||
// If defaultPath is empty, use default download path
|
||||
if defaultPath == "" {
|
||||
defaultPath = GetDefaultDownloadPath()
|
||||
}
|
||||
|
||||
options := wailsRuntime.OpenDialogOptions{
|
||||
Title: "Select Download Folder",
|
||||
DefaultDirectory: defaultPath,
|
||||
}
|
||||
|
||||
selectedPath, err := wailsRuntime.OpenDirectoryDialog(ctx, options)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// If user cancelled, selectedPath will be empty
|
||||
if selectedPath == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return selectedPath, nil
|
||||
}
|
||||
@@ -0,0 +1,898 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// getExecutableName returns the appropriate executable name for the current OS
|
||||
func getExecutableName() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "extractor.exe"
|
||||
}
|
||||
return "extractor"
|
||||
}
|
||||
|
||||
// KillAllExtractorProcesses kills all running extractor processes
|
||||
// This is useful for cleanup when starting fresh or when user stops fetch
|
||||
func KillAllExtractorProcesses() {
|
||||
exeName := getExecutableName()
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if runtime.GOOS == "windows" {
|
||||
// Use taskkill on Windows
|
||||
cmd = exec.Command("taskkill", "/F", "/IM", exeName)
|
||||
} else {
|
||||
// Use pkill on Unix
|
||||
cmd = exec.Command("pkill", "-f", exeName)
|
||||
}
|
||||
|
||||
hideWindow(cmd)
|
||||
cmd.CombinedOutput() // Ignore errors - it's okay if no processes found
|
||||
}
|
||||
|
||||
// parseExtractorError parses the extractor output and returns a user-friendly error message
|
||||
// while preserving the original error from gallery-dl
|
||||
func parseExtractorError(output string, username string) string {
|
||||
outputLower := strings.ToLower(output)
|
||||
|
||||
// Extract the actual error line from gallery-dl output
|
||||
lines := strings.Split(output, "\n")
|
||||
var errorLine string
|
||||
for _, line := range lines {
|
||||
lineLower := strings.ToLower(line)
|
||||
if strings.Contains(lineLower, "error:") || strings.Contains(lineLower, "exception") {
|
||||
errorLine = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
if errorLine == "" {
|
||||
errorLine = strings.TrimSpace(output)
|
||||
}
|
||||
|
||||
// Truncate if too long
|
||||
if len(errorLine) > 300 {
|
||||
errorLine = errorLine[:300] + "..."
|
||||
}
|
||||
|
||||
// Add context hint based on error type, but keep original message
|
||||
var hint string
|
||||
if strings.Contains(outputLower, "unable to retrieve tweets from this timeline") {
|
||||
hint = " [Hint: End of timeline reached or rate limited - data already fetched has been saved]"
|
||||
} else if strings.Contains(outputLower, "rate limit") || strings.Contains(output, "429") {
|
||||
hint = " [Hint: Wait 5-15 minutes before retrying]"
|
||||
} else if strings.Contains(output, "401") || strings.Contains(outputLower, "unauthorized") {
|
||||
hint = " [Hint: Auth token may be invalid or expired]"
|
||||
} else if strings.Contains(output, "404") {
|
||||
hint = fmt.Sprintf(" [Hint: @%s may not exist or is suspended]", username)
|
||||
} else if strings.Contains(outputLower, "protected") || strings.Contains(output, "403") {
|
||||
hint = " [Hint: Protected account - need to follow and use auth token]"
|
||||
}
|
||||
|
||||
return errorLine + hint
|
||||
}
|
||||
|
||||
// TweetIDString is a custom type that unmarshals int64 but marshals as string
|
||||
type TweetIDString int64
|
||||
|
||||
// MarshalJSON converts TweetIDString to JSON string to preserve precision in JavaScript
|
||||
func (t TweetIDString) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf(`"%d"`, t)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON accepts both number and string from JSON
|
||||
func (t *TweetIDString) UnmarshalJSON(data []byte) error {
|
||||
// Try to unmarshal as number first (from extractor)
|
||||
var num int64
|
||||
if err := json.Unmarshal(data, &num); err == nil {
|
||||
*t = TweetIDString(num)
|
||||
return nil
|
||||
}
|
||||
// Try as string (for future compatibility)
|
||||
var str string
|
||||
if err := json.Unmarshal(data, &str); err == nil {
|
||||
parsed, err := fmt.Sscanf(str, "%d", &num)
|
||||
if err != nil || parsed != 1 {
|
||||
return fmt.Errorf("invalid tweet_id string: %s", str)
|
||||
}
|
||||
*t = TweetIDString(num)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("tweet_id must be number or string")
|
||||
}
|
||||
|
||||
// Author represents tweet author information from extractor
|
||||
type Author struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
// UserInfo represents full user information from extractor
|
||||
type UserInfo struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Nick string `json:"nick"`
|
||||
Location string `json:"location"`
|
||||
Date string `json:"date"`
|
||||
Verified bool `json:"verified"`
|
||||
Protected bool `json:"protected"`
|
||||
ProfileBanner string `json:"profile_banner"`
|
||||
ProfileImage string `json:"profile_image"`
|
||||
FavouritesCount int `json:"favourites_count"`
|
||||
FollowersCount int `json:"followers_count"`
|
||||
FriendsCount int `json:"friends_count"`
|
||||
ListedCount int `json:"listed_count"`
|
||||
MediaCount int `json:"media_count"`
|
||||
StatusesCount int `json:"statuses_count"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// CLIMediaItem represents a single media entry from extractor CLI
|
||||
type CLIMediaItem struct {
|
||||
URL string `json:"url"`
|
||||
TweetID TweetIDString `json:"tweet_id"`
|
||||
RetweetID TweetIDString `json:"retweet_id"`
|
||||
QuoteID TweetIDString `json:"quote_id"`
|
||||
ReplyID TweetIDString `json:"reply_id"`
|
||||
ConversationID TweetIDString `json:"conversation_id"`
|
||||
Date string `json:"date"`
|
||||
Extension string `json:"extension"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Type string `json:"type"`
|
||||
Bitrate int `json:"bitrate"`
|
||||
Duration float64 `json:"duration"`
|
||||
Author UserInfo `json:"author"`
|
||||
User UserInfo `json:"user"`
|
||||
Content string `json:"content"`
|
||||
FavoriteCount int `json:"favorite_count"`
|
||||
RetweetCount int `json:"retweet_count"`
|
||||
ReplyCount int `json:"reply_count"`
|
||||
QuoteCount int `json:"quote_count"`
|
||||
BookmarkCount int `json:"bookmark_count"`
|
||||
ViewCount int `json:"view_count"`
|
||||
Source string `json:"source"`
|
||||
Sensitive bool `json:"sensitive"`
|
||||
}
|
||||
|
||||
// TweetMetadata represents tweet metadata from extractor
|
||||
type TweetMetadata struct {
|
||||
TweetID TweetIDString `json:"tweet_id"`
|
||||
RetweetID TweetIDString `json:"retweet_id,omitempty"`
|
||||
QuoteID TweetIDString `json:"quote_id,omitempty"`
|
||||
ReplyID TweetIDString `json:"reply_id,omitempty"`
|
||||
ConversationID TweetIDString `json:"conversation_id,omitempty"`
|
||||
Date string `json:"date"`
|
||||
Author Author `json:"author"`
|
||||
Content string `json:"content"`
|
||||
Lang string `json:"lang,omitempty"`
|
||||
Hashtags []string `json:"hashtags,omitempty"`
|
||||
FavoriteCount int `json:"favorite_count"`
|
||||
RetweetCount int `json:"retweet_count"`
|
||||
QuoteCount int `json:"quote_count,omitempty"`
|
||||
ReplyCount int `json:"reply_count,omitempty"`
|
||||
BookmarkCount int `json:"bookmark_count,omitempty"`
|
||||
ViewCount int `json:"view_count,omitempty"`
|
||||
Sensitive bool `json:"sensitive,omitempty"`
|
||||
}
|
||||
|
||||
// CLIResponse represents the raw response from extractor CLI
|
||||
type CLIResponse struct {
|
||||
Media []CLIMediaItem `json:"media"`
|
||||
Metadata []TweetMetadata `json:"metadata"`
|
||||
Cursor string `json:"cursor,omitempty"` // Cursor for resume
|
||||
Total int `json:"total,omitempty"` // Total items fetched
|
||||
Completed bool `json:"completed,omitempty"` // True if all fetched
|
||||
}
|
||||
|
||||
// TimelineEntry represents a single media entry for frontend (converted from MediaItem)
|
||||
type TimelineEntry struct {
|
||||
URL string `json:"url"`
|
||||
Date string `json:"date"`
|
||||
TweetID TweetIDString `json:"tweet_id"`
|
||||
Type string `json:"type"`
|
||||
IsRetweet bool `json:"is_retweet"`
|
||||
Extension string `json:"extension"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Content string `json:"content,omitempty"`
|
||||
ViewCount int `json:"view_count,omitempty"`
|
||||
BookmarkCount int `json:"bookmark_count,omitempty"`
|
||||
FavoriteCount int `json:"favorite_count,omitempty"`
|
||||
RetweetCount int `json:"retweet_count,omitempty"`
|
||||
ReplyCount int `json:"reply_count,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Verified bool `json:"verified,omitempty"`
|
||||
}
|
||||
|
||||
// AccountInfo represents Twitter account information (derived from metadata)
|
||||
type AccountInfo struct {
|
||||
Name string `json:"name"`
|
||||
Nick string `json:"nick"`
|
||||
Date string `json:"date"`
|
||||
FollowersCount int `json:"followers_count"`
|
||||
FriendsCount int `json:"friends_count"`
|
||||
ProfileImage string `json:"profile_image"`
|
||||
StatusesCount int `json:"statuses_count"`
|
||||
}
|
||||
|
||||
// ExtractMetadata represents extraction metadata
|
||||
type ExtractMetadata struct {
|
||||
NewEntries int `json:"new_entries"`
|
||||
Page int `json:"page"`
|
||||
BatchSize int `json:"batch_size"`
|
||||
HasMore bool `json:"has_more"`
|
||||
Cursor string `json:"cursor,omitempty"` // Cursor for resume capability
|
||||
Completed bool `json:"completed,omitempty"` // True if all media fetched
|
||||
}
|
||||
|
||||
// TwitterResponse represents the full response for frontend
|
||||
type TwitterResponse struct {
|
||||
AccountInfo AccountInfo `json:"account_info"`
|
||||
TotalURLs int `json:"total_urls"`
|
||||
Timeline []TimelineEntry `json:"timeline"`
|
||||
Metadata ExtractMetadata `json:"metadata"`
|
||||
Cursor string `json:"cursor,omitempty"` // Cursor for next fetch
|
||||
Completed bool `json:"completed,omitempty"` // True if fetch completed
|
||||
}
|
||||
|
||||
// TimelineRequest represents request parameters for timeline extraction
|
||||
type TimelineRequest struct {
|
||||
Username string `json:"username"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
TimelineType string `json:"timeline_type"` // media, timeline, tweets, with_replies, likes, bookmarks
|
||||
BatchSize int `json:"batch_size"` // 0 = all
|
||||
Page int `json:"page"`
|
||||
MediaType string `json:"media_type"` // all, image, video, gif
|
||||
Retweets bool `json:"retweets"`
|
||||
Cursor string `json:"cursor,omitempty"` // Resume from this cursor position
|
||||
}
|
||||
|
||||
// DateRangeRequest represents request parameters for date range extraction
|
||||
type DateRangeRequest struct {
|
||||
Username string `json:"username"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
StartDate string `json:"start_date"` // YYYY-MM-DD
|
||||
EndDate string `json:"end_date"` // YYYY-MM-DD
|
||||
MediaFilter string `json:"media_filter"`
|
||||
Retweets bool `json:"retweets"`
|
||||
}
|
||||
|
||||
// buildTwitterURL constructs the Twitter URL based on username and timeline type
|
||||
func buildTwitterURL(username, timelineType string) string {
|
||||
// Special case: bookmarks don't need username
|
||||
if timelineType == "bookmarks" {
|
||||
return "https://x.com/i/bookmarks"
|
||||
}
|
||||
|
||||
// Clean username - extract handle from URL if needed
|
||||
username = cleanUsername(username)
|
||||
|
||||
// Build URL based on timeline type
|
||||
baseURL := "https://x.com/" + username
|
||||
switch timelineType {
|
||||
case "media":
|
||||
return baseURL + "/media"
|
||||
case "timeline":
|
||||
return baseURL + "/timeline" // Best for cursor support
|
||||
case "tweets":
|
||||
return baseURL + "/tweets"
|
||||
case "with_replies":
|
||||
return baseURL + "/with_replies"
|
||||
case "likes":
|
||||
return baseURL + "/likes"
|
||||
default:
|
||||
return baseURL + "/timeline" // Default to timeline for reliable cursor
|
||||
}
|
||||
}
|
||||
|
||||
// cleanUsername extracts the handle from different input formats
|
||||
// Handles: @username, username, https://x.com/username, https://x.com/username/media, etc.
|
||||
func cleanUsername(username string) string {
|
||||
username = strings.TrimSpace(username)
|
||||
username = strings.TrimPrefix(username, "@")
|
||||
|
||||
if strings.Contains(username, "x.com/") || strings.Contains(username, "twitter.com/") {
|
||||
parsed := username
|
||||
if !strings.HasPrefix(parsed, "http://") && !strings.HasPrefix(parsed, "https://") {
|
||||
parsed = "https://" + strings.TrimPrefix(parsed, "//")
|
||||
}
|
||||
if u, err := url.Parse(parsed); err == nil {
|
||||
segments := strings.Split(strings.Trim(u.Path, "/"), "/")
|
||||
// Skip special paths like /i/bookmarks, /search, /home, /explore
|
||||
if len(segments) > 0 && segments[0] != "" {
|
||||
firstSegment := strings.ToLower(segments[0])
|
||||
// These are not usernames
|
||||
if firstSegment == "i" || firstSegment == "search" || firstSegment == "home" || firstSegment == "explore" || firstSegment == "settings" || firstSegment == "messages" || firstSegment == "notifications" {
|
||||
return username // Return as-is, let caller handle
|
||||
}
|
||||
return segments[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return username
|
||||
}
|
||||
|
||||
// ensureURLScheme normalises URLs so the CLI accepts them
|
||||
func ensureURLScheme(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return raw
|
||||
}
|
||||
if strings.HasPrefix(raw, "http://") || strings.HasPrefix(raw, "https://") {
|
||||
return raw
|
||||
}
|
||||
if strings.HasPrefix(raw, "//") {
|
||||
return "https:" + raw
|
||||
}
|
||||
return "https://" + strings.TrimPrefix(raw, "//")
|
||||
}
|
||||
|
||||
// buildSearchURL assembles an X.com search URL for date range queries
|
||||
func buildSearchURL(username, startDate, endDate, mediaFilter string, includeRetweets bool) string {
|
||||
trimmed := strings.TrimSpace(username)
|
||||
lower := strings.ToLower(trimmed)
|
||||
if strings.Contains(lower, "search?q=") {
|
||||
return ensureURLScheme(trimmed)
|
||||
}
|
||||
|
||||
handle := cleanUsername(trimmed)
|
||||
var parts []string
|
||||
if handle != "" {
|
||||
parts = append(parts, fmt.Sprintf("from:%s", handle))
|
||||
}
|
||||
if startDate != "" {
|
||||
parts = append(parts, fmt.Sprintf("since:%s", startDate))
|
||||
}
|
||||
if endDate != "" {
|
||||
parts = append(parts, fmt.Sprintf("until:%s", endDate))
|
||||
}
|
||||
|
||||
switch strings.ToLower(strings.TrimSpace(mediaFilter)) {
|
||||
case "image", "images", "photo", "photos":
|
||||
parts = append(parts, "filter:images")
|
||||
case "video", "videos", "gif", "gifs":
|
||||
parts = append(parts, "filter:videos")
|
||||
case "text":
|
||||
parts = append(parts, "-filter:media")
|
||||
default:
|
||||
parts = append(parts, "filter:media")
|
||||
}
|
||||
|
||||
if !includeRetweets {
|
||||
parts = append(parts, "-filter:retweets")
|
||||
}
|
||||
|
||||
query := url.QueryEscape(strings.Join(parts, " "))
|
||||
return fmt.Sprintf("https://x.com/search?q=%s&src=typed_query&f=live", query)
|
||||
}
|
||||
|
||||
// convertMetadataToTimelineEntry converts metadata-only tweets to timeline entries
|
||||
func convertMetadataToTimelineEntry(meta TweetMetadata) TimelineEntry {
|
||||
return TimelineEntry{
|
||||
URL: "",
|
||||
Date: meta.Date,
|
||||
TweetID: meta.TweetID,
|
||||
Type: "text",
|
||||
IsRetweet: meta.RetweetID != 0,
|
||||
Extension: "txt",
|
||||
Width: 0,
|
||||
Height: 0,
|
||||
Content: meta.Content,
|
||||
ViewCount: meta.ViewCount,
|
||||
BookmarkCount: meta.BookmarkCount,
|
||||
FavoriteCount: meta.FavoriteCount,
|
||||
RetweetCount: meta.RetweetCount,
|
||||
ReplyCount: meta.ReplyCount,
|
||||
}
|
||||
}
|
||||
|
||||
// convertToTimelineEntry converts CLIMediaItem to TimelineEntry
|
||||
func convertToTimelineEntry(media CLIMediaItem) TimelineEntry {
|
||||
entry := TimelineEntry{
|
||||
URL: media.URL,
|
||||
TweetID: media.TweetID,
|
||||
Date: media.Date,
|
||||
Extension: media.Extension,
|
||||
Width: media.Width,
|
||||
Height: media.Height,
|
||||
IsRetweet: media.RetweetID != 0,
|
||||
Content: media.Content,
|
||||
ViewCount: media.ViewCount,
|
||||
BookmarkCount: media.BookmarkCount,
|
||||
FavoriteCount: media.FavoriteCount,
|
||||
RetweetCount: media.RetweetCount,
|
||||
ReplyCount: media.ReplyCount,
|
||||
Source: media.Source,
|
||||
Verified: media.Author.Verified,
|
||||
}
|
||||
|
||||
// Determine type - media item already has type from CLI
|
||||
if media.Type != "" {
|
||||
entry.Type = media.Type
|
||||
} else {
|
||||
switch strings.ToLower(media.Extension) {
|
||||
case "mp4", "webm":
|
||||
entry.Type = "video"
|
||||
case "gif":
|
||||
entry.Type = "gif"
|
||||
default:
|
||||
entry.Type = "photo"
|
||||
}
|
||||
}
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
// getExtractorPath returns the path to extractor binary
|
||||
// Binary is stored in ~/.twitterxmediabatchdownloader/ (same as ffmpeg and database)
|
||||
func getExtractorPath() string {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
baseDir := filepath.Join(homeDir, ".twitterxmediabatchdownloader")
|
||||
return filepath.Join(baseDir, getExecutableName())
|
||||
}
|
||||
|
||||
// getHashFilePath returns the path to the hash file for version checking
|
||||
func getHashFilePath() string {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
baseDir := filepath.Join(homeDir, ".twitterxmediabatchdownloader")
|
||||
return filepath.Join(baseDir, "extractor.sha256")
|
||||
}
|
||||
|
||||
// calculateHash calculates SHA256 hash of data
|
||||
func calculateHash(data []byte) string {
|
||||
hash := sha256.Sum256(data)
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
// ensureExtractor ensures the extractor binary exists
|
||||
// Extracts from embedded binary if not present or if hash differs (update)
|
||||
func ensureExtractor() (string, error) {
|
||||
exePath := getExtractorPath()
|
||||
hashPath := getHashFilePath()
|
||||
baseDir := filepath.Dir(exePath)
|
||||
|
||||
// Create directory if not exists
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to create directory: %v", err)
|
||||
}
|
||||
|
||||
// Calculate hash of embedded binary
|
||||
embeddedHash := calculateHash(extractorBin)
|
||||
|
||||
// Check if binary and hash file exist
|
||||
if _, err := os.Stat(exePath); err == nil {
|
||||
// Binary exists - check hash
|
||||
if storedHash, err := os.ReadFile(hashPath); err == nil {
|
||||
if string(storedHash) == embeddedHash {
|
||||
return exePath, nil // Already extracted and up to date
|
||||
}
|
||||
}
|
||||
// Hash differs or missing - need to update
|
||||
os.Remove(exePath)
|
||||
}
|
||||
|
||||
// Extract binary
|
||||
if err := os.WriteFile(exePath, extractorBin, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to write extractor: %v", err)
|
||||
}
|
||||
|
||||
// Save hash for future comparison
|
||||
if err := os.WriteFile(hashPath, []byte(embeddedHash), 0644); err != nil {
|
||||
// Non-fatal - binary still works, just won't skip next time
|
||||
fmt.Printf("Warning: failed to save hash file: %v\n", err)
|
||||
}
|
||||
|
||||
return exePath, nil
|
||||
}
|
||||
|
||||
// ExtractTimeline extracts media from user timeline using the new CLI
|
||||
func ExtractTimeline(req TimelineRequest) (*TwitterResponse, error) {
|
||||
// Get or extract extractor binary (persistent, not temp)
|
||||
exePath, err := ensureExtractor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Determine the right endpoint based on what user wants:
|
||||
// - Media (all/image/video/gif): Use /media endpoint - fastest and most reliable
|
||||
// - Text tweets: Use /tweets endpoint with --text-tweets
|
||||
// - With retweets: Use /tweets endpoint (retweets not available on /media)
|
||||
isTextOnly := req.MediaType == "text"
|
||||
wantsRetweets := req.Retweets
|
||||
|
||||
timelineType := req.TimelineType
|
||||
if timelineType == "" {
|
||||
if isTextOnly {
|
||||
// Text-only tweets need /tweets endpoint
|
||||
timelineType = "tweets"
|
||||
} else if wantsRetweets {
|
||||
// Retweets need /tweets endpoint (not available on /media)
|
||||
timelineType = "tweets"
|
||||
} else {
|
||||
// Default: /media endpoint - fastest for media-only fetch
|
||||
timelineType = "media"
|
||||
}
|
||||
}
|
||||
|
||||
url := buildTwitterURL(req.Username, timelineType)
|
||||
|
||||
// Build command arguments for new CLI format
|
||||
// Format: extractor.exe URL --auth-token TOKEN --json [options]
|
||||
args := []string{url}
|
||||
|
||||
// Add auth token
|
||||
if req.AuthToken != "" {
|
||||
args = append(args, "--auth-token", req.AuthToken)
|
||||
} else {
|
||||
args = append(args, "--guest")
|
||||
}
|
||||
|
||||
// Always request JSON output with metadata
|
||||
args = append(args, "--json", "--metadata")
|
||||
|
||||
// Add limit if specified
|
||||
if req.BatchSize > 0 {
|
||||
args = append(args, "--limit", fmt.Sprintf("%d", req.BatchSize))
|
||||
}
|
||||
|
||||
// Handle retweets - only relevant when using /tweets endpoint
|
||||
if timelineType == "tweets" || timelineType == "timeline" {
|
||||
if req.Retweets {
|
||||
args = append(args, "--retweets", "include")
|
||||
} else {
|
||||
args = append(args, "--retweets", "skip")
|
||||
}
|
||||
}
|
||||
|
||||
// Only add --text-tweets when explicitly requesting text content
|
||||
if isTextOnly {
|
||||
args = append(args, "--text-tweets")
|
||||
}
|
||||
|
||||
// Handle media type filter using --type parameter
|
||||
if req.MediaType != "" && req.MediaType != "all" && !isTextOnly {
|
||||
switch req.MediaType {
|
||||
case "image":
|
||||
args = append(args, "--type", "photo")
|
||||
case "video":
|
||||
args = append(args, "--type", "video")
|
||||
case "gif":
|
||||
args = append(args, "--type", "animated_gif")
|
||||
}
|
||||
}
|
||||
|
||||
// Add cursor for resume capability
|
||||
if req.Cursor != "" {
|
||||
args = append(args, "--cursor", req.Cursor)
|
||||
}
|
||||
|
||||
// Execute command with UTF-8 encoding
|
||||
cmd := exec.Command(exePath, args...)
|
||||
cmd.Env = append(os.Environ(),
|
||||
"PYTHONIOENCODING=utf-8",
|
||||
"PYTHONUTF8=1",
|
||||
)
|
||||
hideWindow(cmd) // Hide console window on Windows
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
// Ensure process is killed after completion
|
||||
if cmd.Process != nil {
|
||||
cmd.Process.Kill()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
outputStr := string(output)
|
||||
errorMsg := parseExtractorError(outputStr, req.Username)
|
||||
return nil, fmt.Errorf("%s", errorMsg)
|
||||
}
|
||||
|
||||
// Find JSON in output (skip any info messages)
|
||||
jsonStr := extractJSON(string(output))
|
||||
if jsonStr == "" {
|
||||
outputStr := string(output)
|
||||
if strings.TrimSpace(outputStr) == "" {
|
||||
return nil, fmt.Errorf("empty_response: Extractor returned no data. The timeline may be empty or inaccessible")
|
||||
}
|
||||
return nil, fmt.Errorf("parse_error: Could not parse extractor output. Raw output: %s", outputStr)
|
||||
}
|
||||
|
||||
// Parse CLI response
|
||||
var cliResponse CLIResponse
|
||||
if err := json.Unmarshal([]byte(jsonStr), &cliResponse); err != nil {
|
||||
return nil, fmt.Errorf("json_error: Failed to parse JSON response: %v", err)
|
||||
}
|
||||
|
||||
// Convert to frontend format
|
||||
var timeline []TimelineEntry
|
||||
accountInfo := AccountInfo{
|
||||
Name: req.Username,
|
||||
Nick: req.Username,
|
||||
}
|
||||
|
||||
// Build a set of tweet IDs that have media
|
||||
mediaTweetIDs := make(map[int64]bool)
|
||||
for _, media := range cliResponse.Media {
|
||||
mediaTweetIDs[int64(media.TweetID)] = true
|
||||
}
|
||||
|
||||
if isTextOnly {
|
||||
// Text-only mode: get tweets from metadata that don't have media
|
||||
timeline = make([]TimelineEntry, 0)
|
||||
for _, meta := range cliResponse.Metadata {
|
||||
if !mediaTweetIDs[int64(meta.TweetID)] {
|
||||
timeline = append(timeline, convertMetadataToTimelineEntry(meta))
|
||||
}
|
||||
}
|
||||
|
||||
// Get account info from first media item if available, otherwise from metadata
|
||||
if len(cliResponse.Media) > 0 {
|
||||
user := cliResponse.Media[0].User
|
||||
accountInfo.Name = user.Name
|
||||
accountInfo.Nick = user.Nick
|
||||
accountInfo.Date = user.Date
|
||||
accountInfo.FollowersCount = user.FollowersCount
|
||||
accountInfo.FriendsCount = user.FriendsCount
|
||||
accountInfo.ProfileImage = user.ProfileImage
|
||||
accountInfo.StatusesCount = user.StatusesCount
|
||||
} else if len(cliResponse.Metadata) > 0 {
|
||||
firstMeta := cliResponse.Metadata[0]
|
||||
accountInfo.Name = firstMeta.Author.Name
|
||||
accountInfo.Nick = firstMeta.Author.Nick
|
||||
}
|
||||
} else if len(cliResponse.Media) > 0 {
|
||||
// Normal media items only (text tweets handled separately with "text" media type)
|
||||
timeline = make([]TimelineEntry, 0, len(cliResponse.Media))
|
||||
|
||||
// Add media items
|
||||
for _, media := range cliResponse.Media {
|
||||
timeline = append(timeline, convertToTimelineEntry(media))
|
||||
}
|
||||
|
||||
// Get account info from first media item
|
||||
user := cliResponse.Media[0].User
|
||||
accountInfo.Name = user.Name
|
||||
accountInfo.Nick = user.Nick
|
||||
accountInfo.Date = user.Date
|
||||
accountInfo.FollowersCount = user.FollowersCount
|
||||
accountInfo.FriendsCount = user.FriendsCount
|
||||
accountInfo.ProfileImage = user.ProfileImage
|
||||
accountInfo.StatusesCount = user.StatusesCount
|
||||
} else if len(cliResponse.Metadata) > 0 {
|
||||
// Fallback: Text-only tweets (no media) - convert metadata to timeline entries
|
||||
timeline = make([]TimelineEntry, 0, len(cliResponse.Metadata))
|
||||
for _, meta := range cliResponse.Metadata {
|
||||
entry := TimelineEntry{
|
||||
URL: "", // No media URL for text tweets
|
||||
TweetID: meta.TweetID,
|
||||
Date: meta.Date,
|
||||
Type: "text",
|
||||
IsRetweet: meta.RetweetID != 0,
|
||||
Extension: "txt",
|
||||
Width: 0,
|
||||
Height: 0,
|
||||
Content: meta.Content,
|
||||
ViewCount: meta.ViewCount,
|
||||
BookmarkCount: meta.BookmarkCount,
|
||||
FavoriteCount: meta.FavoriteCount,
|
||||
RetweetCount: meta.RetweetCount,
|
||||
ReplyCount: meta.ReplyCount,
|
||||
}
|
||||
timeline = append(timeline, entry)
|
||||
}
|
||||
// Get account info from first metadata
|
||||
firstMeta := cliResponse.Metadata[0]
|
||||
accountInfo.Name = firstMeta.Author.Name
|
||||
accountInfo.Nick = firstMeta.Author.Nick
|
||||
}
|
||||
|
||||
// Determine if there's more data to fetch
|
||||
hasMore := cliResponse.Cursor != "" && !cliResponse.Completed
|
||||
|
||||
response := &TwitterResponse{
|
||||
AccountInfo: accountInfo,
|
||||
TotalURLs: len(timeline),
|
||||
Timeline: timeline,
|
||||
Metadata: ExtractMetadata{
|
||||
NewEntries: len(timeline),
|
||||
Page: req.Page,
|
||||
BatchSize: req.BatchSize,
|
||||
HasMore: hasMore,
|
||||
Cursor: cliResponse.Cursor,
|
||||
Completed: cliResponse.Completed,
|
||||
},
|
||||
Cursor: cliResponse.Cursor,
|
||||
Completed: cliResponse.Completed,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// ExtractDateRange extracts media based on date range using the new CLI
|
||||
func ExtractDateRange(req DateRangeRequest) (*TwitterResponse, error) {
|
||||
// Get or extract extractor binary (persistent, not temp)
|
||||
exePath, err := ensureExtractor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mediaFilter := strings.ToLower(strings.TrimSpace(req.MediaFilter))
|
||||
url := buildSearchURL(req.Username, req.StartDate, req.EndDate, mediaFilter, req.Retweets)
|
||||
|
||||
// Build command arguments
|
||||
args := []string{url}
|
||||
|
||||
// Add auth token
|
||||
if req.AuthToken != "" {
|
||||
args = append(args, "--auth-token", req.AuthToken)
|
||||
} else {
|
||||
args = append(args, "--guest")
|
||||
}
|
||||
|
||||
// Always request JSON output with metadata
|
||||
args = append(args, "--json", "--metadata")
|
||||
|
||||
if req.Retweets {
|
||||
args = append(args, "--retweets", "include")
|
||||
} else {
|
||||
args = append(args, "--retweets", "skip")
|
||||
}
|
||||
|
||||
isTextOnly := mediaFilter == "text"
|
||||
if isTextOnly {
|
||||
args = append(args, "--text-tweets")
|
||||
}
|
||||
|
||||
// Execute command with UTF-8 encoding
|
||||
cmd := exec.Command(exePath, args...)
|
||||
cmd.Env = append(os.Environ(),
|
||||
"PYTHONIOENCODING=utf-8",
|
||||
"PYTHONUTF8=1",
|
||||
)
|
||||
hideWindow(cmd)
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
// Ensure process is killed after completion
|
||||
if cmd.Process != nil {
|
||||
cmd.Process.Kill()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
outputStr := string(output)
|
||||
errorMsg := parseExtractorError(outputStr, req.Username)
|
||||
return nil, fmt.Errorf("%s", errorMsg)
|
||||
}
|
||||
|
||||
// Find JSON in output (skip any info messages)
|
||||
jsonStr := extractJSON(string(output))
|
||||
if jsonStr == "" {
|
||||
outputStr := string(output)
|
||||
if strings.TrimSpace(outputStr) == "" {
|
||||
return nil, fmt.Errorf("empty_response: Extractor returned no data. The timeline may be empty or inaccessible")
|
||||
}
|
||||
return nil, fmt.Errorf("parse_error: Could not parse extractor output. Raw output: %s", outputStr)
|
||||
}
|
||||
|
||||
// Parse CLI response
|
||||
var cliResponse CLIResponse
|
||||
if err := json.Unmarshal([]byte(jsonStr), &cliResponse); err != nil {
|
||||
return nil, fmt.Errorf("json_error: Failed to parse JSON response: %v", err)
|
||||
}
|
||||
|
||||
// Convert to frontend format
|
||||
mediaTweetIDs := make(map[int64]bool)
|
||||
for _, media := range cliResponse.Media {
|
||||
mediaTweetIDs[int64(media.TweetID)] = true
|
||||
}
|
||||
|
||||
timeline := make([]TimelineEntry, 0, len(cliResponse.Media)+len(cliResponse.Metadata))
|
||||
for _, media := range cliResponse.Media {
|
||||
timeline = append(timeline, convertToTimelineEntry(media))
|
||||
}
|
||||
|
||||
if isTextOnly {
|
||||
for _, meta := range cliResponse.Metadata {
|
||||
if !mediaTweetIDs[int64(meta.TweetID)] {
|
||||
timeline = append(timeline, convertMetadataToTimelineEntry(meta))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build account info from first media item (has full user info)
|
||||
accountInfo := AccountInfo{
|
||||
Name: req.Username,
|
||||
Nick: req.Username,
|
||||
}
|
||||
if len(cliResponse.Media) > 0 {
|
||||
user := cliResponse.Media[0].User
|
||||
accountInfo.Name = user.Name
|
||||
accountInfo.Nick = user.Nick
|
||||
accountInfo.Date = user.Date
|
||||
accountInfo.FollowersCount = user.FollowersCount
|
||||
accountInfo.FriendsCount = user.FriendsCount
|
||||
accountInfo.ProfileImage = user.ProfileImage
|
||||
accountInfo.StatusesCount = user.StatusesCount
|
||||
} else if len(cliResponse.Metadata) > 0 {
|
||||
firstMeta := cliResponse.Metadata[0]
|
||||
accountInfo.Name = firstMeta.Author.Name
|
||||
accountInfo.Nick = firstMeta.Author.Nick
|
||||
}
|
||||
|
||||
// Determine if there's more data to fetch
|
||||
hasMore := cliResponse.Cursor != "" && !cliResponse.Completed
|
||||
|
||||
response := &TwitterResponse{
|
||||
AccountInfo: accountInfo,
|
||||
TotalURLs: len(timeline),
|
||||
Timeline: timeline,
|
||||
Metadata: ExtractMetadata{
|
||||
NewEntries: len(timeline),
|
||||
Page: 0,
|
||||
BatchSize: 0,
|
||||
HasMore: hasMore,
|
||||
Cursor: cliResponse.Cursor,
|
||||
Completed: cliResponse.Completed,
|
||||
},
|
||||
Cursor: cliResponse.Cursor,
|
||||
Completed: cliResponse.Completed,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// extractJSON finds and extracts JSON object from output string
|
||||
func extractJSON(output string) string {
|
||||
// Find the start of JSON object
|
||||
start := strings.Index(output, "{")
|
||||
if start == -1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Find the matching closing brace
|
||||
depth := 0
|
||||
for i := start; i < len(output); i++ {
|
||||
if output[i] == '{' {
|
||||
depth++
|
||||
} else if output[i] == '}' {
|
||||
depth--
|
||||
if depth == 0 {
|
||||
return output[start : i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetThumbnailURL converts a Twitter media URL to thumbnail size
|
||||
func GetThumbnailURL(url string) string {
|
||||
// For images: https://pbs.twimg.com/media/XXX?format=jpg&name=thumb
|
||||
if strings.Contains(url, "pbs.twimg.com/media/") {
|
||||
// Check if it already has format parameter
|
||||
if strings.Contains(url, "?format=") {
|
||||
// Replace name parameter with thumb
|
||||
if strings.Contains(url, "&name=") {
|
||||
parts := strings.Split(url, "&name=")
|
||||
return parts[0] + "&name=thumb"
|
||||
}
|
||||
return url + "&name=thumb"
|
||||
}
|
||||
// Add format and name parameters
|
||||
if strings.Contains(url, "?") {
|
||||
return url + "&name=thumb"
|
||||
}
|
||||
return url + "?format=jpg&name=thumb"
|
||||
}
|
||||
return url
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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'
|
||||
]
|
||||
@@ -1,15 +0,0 @@
|
||||
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
|
||||
@@ -1,420 +0,0 @@
|
||||
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()]
|
||||
@@ -1,307 +0,0 @@
|
||||
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
|
||||
@@ -1,329 +0,0 @@
|
||||
import re
|
||||
import json
|
||||
from datetime import datetime
|
||||
from gallery_dl.extractor import twitter
|
||||
|
||||
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":
|
||||
extractor_class = twitter.TwitterMediaExtractor
|
||||
elif timeline_type == "tweets":
|
||||
extractor_class = twitter.TwitterTweetsExtractor
|
||||
elif timeline_type == "with_replies":
|
||||
extractor_class = twitter.TwitterRepliesExtractor
|
||||
else:
|
||||
extractor_class = twitter.TwitterTimelineExtractor
|
||||
|
||||
match = re.match(extractor_class.pattern, url)
|
||||
if not match:
|
||||
raise ValueError(f"Invalid URL for {timeline_type}: {url}")
|
||||
|
||||
extractor = extractor_class(match)
|
||||
|
||||
config_dict = {
|
||||
"cookies": {
|
||||
"auth_token": auth_token
|
||||
},
|
||||
"retweets": retweets
|
||||
}
|
||||
|
||||
if batch_size > 0:
|
||||
config_dict["count"] = batch_size
|
||||
|
||||
extractor.config = lambda key, default=None: config_dict.get(key, default)
|
||||
|
||||
try:
|
||||
extractor.initialize()
|
||||
|
||||
api = twitter.TwitterAPI(extractor)
|
||||
try:
|
||||
if username.startswith("id:"):
|
||||
user = api.user_by_rest_id(username[3:])
|
||||
else:
|
||||
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
|
||||
|
||||
structured_output = {
|
||||
'account_info': {},
|
||||
'total_urls': 0,
|
||||
'timeline': []
|
||||
}
|
||||
|
||||
iterator = iter(extractor)
|
||||
|
||||
if batch_size > 0 and page > 0:
|
||||
items_to_skip = page * batch_size
|
||||
|
||||
if hasattr(extractor, '_cursor') and extractor._cursor:
|
||||
pass
|
||||
else:
|
||||
skipped = 0
|
||||
try:
|
||||
for _ in range(items_to_skip):
|
||||
next(iterator)
|
||||
skipped += 1
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
new_timeline_entries = []
|
||||
|
||||
items_to_fetch = batch_size if batch_size > 0 else float('inf')
|
||||
items_fetched = 0
|
||||
|
||||
try:
|
||||
while items_fetched < items_to_fetch:
|
||||
item = next(iterator)
|
||||
items_fetched += 1
|
||||
|
||||
if isinstance(item, tuple) and len(item) >= 3:
|
||||
media_url = item[1]
|
||||
tweet_data = item[2]
|
||||
|
||||
if not structured_output['account_info'] and 'user' in tweet_data:
|
||||
user = tweet_data['user']
|
||||
user_date = user.get('date', '')
|
||||
if isinstance(user_date, datetime):
|
||||
user_date = user_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
structured_output['account_info'] = {
|
||||
'name': user.get('name', ''),
|
||||
'nick': user.get('nick', ''),
|
||||
'date': user_date,
|
||||
'followers_count': user.get('followers_count', 0),
|
||||
'friends_count': user.get('friends_count', 0),
|
||||
'profile_image': user.get('profile_image', ''),
|
||||
'statuses_count': user.get('statuses_count', 0)
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
(media_type == 'gif' and 'video.twimg.com' in media_url and tweet_data.get('type') == 'animated_gif')
|
||||
):
|
||||
new_timeline_entries.append(timeline_entry)
|
||||
structured_output['total_urls'] += 1
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
structured_output['timeline'].extend(new_timeline_entries)
|
||||
|
||||
cursor_info = None
|
||||
if hasattr(extractor, '_cursor') and extractor._cursor:
|
||||
cursor_info = extractor._cursor
|
||||
|
||||
structured_output['metadata'] = {
|
||||
"new_entries": len(new_timeline_entries),
|
||||
"page": page,
|
||||
"batch_size": batch_size,
|
||||
"has_more": batch_size > 0 and items_fetched == batch_size,
|
||||
"cursor": cursor_info
|
||||
}
|
||||
|
||||
if not structured_output['account_info']:
|
||||
raise ValueError("Failed to fetch account information. Please check the username and auth token.")
|
||||
|
||||
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 main():
|
||||
username = ""
|
||||
auth_token = ""
|
||||
timeline_type = "media"
|
||||
batch_size = 100
|
||||
page = 0
|
||||
media_type = "all"
|
||||
|
||||
try:
|
||||
data = get_metadata(
|
||||
username=username,
|
||||
auth_token=auth_token,
|
||||
timeline_type=timeline_type,
|
||||
batch_size=batch_size,
|
||||
page=page,
|
||||
media_type=media_type
|
||||
)
|
||||
print(json.dumps(data, ensure_ascii=False, indent=2))
|
||||
except Exception as e:
|
||||
error_str = str(e)
|
||||
if error_str == "None":
|
||||
print(json.dumps({"error": "Failed to authenticate. Please verify your auth token is valid and not expired."}, ensure_ascii=False))
|
||||
else:
|
||||
print(json.dumps({"error": error_str}, ensure_ascii=False))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,60 +0,0 @@
|
||||
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))
|
||||
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"$schema": "https://ui.shadcn.com/schema.json",
|
||||
"style": "new-york",
|
||||
"rsc": false,
|
||||
"tsx": true,
|
||||
"tailwind": {
|
||||
"config": "",
|
||||
"css": "src/index.css",
|
||||
"baseColor": "neutral",
|
||||
"cssVariables": true,
|
||||
"prefix": ""
|
||||
},
|
||||
"iconLibrary": "lucide",
|
||||
"aliases": {
|
||||
"components": "@/components",
|
||||
"utils": "@/lib/utils",
|
||||
"ui": "@/components/ui",
|
||||
"lib": "@/lib",
|
||||
"hooks": "@/hooks"
|
||||
},
|
||||
"registries": {}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import js from '@eslint/js'
|
||||
import globals from 'globals'
|
||||
import reactHooks from 'eslint-plugin-react-hooks'
|
||||
import reactRefresh from 'eslint-plugin-react-refresh'
|
||||
import tseslint from 'typescript-eslint'
|
||||
import { defineConfig, globalIgnores } from 'eslint/config'
|
||||
|
||||
export default defineConfig([
|
||||
globalIgnores(['dist']),
|
||||
{
|
||||
files: ['**/*.{ts,tsx}'],
|
||||
extends: [
|
||||
js.configs.recommended,
|
||||
tseslint.configs.recommended,
|
||||
reactHooks.configs.flat.recommended,
|
||||
reactRefresh.configs.vite,
|
||||
],
|
||||
languageOptions: {
|
||||
ecmaVersion: 2020,
|
||||
globals: globals.browser,
|
||||
},
|
||||
},
|
||||
])
|
||||
@@ -0,0 +1,16 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/icon.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&family=Google+Sans+Code:ital,wght@0,300..800;1,300..800&family=Google+Sans+Flex:opsz,wght@6..144,1..1000&family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=Manrope:wght@200..800&family=Plus+Jakarta+Sans:ital,wght@0,200..800;1,200..800&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Roboto:ital,wght@0,100..900;1,100..900&family=Space+Grotesk:wght@300..700&display=swap" rel="stylesheet">
|
||||
<title>Twitter/X Media Batch Downloader</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "frontend",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc -b && vite build",
|
||||
"lint": "eslint .",
|
||||
"preview": "vite preview",
|
||||
"generate-icon": "node scripts/generate-icon.js",
|
||||
"postinstall": "node scripts/generate-icon.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@radix-ui/react-accordion": "^1.2.12",
|
||||
"@radix-ui/react-checkbox": "^1.3.3",
|
||||
"@radix-ui/react-context-menu": "^2.2.16",
|
||||
"@radix-ui/react-dialog": "^1.1.15",
|
||||
"@radix-ui/react-label": "^2.1.8",
|
||||
"@radix-ui/react-progress": "^1.1.8",
|
||||
"@radix-ui/react-radio-group": "^1.3.8",
|
||||
"@radix-ui/react-select": "^2.2.6",
|
||||
"@radix-ui/react-slot": "^1.2.4",
|
||||
"@radix-ui/react-switch": "^1.1.4",
|
||||
"@radix-ui/react-tabs": "^1.1.13",
|
||||
"@radix-ui/react-tooltip": "^1.2.8",
|
||||
"@tailwindcss/vite": "^4.1.17",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"lucide-react": "^0.554.0",
|
||||
"next-themes": "^0.4.6",
|
||||
"react": "^19.2.0",
|
||||
"react-dom": "^19.2.0",
|
||||
"sonner": "^2.0.7",
|
||||
"tailwind-merge": "^3.4.0",
|
||||
"tailwindcss": "^4.1.17"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.39.1",
|
||||
"@types/node": "^24.10.1",
|
||||
"@types/react": "^19.2.6",
|
||||
"@types/react-dom": "^19.2.3",
|
||||
"@vitejs/plugin-react": "^5.1.1",
|
||||
"eslint": "^9.39.1",
|
||||
"eslint-plugin-react-hooks": "^7.0.1",
|
||||
"eslint-plugin-react-refresh": "^0.4.24",
|
||||
"globals": "^16.5.0",
|
||||
"sharp": "^0.34.5",
|
||||
"tw-animate-css": "^1.4.0",
|
||||
"typescript": "~5.9.3",
|
||||
"typescript-eslint": "^8.47.0",
|
||||
"vite": "^7.2.4"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
6d6b26a2fed70996ef14dbc3e325f141
|
||||
Generated
+3949
File diff suppressed because it is too large.
Load diff
File renamed without changes.
|
Before Width: | Height: | Size: 865 B After Width: | Height: | Size: 865 B |
@@ -0,0 +1,33 @@
|
||||
import sharp from 'sharp';
|
||||
import { readFileSync, mkdirSync } from 'fs';
|
||||
import { dirname, join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const rootDir = join(__dirname, '..', '..');
|
||||
|
||||
const svgPath = join(rootDir, 'frontend', 'public', 'icon.svg');
|
||||
const outputPath = join(rootDir, 'build', 'appicon.png');
|
||||
|
||||
async function generateIcon() {
|
||||
try {
|
||||
// Ensure build directory exists
|
||||
mkdirSync(join(rootDir, 'build'), { recursive: true });
|
||||
|
||||
// Read SVG
|
||||
const svgBuffer = readFileSync(svgPath);
|
||||
|
||||
// Convert SVG to PNG (1024x1024 for Wails)
|
||||
await sharp(svgBuffer)
|
||||
.resize(1024, 1024)
|
||||
.png()
|
||||
.toFile(outputPath);
|
||||
|
||||
console.log('✓ Icon generated:', outputPath);
|
||||
} catch (error) {
|
||||
console.error('✗ Failed to generate icon:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
generateIcon();
|
||||
@@ -0,0 +1,592 @@
|
||||
import { useState, useEffect, useRef, useCallback } from "react";
|
||||
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||
import { getSettings, applyThemeMode, applyFont } from "@/lib/settings";
|
||||
import { applyTheme } from "@/lib/themes";
|
||||
import { toastWithSound as toast } from "@/lib/toast-with-sound";
|
||||
import { logger } from "@/lib/logger";
|
||||
import {
|
||||
saveFetchState,
|
||||
getFetchState,
|
||||
clearFetchState,
|
||||
getResumableInfo,
|
||||
stateToResponse,
|
||||
mergeTimelines,
|
||||
type FetchState,
|
||||
} from "@/lib/fetch-state";
|
||||
|
||||
// Components
|
||||
import { TitleBar } from "@/components/TitleBar";
|
||||
import { Sidebar, type PageType } from "@/components/Sidebar";
|
||||
import { Header } from "@/components/Header";
|
||||
import { SearchBar, type FetchMode, type PrivateType } from "@/components/SearchBar";
|
||||
import { MediaList } from "@/components/MediaList";
|
||||
import { DatabaseView } from "@/components/DatabaseView";
|
||||
import { SettingsPage } from "@/components/SettingsPage";
|
||||
import { DebugLoggerPage } from "@/components/DebugLoggerPage";
|
||||
import type { HistoryItem } from "@/components/FetchHistory";
|
||||
import type { TwitterResponse } from "@/types/api";
|
||||
|
||||
// Wails bindings
|
||||
import { ExtractTimeline, ExtractDateRange, SaveAccountToDBWithStatus, CleanupExtractorProcesses } from "../wailsjs/go/main/App";
|
||||
|
||||
const HISTORY_KEY = "twitter_media_fetch_history";
|
||||
const MAX_HISTORY = 10;
|
||||
const CURRENT_VERSION = "4.0";
|
||||
const BATCH_SIZE = 200; // Fetch in batches for progressive display and resume
|
||||
|
||||
function App() {
|
||||
const [currentPage, setCurrentPage] = useState<PageType>("main");
|
||||
const [username, setUsername] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [result, setResult] = useState<TwitterResponse | null>(null);
|
||||
const [fetchedMediaType, setFetchedMediaType] = useState<string>("all");
|
||||
const [hasUpdate, setHasUpdate] = useState(false);
|
||||
const [releaseDate, setReleaseDate] = useState<string | null>(null);
|
||||
const [fetchHistory, setFetchHistory] = useState<HistoryItem[]>([]);
|
||||
const [resumeInfo, setResumeInfo] = useState<{ canResume: boolean; mediaCount: number } | null>(null);
|
||||
const stopFetchRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
const settings = getSettings();
|
||||
applyThemeMode(settings.themeMode);
|
||||
applyTheme(settings.theme);
|
||||
applyFont(settings.fontFamily);
|
||||
|
||||
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const handleChange = () => {
|
||||
const currentSettings = getSettings();
|
||||
if (currentSettings.themeMode === "auto") {
|
||||
applyThemeMode("auto");
|
||||
applyTheme(currentSettings.theme);
|
||||
}
|
||||
};
|
||||
|
||||
mediaQuery.addEventListener("change", handleChange);
|
||||
checkForUpdates();
|
||||
loadHistory();
|
||||
|
||||
return () => {
|
||||
mediaQuery.removeEventListener("change", handleChange);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const checkForUpdates = async () => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
"https://api.github.com/repos/afkarxyz/Twitter-X-Media-Batch-Downloader/releases/latest"
|
||||
);
|
||||
const data = await response.json();
|
||||
const latestVersion = data.tag_name?.replace(/^v/, "") || "";
|
||||
|
||||
if (data.published_at) {
|
||||
setReleaseDate(data.published_at);
|
||||
}
|
||||
|
||||
if (latestVersion && latestVersion > CURRENT_VERSION) {
|
||||
setHasUpdate(true);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to check for updates:", err);
|
||||
}
|
||||
};
|
||||
|
||||
const loadHistory = () => {
|
||||
try {
|
||||
const saved = localStorage.getItem(HISTORY_KEY);
|
||||
if (saved) {
|
||||
setFetchHistory(JSON.parse(saved));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to load history:", err);
|
||||
}
|
||||
};
|
||||
|
||||
const saveHistory = (history: HistoryItem[]) => {
|
||||
try {
|
||||
localStorage.setItem(HISTORY_KEY, JSON.stringify(history));
|
||||
} catch (err) {
|
||||
console.error("Failed to save history:", err);
|
||||
}
|
||||
};
|
||||
|
||||
const addToHistory = (data: TwitterResponse, inputUsername: string) => {
|
||||
// Clean username (remove @ and extract from URL if needed)
|
||||
let cleanUsername = inputUsername.trim();
|
||||
if (cleanUsername.startsWith("@")) {
|
||||
cleanUsername = cleanUsername.slice(1);
|
||||
}
|
||||
if (cleanUsername.includes("x.com/") || cleanUsername.includes("twitter.com/")) {
|
||||
const match = cleanUsername.match(/(?:x\.com|twitter\.com)\/([^/?]+)/);
|
||||
if (match) cleanUsername = match[1];
|
||||
}
|
||||
|
||||
setFetchHistory((prev) => {
|
||||
// Use username from API response (account_info.name) for consistency
|
||||
const apiUsername = data.account_info.name;
|
||||
const filtered = prev.filter((h) => h.username.toLowerCase() !== apiUsername.toLowerCase());
|
||||
const newItem: HistoryItem = {
|
||||
id: crypto.randomUUID(),
|
||||
username: apiUsername, // username/handle from API
|
||||
name: data.account_info.nick, // display name from API
|
||||
image: data.account_info.profile_image,
|
||||
mediaCount: data.total_urls,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
const updated = [newItem, ...filtered].slice(0, MAX_HISTORY);
|
||||
saveHistory(updated);
|
||||
return updated;
|
||||
});
|
||||
};
|
||||
|
||||
const removeFromHistory = (id: string) => {
|
||||
setFetchHistory((prev) => {
|
||||
const updated = prev.filter((h) => h.id !== id);
|
||||
saveHistory(updated);
|
||||
return updated;
|
||||
});
|
||||
};
|
||||
|
||||
const handleHistorySelect = (item: HistoryItem) => {
|
||||
setUsername(item.username);
|
||||
};
|
||||
|
||||
// Check for resumable fetch when username changes
|
||||
const checkResumable = useCallback((user: string) => {
|
||||
if (!user.trim()) {
|
||||
setResumeInfo(null);
|
||||
return;
|
||||
}
|
||||
const info = getResumableInfo(user.trim());
|
||||
setResumeInfo(info.canResume ? { canResume: true, mediaCount: info.mediaCount } : null);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
checkResumable(username);
|
||||
}, [username, checkResumable]);
|
||||
|
||||
const handleStopFetch = async () => {
|
||||
stopFetchRef.current = true;
|
||||
logger.info("Stopping fetch...");
|
||||
toast.info("Stopping...");
|
||||
// Kill any running extractor processes
|
||||
try {
|
||||
await CleanupExtractorProcesses();
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
};
|
||||
|
||||
const handleFetch = async (
|
||||
useDateRange: boolean,
|
||||
startDate?: string,
|
||||
endDate?: string,
|
||||
mediaType?: string,
|
||||
retweets?: boolean,
|
||||
mode: FetchMode = "public",
|
||||
privateType?: PrivateType,
|
||||
authToken?: string,
|
||||
isResume?: boolean
|
||||
) => {
|
||||
// Prevent multiple concurrent fetches
|
||||
if (loading) {
|
||||
logger.warning("Fetch already in progress, please wait or stop first");
|
||||
toast.warning("Fetch already in progress");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate based on mode
|
||||
const isBookmarks = mode === "private" && privateType === "bookmarks";
|
||||
const isLikes = mode === "private" && privateType === "likes";
|
||||
|
||||
// Username required for public mode and likes mode
|
||||
if (!isBookmarks && !username.trim()) {
|
||||
toast.error("Please enter a username");
|
||||
return;
|
||||
}
|
||||
|
||||
// Use auth token from SearchBar
|
||||
if (!authToken?.trim()) {
|
||||
toast.error("Please enter your auth token");
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset state for new fetch
|
||||
setLoading(true);
|
||||
setFetchedMediaType(mediaType || "all");
|
||||
stopFetchRef.current = false;
|
||||
|
||||
// Cleanup any leftover processes from previous fetch
|
||||
try {
|
||||
await CleanupExtractorProcesses();
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
|
||||
// Determine what we're fetching for logging
|
||||
const fetchTarget = isBookmarks
|
||||
? "your bookmarks"
|
||||
: isLikes
|
||||
? "your likes"
|
||||
: `@${username}`;
|
||||
|
||||
// Check for resume state
|
||||
const cleanUsername = isBookmarks ? "bookmarks" : username.trim();
|
||||
let existingState: FetchState | null = null;
|
||||
let cursor: string | undefined;
|
||||
let allTimeline: TwitterResponse["timeline"] = [];
|
||||
let accountInfo: TwitterResponse["account_info"] | null = null;
|
||||
|
||||
if (isResume) {
|
||||
existingState = getFetchState(cleanUsername);
|
||||
if (existingState && existingState.cursor && !existingState.completed) {
|
||||
cursor = existingState.cursor;
|
||||
allTimeline = existingState.timeline;
|
||||
accountInfo = existingState.accountInfo;
|
||||
logger.info(`Resuming ${fetchTarget} from ${allTimeline.length} items...`);
|
||||
|
||||
// Show existing data immediately
|
||||
if (accountInfo) {
|
||||
setResult({
|
||||
account_info: accountInfo,
|
||||
timeline: allTimeline,
|
||||
total_urls: allTimeline.length,
|
||||
metadata: { new_entries: 0, page: 0, batch_size: 0, has_more: true },
|
||||
});
|
||||
}
|
||||
} else {
|
||||
toast.error("No resumable fetch found");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Fresh fetch - clear any existing state
|
||||
clearFetchState(cleanUsername);
|
||||
setResult(null);
|
||||
logger.info(`Fetching ${fetchTarget}...`);
|
||||
}
|
||||
|
||||
try {
|
||||
let finalData: TwitterResponse | null = null;
|
||||
|
||||
if (useDateRange && startDate && endDate && mode === "public") {
|
||||
// Date range mode - single fetch (only for public mode)
|
||||
logger.info(`Using date range: ${startDate} to ${endDate}`);
|
||||
const response = await ExtractDateRange({
|
||||
username: username.trim(),
|
||||
auth_token: authToken.trim(),
|
||||
start_date: startDate,
|
||||
end_date: endDate,
|
||||
media_filter: mediaType || "all",
|
||||
retweets: retweets || false,
|
||||
});
|
||||
finalData = JSON.parse(response);
|
||||
} else {
|
||||
// Timeline mode with batching for progressive display and resume
|
||||
let timelineType = "timeline";
|
||||
if (isBookmarks) {
|
||||
timelineType = "bookmarks";
|
||||
} else if (isLikes) {
|
||||
timelineType = "likes";
|
||||
}
|
||||
|
||||
let hasMore = true;
|
||||
let page = 0;
|
||||
|
||||
while (hasMore && !stopFetchRef.current) {
|
||||
const batchNum = page + 1;
|
||||
logger.info(`Fetching batch ${batchNum}${cursor ? " (resuming)" : ""}...`);
|
||||
|
||||
const response = await ExtractTimeline({
|
||||
username: isBookmarks ? "" : username.trim(),
|
||||
auth_token: authToken.trim(),
|
||||
timeline_type: timelineType,
|
||||
batch_size: BATCH_SIZE,
|
||||
page: page,
|
||||
media_type: mediaType || "all",
|
||||
retweets: retweets || false,
|
||||
cursor: cursor,
|
||||
});
|
||||
|
||||
const data: TwitterResponse = JSON.parse(response);
|
||||
|
||||
// Set account info from first response
|
||||
if (!accountInfo && data.account_info) {
|
||||
accountInfo = data.account_info;
|
||||
if (isBookmarks) {
|
||||
accountInfo.name = "bookmarks";
|
||||
accountInfo.nick = "My Bookmarks";
|
||||
}
|
||||
}
|
||||
|
||||
// Merge new entries (deduplicate)
|
||||
allTimeline = mergeTimelines(allTimeline, data.timeline);
|
||||
|
||||
// Update cursor for next batch
|
||||
cursor = data.cursor;
|
||||
hasMore = !!data.cursor && !data.completed;
|
||||
page++;
|
||||
|
||||
// Update UI progressively - show results immediately
|
||||
if (accountInfo) {
|
||||
setResult({
|
||||
account_info: accountInfo,
|
||||
timeline: allTimeline,
|
||||
total_urls: allTimeline.length,
|
||||
metadata: {
|
||||
new_entries: data.timeline.length,
|
||||
page: page,
|
||||
batch_size: BATCH_SIZE,
|
||||
has_more: hasMore,
|
||||
cursor: cursor,
|
||||
completed: !hasMore,
|
||||
},
|
||||
cursor: cursor,
|
||||
completed: !hasMore,
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`Fetched ${allTimeline.length} items total`);
|
||||
|
||||
// Save state periodically (every 3 batches) or when stopping
|
||||
// This reduces localStorage writes for better performance
|
||||
if (page % 3 === 0 || !hasMore || stopFetchRef.current) {
|
||||
saveFetchState({
|
||||
username: cleanUsername,
|
||||
cursor: cursor || "",
|
||||
timeline: allTimeline,
|
||||
accountInfo: accountInfo,
|
||||
totalFetched: allTimeline.length,
|
||||
completed: !hasMore,
|
||||
authToken: authToken.trim(),
|
||||
mediaType: mediaType || "all",
|
||||
retweets: retweets || false,
|
||||
timelineType: timelineType,
|
||||
});
|
||||
|
||||
// Also save to database for persistence (can resume even after app restart)
|
||||
if (accountInfo) {
|
||||
const currentResponse: TwitterResponse = {
|
||||
account_info: accountInfo,
|
||||
timeline: allTimeline,
|
||||
total_urls: allTimeline.length,
|
||||
metadata: {
|
||||
new_entries: data.timeline.length,
|
||||
page: page,
|
||||
batch_size: BATCH_SIZE,
|
||||
has_more: hasMore,
|
||||
cursor: cursor,
|
||||
completed: !hasMore,
|
||||
},
|
||||
cursor: cursor,
|
||||
completed: !hasMore,
|
||||
};
|
||||
try {
|
||||
await SaveAccountToDBWithStatus(
|
||||
accountInfo.name,
|
||||
accountInfo.nick,
|
||||
accountInfo.profile_image,
|
||||
allTimeline.length,
|
||||
JSON.stringify(currentResponse),
|
||||
mediaType || "all",
|
||||
cursor || "",
|
||||
!hasMore
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("Failed to save progress to database:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If stopped by user, keep state for resume
|
||||
if (stopFetchRef.current) {
|
||||
logger.info(`Stopped at ${allTimeline.length} items - can resume later`);
|
||||
toast.info(`Stopped at ${allTimeline.length} items`);
|
||||
setResumeInfo({ canResume: true, mediaCount: allTimeline.length });
|
||||
} else {
|
||||
// Completed - clear state
|
||||
clearFetchState(cleanUsername);
|
||||
setResumeInfo(null);
|
||||
}
|
||||
|
||||
// Build final response
|
||||
finalData = accountInfo
|
||||
? {
|
||||
account_info: accountInfo,
|
||||
timeline: allTimeline,
|
||||
total_urls: allTimeline.length,
|
||||
metadata: {
|
||||
new_entries: allTimeline.length,
|
||||
page: page,
|
||||
batch_size: BATCH_SIZE,
|
||||
has_more: false,
|
||||
cursor: cursor,
|
||||
completed: !stopFetchRef.current,
|
||||
},
|
||||
cursor: cursor,
|
||||
completed: !stopFetchRef.current,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
if (finalData) {
|
||||
setResult(finalData);
|
||||
|
||||
// Only add to history for public mode
|
||||
if (mode === "public") {
|
||||
addToHistory(finalData, username);
|
||||
}
|
||||
|
||||
// Save to database with media type and completion status
|
||||
try {
|
||||
await SaveAccountToDBWithStatus(
|
||||
finalData.account_info.name,
|
||||
finalData.account_info.nick,
|
||||
finalData.account_info.profile_image,
|
||||
finalData.total_urls,
|
||||
JSON.stringify(finalData),
|
||||
mediaType || "all",
|
||||
finalData.cursor || "",
|
||||
finalData.completed ?? true
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("Failed to save to database:", err);
|
||||
}
|
||||
|
||||
if (!stopFetchRef.current) {
|
||||
logger.success(`Found ${finalData.total_urls} media items`);
|
||||
toast.success(`${finalData.total_urls} media items found`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
logger.error(`Failed to fetch: ${errorMsg}`);
|
||||
toast.error("Failed to fetch media");
|
||||
|
||||
// On error, check if we have partial data saved
|
||||
const savedState = getFetchState(cleanUsername);
|
||||
if (savedState && savedState.timeline.length > 0) {
|
||||
const partialResponse = stateToResponse(savedState);
|
||||
if (partialResponse) {
|
||||
setResult(partialResponse);
|
||||
setResumeInfo({ canResume: true, mediaCount: savedState.timeline.length });
|
||||
toast.info(`Saved ${savedState.timeline.length} items - can resume`);
|
||||
|
||||
// Also save partial data to database for persistence
|
||||
try {
|
||||
await SaveAccountToDBWithStatus(
|
||||
partialResponse.account_info.name,
|
||||
partialResponse.account_info.nick,
|
||||
partialResponse.account_info.profile_image,
|
||||
partialResponse.total_urls,
|
||||
JSON.stringify(partialResponse),
|
||||
mediaType || "all",
|
||||
savedState.cursor || "",
|
||||
false // not completed
|
||||
);
|
||||
} catch (dbErr) {
|
||||
console.error("Failed to save partial data to database:", dbErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Handle resume fetch
|
||||
const handleResume = (authToken: string, mediaType?: string, retweets?: boolean) => {
|
||||
handleFetch(false, undefined, undefined, mediaType, retweets, "public", undefined, authToken, true);
|
||||
};
|
||||
|
||||
// Clear resume state
|
||||
const handleClearResume = () => {
|
||||
if (username.trim()) {
|
||||
clearFetchState(username.trim());
|
||||
setResumeInfo(null);
|
||||
toast.info("Resume data cleared");
|
||||
}
|
||||
};
|
||||
|
||||
const handleLoadFromDB = (responseJSON: string, loadedUsername: string) => {
|
||||
try {
|
||||
const data: TwitterResponse = JSON.parse(responseJSON);
|
||||
setResult(data);
|
||||
setUsername(loadedUsername);
|
||||
setCurrentPage("main");
|
||||
toast.success(`Loaded @${loadedUsername} from database`);
|
||||
} catch (error) {
|
||||
toast.error("Failed to parse saved data");
|
||||
}
|
||||
};
|
||||
|
||||
const renderPage = () => {
|
||||
switch (currentPage) {
|
||||
case "settings":
|
||||
return <SettingsPage />;
|
||||
case "debug":
|
||||
return <DebugLoggerPage />;
|
||||
case "database":
|
||||
return (
|
||||
<DatabaseView
|
||||
onBack={() => setCurrentPage("main")}
|
||||
onLoadAccount={handleLoadFromDB}
|
||||
/>
|
||||
);
|
||||
default:
|
||||
return (
|
||||
<>
|
||||
<Header
|
||||
version={CURRENT_VERSION}
|
||||
hasUpdate={hasUpdate}
|
||||
releaseDate={releaseDate}
|
||||
/>
|
||||
|
||||
<SearchBar
|
||||
username={username}
|
||||
loading={loading}
|
||||
onUsernameChange={setUsername}
|
||||
onFetch={handleFetch}
|
||||
onStopFetch={handleStopFetch}
|
||||
onResume={handleResume}
|
||||
onClearResume={handleClearResume}
|
||||
resumeInfo={resumeInfo}
|
||||
history={fetchHistory}
|
||||
onHistorySelect={handleHistorySelect}
|
||||
onHistoryRemove={removeFromHistory}
|
||||
hasResult={!!result}
|
||||
/>
|
||||
|
||||
{result && (
|
||||
<div className="mt-4">
|
||||
<MediaList
|
||||
accountInfo={result.account_info}
|
||||
timeline={result.timeline}
|
||||
totalUrls={result.total_urls}
|
||||
fetchedMediaType={fetchedMediaType}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<div className="min-h-screen bg-background flex flex-col">
|
||||
<TitleBar />
|
||||
<Sidebar currentPage={currentPage} onPageChange={setCurrentPage} />
|
||||
|
||||
{/* Main content area with sidebar offset */}
|
||||
<div className="flex-1 ml-14 mt-10 p-4 md:p-8">
|
||||
<div className="max-w-5xl mx-auto">
|
||||
{renderPage()}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,132 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { Bug, Trash2, X, Copy, Check } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { logger, type LogEntry } from "@/lib/logger";
|
||||
|
||||
const levelColors: Record<string, string> = {
|
||||
info: "text-blue-500",
|
||||
success: "text-green-500",
|
||||
warning: "text-yellow-500",
|
||||
error: "text-red-500",
|
||||
debug: "text-gray-500",
|
||||
};
|
||||
|
||||
function formatTime(date: Date): string {
|
||||
return date.toLocaleTimeString("en-US", {
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
}
|
||||
|
||||
export function DebugLogger() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [logs, setLogs] = useState<LogEntry[]>([]);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = logger.subscribe(() => {
|
||||
setLogs(logger.getLogs());
|
||||
});
|
||||
setLogs(logger.getLogs());
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (scrollRef.current) {
|
||||
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
||||
}
|
||||
}, [logs]);
|
||||
|
||||
const handleClear = () => {
|
||||
logger.clear();
|
||||
};
|
||||
|
||||
const handleCopy = async () => {
|
||||
const logText = logs
|
||||
.map((log) => `[${formatTime(log.timestamp)}] [${log.level}] ${log.message}`)
|
||||
.join("\n");
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(logText);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 500);
|
||||
} catch (err) {
|
||||
console.error("Failed to copy logs:", err);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6 opacity-50 hover:opacity-100"
|
||||
>
|
||||
<Bug className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[600px] max-h-[80vh] p-6 [&>button]:hidden">
|
||||
<DialogTitle className="text-sm font-medium">Debug Logs</DialogTitle>
|
||||
<div className="absolute right-4 top-4 flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6 opacity-70 hover:opacity-100"
|
||||
onClick={handleCopy}
|
||||
disabled={logs.length === 0}
|
||||
>
|
||||
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6 opacity-70 hover:opacity-100"
|
||||
onClick={handleClear}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6 opacity-70 hover:opacity-100"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<div
|
||||
ref={scrollRef}
|
||||
className="h-[400px] overflow-y-auto bg-muted/50 rounded-md p-3 font-mono text-xs"
|
||||
>
|
||||
{logs.length === 0 ? (
|
||||
<p className="text-muted-foreground lowercase">no logs yet...</p>
|
||||
) : (
|
||||
logs.map((log, i) => (
|
||||
<div key={i} className="flex gap-2 py-0.5">
|
||||
<span className="text-muted-foreground shrink-0">
|
||||
[{formatTime(log.timestamp)}]
|
||||
</span>
|
||||
<span className={`shrink-0 w-16 ${levelColors[log.level]}`}>
|
||||
[{log.level}]
|
||||
</span>
|
||||
<span className="break-all">{log.message}</span>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { Trash2, Copy, Check } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { logger, type LogEntry } from "@/lib/logger";
|
||||
|
||||
const levelColors: Record<string, string> = {
|
||||
info: "text-blue-500",
|
||||
success: "text-green-500",
|
||||
warning: "text-yellow-500",
|
||||
error: "text-red-500",
|
||||
debug: "text-gray-500",
|
||||
};
|
||||
|
||||
function formatTime(date: Date): string {
|
||||
return date.toLocaleTimeString("en-US", {
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
}
|
||||
|
||||
export function DebugLoggerPage() {
|
||||
const [logs, setLogs] = useState<LogEntry[]>([]);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = logger.subscribe(() => {
|
||||
setLogs(logger.getLogs());
|
||||
});
|
||||
setLogs(logger.getLogs());
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (scrollRef.current) {
|
||||
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
||||
}
|
||||
}, [logs]);
|
||||
|
||||
const handleClear = () => {
|
||||
logger.clear();
|
||||
};
|
||||
|
||||
const handleCopy = async () => {
|
||||
const logText = logs
|
||||
.map((log) => `[${formatTime(log.timestamp)}] [${log.level}] ${log.message}`)
|
||||
.join("\n");
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(logText);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 500);
|
||||
} catch (err) {
|
||||
console.error("Failed to copy logs:", err);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold">Debug Logs</h1>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleCopy}
|
||||
disabled={logs.length === 0}
|
||||
className="gap-1.5"
|
||||
>
|
||||
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
|
||||
Copy
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleClear}
|
||||
className="gap-1.5"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
Clear
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
ref={scrollRef}
|
||||
className="h-[calc(100vh-220px)] overflow-y-auto bg-muted/50 rounded-md p-3 font-mono text-xs"
|
||||
>
|
||||
{logs.length === 0 ? (
|
||||
<p className="text-muted-foreground lowercase">no logs yet...</p>
|
||||
) : (
|
||||
logs.map((log, i) => (
|
||||
<div key={i} className="flex gap-2 py-0.5">
|
||||
<span className="text-muted-foreground shrink-0">
|
||||
[{formatTime(log.timestamp)}]
|
||||
</span>
|
||||
<span className={`shrink-0 w-16 ${levelColors[log.level]}`}>
|
||||
[{log.level}]
|
||||
</span>
|
||||
<span className="break-all">{log.message}</span>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { X, User } from "lucide-react";
|
||||
|
||||
export interface HistoryItem {
|
||||
id: string;
|
||||
username: string;
|
||||
name: string;
|
||||
image: string;
|
||||
mediaCount: number;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
interface FetchHistoryProps {
|
||||
history: HistoryItem[];
|
||||
onSelect: (item: HistoryItem) => void;
|
||||
onRemove: (id: string) => void;
|
||||
}
|
||||
|
||||
export function FetchHistory({ history, onSelect, onRemove }: FetchHistoryProps) {
|
||||
if (history.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">Recent Fetches</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{history.map((item) => (
|
||||
<div
|
||||
key={item.id}
|
||||
className="flex items-center gap-2 px-3 py-1.5 bg-muted/50 rounded-full hover:bg-muted transition-colors group cursor-pointer"
|
||||
onClick={() => onSelect(item)}
|
||||
>
|
||||
{item.image ? (
|
||||
<img
|
||||
src={item.image}
|
||||
alt={item.name}
|
||||
className="w-5 h-5 rounded-full"
|
||||
/>
|
||||
) : (
|
||||
<User className="w-5 h-5 text-muted-foreground" />
|
||||
)}
|
||||
<span className="text-sm">@{item.username}</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-4 w-4 opacity-0 group-hover:opacity-100 transition-opacity"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onRemove(item.id);
|
||||
}}
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { openExternal } from "@/lib/utils";
|
||||
import { formatRelativeTime } from "@/lib/relative-time";
|
||||
|
||||
interface HeaderProps {
|
||||
version: string;
|
||||
hasUpdate: boolean;
|
||||
releaseDate?: string | null;
|
||||
}
|
||||
|
||||
export function Header({ version, hasUpdate, releaseDate }: HeaderProps) {
|
||||
return (
|
||||
<div className="relative">
|
||||
<div className="text-center space-y-2">
|
||||
<div className="flex items-center justify-center gap-3">
|
||||
<img
|
||||
src="/icon.svg"
|
||||
alt="Twitter/X Media Batch Downloader"
|
||||
className="w-12 h-12 cursor-pointer"
|
||||
onClick={() => window.location.reload()}
|
||||
/>
|
||||
<h1
|
||||
className="text-3xl font-bold cursor-pointer"
|
||||
onClick={() => window.location.reload()}
|
||||
>
|
||||
Twitter/X Media Batch Downloader
|
||||
</h1>
|
||||
<div className="relative">
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Badge variant="default" asChild>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openExternal("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/releases")}
|
||||
className="cursor-pointer hover:opacity-80 transition-opacity"
|
||||
>
|
||||
v{version}
|
||||
</button>
|
||||
</Badge>
|
||||
</TooltipTrigger>
|
||||
{hasUpdate && releaseDate && (
|
||||
<TooltipContent>
|
||||
<p>{formatRelativeTime(releaseDate)}</p>
|
||||
</TooltipContent>
|
||||
)}
|
||||
</Tooltip>
|
||||
{hasUpdate && (
|
||||
<span className="absolute -top-1 -right-1 flex h-3 w-3">
|
||||
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
|
||||
<span className="relative inline-flex rounded-full h-3 w-3 bg-green-500"></span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,483 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { InputWithContext } from "@/components/ui/input-with-context";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Search,
|
||||
XCircle,
|
||||
Calendar,
|
||||
StopCircle,
|
||||
Globe,
|
||||
Lock,
|
||||
Bookmark,
|
||||
Heart,
|
||||
Key,
|
||||
Eye,
|
||||
EyeOff,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
Info,
|
||||
RotateCcw,
|
||||
Trash2,
|
||||
} from "lucide-react";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { Settings as SettingsIcon } from "lucide-react";
|
||||
import { FetchHistory } from "@/components/FetchHistory";
|
||||
import type { HistoryItem } from "@/components/FetchHistory";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export type FetchMode = "public" | "private";
|
||||
export type PrivateType = "bookmarks" | "likes";
|
||||
|
||||
// Local storage keys for auth tokens
|
||||
const PUBLIC_AUTH_TOKEN_KEY = "twitter_public_auth_token";
|
||||
const PRIVATE_AUTH_TOKEN_KEY = "twitter_private_auth_token";
|
||||
|
||||
interface SearchBarProps {
|
||||
username: string;
|
||||
loading: boolean;
|
||||
onUsernameChange: (username: string) => void;
|
||||
onFetch: (
|
||||
useDateRange: boolean,
|
||||
startDate?: string,
|
||||
endDate?: string,
|
||||
mediaType?: string,
|
||||
retweets?: boolean,
|
||||
mode?: FetchMode,
|
||||
privateType?: PrivateType,
|
||||
authToken?: string,
|
||||
isResume?: boolean
|
||||
) => void;
|
||||
onStopFetch: () => void;
|
||||
onResume?: (authToken: string, mediaType?: string, retweets?: boolean) => void;
|
||||
onClearResume?: () => void;
|
||||
resumeInfo?: { canResume: boolean; mediaCount: number } | null;
|
||||
history: HistoryItem[];
|
||||
onHistorySelect: (item: HistoryItem) => void;
|
||||
onHistoryRemove: (id: string) => void;
|
||||
hasResult: boolean;
|
||||
}
|
||||
|
||||
export function SearchBar({
|
||||
username,
|
||||
loading,
|
||||
onUsernameChange,
|
||||
onFetch,
|
||||
onStopFetch,
|
||||
onResume,
|
||||
onClearResume,
|
||||
resumeInfo,
|
||||
history,
|
||||
onHistorySelect,
|
||||
onHistoryRemove,
|
||||
hasResult,
|
||||
}: SearchBarProps) {
|
||||
const [useDateRange, setUseDateRange] = useState(false);
|
||||
const [startDate, setStartDate] = useState("");
|
||||
const [endDate, setEndDate] = useState("");
|
||||
const [mediaType, setMediaType] = useState("all");
|
||||
const [retweets, setRetweets] = useState(false);
|
||||
const [mode, setMode] = useState<FetchMode>("public");
|
||||
const [privateType, setPrivateType] = useState<PrivateType>("bookmarks");
|
||||
const [showAuthInput, setShowAuthInput] = useState(false);
|
||||
const [showAdvanced, setShowAdvanced] = useState(false);
|
||||
|
||||
// Separate auth tokens for public and private modes
|
||||
const [publicAuthToken, setPublicAuthToken] = useState("");
|
||||
const [privateAuthToken, setPrivateAuthToken] = useState("");
|
||||
const [showPublicToken, setShowPublicToken] = useState(false);
|
||||
const [showPrivateToken, setShowPrivateToken] = useState(false);
|
||||
|
||||
// Load saved auth tokens on mount
|
||||
useEffect(() => {
|
||||
const savedPublicToken = localStorage.getItem(PUBLIC_AUTH_TOKEN_KEY) || "";
|
||||
const savedPrivateToken = localStorage.getItem(PRIVATE_AUTH_TOKEN_KEY) || "";
|
||||
setPublicAuthToken(savedPublicToken);
|
||||
setPrivateAuthToken(savedPrivateToken);
|
||||
}, []);
|
||||
|
||||
// Save auth tokens when they change
|
||||
const handlePublicTokenChange = (value: string) => {
|
||||
setPublicAuthToken(value);
|
||||
localStorage.setItem(PUBLIC_AUTH_TOKEN_KEY, value);
|
||||
};
|
||||
|
||||
const handlePrivateTokenChange = (value: string) => {
|
||||
setPrivateAuthToken(value);
|
||||
localStorage.setItem(PRIVATE_AUTH_TOKEN_KEY, value);
|
||||
};
|
||||
|
||||
const handleFetch = () => {
|
||||
const authToken = mode === "public" ? publicAuthToken : privateAuthToken;
|
||||
onFetch(useDateRange, startDate, endDate, mediaType, retweets, mode, privateType, authToken, false);
|
||||
};
|
||||
|
||||
const handleResume = () => {
|
||||
const authToken = mode === "public" ? publicAuthToken : privateAuthToken;
|
||||
if (onResume) {
|
||||
onResume(authToken, mediaType, retweets);
|
||||
}
|
||||
};
|
||||
|
||||
const currentAuthToken = mode === "public" ? publicAuthToken : privateAuthToken;
|
||||
const hasAuthToken = currentAuthToken.trim().length > 0;
|
||||
// Likes needs username (URL is /username/likes), bookmarks doesn't
|
||||
const isLikesMode = mode === "private" && privateType === "likes";
|
||||
const isBookmarksMode = mode === "private" && privateType === "bookmarks";
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* Mode Toggle */}
|
||||
<div className="flex justify-center">
|
||||
<div className="flex gap-1 p-1 bg-muted rounded-lg w-fit">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setMode("public")}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-all cursor-pointer",
|
||||
mode === "public"
|
||||
? "bg-background text-foreground shadow-sm"
|
||||
: "text-muted-foreground hover:text-foreground"
|
||||
)}
|
||||
>
|
||||
<Globe className="h-4 w-4" />
|
||||
Public
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setMode("private")}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-all cursor-pointer",
|
||||
mode === "private"
|
||||
? "bg-background text-foreground shadow-sm"
|
||||
: "text-muted-foreground hover:text-foreground"
|
||||
)}
|
||||
>
|
||||
<Lock className="h-4 w-4" />
|
||||
Private
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Username Input - for public mode and likes mode */}
|
||||
{(mode === "public" || isLikesMode) && (
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="username">
|
||||
{isLikesMode ? "Your Username" : "Username"}
|
||||
</Label>
|
||||
|
||||
<div className="flex gap-2">
|
||||
<div className="relative flex-1">
|
||||
<InputWithContext
|
||||
id="username"
|
||||
placeholder={
|
||||
isLikesMode
|
||||
? "your_username or @your_username or https://x.com/your_username"
|
||||
: "masteraoko or @masteraoko or https://x.com/masteraoko"
|
||||
}
|
||||
value={username}
|
||||
onChange={(e) => onUsernameChange(e.target.value)}
|
||||
onKeyDown={(e) => e.key === "Enter" && handleFetch()}
|
||||
className="pr-8"
|
||||
/>
|
||||
{username && (
|
||||
<button
|
||||
type="button"
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
|
||||
onClick={() => onUsernameChange("")}
|
||||
>
|
||||
<XCircle className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{loading && (
|
||||
<Button variant="destructive" onClick={onStopFetch}>
|
||||
<StopCircle className="h-4 w-4" />
|
||||
Stop
|
||||
</Button>
|
||||
)}
|
||||
{/* Resume button - show when there's resumable data */}
|
||||
{!loading && resumeInfo?.canResume && mode === "public" && (
|
||||
<>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button variant="outline" size="icon" onClick={onClearResume}>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Clear resume data</TooltipContent>
|
||||
</Tooltip>
|
||||
<Button variant="secondary" onClick={handleResume} disabled={!hasAuthToken}>
|
||||
<RotateCcw className="h-4 w-4" />
|
||||
Resume ({resumeInfo.mediaCount})
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
<Button onClick={handleFetch} disabled={loading || !hasAuthToken}>
|
||||
{loading ? (
|
||||
<>
|
||||
<Spinner />
|
||||
Fetching...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
{isLikesMode ? <Heart className="h-4 w-4" /> : <Search className="h-4 w-4" />}
|
||||
{isLikesMode ? "Fetch Likes" : "Fetch"}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Auth Token & Advanced Settings Row */}
|
||||
<div className="flex items-center gap-2">
|
||||
{/* Auth Token Button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAuthInput(!showAuthInput)}
|
||||
className="flex items-center gap-2 px-4 py-2 border rounded-lg text-sm font-medium hover:bg-muted/50 transition-colors cursor-pointer"
|
||||
>
|
||||
<Key className={cn("h-4 w-4", hasAuthToken ? "text-green-500" : "text-destructive")} />
|
||||
Auth Token
|
||||
{showAuthInput ? <ChevronUp className="h-4 w-4" /> : <ChevronDown className="h-4 w-4" />}
|
||||
</button>
|
||||
|
||||
{/* Advanced Settings Button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAdvanced(!showAdvanced)}
|
||||
className="flex items-center gap-2 px-4 py-2 border rounded-lg text-sm font-medium hover:bg-muted/50 transition-colors cursor-pointer"
|
||||
>
|
||||
<SettingsIcon className="h-4 w-4" />
|
||||
Advanced Settings
|
||||
{showAdvanced ? <ChevronUp className="h-4 w-4" /> : <ChevronDown className="h-4 w-4" />}
|
||||
</button>
|
||||
|
||||
{/* Private Mode Options - My Bookmarks / My Likes */}
|
||||
{mode === "private" && (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setPrivateType("bookmarks")}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-lg border text-sm font-medium transition-all cursor-pointer",
|
||||
privateType === "bookmarks"
|
||||
? "border-primary bg-primary/10 text-primary"
|
||||
: "border-border hover:border-primary/50"
|
||||
)}
|
||||
>
|
||||
<Bookmark className="h-4 w-4" />
|
||||
My Bookmarks
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setPrivateType("likes")}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-lg border text-sm font-medium transition-all cursor-pointer",
|
||||
privateType === "likes"
|
||||
? "border-primary bg-primary/10 text-primary"
|
||||
: "border-border hover:border-primary/50"
|
||||
)}
|
||||
>
|
||||
<Heart className="h-4 w-4" />
|
||||
My Likes
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Bookmarks Mode - Fetch button inline */}
|
||||
{isBookmarksMode && (
|
||||
<div className="flex items-center gap-2 ml-auto">
|
||||
{loading && (
|
||||
<Button variant="destructive" onClick={onStopFetch}>
|
||||
<StopCircle className="h-4 w-4" />
|
||||
Stop
|
||||
</Button>
|
||||
)}
|
||||
<Button onClick={handleFetch} disabled={loading || !hasAuthToken}>
|
||||
{loading ? (
|
||||
<>
|
||||
<Spinner />
|
||||
Fetching...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Bookmark className="h-4 w-4" />
|
||||
Fetch Bookmarks
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Auth Token Input (collapsible) */}
|
||||
{showAuthInput && (
|
||||
<div className="p-3 border rounded-lg bg-muted/30">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Label htmlFor="auth-token" className="text-sm whitespace-nowrap">
|
||||
Auth Token
|
||||
</Label>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Info className="h-4 w-4 text-muted-foreground cursor-help" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent className="text-center">
|
||||
{mode === "private" ? (
|
||||
<p>Use auth token from the account whose bookmarks/likes you want to fetch</p>
|
||||
) : (
|
||||
<p>Recommended to use a dummy account, not your main account.<br />Excessive usage may cause suspension</p>
|
||||
)}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<div className="relative flex-1">
|
||||
<InputWithContext
|
||||
id="auth-token"
|
||||
type={
|
||||
mode === "public"
|
||||
? showPublicToken
|
||||
? "text"
|
||||
: "password"
|
||||
: showPrivateToken
|
||||
? "text"
|
||||
: "password"
|
||||
}
|
||||
placeholder="Enter your auth_token cookie value"
|
||||
value={currentAuthToken}
|
||||
onChange={(e) =>
|
||||
mode === "public"
|
||||
? handlePublicTokenChange(e.target.value)
|
||||
: handlePrivateTokenChange(e.target.value)
|
||||
}
|
||||
className="pr-10 bg-background"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors"
|
||||
onClick={() =>
|
||||
mode === "public"
|
||||
? setShowPublicToken(!showPublicToken)
|
||||
: setShowPrivateToken(!showPrivateToken)
|
||||
}
|
||||
>
|
||||
{(mode === "public" ? showPublicToken : showPrivateToken) ? (
|
||||
<EyeOff className="h-4 w-4" />
|
||||
) : (
|
||||
<Eye className="h-4 w-4" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Advanced Settings (collapsible) */}
|
||||
{showAdvanced && (
|
||||
<div className="p-3 border rounded-lg bg-muted/30 space-y-3">
|
||||
{/* Options Row */}
|
||||
<div className="flex items-center gap-4">
|
||||
{/* Media Type */}
|
||||
<div className="flex items-center gap-2">
|
||||
<Label htmlFor="media-type" className="text-sm">
|
||||
Media Type
|
||||
</Label>
|
||||
<Select value={mediaType} onValueChange={setMediaType}>
|
||||
<SelectTrigger id="media-type" className="w-auto h-8 bg-background">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">All Media</SelectItem>
|
||||
<SelectItem value="image">Images Only</SelectItem>
|
||||
<SelectItem value="video">Videos Only</SelectItem>
|
||||
<SelectItem value="gif">GIFs Only</SelectItem>
|
||||
<SelectItem value="text">Text Only (No Media)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Include Retweets */}
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="retweets"
|
||||
checked={retweets}
|
||||
onCheckedChange={(checked) => setRetweets(checked as boolean)}
|
||||
className="bg-background"
|
||||
/>
|
||||
<Label htmlFor="retweets" className="text-sm cursor-pointer">
|
||||
Include Retweets
|
||||
</Label>
|
||||
</div>
|
||||
|
||||
{/* Date Range Toggle - only for public mode */}
|
||||
{mode === "public" && (
|
||||
<>
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="date-range"
|
||||
checked={useDateRange}
|
||||
onCheckedChange={(checked) => setUseDateRange(checked as boolean)}
|
||||
className="bg-background"
|
||||
/>
|
||||
<Label
|
||||
htmlFor="date-range"
|
||||
className="text-sm cursor-pointer flex items-center gap-1"
|
||||
>
|
||||
<Calendar className="h-4 w-4" />
|
||||
Date Range
|
||||
</Label>
|
||||
</div>
|
||||
|
||||
{/* Date Range Inputs - inline */}
|
||||
{useDateRange && (
|
||||
<>
|
||||
<InputWithContext
|
||||
id="start-date"
|
||||
type="date"
|
||||
value={startDate}
|
||||
onChange={(e) => setStartDate(e.target.value)}
|
||||
className="w-[140px] h-8 bg-background"
|
||||
/>
|
||||
<span className="text-sm text-muted-foreground">-</span>
|
||||
<InputWithContext
|
||||
id="end-date"
|
||||
type="date"
|
||||
value={endDate}
|
||||
onChange={(e) => setEndDate(e.target.value)}
|
||||
className="w-[140px] h-8 bg-background"
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!hasResult && mode === "public" && (
|
||||
<FetchHistory
|
||||
history={history}
|
||||
onSelect={onHistorySelect}
|
||||
onRemove={onHistoryRemove}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { InputWithContext } from "@/components/ui/input-with-context";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Settings as SettingsIcon, FolderOpen, Save, RotateCcw, X, Info, Download, Check, Volume2 } from "lucide-react";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { getSettings, getSettingsWithDefaults, saveSettings, resetToDefaultSettings, applyThemeMode, type Settings as SettingsType } from "@/lib/settings";
|
||||
import { themes, applyTheme } from "@/lib/themes";
|
||||
import { toastWithSound as toast } from "@/lib/toast-with-sound";
|
||||
import { SelectFolder, IsFFmpegInstalled, DownloadFFmpeg } from "../../wailsjs/go/main/App";
|
||||
|
||||
export function Settings() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [savedSettings, setSavedSettings] = useState<SettingsType>(getSettings());
|
||||
const [tempSettings, setTempSettings] = useState<SettingsType>(savedSettings);
|
||||
const [, setIsLoadingDefaults] = useState(false);
|
||||
const [isDark, setIsDark] = useState(document.documentElement.classList.contains('dark'));
|
||||
const [ffmpegInstalled, setFfmpegInstalled] = useState(false);
|
||||
const [downloadingFFmpeg, setDownloadingFFmpeg] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
applyThemeMode(savedSettings.themeMode);
|
||||
applyTheme(savedSettings.theme);
|
||||
|
||||
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const handleChange = () => {
|
||||
if (savedSettings.themeMode === "auto") {
|
||||
applyThemeMode("auto");
|
||||
applyTheme(savedSettings.theme);
|
||||
}
|
||||
};
|
||||
|
||||
mediaQuery.addEventListener("change", handleChange);
|
||||
return () => mediaQuery.removeEventListener("change", handleChange);
|
||||
}, [savedSettings.themeMode, savedSettings.theme]);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
applyThemeMode(tempSettings.themeMode);
|
||||
applyTheme(tempSettings.theme);
|
||||
|
||||
setTimeout(() => {
|
||||
setIsDark(document.documentElement.classList.contains('dark'));
|
||||
}, 0);
|
||||
|
||||
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const handleChange = () => {
|
||||
if (tempSettings.themeMode === "auto") {
|
||||
applyThemeMode("auto");
|
||||
applyTheme(tempSettings.theme);
|
||||
setTimeout(() => {
|
||||
setIsDark(document.documentElement.classList.contains('dark'));
|
||||
}, 0);
|
||||
}
|
||||
};
|
||||
|
||||
mediaQuery.addEventListener("change", handleChange);
|
||||
return () => mediaQuery.removeEventListener("change", handleChange);
|
||||
}
|
||||
}, [open, tempSettings.themeMode, tempSettings.theme]);
|
||||
|
||||
useEffect(() => {
|
||||
const loadDefaults = async () => {
|
||||
if (!savedSettings.downloadPath) {
|
||||
setIsLoadingDefaults(true);
|
||||
const settingsWithDefaults = await getSettingsWithDefaults();
|
||||
setSavedSettings(settingsWithDefaults);
|
||||
setTempSettings(settingsWithDefaults);
|
||||
setIsLoadingDefaults(false);
|
||||
}
|
||||
};
|
||||
loadDefaults();
|
||||
|
||||
// Check FFmpeg status
|
||||
IsFFmpegInstalled().then(setFfmpegInstalled);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setTempSettings(savedSettings);
|
||||
}
|
||||
}, [open, savedSettings]);
|
||||
|
||||
const handleSave = () => {
|
||||
saveSettings(tempSettings);
|
||||
setSavedSettings(tempSettings);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleReset = async () => {
|
||||
const defaultSettings = await resetToDefaultSettings();
|
||||
setTempSettings(defaultSettings);
|
||||
setSavedSettings(defaultSettings);
|
||||
applyThemeMode(defaultSettings.themeMode);
|
||||
applyTheme(defaultSettings.theme);
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
applyThemeMode(savedSettings.themeMode);
|
||||
applyTheme(savedSettings.theme);
|
||||
setTempSettings(savedSettings);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleOpenChange = (newOpen: boolean) => {
|
||||
if (!newOpen) {
|
||||
applyThemeMode(savedSettings.themeMode);
|
||||
applyTheme(savedSettings.theme);
|
||||
setTempSettings(savedSettings);
|
||||
}
|
||||
setOpen(newOpen);
|
||||
};
|
||||
|
||||
const handleBrowseFolder = async () => {
|
||||
try {
|
||||
const selectedPath = await SelectFolder(tempSettings.downloadPath || "");
|
||||
if (selectedPath && selectedPath.trim() !== "") {
|
||||
setTempSettings((prev) => ({ ...prev, downloadPath: selectedPath }));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error selecting folder:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDownloadFFmpeg = async () => {
|
||||
setDownloadingFFmpeg(true);
|
||||
try {
|
||||
await DownloadFFmpeg();
|
||||
setFfmpegInstalled(true);
|
||||
toast.success("FFmpeg downloaded successfully");
|
||||
} catch (error) {
|
||||
toast.error("Failed to download FFmpeg");
|
||||
console.error("Error downloading FFmpeg:", error);
|
||||
} finally {
|
||||
setDownloadingFFmpeg(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="outline" size="icon">
|
||||
<SettingsIcon className="h-5 w-5" />
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[480px] flex flex-col p-6 [&>button]:hidden" aria-describedby={undefined}>
|
||||
<div className="absolute right-4 top-4">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6 opacity-70 hover:opacity-100"
|
||||
onClick={handleCancel}
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<DialogTitle className="text-sm font-medium">Settings</DialogTitle>
|
||||
|
||||
{/* 2 Column Grid Layout */}
|
||||
<div className="grid grid-cols-2 gap-x-6 gap-y-4 py-2">
|
||||
{/* LEFT COLUMN */}
|
||||
{/* Download Path */}
|
||||
<div className="space-y-2 col-span-2">
|
||||
<Label htmlFor="download-path">Download Path</Label>
|
||||
<div className="flex gap-2">
|
||||
<InputWithContext
|
||||
id="download-path"
|
||||
value={tempSettings.downloadPath}
|
||||
onChange={(e) => setTempSettings((prev) => ({ ...prev, downloadPath: e.target.value }))}
|
||||
placeholder="C:\Users\YourUsername\Pictures"
|
||||
/>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button type="button" onClick={handleBrowseFolder} size="icon">
|
||||
<FolderOpen className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Browse Folder</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* LEFT: Mode */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="theme-mode">Mode</Label>
|
||||
<Select
|
||||
value={tempSettings.themeMode}
|
||||
onValueChange={(value) => setTempSettings((prev) => ({ ...prev, themeMode: value as any }))}
|
||||
>
|
||||
<SelectTrigger id="theme-mode">
|
||||
<SelectValue placeholder="Select theme mode" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="auto">Auto</SelectItem>
|
||||
<SelectItem value="light">Light</SelectItem>
|
||||
<SelectItem value="dark">Dark</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* RIGHT: GIF Conversion */}
|
||||
<div className="space-y-2">
|
||||
<Label className="flex items-center gap-2">
|
||||
GIF Conversion
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Info className="h-3.5 w-3.5 text-muted-foreground cursor-help" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="top">
|
||||
<p>FFmpeg is required to convert Twitter's MP4 to actual GIF format</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</Label>
|
||||
<div className="h-9 flex items-center">
|
||||
{ffmpegInstalled ? (
|
||||
<div className="flex items-center gap-2 text-sm text-green-600 dark:text-green-400">
|
||||
<Check className="h-4 w-4" />
|
||||
Installed
|
||||
</div>
|
||||
) : (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-9"
|
||||
onClick={handleDownloadFFmpeg}
|
||||
disabled={downloadingFFmpeg}
|
||||
>
|
||||
{downloadingFFmpeg ? (
|
||||
<>
|
||||
<Spinner />
|
||||
Downloading...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Download className="h-4 w-4" />
|
||||
Download FFmpeg
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* LEFT: Accent */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="theme">Accent</Label>
|
||||
<Select
|
||||
value={tempSettings.theme}
|
||||
onValueChange={(value) => setTempSettings((prev) => ({ ...prev, theme: value }))}
|
||||
>
|
||||
<SelectTrigger id="theme">
|
||||
<SelectValue placeholder="Select a theme" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{themes.map((theme) => (
|
||||
<SelectItem key={theme.name} value={theme.name}>
|
||||
<span className="flex items-center gap-2">
|
||||
<span
|
||||
className="w-3 h-3 rounded-full border border-border"
|
||||
style={{
|
||||
backgroundColor: isDark
|
||||
? theme.cssVars.dark.primary
|
||||
: theme.cssVars.light.primary
|
||||
}}
|
||||
/>
|
||||
{theme.label}
|
||||
</span>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* RIGHT: Sound Effects */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="sfx-enabled" className="flex items-center gap-2">
|
||||
<Volume2 className="h-4 w-4 text-muted-foreground" />
|
||||
Sound Effects
|
||||
</Label>
|
||||
<div className="h-9 flex items-center">
|
||||
<Switch
|
||||
id="sfx-enabled"
|
||||
checked={tempSettings.sfxEnabled}
|
||||
onCheckedChange={(checked) => setTempSettings(prev => ({ ...prev, sfxEnabled: checked }))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter className="gap-2 sm:justify-between">
|
||||
<Button variant="outline" onClick={handleReset} className="gap-1.5">
|
||||
<RotateCcw className="h-4 w-4" />
|
||||
Reset to Default
|
||||
</Button>
|
||||
<div className="flex gap-2">
|
||||
<Button variant="outline" onClick={handleCancel}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} className="gap-1.5">
|
||||
<Save className="h-4 w-4" />
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { InputWithContext } from "@/components/ui/input-with-context";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { FolderOpen, Save, RotateCcw, Info, Download, Check, Volume2 } from "lucide-react";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { getSettings, getSettingsWithDefaults, saveSettings, resetToDefaultSettings, applyThemeMode, applyFont, FONT_OPTIONS, type Settings as SettingsType, type FontFamily, type GifQuality, type GifResolution } from "@/lib/settings";
|
||||
import { themes, applyTheme } from "@/lib/themes";
|
||||
import { SelectFolder, IsFFmpegInstalled, DownloadFFmpeg } from "../../wailsjs/go/main/App";
|
||||
import { toastWithSound as toast } from "@/lib/toast-with-sound";
|
||||
|
||||
export function SettingsPage() {
|
||||
const [savedSettings, setSavedSettings] = useState<SettingsType>(getSettings());
|
||||
const [tempSettings, setTempSettings] = useState<SettingsType>(savedSettings);
|
||||
const [isDark, setIsDark] = useState(document.documentElement.classList.contains('dark'));
|
||||
const [ffmpegInstalled, setFfmpegInstalled] = useState(false);
|
||||
const [downloadingFFmpeg, setDownloadingFFmpeg] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
applyThemeMode(savedSettings.themeMode);
|
||||
applyTheme(savedSettings.theme);
|
||||
|
||||
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const handleChange = () => {
|
||||
if (savedSettings.themeMode === "auto") {
|
||||
applyThemeMode("auto");
|
||||
applyTheme(savedSettings.theme);
|
||||
}
|
||||
};
|
||||
|
||||
mediaQuery.addEventListener("change", handleChange);
|
||||
return () => mediaQuery.removeEventListener("change", handleChange);
|
||||
}, [savedSettings.themeMode, savedSettings.theme]);
|
||||
|
||||
useEffect(() => {
|
||||
applyThemeMode(tempSettings.themeMode);
|
||||
applyTheme(tempSettings.theme);
|
||||
applyFont(tempSettings.fontFamily);
|
||||
setTimeout(() => {
|
||||
setIsDark(document.documentElement.classList.contains('dark'));
|
||||
}, 0);
|
||||
}, [tempSettings.themeMode, tempSettings.theme, tempSettings.fontFamily]);
|
||||
|
||||
useEffect(() => {
|
||||
const loadDefaults = async () => {
|
||||
if (!savedSettings.downloadPath) {
|
||||
const settingsWithDefaults = await getSettingsWithDefaults();
|
||||
setSavedSettings(settingsWithDefaults);
|
||||
setTempSettings(settingsWithDefaults);
|
||||
}
|
||||
};
|
||||
loadDefaults();
|
||||
|
||||
// Check FFmpeg status
|
||||
IsFFmpegInstalled().then(setFfmpegInstalled);
|
||||
}, []);
|
||||
|
||||
const handleSave = () => {
|
||||
saveSettings(tempSettings);
|
||||
setSavedSettings(tempSettings);
|
||||
toast.success("Settings saved");
|
||||
};
|
||||
|
||||
const handleReset = async () => {
|
||||
const defaultSettings = await resetToDefaultSettings();
|
||||
setTempSettings(defaultSettings);
|
||||
setSavedSettings(defaultSettings);
|
||||
applyThemeMode(defaultSettings.themeMode);
|
||||
applyTheme(defaultSettings.theme);
|
||||
applyFont(defaultSettings.fontFamily);
|
||||
toast.success("Settings reset to default");
|
||||
};
|
||||
|
||||
const handleBrowseFolder = async () => {
|
||||
try {
|
||||
const selectedPath = await SelectFolder(tempSettings.downloadPath || "");
|
||||
if (selectedPath && selectedPath.trim() !== "") {
|
||||
setTempSettings((prev) => ({ ...prev, downloadPath: selectedPath }));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error selecting folder:", error);
|
||||
toast.error(`Error selecting folder: ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDownloadFFmpeg = async () => {
|
||||
setDownloadingFFmpeg(true);
|
||||
try {
|
||||
await DownloadFFmpeg();
|
||||
setFfmpegInstalled(true);
|
||||
toast.success("FFmpeg downloaded successfully");
|
||||
} catch (error) {
|
||||
toast.error("Failed to download FFmpeg");
|
||||
console.error("Error downloading FFmpeg:", error);
|
||||
} finally {
|
||||
setDownloadingFFmpeg(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<h1 className="text-2xl font-bold">Settings</h1>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
{/* Left Column */}
|
||||
<div className="space-y-4">
|
||||
{/* Download Path */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="download-path">Download Path</Label>
|
||||
<div className="flex gap-2">
|
||||
<InputWithContext
|
||||
id="download-path"
|
||||
value={tempSettings.downloadPath}
|
||||
onChange={(e) => setTempSettings((prev) => ({ ...prev, downloadPath: e.target.value }))}
|
||||
placeholder="C:\Users\YourUsername\Pictures"
|
||||
/>
|
||||
<Button type="button" onClick={handleBrowseFolder} className="gap-1.5">
|
||||
<FolderOpen className="h-4 w-4" />
|
||||
Browse
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Theme Mode */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="theme-mode">Mode</Label>
|
||||
<Select
|
||||
value={tempSettings.themeMode}
|
||||
onValueChange={(value: "auto" | "light" | "dark") => setTempSettings((prev) => ({ ...prev, themeMode: value }))}
|
||||
>
|
||||
<SelectTrigger id="theme-mode">
|
||||
<SelectValue placeholder="Select theme mode" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="auto">Auto</SelectItem>
|
||||
<SelectItem value="light">Light</SelectItem>
|
||||
<SelectItem value="dark">Dark</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Accent */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="theme">Accent</Label>
|
||||
<Select
|
||||
value={tempSettings.theme}
|
||||
onValueChange={(value) => setTempSettings((prev) => ({ ...prev, theme: value }))}
|
||||
>
|
||||
<SelectTrigger id="theme">
|
||||
<SelectValue placeholder="Select a theme" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{themes.map((theme) => (
|
||||
<SelectItem key={theme.name} value={theme.name}>
|
||||
<span className="flex items-center gap-2">
|
||||
<span
|
||||
className="w-3 h-3 rounded-full border border-border"
|
||||
style={{
|
||||
backgroundColor: isDark ? theme.cssVars.dark.primary : theme.cssVars.light.primary
|
||||
}}
|
||||
/>
|
||||
{theme.label}
|
||||
</span>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Font */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="font">Font</Label>
|
||||
<Select
|
||||
value={tempSettings.fontFamily}
|
||||
onValueChange={(value: FontFamily) => setTempSettings((prev) => ({ ...prev, fontFamily: value }))}
|
||||
>
|
||||
<SelectTrigger id="font">
|
||||
<SelectValue placeholder="Select a font" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{FONT_OPTIONS.map((font) => (
|
||||
<SelectItem key={font.value} value={font.value}>
|
||||
<span style={{ fontFamily: font.fontFamily }}>{font.label}</span>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Sound Effects */}
|
||||
<div className="flex items-center gap-3 pt-2">
|
||||
<Volume2 className="h-4 w-4 text-muted-foreground" />
|
||||
<Label htmlFor="sfx-enabled" className="cursor-pointer text-sm">Sound Effects</Label>
|
||||
<Switch
|
||||
id="sfx-enabled"
|
||||
checked={tempSettings.sfxEnabled}
|
||||
onCheckedChange={(checked) => setTempSettings(prev => ({ ...prev, sfxEnabled: checked }))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right Column */}
|
||||
<div className="space-y-4">
|
||||
{/* GIF Conversion */}
|
||||
<div className="space-y-2">
|
||||
<Label className="flex items-center gap-2">
|
||||
GIF Conversion
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Info className="h-3.5 w-3.5 text-muted-foreground cursor-help" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="top">
|
||||
<p>FFmpeg is required to convert Twitter's MP4 to actual GIF format</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</Label>
|
||||
<div className="h-9 flex items-center">
|
||||
{ffmpegInstalled ? (
|
||||
<div className="flex items-center gap-2 text-sm text-green-600 dark:text-green-400">
|
||||
<Check className="h-4 w-4" />
|
||||
Installed
|
||||
</div>
|
||||
) : (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-9"
|
||||
onClick={handleDownloadFFmpeg}
|
||||
disabled={downloadingFFmpeg}
|
||||
>
|
||||
{downloadingFFmpeg ? (
|
||||
<>
|
||||
<Spinner />
|
||||
Downloading...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Download className="h-4 w-4" />
|
||||
Download FFmpeg
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* GIF Quality - only show if FFmpeg installed */}
|
||||
{ffmpegInstalled && (
|
||||
<>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="gif-quality">GIF Quality</Label>
|
||||
<Select
|
||||
value={tempSettings.gifQuality}
|
||||
onValueChange={(value: GifQuality) => {
|
||||
setTempSettings((prev) => ({
|
||||
...prev,
|
||||
gifQuality: value,
|
||||
// Auto-select "original" when switching to "better"
|
||||
gifResolution: value === "better" ? "original" : prev.gifResolution,
|
||||
}));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger id="gif-quality">
|
||||
<SelectValue placeholder="Select quality" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="fast">Fast (Simple conversion)</SelectItem>
|
||||
<SelectItem value="better">Better (Optimized palette)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* GIF Resolution - only show if Better quality selected */}
|
||||
{tempSettings.gifQuality === "better" && (
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="gif-resolution">GIF Resolution</Label>
|
||||
<Select
|
||||
value={tempSettings.gifResolution}
|
||||
onValueChange={(value: GifResolution) => setTempSettings((prev) => ({ ...prev, gifResolution: value }))}
|
||||
>
|
||||
<SelectTrigger id="gif-resolution">
|
||||
<SelectValue placeholder="Select resolution" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="original">Original (No scaling, 15 fps)</SelectItem>
|
||||
<SelectItem value="high">High (800px, 15 fps)</SelectItem>
|
||||
<SelectItem value="medium">Medium (600px, 10 fps)</SelectItem>
|
||||
<SelectItem value="low">Low (400px, 8 fps)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex gap-2 justify-between pt-4 border-t">
|
||||
<Button variant="outline" onClick={handleReset} className="gap-1.5">
|
||||
<RotateCcw className="h-4 w-4" />
|
||||
Reset to Default
|
||||
</Button>
|
||||
<Button onClick={handleSave} className="gap-1.5">
|
||||
<Save className="h-4 w-4" />
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { Home, Settings, Bug, Database, LayoutGrid } from "lucide-react";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { openExternal } from "@/lib/utils";
|
||||
|
||||
export type PageType = "main" | "settings" | "debug" | "database";
|
||||
|
||||
interface SidebarProps {
|
||||
currentPage: PageType;
|
||||
onPageChange: (page: PageType) => void;
|
||||
}
|
||||
|
||||
export function Sidebar({ currentPage, onPageChange }: SidebarProps) {
|
||||
const navItems = [
|
||||
{ id: "main" as PageType, icon: Home, label: "Home" },
|
||||
{ id: "settings" as PageType, icon: Settings, label: "Settings" },
|
||||
{ id: "database" as PageType, icon: Database, label: "Saved Accounts" },
|
||||
{ id: "debug" as PageType, icon: Bug, label: "Debug Logs" },
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="fixed left-0 top-0 h-full w-14 bg-card border-r border-border flex flex-col items-center py-14 z-30">
|
||||
<div className="flex flex-col gap-2 flex-1">
|
||||
{navItems.map((item) => (
|
||||
<Tooltip key={item.id} delayDuration={0}>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant={currentPage === item.id ? "secondary" : "ghost"}
|
||||
size="icon"
|
||||
className="h-10 w-10"
|
||||
onClick={() => onPageChange(item.id)}
|
||||
>
|
||||
<item.icon className="h-5 w-5" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="right">
|
||||
<p>{item.label}</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* GitHub - below debug */}
|
||||
<Tooltip delayDuration={0}>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-10 w-10"
|
||||
onClick={() => openExternal("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/issues")}
|
||||
>
|
||||
<svg viewBox="0 0 24 24" className="h-5 w-5" fill="currentColor">
|
||||
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" />
|
||||
</svg>
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="right">
|
||||
<p>Report Bug</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
|
||||
{/* Other Projects at bottom */}
|
||||
<div className="mt-auto">
|
||||
<Tooltip delayDuration={0}>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-10 w-10"
|
||||
onClick={() => openExternal("https://exyezed.cc/")}
|
||||
>
|
||||
<LayoutGrid className="h-5 w-5" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="right">
|
||||
<p>Other Projects</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { X, Minus, Maximize } from "lucide-react";
|
||||
import { WindowMinimise, WindowToggleMaximise, Quit } from "../../wailsjs/runtime/runtime";
|
||||
|
||||
export function TitleBar() {
|
||||
const handleMinimize = () => {
|
||||
WindowMinimise();
|
||||
};
|
||||
|
||||
const handleMaximize = () => {
|
||||
WindowToggleMaximise();
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
Quit();
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Draggable area */}
|
||||
<div
|
||||
className="fixed top-0 left-14 right-0 h-10 z-40 bg-background/80 backdrop-blur-sm"
|
||||
style={{ "--wails-draggable": "drag" } as React.CSSProperties}
|
||||
onDoubleClick={handleMaximize}
|
||||
/>
|
||||
|
||||
{/* Window control buttons - Windows style, right side */}
|
||||
<div className="fixed top-1.5 right-2 z-50 flex h-7 gap-0.5">
|
||||
<button
|
||||
onClick={handleMinimize}
|
||||
className="w-8 h-7 flex items-center justify-center hover:bg-muted transition-colors rounded"
|
||||
style={{ "--wails-draggable": "no-drag" } as React.CSSProperties}
|
||||
aria-label="Minimize"
|
||||
>
|
||||
<Minus className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
<button
|
||||
onClick={handleMaximize}
|
||||
className="w-8 h-7 flex items-center justify-center hover:bg-muted transition-colors rounded"
|
||||
style={{ "--wails-draggable": "no-drag" } as React.CSSProperties}
|
||||
aria-label="Maximize"
|
||||
>
|
||||
<Maximize className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="w-8 h-7 flex items-center justify-center hover:bg-destructive hover:text-white transition-colors rounded"
|
||||
style={{ "--wails-draggable": "no-drag" } as React.CSSProperties}
|
||||
aria-label="Close"
|
||||
>
|
||||
<X className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as AccordionPrimitive from "@radix-ui/react-accordion"
|
||||
import { ChevronDownIcon } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Accordion({
|
||||
...props
|
||||
}: React.ComponentProps<typeof AccordionPrimitive.Root>) {
|
||||
return <AccordionPrimitive.Root data-slot="accordion" {...props} />
|
||||
}
|
||||
|
||||
function AccordionItem({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof AccordionPrimitive.Item>) {
|
||||
return (
|
||||
<AccordionPrimitive.Item
|
||||
data-slot="accordion-item"
|
||||
className={cn("border-b last:border-b-0", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function AccordionTrigger({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof AccordionPrimitive.Trigger>) {
|
||||
return (
|
||||
<AccordionPrimitive.Header className="flex">
|
||||
<AccordionPrimitive.Trigger
|
||||
data-slot="accordion-trigger"
|
||||
className={cn(
|
||||
"focus-visible:border-ring focus-visible:ring-ring/50 flex flex-1 items-start justify-between gap-4 rounded-md py-4 text-left text-sm font-medium transition-all outline-none hover:underline focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50 [&[data-state=open]>svg]:rotate-180",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<ChevronDownIcon className="text-muted-foreground pointer-events-none size-4 shrink-0 translate-y-0.5 transition-transform duration-200" />
|
||||
</AccordionPrimitive.Trigger>
|
||||
</AccordionPrimitive.Header>
|
||||
)
|
||||
}
|
||||
|
||||
function AccordionContent({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof AccordionPrimitive.Content>) {
|
||||
return (
|
||||
<AccordionPrimitive.Content
|
||||
data-slot="accordion-content"
|
||||
className="data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down overflow-hidden text-sm"
|
||||
{...props}
|
||||
>
|
||||
<div className={cn("pt-0 pb-4", className)}>{children}</div>
|
||||
</AccordionPrimitive.Content>
|
||||
)
|
||||
}
|
||||
|
||||
export { Accordion, AccordionItem, AccordionTrigger, AccordionContent }
|
||||
@@ -0,0 +1,46 @@
|
||||
import * as React from "react"
|
||||
import { Slot } from "@radix-ui/react-slot"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const badgeVariants = cva(
|
||||
"inline-flex items-center justify-center rounded-full border px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default:
|
||||
"border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90",
|
||||
secondary:
|
||||
"border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",
|
||||
destructive:
|
||||
"border-transparent bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
|
||||
outline:
|
||||
"text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Badge({
|
||||
className,
|
||||
variant,
|
||||
asChild = false,
|
||||
...props
|
||||
}: React.ComponentProps<"span"> &
|
||||
VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
|
||||
const Comp = asChild ? Slot : "span"
|
||||
|
||||
return (
|
||||
<Comp
|
||||
data-slot="badge"
|
||||
className={cn(badgeVariants({ variant }), className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Badge, badgeVariants }
|
||||
@@ -0,0 +1,60 @@
|
||||
import * as React from "react"
|
||||
import { Slot } from "@radix-ui/react-slot"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const buttonVariants = cva(
|
||||
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all cursor-pointer disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 shrink-0 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground hover:bg-primary/90",
|
||||
destructive:
|
||||
"bg-destructive text-white hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
|
||||
outline:
|
||||
"border bg-background shadow-xs hover:bg-accent hover:text-accent-foreground dark:bg-input/30 dark:border-input dark:hover:bg-input/50",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
||||
ghost:
|
||||
"hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50",
|
||||
link: "text-primary underline-offset-4 hover:underline",
|
||||
},
|
||||
size: {
|
||||
default: "h-9 px-4 py-2 has-[>svg]:px-3",
|
||||
sm: "h-8 rounded-md gap-1.5 px-3 has-[>svg]:px-2.5",
|
||||
lg: "h-10 rounded-md px-6 has-[>svg]:px-4",
|
||||
icon: "size-9",
|
||||
"icon-sm": "size-8",
|
||||
"icon-lg": "size-10",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Button({
|
||||
className,
|
||||
variant,
|
||||
size,
|
||||
asChild = false,
|
||||
...props
|
||||
}: React.ComponentProps<"button"> &
|
||||
VariantProps<typeof buttonVariants> & {
|
||||
asChild?: boolean
|
||||
}) {
|
||||
const Comp = asChild ? Slot : "button"
|
||||
|
||||
return (
|
||||
<Comp
|
||||
data-slot="button"
|
||||
className={cn(buttonVariants({ variant, size, className }))}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Button, buttonVariants }
|
||||
@@ -0,0 +1,92 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Card({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card"
|
||||
className={cn(
|
||||
"bg-card text-card-foreground flex flex-col gap-6 rounded-xl border py-6 shadow-sm",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-header"
|
||||
className={cn(
|
||||
"@container/card-header grid auto-rows-min grid-rows-[auto_auto] items-start gap-2 px-6 has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-6",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-title"
|
||||
className={cn("leading-none font-semibold", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-description"
|
||||
className={cn("text-muted-foreground text-sm", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardAction({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-action"
|
||||
className={cn(
|
||||
"col-start-2 row-span-2 row-start-1 self-start justify-self-end",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-content"
|
||||
className={cn("px-6", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-footer"
|
||||
className={cn("flex items-center px-6 [.border-t]:pt-6", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Card,
|
||||
CardHeader,
|
||||
CardFooter,
|
||||
CardTitle,
|
||||
CardAction,
|
||||
CardDescription,
|
||||
CardContent,
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
|
||||
import { CheckIcon } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Checkbox({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof CheckboxPrimitive.Root>) {
|
||||
return (
|
||||
<CheckboxPrimitive.Root
|
||||
data-slot="checkbox"
|
||||
className={cn(
|
||||
"peer border-input dark:bg-input/30 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground dark:data-[state=checked]:bg-primary data-[state=checked]:border-primary focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive size-4 shrink-0 rounded-[4px] border shadow-xs transition-shadow outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<CheckboxPrimitive.Indicator
|
||||
data-slot="checkbox-indicator"
|
||||
className="grid place-content-center text-current transition-none"
|
||||
>
|
||||
<CheckIcon className="size-3.5" />
|
||||
</CheckboxPrimitive.Indicator>
|
||||
</CheckboxPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
export { Checkbox }
|
||||
@@ -0,0 +1,252 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as ContextMenuPrimitive from "@radix-ui/react-context-menu"
|
||||
import { CheckIcon, ChevronRightIcon, CircleIcon } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function ContextMenu({
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Root>) {
|
||||
return <ContextMenuPrimitive.Root data-slot="context-menu" {...props} />
|
||||
}
|
||||
|
||||
function ContextMenuTrigger({
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Trigger>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Trigger data-slot="context-menu-trigger" {...props} />
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuGroup({
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Group>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Group data-slot="context-menu-group" {...props} />
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuPortal({
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Portal>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Portal data-slot="context-menu-portal" {...props} />
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuSub({
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Sub>) {
|
||||
return <ContextMenuPrimitive.Sub data-slot="context-menu-sub" {...props} />
|
||||
}
|
||||
|
||||
function ContextMenuRadioGroup({
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.RadioGroup>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.RadioGroup
|
||||
data-slot="context-menu-radio-group"
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuSubTrigger({
|
||||
className,
|
||||
inset,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.SubTrigger> & {
|
||||
inset?: boolean
|
||||
}) {
|
||||
return (
|
||||
<ContextMenuPrimitive.SubTrigger
|
||||
data-slot="context-menu-sub-trigger"
|
||||
data-inset={inset}
|
||||
className={cn(
|
||||
"focus:bg-accent focus:text-accent-foreground data-[state=open]:bg-accent data-[state=open]:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground flex cursor-default items-center rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-inset:pl-8 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<ChevronRightIcon className="ml-auto" />
|
||||
</ContextMenuPrimitive.SubTrigger>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuSubContent({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.SubContent>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.SubContent
|
||||
data-slot="context-menu-sub-content"
|
||||
className={cn(
|
||||
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 min-w-32 origin-(--radix-context-menu-content-transform-origin) overflow-hidden rounded-md border p-1 shadow-lg",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuContent({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Content>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Portal>
|
||||
<ContextMenuPrimitive.Content
|
||||
data-slot="context-menu-content"
|
||||
className={cn(
|
||||
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 max-h-(--radix-context-menu-content-available-height) min-w-32 origin-(--radix-context-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-md border p-1 shadow-md",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
</ContextMenuPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuItem({
|
||||
className,
|
||||
inset,
|
||||
variant = "default",
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Item> & {
|
||||
inset?: boolean
|
||||
variant?: "default" | "destructive"
|
||||
}) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Item
|
||||
data-slot="context-menu-item"
|
||||
data-inset={inset}
|
||||
data-variant={variant}
|
||||
className={cn(
|
||||
"focus:bg-accent focus:text-accent-foreground data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 dark:data-[variant=destructive]:focus:bg-destructive/20 data-[variant=destructive]:focus:text-destructive data-[variant=destructive]:*:[svg]:text-destructive! [&_svg:not([class*='text-'])]:text-muted-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 data-inset:pl-8 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuCheckboxItem({
|
||||
className,
|
||||
children,
|
||||
checked,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.CheckboxItem>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.CheckboxItem
|
||||
data-slot="context-menu-checkbox-item"
|
||||
className={cn(
|
||||
"focus:bg-accent focus:text-accent-foreground relative flex cursor-default items-center gap-2 rounded-sm py-1.5 pr-2 pl-8 text-sm outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
checked={checked}
|
||||
{...props}
|
||||
>
|
||||
<span className="pointer-events-none absolute left-2 flex size-3.5 items-center justify-center">
|
||||
<ContextMenuPrimitive.ItemIndicator>
|
||||
<CheckIcon className="size-4" />
|
||||
</ContextMenuPrimitive.ItemIndicator>
|
||||
</span>
|
||||
{children}
|
||||
</ContextMenuPrimitive.CheckboxItem>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuRadioItem({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.RadioItem>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.RadioItem
|
||||
data-slot="context-menu-radio-item"
|
||||
className={cn(
|
||||
"focus:bg-accent focus:text-accent-foreground relative flex cursor-default items-center gap-2 rounded-sm py-1.5 pr-2 pl-8 text-sm outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<span className="pointer-events-none absolute left-2 flex size-3.5 items-center justify-center">
|
||||
<ContextMenuPrimitive.ItemIndicator>
|
||||
<CircleIcon className="size-2 fill-current" />
|
||||
</ContextMenuPrimitive.ItemIndicator>
|
||||
</span>
|
||||
{children}
|
||||
</ContextMenuPrimitive.RadioItem>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuLabel({
|
||||
className,
|
||||
inset,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Label> & {
|
||||
inset?: boolean
|
||||
}) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Label
|
||||
data-slot="context-menu-label"
|
||||
data-inset={inset}
|
||||
className={cn(
|
||||
"text-foreground px-2 py-1.5 text-sm font-medium data-inset:pl-8",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuSeparator({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ContextMenuPrimitive.Separator>) {
|
||||
return (
|
||||
<ContextMenuPrimitive.Separator
|
||||
data-slot="context-menu-separator"
|
||||
className={cn("bg-border -mx-1 my-1 h-px", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ContextMenuShortcut({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"span">) {
|
||||
return (
|
||||
<span
|
||||
data-slot="context-menu-shortcut"
|
||||
className={cn(
|
||||
"text-muted-foreground ml-auto text-xs tracking-widest",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
ContextMenu,
|
||||
ContextMenuTrigger,
|
||||
ContextMenuContent,
|
||||
ContextMenuItem,
|
||||
ContextMenuCheckboxItem,
|
||||
ContextMenuRadioItem,
|
||||
ContextMenuLabel,
|
||||
ContextMenuSeparator,
|
||||
ContextMenuShortcut,
|
||||
ContextMenuGroup,
|
||||
ContextMenuPortal,
|
||||
ContextMenuSub,
|
||||
ContextMenuSubContent,
|
||||
ContextMenuSubTrigger,
|
||||
ContextMenuRadioGroup,
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as DialogPrimitive from "@radix-ui/react-dialog"
|
||||
import { XIcon } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Dialog({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
|
||||
return <DialogPrimitive.Root data-slot="dialog" {...props} />
|
||||
}
|
||||
|
||||
function DialogTrigger({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
|
||||
return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
|
||||
}
|
||||
|
||||
function DialogPortal({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
|
||||
return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
|
||||
}
|
||||
|
||||
function DialogClose({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
|
||||
return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
|
||||
}
|
||||
|
||||
function DialogOverlay({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
|
||||
return (
|
||||
<DialogPrimitive.Overlay
|
||||
data-slot="dialog-overlay"
|
||||
className={cn(
|
||||
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogContent({
|
||||
className,
|
||||
children,
|
||||
showCloseButton = true,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
|
||||
showCloseButton?: boolean
|
||||
}) {
|
||||
return (
|
||||
<DialogPortal data-slot="dialog-portal">
|
||||
<DialogOverlay />
|
||||
<DialogPrimitive.Content
|
||||
data-slot="dialog-content"
|
||||
className={cn(
|
||||
"bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border p-6 shadow-lg duration-200 sm:max-w-lg",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
{showCloseButton && (
|
||||
<DialogPrimitive.Close
|
||||
data-slot="dialog-close"
|
||||
className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
|
||||
>
|
||||
<XIcon />
|
||||
<span className="sr-only">Close</span>
|
||||
</DialogPrimitive.Close>
|
||||
)}
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPortal>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="dialog-header"
|
||||
className={cn("flex flex-col gap-2 text-center sm:text-left", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="dialog-footer"
|
||||
className={cn(
|
||||
"flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogTitle({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
|
||||
return (
|
||||
<DialogPrimitive.Title
|
||||
data-slot="dialog-title"
|
||||
className={cn("text-lg leading-none font-semibold", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogDescription({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
|
||||
return (
|
||||
<DialogPrimitive.Description
|
||||
data-slot="dialog-description"
|
||||
className={cn("text-muted-foreground text-sm", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogOverlay,
|
||||
DialogPortal,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
import * as React from "react";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import {
|
||||
ContextMenu,
|
||||
ContextMenuContent,
|
||||
ContextMenuItem,
|
||||
ContextMenuSeparator,
|
||||
ContextMenuTrigger,
|
||||
} from "@/components/ui/context-menu";
|
||||
import { Scissors, Copy, Clipboard, Type } from "lucide-react";
|
||||
|
||||
export interface InputWithContextProps
|
||||
extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||
onValueChange?: (value: string) => void;
|
||||
}
|
||||
|
||||
const InputWithContext = React.forwardRef<HTMLInputElement, InputWithContextProps>(
|
||||
({ className, type, onValueChange, onChange, ...props }, ref) => {
|
||||
const inputRef = React.useRef<HTMLInputElement>(null);
|
||||
const [hasSelection, setHasSelection] = React.useState(false);
|
||||
const [canPaste, setCanPaste] = React.useState(false);
|
||||
|
||||
// Combine refs
|
||||
React.useImperativeHandle(ref, () => inputRef.current as HTMLInputElement);
|
||||
|
||||
// Check selection state
|
||||
const updateSelectionState = () => {
|
||||
const input = inputRef.current;
|
||||
if (!input) return;
|
||||
const start = input.selectionStart ?? 0;
|
||||
const end = input.selectionEnd ?? 0;
|
||||
setHasSelection(start !== end);
|
||||
};
|
||||
|
||||
// Check clipboard permission when user explicitly opens the context menu.
|
||||
const checkClipboard = async () => {
|
||||
try {
|
||||
const text = await navigator.clipboard.readText();
|
||||
setCanPaste(text.length > 0);
|
||||
} catch {
|
||||
setCanPaste(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCut = async () => {
|
||||
const input = inputRef.current;
|
||||
if (!input) return;
|
||||
|
||||
const start = input.selectionStart ?? 0;
|
||||
const end = input.selectionEnd ?? 0;
|
||||
const selectedText = input.value.substring(start, end);
|
||||
|
||||
if (selectedText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(selectedText);
|
||||
const newValue = input.value.substring(0, start) + input.value.substring(end);
|
||||
|
||||
// Update value and trigger change
|
||||
input.value = newValue;
|
||||
input.setSelectionRange(start, start);
|
||||
|
||||
// Trigger React onChange
|
||||
if (onChange) {
|
||||
const event = {
|
||||
target: input,
|
||||
currentTarget: input,
|
||||
} as React.ChangeEvent<HTMLInputElement>;
|
||||
onChange(event);
|
||||
}
|
||||
|
||||
if (onValueChange) {
|
||||
onValueChange(newValue);
|
||||
}
|
||||
|
||||
input.focus();
|
||||
} catch (err) {
|
||||
console.error("Failed to cut:", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleCopy = async () => {
|
||||
const input = inputRef.current;
|
||||
if (!input) return;
|
||||
|
||||
const start = input.selectionStart ?? 0;
|
||||
const end = input.selectionEnd ?? 0;
|
||||
const selectedText = input.value.substring(start, end);
|
||||
|
||||
if (selectedText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(selectedText);
|
||||
input.focus();
|
||||
} catch (err) {
|
||||
console.error("Failed to copy:", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handlePaste = async () => {
|
||||
const input = inputRef.current;
|
||||
if (!input) return;
|
||||
|
||||
try {
|
||||
const text = await navigator.clipboard.readText();
|
||||
const start = input.selectionStart ?? 0;
|
||||
const end = input.selectionEnd ?? 0;
|
||||
|
||||
const newValue =
|
||||
input.value.substring(0, start) + text + input.value.substring(end);
|
||||
|
||||
// Update value and trigger change
|
||||
input.value = newValue;
|
||||
const newPosition = start + text.length;
|
||||
input.setSelectionRange(newPosition, newPosition);
|
||||
|
||||
// Trigger React onChange
|
||||
if (onChange) {
|
||||
const event = {
|
||||
target: input,
|
||||
currentTarget: input,
|
||||
} as React.ChangeEvent<HTMLInputElement>;
|
||||
onChange(event);
|
||||
}
|
||||
|
||||
if (onValueChange) {
|
||||
onValueChange(newValue);
|
||||
}
|
||||
|
||||
input.focus();
|
||||
await checkClipboard();
|
||||
} catch (err) {
|
||||
console.error("Failed to paste:", err);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSelectAll = () => {
|
||||
const input = inputRef.current;
|
||||
if (!input) return;
|
||||
input.select();
|
||||
input.focus();
|
||||
updateSelectionState();
|
||||
};
|
||||
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
if (onChange) {
|
||||
onChange(e);
|
||||
}
|
||||
if (onValueChange) {
|
||||
onValueChange(e.target.value);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ContextMenu
|
||||
onOpenChange={(open) => {
|
||||
if (open) {
|
||||
checkClipboard();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<ContextMenuTrigger asChild>
|
||||
<Input
|
||||
ref={inputRef}
|
||||
type={type}
|
||||
className={className}
|
||||
onChange={handleInputChange}
|
||||
onSelect={updateSelectionState}
|
||||
onMouseUp={updateSelectionState}
|
||||
onKeyUp={updateSelectionState}
|
||||
{...props}
|
||||
/>
|
||||
</ContextMenuTrigger>
|
||||
<ContextMenuContent className="w-48">
|
||||
<ContextMenuItem
|
||||
onSelect={handleCut}
|
||||
disabled={!hasSelection || props.disabled || props.readOnly}
|
||||
>
|
||||
<Scissors className="mr-2 h-4 w-4" />
|
||||
Cut
|
||||
<span className="ml-auto text-xs text-muted-foreground">Ctrl+X</span>
|
||||
</ContextMenuItem>
|
||||
<ContextMenuItem
|
||||
onSelect={handleCopy}
|
||||
disabled={!hasSelection || props.disabled}
|
||||
>
|
||||
<Copy className="mr-2 h-4 w-4" />
|
||||
Copy
|
||||
<span className="ml-auto text-xs text-muted-foreground">Ctrl+C</span>
|
||||
</ContextMenuItem>
|
||||
<ContextMenuItem
|
||||
onSelect={handlePaste}
|
||||
disabled={!canPaste || props.disabled || props.readOnly}
|
||||
>
|
||||
<Clipboard className="mr-2 h-4 w-4" />
|
||||
Paste
|
||||
<span className="ml-auto text-xs text-muted-foreground">Ctrl+V</span>
|
||||
</ContextMenuItem>
|
||||
<ContextMenuSeparator />
|
||||
<ContextMenuItem
|
||||
onSelect={handleSelectAll}
|
||||
disabled={!inputRef.current?.value || props.disabled}
|
||||
>
|
||||
<Type className="mr-2 h-4 w-4" />
|
||||
Select All
|
||||
<span className="ml-auto text-xs text-muted-foreground">Ctrl+A</span>
|
||||
</ContextMenuItem>
|
||||
</ContextMenuContent>
|
||||
</ContextMenu>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
InputWithContext.displayName = "InputWithContext";
|
||||
|
||||
export { InputWithContext };
|
||||
@@ -0,0 +1,21 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
return (
|
||||
<input
|
||||
type={type}
|
||||
data-slot="input"
|
||||
className={cn(
|
||||
"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
||||
"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
|
||||
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Input }
|
||||
@@ -0,0 +1,24 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as LabelPrimitive from "@radix-ui/react-label"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Label({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof LabelPrimitive.Root>) {
|
||||
return (
|
||||
<LabelPrimitive.Root
|
||||
data-slot="label"
|
||||
className={cn(
|
||||
"flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Label }
|
||||
@@ -0,0 +1,127 @@
|
||||
import * as React from "react"
|
||||
import {
|
||||
ChevronLeftIcon,
|
||||
ChevronRightIcon,
|
||||
MoreHorizontalIcon,
|
||||
} from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Button, buttonVariants } from "@/components/ui/button"
|
||||
|
||||
function Pagination({ className, ...props }: React.ComponentProps<"nav">) {
|
||||
return (
|
||||
<nav
|
||||
role="navigation"
|
||||
aria-label="pagination"
|
||||
data-slot="pagination"
|
||||
className={cn("mx-auto flex w-full justify-center", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function PaginationContent({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"ul">) {
|
||||
return (
|
||||
<ul
|
||||
data-slot="pagination-content"
|
||||
className={cn("flex flex-row items-center gap-1", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function PaginationItem({ ...props }: React.ComponentProps<"li">) {
|
||||
return <li data-slot="pagination-item" {...props} />
|
||||
}
|
||||
|
||||
type PaginationLinkProps = {
|
||||
isActive?: boolean
|
||||
} & Pick<React.ComponentProps<typeof Button>, "size"> &
|
||||
React.ComponentProps<"a">
|
||||
|
||||
function PaginationLink({
|
||||
className,
|
||||
isActive,
|
||||
size = "icon",
|
||||
...props
|
||||
}: PaginationLinkProps) {
|
||||
return (
|
||||
<a
|
||||
aria-current={isActive ? "page" : undefined}
|
||||
data-slot="pagination-link"
|
||||
data-active={isActive}
|
||||
className={cn(
|
||||
buttonVariants({
|
||||
variant: isActive ? "outline" : "ghost",
|
||||
size,
|
||||
}),
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function PaginationPrevious({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof PaginationLink>) {
|
||||
return (
|
||||
<PaginationLink
|
||||
aria-label="Go to previous page"
|
||||
size="default"
|
||||
className={cn("gap-1 px-2.5 sm:pl-2.5", className)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronLeftIcon />
|
||||
<span className="hidden sm:block">Previous</span>
|
||||
</PaginationLink>
|
||||
)
|
||||
}
|
||||
|
||||
function PaginationNext({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof PaginationLink>) {
|
||||
return (
|
||||
<PaginationLink
|
||||
aria-label="Go to next page"
|
||||
size="default"
|
||||
className={cn("gap-1 px-2.5 sm:pr-2.5", className)}
|
||||
{...props}
|
||||
>
|
||||
<span className="hidden sm:block">Next</span>
|
||||
<ChevronRightIcon />
|
||||
</PaginationLink>
|
||||
)
|
||||
}
|
||||
|
||||
function PaginationEllipsis({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"span">) {
|
||||
return (
|
||||
<span
|
||||
aria-hidden
|
||||
data-slot="pagination-ellipsis"
|
||||
className={cn("flex size-9 items-center justify-center", className)}
|
||||
{...props}
|
||||
>
|
||||
<MoreHorizontalIcon className="size-4" />
|
||||
<span className="sr-only">More pages</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Pagination,
|
||||
PaginationContent,
|
||||
PaginationLink,
|
||||
PaginationItem,
|
||||
PaginationPrevious,
|
||||
PaginationNext,
|
||||
PaginationEllipsis,
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as ProgressPrimitive from "@radix-ui/react-progress"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Progress({
|
||||
className,
|
||||
value,
|
||||
...props
|
||||
}: React.ComponentProps<typeof ProgressPrimitive.Root>) {
|
||||
return (
|
||||
<ProgressPrimitive.Root
|
||||
data-slot="progress"
|
||||
className={cn(
|
||||
"bg-primary/20 relative h-2 w-full overflow-hidden rounded-full",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ProgressPrimitive.Indicator
|
||||
data-slot="progress-indicator"
|
||||
className="bg-primary h-full w-full flex-1 transition-all"
|
||||
style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
|
||||
/>
|
||||
</ProgressPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
export { Progress }
|
||||
@@ -0,0 +1,45 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"
|
||||
import { CircleIcon } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function RadioGroup({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof RadioGroupPrimitive.Root>) {
|
||||
return (
|
||||
<RadioGroupPrimitive.Root
|
||||
data-slot="radio-group"
|
||||
className={cn("grid gap-3", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function RadioGroupItem({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof RadioGroupPrimitive.Item>) {
|
||||
return (
|
||||
<RadioGroupPrimitive.Item
|
||||
data-slot="radio-group-item"
|
||||
className={cn(
|
||||
"border-input text-primary focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:bg-input/30 aspect-square size-4 shrink-0 rounded-full border shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<RadioGroupPrimitive.Indicator
|
||||
data-slot="radio-group-indicator"
|
||||
className="relative flex items-center justify-center"
|
||||
>
|
||||
<CircleIcon className="fill-primary absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2" />
|
||||
</RadioGroupPrimitive.Indicator>
|
||||
</RadioGroupPrimitive.Item>
|
||||
)
|
||||
}
|
||||
|
||||
export { RadioGroup, RadioGroupItem }
|
||||
@@ -0,0 +1,185 @@
|
||||
import * as React from "react"
|
||||
import * as SelectPrimitive from "@radix-ui/react-select"
|
||||
import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Select({
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Root>) {
|
||||
return <SelectPrimitive.Root data-slot="select" {...props} />
|
||||
}
|
||||
|
||||
function SelectGroup({
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Group>) {
|
||||
return <SelectPrimitive.Group data-slot="select-group" {...props} />
|
||||
}
|
||||
|
||||
function SelectValue({
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Value>) {
|
||||
return <SelectPrimitive.Value data-slot="select-value" {...props} />
|
||||
}
|
||||
|
||||
function SelectTrigger({
|
||||
className,
|
||||
size = "default",
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Trigger> & {
|
||||
size?: "sm" | "default"
|
||||
}) {
|
||||
return (
|
||||
<SelectPrimitive.Trigger
|
||||
data-slot="select-trigger"
|
||||
data-size={size}
|
||||
className={cn(
|
||||
"border-input data-placeholder:text-muted-foreground [&_svg:not([class*='text-'])]:text-muted-foreground focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:bg-input/30 dark:hover:bg-input/50 flex w-fit items-center justify-between gap-2 rounded-md border bg-transparent px-3 py-2 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<SelectPrimitive.Icon asChild>
|
||||
<ChevronDownIcon className="size-4 opacity-50" />
|
||||
</SelectPrimitive.Icon>
|
||||
</SelectPrimitive.Trigger>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectContent({
|
||||
className,
|
||||
children,
|
||||
position = "popper",
|
||||
align = "center",
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Content>) {
|
||||
return (
|
||||
<SelectPrimitive.Portal>
|
||||
<SelectPrimitive.Content
|
||||
data-slot="select-content"
|
||||
className={cn(
|
||||
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 relative z-50 max-h-(--radix-select-content-available-height) min-w-32 origin-(--radix-select-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-md border shadow-md",
|
||||
position === "popper" &&
|
||||
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
|
||||
className
|
||||
)}
|
||||
position={position}
|
||||
align={align}
|
||||
{...props}
|
||||
>
|
||||
<SelectScrollUpButton />
|
||||
<SelectPrimitive.Viewport
|
||||
className={cn(
|
||||
"p-1",
|
||||
position === "popper" &&
|
||||
"h-(--radix-select-trigger-height) w-full min-w-(--radix-select-trigger-width) scroll-my-1"
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</SelectPrimitive.Viewport>
|
||||
<SelectScrollDownButton />
|
||||
</SelectPrimitive.Content>
|
||||
</SelectPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectLabel({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Label>) {
|
||||
return (
|
||||
<SelectPrimitive.Label
|
||||
data-slot="select-label"
|
||||
className={cn("text-muted-foreground px-2 py-1.5 text-xs", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectItem({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Item>) {
|
||||
return (
|
||||
<SelectPrimitive.Item
|
||||
data-slot="select-item"
|
||||
className={cn(
|
||||
"focus:bg-accent focus:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<span className="absolute right-2 flex size-3.5 items-center justify-center">
|
||||
<SelectPrimitive.ItemIndicator>
|
||||
<CheckIcon className="size-4" />
|
||||
</SelectPrimitive.ItemIndicator>
|
||||
</span>
|
||||
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
|
||||
</SelectPrimitive.Item>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectSeparator({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
|
||||
return (
|
||||
<SelectPrimitive.Separator
|
||||
data-slot="select-separator"
|
||||
className={cn("bg-border pointer-events-none -mx-1 my-1 h-px", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectScrollUpButton({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
|
||||
return (
|
||||
<SelectPrimitive.ScrollUpButton
|
||||
data-slot="select-scroll-up-button"
|
||||
className={cn(
|
||||
"flex cursor-default items-center justify-center py-1",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronUpIcon className="size-4" />
|
||||
</SelectPrimitive.ScrollUpButton>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectScrollDownButton({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
|
||||
return (
|
||||
<SelectPrimitive.ScrollDownButton
|
||||
data-slot="select-scroll-down-button"
|
||||
className={cn(
|
||||
"flex cursor-default items-center justify-center py-1",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronDownIcon className="size-4" />
|
||||
</SelectPrimitive.ScrollDownButton>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectLabel,
|
||||
SelectScrollDownButton,
|
||||
SelectScrollUpButton,
|
||||
SelectSeparator,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
CircleCheckIcon,
|
||||
InfoIcon,
|
||||
Loader2Icon,
|
||||
OctagonXIcon,
|
||||
TriangleAlertIcon,
|
||||
} from "lucide-react"
|
||||
import { useTheme } from "next-themes"
|
||||
import { Toaster as Sonner, type ToasterProps } from "sonner"
|
||||
|
||||
const Toaster = ({ ...props }: ToasterProps) => {
|
||||
const { theme = "system" } = useTheme()
|
||||
|
||||
return (
|
||||
<Sonner
|
||||
theme={theme as ToasterProps["theme"]}
|
||||
className="toaster group"
|
||||
icons={{
|
||||
success: <CircleCheckIcon className="size-4" />,
|
||||
info: <InfoIcon className="size-4" />,
|
||||
warning: <TriangleAlertIcon className="size-4" />,
|
||||
error: <OctagonXIcon className="size-4" />,
|
||||
loading: <Loader2Icon className="size-4 animate-spin" />,
|
||||
}}
|
||||
toastOptions={{
|
||||
classNames: {
|
||||
success: "border-green-500 bg-green-50 text-green-900 dark:bg-green-950 dark:text-green-100 [&>svg]:text-green-500",
|
||||
error: "border-red-500 bg-red-50 text-red-900 dark:bg-red-950 dark:text-red-100 [&>svg]:text-red-500",
|
||||
warning: "border-yellow-500 bg-yellow-50 text-yellow-900 dark:bg-yellow-950 dark:text-yellow-100 [&>svg]:text-yellow-500",
|
||||
info: "border-blue-500 bg-blue-50 text-blue-900 dark:bg-blue-950 dark:text-blue-100 [&>svg]:text-blue-500",
|
||||
},
|
||||
}}
|
||||
style={
|
||||
{
|
||||
"--normal-bg": "var(--popover)",
|
||||
"--normal-text": "var(--popover-foreground)",
|
||||
"--normal-border": "var(--border)",
|
||||
"--border-radius": "var(--radius)",
|
||||
left: "calc(56px + 1rem)",
|
||||
} as React.CSSProperties
|
||||
}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Toaster }
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Loader2 } from "lucide-react"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Spinner({ className, ...props }: React.ComponentProps<"svg">) {
|
||||
return (
|
||||
<Loader2
|
||||
role="status"
|
||||
aria-label="Loading"
|
||||
className={cn("size-4 animate-spin", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Spinner }
|
||||
@@ -0,0 +1,31 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as SwitchPrimitive from "@radix-ui/react-switch"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Switch({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SwitchPrimitive.Root>) {
|
||||
return (
|
||||
<SwitchPrimitive.Root
|
||||
data-slot="switch"
|
||||
className={cn(
|
||||
"peer data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:border-ring focus-visible:ring-ring/50 inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border border-transparent shadow-xs transition-all outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<SwitchPrimitive.Thumb
|
||||
data-slot="switch-thumb"
|
||||
className={cn(
|
||||
"pointer-events-none block size-4 rounded-full shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0 data-[state=checked]:bg-primary-foreground data-[state=unchecked]:bg-background"
|
||||
)}
|
||||
/>
|
||||
</SwitchPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
export { Switch }
|
||||
@@ -0,0 +1,61 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as TooltipPrimitive from "@radix-ui/react-tooltip"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function TooltipProvider({
|
||||
delayDuration = 0,
|
||||
...props
|
||||
}: React.ComponentProps<typeof TooltipPrimitive.Provider>) {
|
||||
return (
|
||||
<TooltipPrimitive.Provider
|
||||
data-slot="tooltip-provider"
|
||||
delayDuration={delayDuration}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function Tooltip({
|
||||
...props
|
||||
}: React.ComponentProps<typeof TooltipPrimitive.Root>) {
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<TooltipPrimitive.Root data-slot="tooltip" {...props} />
|
||||
</TooltipProvider>
|
||||
)
|
||||
}
|
||||
|
||||
function TooltipTrigger({
|
||||
...props
|
||||
}: React.ComponentProps<typeof TooltipPrimitive.Trigger>) {
|
||||
return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} />
|
||||
}
|
||||
|
||||
function TooltipContent({
|
||||
className,
|
||||
sideOffset = 0,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof TooltipPrimitive.Content>) {
|
||||
return (
|
||||
<TooltipPrimitive.Portal>
|
||||
<TooltipPrimitive.Content
|
||||
data-slot="tooltip-content"
|
||||
sideOffset={sideOffset}
|
||||
className={cn(
|
||||
"bg-foreground text-background animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 w-fit origin-(--radix-tooltip-content-transform-origin) rounded-md px-3 py-1.5 text-xs text-balance",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<TooltipPrimitive.Arrow className="bg-foreground fill-foreground z-50 size-2.5 translate-y-[calc(-50%-2px)] rotate-45 rounded-[2px]" />
|
||||
</TooltipPrimitive.Content>
|
||||
</TooltipPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
|
||||
@@ -0,0 +1,251 @@
|
||||
@import "tailwindcss";
|
||||
@import "tw-animate-css";
|
||||
|
||||
@custom-variant dark (&:is(.dark *));
|
||||
|
||||
@theme inline {
|
||||
--radius-sm: calc(var(--radius) - 4px);
|
||||
--radius-md: calc(var(--radius) - 2px);
|
||||
--radius-lg: var(--radius);
|
||||
--radius-xl: calc(var(--radius) + 4px);
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--color-card: var(--card);
|
||||
--color-card-foreground: var(--card-foreground);
|
||||
--color-popover: var(--popover);
|
||||
--color-popover-foreground: var(--popover-foreground);
|
||||
--color-primary: var(--primary);
|
||||
--color-primary-foreground: var(--primary-foreground);
|
||||
--color-secondary: var(--secondary);
|
||||
--color-secondary-foreground: var(--secondary-foreground);
|
||||
--color-muted: var(--muted);
|
||||
--color-muted-foreground: var(--muted-foreground);
|
||||
--color-accent: var(--accent);
|
||||
--color-accent-foreground: var(--accent-foreground);
|
||||
--color-destructive: var(--destructive);
|
||||
--color-border: var(--border);
|
||||
--color-input: var(--input);
|
||||
--color-ring: var(--ring);
|
||||
--color-chart-1: var(--chart-1);
|
||||
--color-chart-2: var(--chart-2);
|
||||
--color-chart-3: var(--chart-3);
|
||||
--color-chart-4: var(--chart-4);
|
||||
--color-chart-5: var(--chart-5);
|
||||
--color-sidebar: var(--sidebar);
|
||||
--color-sidebar-foreground: var(--sidebar-foreground);
|
||||
--color-sidebar-primary: var(--sidebar-primary);
|
||||
--color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
|
||||
--color-sidebar-accent: var(--sidebar-accent);
|
||||
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
|
||||
--color-sidebar-border: var(--sidebar-border);
|
||||
--color-sidebar-ring: var(--sidebar-ring);
|
||||
}
|
||||
|
||||
:root {
|
||||
--radius: 0.625rem;
|
||||
--background: oklch(1 0 0);
|
||||
--foreground: oklch(0.145 0 0);
|
||||
--card: oklch(1 0 0);
|
||||
--card-foreground: oklch(0.145 0 0);
|
||||
--popover: oklch(1 0 0);
|
||||
--popover-foreground: oklch(0.145 0 0);
|
||||
--primary: oklch(0.205 0 0);
|
||||
--primary-foreground: oklch(0.985 0 0);
|
||||
--secondary: oklch(0.97 0 0);
|
||||
--secondary-foreground: oklch(0.205 0 0);
|
||||
--muted: oklch(0.97 0 0);
|
||||
--muted-foreground: oklch(0.556 0 0);
|
||||
--accent: oklch(0.97 0 0);
|
||||
--accent-foreground: oklch(0.205 0 0);
|
||||
--destructive: oklch(0.577 0.245 27.325);
|
||||
--border: oklch(0.922 0 0);
|
||||
--input: oklch(0.922 0 0);
|
||||
--ring: oklch(0.708 0 0);
|
||||
--chart-1: oklch(0.646 0.222 41.116);
|
||||
--chart-2: oklch(0.6 0.118 184.704);
|
||||
--chart-3: oklch(0.398 0.07 227.392);
|
||||
--chart-4: oklch(0.828 0.189 84.429);
|
||||
--chart-5: oklch(0.769 0.188 70.08);
|
||||
--sidebar: oklch(0.985 0 0);
|
||||
--sidebar-foreground: oklch(0.145 0 0);
|
||||
--sidebar-primary: oklch(0.205 0 0);
|
||||
--sidebar-primary-foreground: oklch(0.985 0 0);
|
||||
--sidebar-accent: oklch(0.97 0 0);
|
||||
--sidebar-accent-foreground: oklch(0.205 0 0);
|
||||
--sidebar-border: oklch(0.922 0 0);
|
||||
--sidebar-ring: oklch(0.708 0 0);
|
||||
}
|
||||
|
||||
.dark {
|
||||
--background: oklch(0.145 0 0);
|
||||
--foreground: oklch(0.985 0 0);
|
||||
--card: oklch(0.205 0 0);
|
||||
--card-foreground: oklch(0.985 0 0);
|
||||
--popover: oklch(0.205 0 0);
|
||||
--popover-foreground: oklch(0.985 0 0);
|
||||
--primary: oklch(0.922 0 0);
|
||||
--primary-foreground: oklch(0.205 0 0);
|
||||
--secondary: oklch(0.269 0 0);
|
||||
--secondary-foreground: oklch(0.985 0 0);
|
||||
--muted: oklch(0.269 0 0);
|
||||
--muted-foreground: oklch(0.708 0 0);
|
||||
--accent: oklch(0.269 0 0);
|
||||
--accent-foreground: oklch(0.985 0 0);
|
||||
--destructive: oklch(0.704 0.191 22.216);
|
||||
--border: oklch(1 0 0 / 10%);
|
||||
--input: oklch(1 0 0 / 15%);
|
||||
--ring: oklch(0.556 0 0);
|
||||
--chart-1: oklch(0.488 0.243 264.376);
|
||||
--chart-2: oklch(0.696 0.17 162.48);
|
||||
--chart-3: oklch(0.769 0.188 70.08);
|
||||
--chart-4: oklch(0.627 0.265 303.9);
|
||||
--chart-5: oklch(0.645 0.246 16.439);
|
||||
--sidebar: oklch(0.205 0 0);
|
||||
--sidebar-foreground: oklch(0.985 0 0);
|
||||
--sidebar-primary: oklch(0.488 0.243 264.376);
|
||||
--sidebar-primary-foreground: oklch(0.985 0 0);
|
||||
--sidebar-accent: oklch(0.269 0 0);
|
||||
--sidebar-accent-foreground: oklch(0.985 0 0);
|
||||
--sidebar-border: oklch(1 0 0 / 10%);
|
||||
--sidebar-ring: oklch(0.556 0 0);
|
||||
}
|
||||
|
||||
@layer base {
|
||||
* {
|
||||
@apply border-border outline-ring/50;
|
||||
}
|
||||
body {
|
||||
@apply bg-background text-foreground;
|
||||
font-family: "Google Sans Flex", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
}
|
||||
code, pre, .font-mono {
|
||||
font-family: "Google Sans Code", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace !important;
|
||||
}
|
||||
}
|
||||
|
||||
@theme inline {
|
||||
--font-mono: "Google Sans Code", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
||||
}
|
||||
|
||||
/* Sonner Toast Styling */
|
||||
[data-sonner-toast] {
|
||||
@apply rounded-lg shadow-lg border;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="success"] {
|
||||
@apply bg-green-50 border-green-200 text-green-900;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="success"] [data-icon] {
|
||||
@apply text-green-600;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="error"] {
|
||||
@apply bg-red-50 border-red-200 text-red-900;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="error"] [data-icon] {
|
||||
@apply text-red-600;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="warning"] {
|
||||
@apply bg-yellow-50 border-yellow-200 text-yellow-900;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="warning"] [data-icon] {
|
||||
@apply text-yellow-600;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="info"] {
|
||||
@apply bg-blue-50 border-blue-200 text-blue-900;
|
||||
}
|
||||
|
||||
[data-sonner-toast][data-type="info"] [data-icon] {
|
||||
@apply text-blue-600;
|
||||
}
|
||||
|
||||
/* Dark mode toast styling */
|
||||
.dark [data-sonner-toast][data-type="success"] {
|
||||
@apply bg-green-950 border-green-800 text-green-100;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="success"] [data-icon] {
|
||||
@apply text-green-400;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="error"] {
|
||||
@apply bg-red-950 border-red-800 text-red-100;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="error"] [data-icon] {
|
||||
@apply text-red-400;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="warning"] {
|
||||
@apply bg-yellow-950 border-yellow-800 text-yellow-100;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="warning"] [data-icon] {
|
||||
@apply text-yellow-400;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="info"] {
|
||||
@apply bg-blue-950 border-blue-800 text-blue-100;
|
||||
}
|
||||
|
||||
.dark [data-sonner-toast][data-type="info"] [data-icon] {
|
||||
@apply text-blue-400;
|
||||
}
|
||||
|
||||
/* Custom Scrollbar - mengikuti primary color theme */
|
||||
::-webkit-scrollbar {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background: var(--secondary);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: var(--primary);
|
||||
border-radius: 4px;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background: var(--primary);
|
||||
filter: brightness(1.2);
|
||||
}
|
||||
|
||||
/* Firefox scrollbar support */
|
||||
* {
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: var(--primary) var(--secondary);
|
||||
}
|
||||
|
||||
/* Custom scrollbar class untuk komponen tertentu */
|
||||
.custom-scrollbar::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
}
|
||||
|
||||
.custom-scrollbar::-webkit-scrollbar-track {
|
||||
background: var(--muted);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.custom-scrollbar::-webkit-scrollbar-thumb {
|
||||
background: var(--primary);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.custom-scrollbar::-webkit-scrollbar-thumb:hover {
|
||||
filter: brightness(1.2);
|
||||
}
|
||||
|
||||
/* Date input styling for dark mode */
|
||||
.dark input[type="date"]::-webkit-calendar-picker-indicator {
|
||||
filter: invert(1);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// Audio utility for toast notifications using Web Audio API
|
||||
|
||||
class AudioManager {
|
||||
private audioContext: AudioContext | null = null;
|
||||
|
||||
private getAudioContext(): AudioContext {
|
||||
if (!this.audioContext) {
|
||||
this.audioContext = new (window.AudioContext || (window as any).webkitAudioContext)();
|
||||
}
|
||||
return this.audioContext;
|
||||
}
|
||||
|
||||
// Generate a simple tone using oscillator
|
||||
private playTone(frequency: number, duration: number, type: OscillatorType = 'sine', volume: number = 0.3) {
|
||||
try {
|
||||
const ctx = this.getAudioContext();
|
||||
const oscillator = ctx.createOscillator();
|
||||
const gainNode = ctx.createGain();
|
||||
|
||||
oscillator.connect(gainNode);
|
||||
gainNode.connect(ctx.destination);
|
||||
|
||||
oscillator.frequency.value = frequency;
|
||||
oscillator.type = type;
|
||||
|
||||
gainNode.gain.setValueAtTime(volume, ctx.currentTime);
|
||||
gainNode.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + duration);
|
||||
|
||||
oscillator.start(ctx.currentTime);
|
||||
oscillator.stop(ctx.currentTime + duration);
|
||||
} catch (error) {
|
||||
console.error('Error playing audio:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Success sound - pleasant ascending tones
|
||||
playSuccess() {
|
||||
const ctx = this.getAudioContext();
|
||||
const now = ctx.currentTime;
|
||||
|
||||
// First tone
|
||||
this.playToneAt(523.25, 0.08, 'sine', 0.2, now); // C5
|
||||
// Second tone
|
||||
this.playToneAt(659.25, 0.08, 'sine', 0.2, now + 0.08); // E5
|
||||
// Third tone
|
||||
this.playToneAt(783.99, 0.15, 'sine', 0.25, now + 0.16); // G5
|
||||
}
|
||||
|
||||
// Error sound - descending tones
|
||||
playError() {
|
||||
const ctx = this.getAudioContext();
|
||||
const now = ctx.currentTime;
|
||||
|
||||
// First tone
|
||||
this.playToneAt(392.00, 0.1, 'square', 0.15, now); // G4
|
||||
// Second tone
|
||||
this.playToneAt(329.63, 0.2, 'square', 0.2, now + 0.1); // E4
|
||||
}
|
||||
|
||||
// Warning sound - alternating tones
|
||||
playWarning() {
|
||||
const ctx = this.getAudioContext();
|
||||
const now = ctx.currentTime;
|
||||
|
||||
// First tone
|
||||
this.playToneAt(440.00, 0.1, 'triangle', 0.2, now); // A4
|
||||
// Second tone
|
||||
this.playToneAt(493.88, 0.1, 'triangle', 0.2, now + 0.12); // B4
|
||||
}
|
||||
|
||||
// Info sound - single soft tone
|
||||
playInfo() {
|
||||
this.playTone(523.25, 0.15, 'sine', 0.15); // C5
|
||||
}
|
||||
|
||||
// Helper method to play tone at specific time
|
||||
private playToneAt(frequency: number, duration: number, type: OscillatorType, volume: number, startTime: number) {
|
||||
try {
|
||||
const ctx = this.getAudioContext();
|
||||
const oscillator = ctx.createOscillator();
|
||||
const gainNode = ctx.createGain();
|
||||
|
||||
oscillator.connect(gainNode);
|
||||
gainNode.connect(ctx.destination);
|
||||
|
||||
oscillator.frequency.value = frequency;
|
||||
oscillator.type = type;
|
||||
|
||||
gainNode.gain.setValueAtTime(volume, startTime);
|
||||
gainNode.gain.exponentialRampToValueAtTime(0.01, startTime + duration);
|
||||
|
||||
oscillator.start(startTime);
|
||||
oscillator.stop(startTime + duration);
|
||||
} catch (error) {
|
||||
console.error('Error playing audio:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export singleton instance
|
||||
export const audioManager = new AudioManager();
|
||||
|
||||
// Helper functions for easy use
|
||||
export const playSuccessSound = () => audioManager.playSuccess();
|
||||
export const playErrorSound = () => audioManager.playError();
|
||||
export const playWarningSound = () => audioManager.playWarning();
|
||||
export const playInfoSound = () => audioManager.playInfo();
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* Fetch State Manager
|
||||
* Manages resumable fetch state for large accounts
|
||||
*/
|
||||
|
||||
import type { TwitterResponse, TimelineEntry, AccountInfo } from "@/types/api";
|
||||
|
||||
const FETCH_STATE_KEY = "twitter_fetch_state";
|
||||
|
||||
export interface FetchState {
|
||||
username: string;
|
||||
cursor: string;
|
||||
timeline: TimelineEntry[];
|
||||
accountInfo: AccountInfo | null;
|
||||
totalFetched: number;
|
||||
completed: boolean;
|
||||
lastUpdated: number;
|
||||
authToken: string; // Encrypted/hashed for security
|
||||
mediaType: string;
|
||||
retweets: boolean;
|
||||
timelineType: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save current fetch state for resume capability
|
||||
*/
|
||||
export function saveFetchState(state: Partial<FetchState> & { username: string }): void {
|
||||
try {
|
||||
const existing = getFetchState(state.username);
|
||||
const updated: FetchState = {
|
||||
username: state.username,
|
||||
cursor: state.cursor || "",
|
||||
timeline: state.timeline || existing?.timeline || [],
|
||||
accountInfo: state.accountInfo || existing?.accountInfo || null,
|
||||
totalFetched: state.totalFetched ?? existing?.totalFetched ?? 0,
|
||||
completed: state.completed ?? false,
|
||||
lastUpdated: Date.now(),
|
||||
authToken: state.authToken || existing?.authToken || "",
|
||||
mediaType: state.mediaType || existing?.mediaType || "all",
|
||||
retweets: state.retweets ?? existing?.retweets ?? false,
|
||||
timelineType: state.timelineType || existing?.timelineType || "timeline",
|
||||
};
|
||||
|
||||
// Get all states
|
||||
const allStates = getAllFetchStates();
|
||||
allStates[state.username.toLowerCase()] = updated;
|
||||
|
||||
localStorage.setItem(FETCH_STATE_KEY, JSON.stringify(allStates));
|
||||
} catch (error) {
|
||||
console.error("Failed to save fetch state:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fetch state for a specific username
|
||||
*/
|
||||
export function getFetchState(username: string): FetchState | null {
|
||||
try {
|
||||
const allStates = getAllFetchStates();
|
||||
return allStates[username.toLowerCase()] || null;
|
||||
} catch (error) {
|
||||
console.error("Failed to get fetch state:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all fetch states
|
||||
*/
|
||||
export function getAllFetchStates(): Record<string, FetchState> {
|
||||
try {
|
||||
const stored = localStorage.getItem(FETCH_STATE_KEY);
|
||||
if (stored) {
|
||||
return JSON.parse(stored);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to parse fetch states:", error);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there's a resumable fetch for username
|
||||
*/
|
||||
export function hasResumableFetch(username: string): boolean {
|
||||
const state = getFetchState(username);
|
||||
if (!state) return false;
|
||||
|
||||
// Has cursor and not completed
|
||||
return !!state.cursor && !state.completed && state.timeline.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get resumable fetch info for display
|
||||
*/
|
||||
export function getResumableInfo(username: string): {
|
||||
canResume: boolean;
|
||||
mediaCount: number;
|
||||
lastUpdated: Date | null;
|
||||
} {
|
||||
const state = getFetchState(username);
|
||||
if (!state || state.completed || !state.cursor) {
|
||||
return { canResume: false, mediaCount: 0, lastUpdated: null };
|
||||
}
|
||||
|
||||
return {
|
||||
canResume: true,
|
||||
mediaCount: state.timeline.length,
|
||||
lastUpdated: new Date(state.lastUpdated),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear fetch state for username
|
||||
*/
|
||||
export function clearFetchState(username: string): void {
|
||||
try {
|
||||
const allStates = getAllFetchStates();
|
||||
delete allStates[username.toLowerCase()];
|
||||
localStorage.setItem(FETCH_STATE_KEY, JSON.stringify(allStates));
|
||||
} catch (error) {
|
||||
console.error("Failed to clear fetch state:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all incomplete fetch states (cleanup)
|
||||
*/
|
||||
export function clearAllIncompleteFetchStates(): void {
|
||||
try {
|
||||
const allStates = getAllFetchStates();
|
||||
const completed: Record<string, FetchState> = {};
|
||||
|
||||
for (const [key, state] of Object.entries(allStates)) {
|
||||
if (state.completed) {
|
||||
completed[key] = state;
|
||||
}
|
||||
}
|
||||
|
||||
localStorage.setItem(FETCH_STATE_KEY, JSON.stringify(completed));
|
||||
} catch (error) {
|
||||
console.error("Failed to clear fetch states:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert fetch state to TwitterResponse format
|
||||
*/
|
||||
export function stateToResponse(state: FetchState): TwitterResponse | null {
|
||||
if (!state.accountInfo) return null;
|
||||
|
||||
return {
|
||||
account_info: state.accountInfo,
|
||||
total_urls: state.timeline.length,
|
||||
timeline: state.timeline,
|
||||
metadata: {
|
||||
new_entries: state.timeline.length,
|
||||
page: 0,
|
||||
batch_size: 0,
|
||||
has_more: !state.completed,
|
||||
cursor: state.cursor,
|
||||
completed: state.completed,
|
||||
},
|
||||
cursor: state.cursor,
|
||||
completed: state.completed,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge new timeline entries with existing (deduplicate by tweet_id)
|
||||
*/
|
||||
export function mergeTimelines(
|
||||
existing: TimelineEntry[],
|
||||
newEntries: TimelineEntry[]
|
||||
): TimelineEntry[] {
|
||||
const seenIds = new Set(existing.map((e) => e.tweet_id));
|
||||
const unique = newEntries.filter((e) => !seenIds.has(e.tweet_id));
|
||||
return [...existing, ...unique];
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
export type LogLevel = "info" | "success" | "warning" | "error" | "debug";
|
||||
|
||||
export interface LogEntry {
|
||||
timestamp: Date;
|
||||
level: LogLevel;
|
||||
message: string;
|
||||
}
|
||||
|
||||
class Logger {
|
||||
private logs: LogEntry[] = [];
|
||||
private maxLogs = 500;
|
||||
private listeners: Set<() => void> = new Set();
|
||||
|
||||
private addLog(level: LogLevel, message: string) {
|
||||
const entry: LogEntry = {
|
||||
timestamp: new Date(),
|
||||
level,
|
||||
message: message.toLowerCase(),
|
||||
};
|
||||
this.logs.push(entry);
|
||||
if (this.logs.length > this.maxLogs) {
|
||||
this.logs.shift();
|
||||
}
|
||||
this.notifyListeners();
|
||||
}
|
||||
|
||||
info(message: string) {
|
||||
this.addLog("info", message);
|
||||
}
|
||||
|
||||
success(message: string) {
|
||||
this.addLog("success", message);
|
||||
}
|
||||
|
||||
warning(message: string) {
|
||||
this.addLog("warning", message);
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
this.addLog("error", message);
|
||||
}
|
||||
|
||||
debug(message: string) {
|
||||
this.addLog("debug", message);
|
||||
}
|
||||
|
||||
getLogs(): LogEntry[] {
|
||||
return [...this.logs];
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.logs = [];
|
||||
this.notifyListeners();
|
||||
}
|
||||
|
||||
subscribe(listener: () => void) {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
private notifyListeners() {
|
||||
this.listeners.forEach((listener) => listener());
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = new Logger();
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Format a date to relative time string with max 2 units
|
||||
* e.g., "23 hours 32 minutes ago", "1 day 14 hours ago"
|
||||
*/
|
||||
export function formatRelativeTime(date: Date | string | number): string {
|
||||
const now = new Date();
|
||||
const target = new Date(date);
|
||||
const diffMs = now.getTime() - target.getTime();
|
||||
|
||||
if (diffMs < 0) return "just now";
|
||||
|
||||
const seconds = Math.floor(diffMs / 1000);
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const days = Math.floor(hours / 24);
|
||||
const weeks = Math.floor(days / 7);
|
||||
const months = Math.floor(days / 30);
|
||||
const years = Math.floor(days / 365);
|
||||
|
||||
const parts: string[] = [];
|
||||
|
||||
if (years > 0) {
|
||||
parts.push(`${years} ${years === 1 ? "year" : "years"}`);
|
||||
const remainingMonths = Math.floor((days % 365) / 30);
|
||||
if (remainingMonths > 0) {
|
||||
parts.push(`${remainingMonths} ${remainingMonths === 1 ? "month" : "months"}`);
|
||||
}
|
||||
} else if (months > 0) {
|
||||
parts.push(`${months} ${months === 1 ? "month" : "months"}`);
|
||||
const remainingDays = days % 30;
|
||||
if (remainingDays > 0) {
|
||||
parts.push(`${remainingDays} ${remainingDays === 1 ? "day" : "days"}`);
|
||||
}
|
||||
} else if (weeks > 0) {
|
||||
parts.push(`${weeks} ${weeks === 1 ? "week" : "weeks"}`);
|
||||
const remainingDays = days % 7;
|
||||
if (remainingDays > 0) {
|
||||
parts.push(`${remainingDays} ${remainingDays === 1 ? "day" : "days"}`);
|
||||
}
|
||||
} else if (days > 0) {
|
||||
parts.push(`${days} ${days === 1 ? "day" : "days"}`);
|
||||
const remainingHours = hours % 24;
|
||||
if (remainingHours > 0) {
|
||||
parts.push(`${remainingHours} ${remainingHours === 1 ? "hour" : "hours"}`);
|
||||
}
|
||||
} else if (hours > 0) {
|
||||
parts.push(`${hours} ${hours === 1 ? "hour" : "hours"}`);
|
||||
const remainingMinutes = minutes % 60;
|
||||
if (remainingMinutes > 0) {
|
||||
parts.push(`${remainingMinutes} ${remainingMinutes === 1 ? "minute" : "minutes"}`);
|
||||
}
|
||||
} else if (minutes > 0) {
|
||||
parts.push(`${minutes} ${minutes === 1 ? "minute" : "minutes"}`);
|
||||
} else {
|
||||
return "just now";
|
||||
}
|
||||
|
||||
return "Released " + parts.slice(0, 2).join(" ") + " ago";
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { GetDefaults } from "../../wailsjs/go/main/App";
|
||||
|
||||
export type FontFamily = "google-sans" | "inter" | "poppins" | "roboto" | "dm-sans" | "plus-jakarta-sans" | "manrope" | "space-grotesk";
|
||||
export type GifQuality = "fast" | "better";
|
||||
export type GifResolution = "original" | "high" | "medium" | "low";
|
||||
|
||||
export interface Settings {
|
||||
downloadPath: string;
|
||||
theme: string;
|
||||
themeMode: "auto" | "light" | "dark";
|
||||
fontFamily: FontFamily;
|
||||
sfxEnabled: boolean;
|
||||
gifQuality: GifQuality;
|
||||
gifResolution: GifResolution;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: Settings = {
|
||||
downloadPath: "",
|
||||
theme: "yellow",
|
||||
themeMode: "auto",
|
||||
fontFamily: "google-sans",
|
||||
sfxEnabled: true,
|
||||
gifQuality: "fast",
|
||||
gifResolution: "high",
|
||||
};
|
||||
|
||||
export const FONT_OPTIONS: { value: FontFamily; label: string; fontFamily: string }[] = [
|
||||
{ value: "google-sans", label: "Google Sans Flex", fontFamily: '"Google Sans Flex", system-ui, sans-serif' },
|
||||
{ value: "inter", label: "Inter", fontFamily: '"Inter", system-ui, sans-serif' },
|
||||
{ value: "poppins", label: "Poppins", fontFamily: '"Poppins", system-ui, sans-serif' },
|
||||
{ value: "roboto", label: "Roboto", fontFamily: '"Roboto", system-ui, sans-serif' },
|
||||
{ value: "dm-sans", label: "DM Sans", fontFamily: '"DM Sans", system-ui, sans-serif' },
|
||||
{ value: "plus-jakarta-sans", label: "Plus Jakarta Sans", fontFamily: '"Plus Jakarta Sans", system-ui, sans-serif' },
|
||||
{ value: "manrope", label: "Manrope", fontFamily: '"Manrope", system-ui, sans-serif' },
|
||||
{ value: "space-grotesk", label: "Space Grotesk", fontFamily: '"Space Grotesk", system-ui, sans-serif' },
|
||||
];
|
||||
|
||||
export function applyFont(fontFamily: FontFamily): void {
|
||||
const font = FONT_OPTIONS.find(f => f.value === fontFamily);
|
||||
if (font) {
|
||||
document.documentElement.style.setProperty('--font-sans', font.fontFamily);
|
||||
document.body.style.fontFamily = font.fontFamily;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchDefaultPath(): Promise<string> {
|
||||
try {
|
||||
const data = await GetDefaults();
|
||||
return data.downloadPath || "";
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch default path:", error);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
const SETTINGS_KEY = "twitter-media-downloader-settings";
|
||||
|
||||
export function getSettings(): Settings {
|
||||
try {
|
||||
const stored = localStorage.getItem(SETTINGS_KEY);
|
||||
if (stored) {
|
||||
const parsed = JSON.parse(stored);
|
||||
return { ...DEFAULT_SETTINGS, ...parsed };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to load settings:", error);
|
||||
}
|
||||
return DEFAULT_SETTINGS;
|
||||
}
|
||||
|
||||
export async function getSettingsWithDefaults(): Promise<Settings> {
|
||||
const settings = getSettings();
|
||||
|
||||
// If downloadPath is empty, fetch from backend
|
||||
if (!settings.downloadPath) {
|
||||
settings.downloadPath = await fetchDefaultPath();
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
export function saveSettings(settings: Settings): void {
|
||||
try {
|
||||
localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
|
||||
} catch (error) {
|
||||
console.error("Failed to save settings:", error);
|
||||
}
|
||||
}
|
||||
|
||||
export function updateSettings(partial: Partial<Settings>): Settings {
|
||||
const current = getSettings();
|
||||
const updated = { ...current, ...partial };
|
||||
saveSettings(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
export async function resetToDefaultSettings(): Promise<Settings> {
|
||||
const defaultPath = await fetchDefaultPath();
|
||||
const defaultSettings = {
|
||||
...DEFAULT_SETTINGS,
|
||||
downloadPath: defaultPath,
|
||||
};
|
||||
saveSettings(defaultSettings);
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
export function applyThemeMode(mode: "auto" | "light" | "dark"): void {
|
||||
if (mode === "auto") {
|
||||
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||
if (prefersDark) {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else {
|
||||
document.documentElement.classList.remove("dark");
|
||||
}
|
||||
} else if (mode === "dark") {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else {
|
||||
document.documentElement.classList.remove("dark");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,470 @@
|
||||
export interface Theme {
|
||||
name: string;
|
||||
label: string;
|
||||
cssVars: {
|
||||
light: Record<string, string>;
|
||||
dark: Record<string, string>;
|
||||
};
|
||||
}
|
||||
|
||||
export const themes: Theme[] = [
|
||||
{
|
||||
name: "neutral",
|
||||
label: "Default",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.145 0 0)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.145 0 0)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.145 0 0)",
|
||||
primary: "oklch(0.205 0 0)",
|
||||
"primary-foreground": "oklch(0.985 0 0)",
|
||||
secondary: "oklch(0.97 0 0)",
|
||||
"secondary-foreground": "oklch(0.205 0 0)",
|
||||
muted: "oklch(0.97 0 0)",
|
||||
"muted-foreground": "oklch(0.556 0 0)",
|
||||
accent: "oklch(0.97 0 0)",
|
||||
"accent-foreground": "oklch(0.205 0 0)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.922 0 0)",
|
||||
input: "oklch(0.922 0 0)",
|
||||
ring: "oklch(0.708 0 0)",
|
||||
"chart-1": "oklch(0.646 0.222 41.116)",
|
||||
"chart-2": "oklch(0.6 0.118 184.704)",
|
||||
"chart-3": "oklch(0.398 0.07 227.392)",
|
||||
"chart-4": "oklch(0.828 0.189 84.429)",
|
||||
"chart-5": "oklch(0.769 0.188 70.08)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.145 0 0)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.205 0 0)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.205 0 0)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.922 0 0)",
|
||||
"primary-foreground": "oklch(0.205 0 0)",
|
||||
secondary: "oklch(0.269 0 0)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.269 0 0)",
|
||||
"muted-foreground": "oklch(0.708 0 0)",
|
||||
accent: "oklch(0.269 0 0)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.556 0 0)",
|
||||
"chart-1": "oklch(0.488 0.243 264.376)",
|
||||
"chart-2": "oklch(0.696 0.17 162.48)",
|
||||
"chart-3": "oklch(0.769 0.188 70.08)",
|
||||
"chart-4": "oklch(0.627 0.265 303.9)",
|
||||
"chart-5": "oklch(0.645 0.246 16.439)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "blue",
|
||||
label: "Blue",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.488 0.243 264.376)",
|
||||
"primary-foreground": "oklch(0.97 0.014 254.604)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.708 0 0)",
|
||||
"chart-1": "oklch(0.809 0.105 251.813)",
|
||||
"chart-2": "oklch(0.623 0.214 259.815)",
|
||||
"chart-3": "oklch(0.546 0.245 262.881)",
|
||||
"chart-4": "oklch(0.488 0.243 264.376)",
|
||||
"chart-5": "oklch(0.424 0.199 265.638)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.488 0.243 264.376)",
|
||||
"primary-foreground": "oklch(0.97 0.014 254.604)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.556 0 0)",
|
||||
"chart-1": "oklch(0.809 0.105 251.813)",
|
||||
"chart-2": "oklch(0.623 0.214 259.815)",
|
||||
"chart-3": "oklch(0.546 0.245 262.881)",
|
||||
"chart-4": "oklch(0.488 0.243 264.376)",
|
||||
"chart-5": "oklch(0.424 0.199 265.638)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "green",
|
||||
label: "Green",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.648 0.2 131.684)",
|
||||
"primary-foreground": "oklch(0.986 0.031 120.757)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.841 0.238 128.85)",
|
||||
"chart-1": "oklch(0.871 0.15 154.449)",
|
||||
"chart-2": "oklch(0.723 0.219 149.579)",
|
||||
"chart-3": "oklch(0.627 0.194 149.214)",
|
||||
"chart-4": "oklch(0.527 0.154 150.069)",
|
||||
"chart-5": "oklch(0.448 0.119 151.328)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.648 0.2 131.684)",
|
||||
"primary-foreground": "oklch(0.986 0.031 120.757)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.405 0.101 131.063)",
|
||||
"chart-1": "oklch(0.871 0.15 154.449)",
|
||||
"chart-2": "oklch(0.723 0.219 149.579)",
|
||||
"chart-3": "oklch(0.627 0.194 149.214)",
|
||||
"chart-4": "oklch(0.527 0.154 150.069)",
|
||||
"chart-5": "oklch(0.448 0.119 151.328)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "orange",
|
||||
label: "Orange",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.646 0.222 41.116)",
|
||||
"primary-foreground": "oklch(0.98 0.016 73.684)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.75 0.183 55.934)",
|
||||
"chart-1": "oklch(0.837 0.128 66.29)",
|
||||
"chart-2": "oklch(0.705 0.213 47.604)",
|
||||
"chart-3": "oklch(0.646 0.222 41.116)",
|
||||
"chart-4": "oklch(0.553 0.195 38.402)",
|
||||
"chart-5": "oklch(0.47 0.157 37.304)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.705 0.213 47.604)",
|
||||
"primary-foreground": "oklch(0.98 0.016 73.684)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.408 0.123 38.172)",
|
||||
"chart-1": "oklch(0.837 0.128 66.29)",
|
||||
"chart-2": "oklch(0.705 0.213 47.604)",
|
||||
"chart-3": "oklch(0.646 0.222 41.116)",
|
||||
"chart-4": "oklch(0.553 0.195 38.402)",
|
||||
"chart-5": "oklch(0.47 0.157 37.304)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "red",
|
||||
label: "Red",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.577 0.245 27.325)",
|
||||
"primary-foreground": "oklch(0.971 0.013 17.38)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.704 0.191 22.216)",
|
||||
"chart-1": "oklch(0.808 0.114 19.571)",
|
||||
"chart-2": "oklch(0.637 0.237 25.331)",
|
||||
"chart-3": "oklch(0.577 0.245 27.325)",
|
||||
"chart-4": "oklch(0.505 0.213 27.518)",
|
||||
"chart-5": "oklch(0.444 0.177 26.899)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.637 0.237 25.331)",
|
||||
"primary-foreground": "oklch(0.971 0.013 17.38)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.396 0.141 25.723)",
|
||||
"chart-1": "oklch(0.808 0.114 19.571)",
|
||||
"chart-2": "oklch(0.637 0.237 25.331)",
|
||||
"chart-3": "oklch(0.577 0.245 27.325)",
|
||||
"chart-4": "oklch(0.505 0.213 27.518)",
|
||||
"chart-5": "oklch(0.444 0.177 26.899)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "rose",
|
||||
label: "Rose",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.586 0.253 17.585)",
|
||||
"primary-foreground": "oklch(0.969 0.015 12.422)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.712 0.194 13.428)",
|
||||
"chart-1": "oklch(0.81 0.117 11.638)",
|
||||
"chart-2": "oklch(0.645 0.246 16.439)",
|
||||
"chart-3": "oklch(0.586 0.253 17.585)",
|
||||
"chart-4": "oklch(0.514 0.222 16.935)",
|
||||
"chart-5": "oklch(0.455 0.188 13.697)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.645 0.246 16.439)",
|
||||
"primary-foreground": "oklch(0.969 0.015 12.422)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.41 0.159 10.272)",
|
||||
"chart-1": "oklch(0.81 0.117 11.638)",
|
||||
"chart-2": "oklch(0.645 0.246 16.439)",
|
||||
"chart-3": "oklch(0.586 0.253 17.585)",
|
||||
"chart-4": "oklch(0.514 0.222 16.935)",
|
||||
"chart-5": "oklch(0.455 0.188 13.697)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "violet",
|
||||
label: "Violet",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.541 0.281 293.009)",
|
||||
"primary-foreground": "oklch(0.969 0.016 293.756)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.702 0.183 293.541)",
|
||||
"chart-1": "oklch(0.811 0.111 293.571)",
|
||||
"chart-2": "oklch(0.606 0.25 292.717)",
|
||||
"chart-3": "oklch(0.541 0.281 293.009)",
|
||||
"chart-4": "oklch(0.491 0.27 292.581)",
|
||||
"chart-5": "oklch(0.432 0.232 292.759)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.606 0.25 292.717)",
|
||||
"primary-foreground": "oklch(0.969 0.016 293.756)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.38 0.189 293.745)",
|
||||
"chart-1": "oklch(0.811 0.111 293.571)",
|
||||
"chart-2": "oklch(0.606 0.25 292.717)",
|
||||
"chart-3": "oklch(0.541 0.281 293.009)",
|
||||
"chart-4": "oklch(0.491 0.27 292.581)",
|
||||
"chart-5": "oklch(0.432 0.232 292.759)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "yellow",
|
||||
label: "Yellow",
|
||||
cssVars: {
|
||||
light: {
|
||||
background: "oklch(1 0 0)",
|
||||
foreground: "oklch(0.141 0.005 285.823)",
|
||||
card: "oklch(1 0 0)",
|
||||
"card-foreground": "oklch(0.141 0.005 285.823)",
|
||||
popover: "oklch(1 0 0)",
|
||||
"popover-foreground": "oklch(0.141 0.005 285.823)",
|
||||
primary: "oklch(0.852 0.199 91.936)",
|
||||
"primary-foreground": "oklch(0.421 0.095 57.708)",
|
||||
secondary: "oklch(0.967 0.001 286.375)",
|
||||
"secondary-foreground": "oklch(0.21 0.006 285.885)",
|
||||
muted: "oklch(0.967 0.001 286.375)",
|
||||
"muted-foreground": "oklch(0.552 0.016 285.938)",
|
||||
accent: "oklch(0.967 0.001 286.375)",
|
||||
"accent-foreground": "oklch(0.21 0.006 285.885)",
|
||||
destructive: "oklch(0.577 0.245 27.325)",
|
||||
border: "oklch(0.92 0.004 286.32)",
|
||||
input: "oklch(0.92 0.004 286.32)",
|
||||
ring: "oklch(0.852 0.199 91.936)",
|
||||
"chart-1": "oklch(0.905 0.182 98.111)",
|
||||
"chart-2": "oklch(0.795 0.184 86.047)",
|
||||
"chart-3": "oklch(0.681 0.162 75.834)",
|
||||
"chart-4": "oklch(0.554 0.135 66.442)",
|
||||
"chart-5": "oklch(0.476 0.114 61.907)",
|
||||
},
|
||||
dark: {
|
||||
background: "oklch(0.141 0.005 285.823)",
|
||||
foreground: "oklch(0.985 0 0)",
|
||||
card: "oklch(0.21 0.006 285.885)",
|
||||
"card-foreground": "oklch(0.985 0 0)",
|
||||
popover: "oklch(0.21 0.006 285.885)",
|
||||
"popover-foreground": "oklch(0.985 0 0)",
|
||||
primary: "oklch(0.795 0.184 86.047)",
|
||||
"primary-foreground": "oklch(0.421 0.095 57.708)",
|
||||
secondary: "oklch(0.274 0.006 286.033)",
|
||||
"secondary-foreground": "oklch(0.985 0 0)",
|
||||
muted: "oklch(0.274 0.006 286.033)",
|
||||
"muted-foreground": "oklch(0.705 0.015 286.067)",
|
||||
accent: "oklch(0.274 0.006 286.033)",
|
||||
"accent-foreground": "oklch(0.985 0 0)",
|
||||
destructive: "oklch(0.704 0.191 22.216)",
|
||||
border: "oklch(1 0 0 / 10%)",
|
||||
input: "oklch(1 0 0 / 15%)",
|
||||
ring: "oklch(0.421 0.095 57.708)",
|
||||
"chart-1": "oklch(0.905 0.182 98.111)",
|
||||
"chart-2": "oklch(0.795 0.184 86.047)",
|
||||
"chart-3": "oklch(0.681 0.162 75.834)",
|
||||
"chart-4": "oklch(0.554 0.135 66.442)",
|
||||
"chart-5": "oklch(0.476 0.114 61.907)",
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export function applyTheme(themeName: string) {
|
||||
const theme = themes.find((t) => t.name === themeName) || themes[0];
|
||||
const root = document.documentElement;
|
||||
const isDark = root.classList.contains("dark");
|
||||
const vars = isDark ? theme.cssVars.dark : theme.cssVars.light;
|
||||
|
||||
Object.entries(vars).forEach(([key, value]) => {
|
||||
root.style.setProperty(`--${key}`, value);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
playSuccessSound,
|
||||
playErrorSound,
|
||||
playWarningSound,
|
||||
playInfoSound,
|
||||
} from "./audio";
|
||||
import { logger } from "./logger";
|
||||
import { getSettings } from "./settings";
|
||||
|
||||
const toastStyle = {
|
||||
className: "font-mono lowercase",
|
||||
};
|
||||
|
||||
// Helper to check if SFX is enabled
|
||||
const isSfxEnabled = () => getSettings().sfxEnabled;
|
||||
|
||||
// Wrapper functions for toast with sound effects
|
||||
export const toastWithSound = {
|
||||
success: (message: string, data?: any) => {
|
||||
const msg = message.toLowerCase();
|
||||
logger.success(msg);
|
||||
if (isSfxEnabled()) playSuccessSound();
|
||||
return toast.success(msg, { ...toastStyle, ...data });
|
||||
},
|
||||
|
||||
error: (message: string, data?: any) => {
|
||||
const msg = message.toLowerCase();
|
||||
logger.error(msg);
|
||||
if (isSfxEnabled()) playErrorSound();
|
||||
return toast.error(msg, { ...toastStyle, ...data });
|
||||
},
|
||||
|
||||
warning: (message: string, data?: any) => {
|
||||
const msg = message.toLowerCase();
|
||||
logger.warning(msg);
|
||||
if (isSfxEnabled()) playWarningSound();
|
||||
return toast.warning(msg, { ...toastStyle, ...data });
|
||||
},
|
||||
|
||||
info: (message: string, data?: any) => {
|
||||
const msg = message.toLowerCase();
|
||||
logger.info(msg);
|
||||
if (isSfxEnabled()) playInfoSound();
|
||||
return toast.info(msg, { ...toastStyle, ...data });
|
||||
},
|
||||
|
||||
// Default toast without specific type
|
||||
message: (message: string, data?: any) => {
|
||||
const msg = message.toLowerCase();
|
||||
logger.info(msg);
|
||||
if (isSfxEnabled()) playInfoSound();
|
||||
return toast(msg, { ...toastStyle, ...data });
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { BrowserOpenURL } from "../../wailsjs/runtime/runtime"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
|
||||
export function sanitizePath(input: string, os: string): string {
|
||||
if (os === "Windows") {
|
||||
return input.replace(/[<>:"/\\|?*]/g, "_");
|
||||
}
|
||||
return input.replace(/\//g, "_");
|
||||
}
|
||||
|
||||
export function joinPath(os: string, ...parts: string[]): string {
|
||||
const sep = os === "Windows" ? "\\" : "/";
|
||||
|
||||
const filtered = parts.filter(Boolean);
|
||||
if (filtered.length === 0) return "";
|
||||
|
||||
const joined = filtered
|
||||
.map((p, i) => {
|
||||
if (i === 0) {
|
||||
return p.replace(/[/\\]+$/g, "");
|
||||
}
|
||||
return p.replace(/^[/\\]+|[/\\]+$/g, "");
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join(sep);
|
||||
|
||||
return joined;
|
||||
}
|
||||
|
||||
export function openExternal(url: string) {
|
||||
if (!url) return;
|
||||
try {
|
||||
BrowserOpenURL(url);
|
||||
} catch (error) {
|
||||
if (typeof window !== "undefined") {
|
||||
window.open(url, "_blank", "noopener,noreferrer");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { StrictMode } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import "./index.css";
|
||||
import App from "./App.tsx";
|
||||
import { Toaster } from "@/components/ui/sonner";
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
<Toaster position="bottom-left" duration={1000} />
|
||||
</StrictMode>
|
||||
);
|
||||
@@ -0,0 +1,76 @@
|
||||
// Twitter/X Media Batch Downloader Types
|
||||
|
||||
export interface AccountInfo {
|
||||
name: string; // username/handle from extractor
|
||||
nick: string; // display name from extractor
|
||||
date: string;
|
||||
followers_count: number;
|
||||
friends_count: number;
|
||||
profile_image: string;
|
||||
statuses_count: number;
|
||||
}
|
||||
|
||||
export interface TimelineEntry {
|
||||
url: string;
|
||||
date: string;
|
||||
tweet_id: string;
|
||||
type: string; // photo, video, gif, text
|
||||
is_retweet: boolean;
|
||||
extension: string;
|
||||
width: number;
|
||||
height: number;
|
||||
content?: string;
|
||||
view_count?: number;
|
||||
bookmark_count?: number;
|
||||
favorite_count?: number;
|
||||
retweet_count?: number;
|
||||
reply_count?: number;
|
||||
source?: string;
|
||||
verified?: boolean;
|
||||
}
|
||||
|
||||
export interface ExtractMetadata {
|
||||
new_entries: number;
|
||||
page: number;
|
||||
batch_size: number;
|
||||
has_more: boolean;
|
||||
cursor?: string; // Cursor for resume capability
|
||||
completed?: boolean; // True if all media fetched
|
||||
}
|
||||
|
||||
export interface TwitterResponse {
|
||||
account_info: AccountInfo;
|
||||
total_urls: number;
|
||||
timeline: TimelineEntry[];
|
||||
metadata: ExtractMetadata;
|
||||
cursor?: string; // Cursor for next fetch (from CLI)
|
||||
completed?: boolean; // True if fetch completed
|
||||
}
|
||||
|
||||
export interface TimelineRequest {
|
||||
username: string;
|
||||
auth_token: string;
|
||||
timeline_type: string; // media, timeline, tweets, with_replies, likes
|
||||
batch_size: number;
|
||||
page: number;
|
||||
media_type: string; // all, image, video, gif
|
||||
retweets: boolean;
|
||||
cursor?: string; // Resume from this cursor position
|
||||
}
|
||||
|
||||
export interface DateRangeRequest {
|
||||
username: string;
|
||||
auth_token: string;
|
||||
start_date: string; // YYYY-MM-DD
|
||||
end_date: string; // YYYY-MM-DD
|
||||
media_filter: string;
|
||||
retweets: boolean;
|
||||
}
|
||||
|
||||
// Settings types
|
||||
export interface Settings {
|
||||
downloadPath: string;
|
||||
theme: string;
|
||||
themeMode: "auto" | "light" | "dark";
|
||||
sfxEnabled: boolean;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||
"target": "ES2022",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"types": ["vite/client"],
|
||||
"skipLibCheck": true,
|
||||
|
||||
/* Bundler mode */
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx",
|
||||
|
||||
/* Linting */
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"erasableSyntaxOnly": false,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedSideEffectImports": true,
|
||||
|
||||
/* Path Mapping */
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": [
|
||||
"./src/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"files": [],
|
||||
"references": [
|
||||
{ "path": "./tsconfig.app.json" },
|
||||
{ "path": "./tsconfig.node.json" }
|
||||
],
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
||||
"target": "ES2023",
|
||||
"lib": ["ES2023"],
|
||||
"module": "ESNext",
|
||||
"types": ["node"],
|
||||
"skipLibCheck": true,
|
||||
|
||||
/* Bundler mode */
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
|
||||
/* Linting */
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"erasableSyntaxOnly": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedSideEffectImports": true
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import path from "path"
|
||||
import tailwindcss from "@tailwindcss/vite"
|
||||
import react from "@vitejs/plugin-react"
|
||||
import { defineConfig } from "vite"
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react(), tailwindcss()],
|
||||
resolve: {
|
||||
alias: {
|
||||
"@": path.resolve(__dirname, "./src"),
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
module twitterxmediabatchdownloader
|
||||
|
||||
go 1.25.4
|
||||
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v1.14.32
|
||||
github.com/ulikunitz/xz v0.5.15
|
||||
github.com/wailsapp/wails/v2 v2.11.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bep/debounce v1.2.1 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/godbus/dbus/v5 v5.2.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/jchv/go-winloader v0.0.0-20250406163304-c1995be93bd1 // indirect
|
||||
github.com/labstack/echo/v4 v4.13.4 // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/leaanthony/go-ansi-parser v1.6.1 // indirect
|
||||
github.com/leaanthony/gosod v1.0.4 // indirect
|
||||
github.com/leaanthony/slicer v1.6.0 // indirect
|
||||
github.com/leaanthony/u v1.1.1 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/samber/lo v1.52.0 // indirect
|
||||
github.com/tkrajina/go-reflector v0.5.8 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
github.com/wailsapp/go-webview2 v1.0.23 // indirect
|
||||
github.com/wailsapp/mimetype v1.4.1 // indirect
|
||||
golang.org/x/crypto v0.45.0 // indirect
|
||||
golang.org/x/net v0.47.0 // indirect
|
||||
golang.org/x/sys v0.38.0 // indirect
|
||||
golang.org/x/text v0.31.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,83 @@
|
||||
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
|
||||
github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
|
||||
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
|
||||
github.com/godbus/dbus/v5 v5.2.0 h1:3WexO+U+yg9T70v9FdHr9kCxYlazaAXUhx2VMkbfax8=
|
||||
github.com/godbus/dbus/v5 v5.2.0/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jchv/go-winloader v0.0.0-20250406163304-c1995be93bd1 h1:njuLRcjAuMKr7kI3D85AXWkw6/+v9PwtV6M6o11sWHQ=
|
||||
github.com/jchv/go-winloader v0.0.0-20250406163304-c1995be93bd1/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
|
||||
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
|
||||
github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||
github.com/leaanthony/debme v1.2.1 h1:9Tgwf+kjcrbMQ4WnPcEIUcQuIZYqdWftzZkBr+i/oOc=
|
||||
github.com/leaanthony/debme v1.2.1/go.mod h1:3V+sCm5tYAgQymvSOfYQ5Xx2JCr+OXiD9Jkw3otUjiA=
|
||||
github.com/leaanthony/go-ansi-parser v1.6.1 h1:xd8bzARK3dErqkPFtoF9F3/HgN8UQk0ed1YDKpEz01A=
|
||||
github.com/leaanthony/go-ansi-parser v1.6.1/go.mod h1:+vva/2y4alzVmmIEpk9QDhA7vLC5zKDTRwfZGOp3IWU=
|
||||
github.com/leaanthony/gosod v1.0.4 h1:YLAbVyd591MRffDgxUOU1NwLhT9T1/YiwjKZpkNFeaI=
|
||||
github.com/leaanthony/gosod v1.0.4/go.mod h1:GKuIL0zzPj3O1SdWQOdgURSuhkF+Urizzxh26t9f1cw=
|
||||
github.com/leaanthony/slicer v1.6.0 h1:1RFP5uiPJvT93TAHi+ipd3NACobkW53yUiBqZheE/Js=
|
||||
github.com/leaanthony/slicer v1.6.0/go.mod h1:o/Iz29g7LN0GqH3aMjWAe90381nyZlDNquK+mtH2Fj8=
|
||||
github.com/leaanthony/u v1.1.1 h1:TUFjwDGlNX+WuwVEzDqQwC2lOv0P4uhTQw7CMFdiK7M=
|
||||
github.com/leaanthony/u v1.1.1/go.mod h1:9+o6hejoRljvZ3BzdYlVL0JYCwtnAsVuN9pVTQcaRfI=
|
||||
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
|
||||
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
|
||||
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
|
||||
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
|
||||
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tkrajina/go-reflector v0.5.8 h1:yPADHrwmUbMq4RGEyaOUpz2H90sRsETNVpjzo3DLVQQ=
|
||||
github.com/tkrajina/go-reflector v0.5.8/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4=
|
||||
github.com/ulikunitz/xz v0.5.15 h1:9DNdB5s+SgV3bQ2ApL10xRc35ck0DuIX/isZvIk+ubY=
|
||||
github.com/ulikunitz/xz v0.5.15/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/wailsapp/go-webview2 v1.0.23 h1:jmv8qhz1lHibCc79bMM/a/FqOnnzOGEisLav+a0b9P0=
|
||||
github.com/wailsapp/go-webview2 v1.0.23/go.mod h1:qJmWAmAmaniuKGZPWwne+uor3AHMB5PFhqiK0Bbj8kc=
|
||||
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
|
||||
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
|
||||
github.com/wailsapp/wails/v2 v2.11.0 h1:seLacV8pqupq32IjS4Y7V8ucab0WZwtK6VvUVxSBtqQ=
|
||||
github.com/wailsapp/wails/v2 v2.11.0/go.mod h1:jrf0ZaM6+GBc1wRmXsM8cIvzlg0karYin3erahI4+0k=
|
||||
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
|
||||
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
|
||||
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
||||
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
||||
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
|
||||
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,62 @@
|
||||
# Building Extractor Binary
|
||||
|
||||
This guide explains how to build the `extractor` binary from `twitter_cli.py`.
|
||||
|
||||
## Quick Start - Use Pre-built Binary
|
||||
|
||||
**You don't need to build the binary yourself!** After running the main application once, the pre-built binary is automatically extracted to:
|
||||
|
||||
```
|
||||
Windows: C:\Users\YourUsername\.twitterxmediabatchdownloader\extractor.exe
|
||||
Linux: ~/.twitterxmediabatchdownloader/extractor
|
||||
macOS: ~/.twitterxmediabatchdownloader/extractor
|
||||
```
|
||||
|
||||
You can use this binary directly for CLI operations without rebuilding.
|
||||
|
||||
### Using the Pre-built Binary
|
||||
|
||||
```powershell
|
||||
# Windows
|
||||
C:\Users\YourUsername\.twitterxmediabatchdownloader\extractor.exe https://x.com/username/media --limit 10 --json --guest
|
||||
|
||||
# Linux/macOS
|
||||
~/.twitterxmediabatchdownloader/extractor https://x.com/username/media --limit 10 --json --guest
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Building from Source (Optional)
|
||||
|
||||
If you need to rebuild the binary (e.g., after modifying `twitter_cli.py`):
|
||||
|
||||
### Prerequisites
|
||||
|
||||
```bash
|
||||
pip install pyinstaller
|
||||
# or
|
||||
pip install nuitka
|
||||
```
|
||||
|
||||
### Build Commands
|
||||
|
||||
```bash
|
||||
# PyInstaller (faster build, larger file)
|
||||
pyinstaller --onefile --name extractor --collect-all gallery_dl twitter_cli.py
|
||||
|
||||
# Nuitka (slower build, better performance, smaller file)
|
||||
python -m nuitka --onefile --output-filename=extractor.exe --include-package=gallery_dl --include-package-data=gallery_dl twitter_cli.py
|
||||
```
|
||||
|
||||
### Output Location
|
||||
|
||||
- PyInstaller: `dist/extractor` or `dist/extractor.exe`
|
||||
- Nuitka: `extractor` or `extractor.exe` (in current directory)
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
- The main application automatically manages the binary (extracts, updates, and uses it)
|
||||
- Manual building is only needed if you're developing or customizing the extractor
|
||||
- The pre-built binary is kept in sync with the application version
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
# twitter_cli.py — Usage Guide
|
||||
|
||||
CLI tool for extracting media and metadata from Twitter/X using gallery-dl extractor.
|
||||
|
||||
## Quickstart
|
||||
|
||||
1. **Set up environment**
|
||||
```powershell
|
||||
cd helper
|
||||
python -m venv .venv
|
||||
.\.venv\Scripts\Activate.ps1
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
2. **Verify gallery-dl**
|
||||
```powershell
|
||||
python -c "import gallery_dl; print(gallery_dl.__version__)"
|
||||
```
|
||||
|
||||
3. **Quick test** (using demo account):
|
||||
```powershell
|
||||
python twitter_cli.py https://x.com/xbatchdemo/media --limit 1 --json --guest
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Command Line Arguments
|
||||
|
||||
### Required
|
||||
- `url` — Twitter/X URL (timeline, media, likes, search, etc.)
|
||||
|
||||
### Authentication
|
||||
- `--auth-token TOKEN` — Your auth_token cookie value
|
||||
- `--guest` — Force guest mode (no authentication)
|
||||
|
||||
### Content Options
|
||||
- `--retweets {skip|include|original}` — Control retweets (default: skip)
|
||||
- `--no-videos` — Skip video downloads
|
||||
- `--text-tweets` — Include tweets without media
|
||||
- `--type {photo|video|animated_gif|all}` — Filter by media type (default: all)
|
||||
|
||||
### Quality & Format
|
||||
- `--size SIZE` — Image size: `orig`, `4096x4096`, `large`, `medium`, `small` (default: orig)
|
||||
|
||||
### Fetch Control
|
||||
- `--limit N` — Maximum media items to fetch (0 = unlimited)
|
||||
- `--cursor CURSOR` — Resume from specific cursor position
|
||||
|
||||
### Output Options
|
||||
- `--json` — Output results as JSON
|
||||
- `--metadata` — Include tweet metadata (author, counts, etc.)
|
||||
- `--output FILE` / `-o FILE` — Save to JSON file with resume capability
|
||||
- `--resume FILE` / `-r FILE` — Resume from previous JSON file
|
||||
- `--progress` — Show progress during fetch
|
||||
|
||||
### Advanced
|
||||
- `--set KEY=VALUE` — Set gallery-dl extractor options (repeatable)
|
||||
- `-v` / `--verbose` — Show detailed metadata during fetch
|
||||
|
||||
---
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Fetch
|
||||
|
||||
```powershell
|
||||
# Guest mode (no auth)
|
||||
python twitter_cli.py https://x.com/username/media --limit 10 --json --guest
|
||||
|
||||
# With authentication
|
||||
python twitter_cli.py https://x.com/username/media --limit 10 --json --auth-token YOUR_TOKEN
|
||||
```
|
||||
|
||||
### Media Type Filtering
|
||||
|
||||
```powershell
|
||||
# Photos only
|
||||
python twitter_cli.py https://x.com/username/media --type photo --json --guest
|
||||
|
||||
# Videos only
|
||||
python twitter_cli.py https://x.com/username/media --type video --json --guest
|
||||
|
||||
# Animated GIFs only
|
||||
python twitter_cli.py https://x.com/username/media --type animated_gif --json --guest
|
||||
```
|
||||
|
||||
### With Metadata
|
||||
|
||||
```powershell
|
||||
# Include tweet metadata (author, date, counts)
|
||||
python twitter_cli.py https://x.com/username/media --limit 5 --json --metadata --guest
|
||||
```
|
||||
|
||||
### Retweets Control
|
||||
|
||||
```powershell
|
||||
# Skip retweets (default)
|
||||
python twitter_cli.py https://x.com/username/timeline --retweets skip --json --guest
|
||||
|
||||
# Include retweets
|
||||
python twitter_cli.py https://x.com/username/timeline --retweets include --json --guest
|
||||
|
||||
# Get original tweet from retweets
|
||||
python twitter_cli.py https://x.com/username/timeline --retweets original --json --guest
|
||||
```
|
||||
|
||||
### Save & Resume
|
||||
|
||||
```powershell
|
||||
# Save to file (with resume capability)
|
||||
python twitter_cli.py https://x.com/username/media --limit 100 --output results.json --progress --guest
|
||||
|
||||
# Resume from saved file
|
||||
python twitter_cli.py https://x.com/username/media --resume results.json --limit 100 --output results.json --guest
|
||||
|
||||
# Fetch all remaining
|
||||
python twitter_cli.py https://x.com/username/media --resume results.json --limit 0 --output results.json --guest
|
||||
```
|
||||
|
||||
### Private Content (Requires Auth)
|
||||
|
||||
```powershell
|
||||
# Your likes
|
||||
python twitter_cli.py https://x.com/username/likes --limit 20 --json --auth-token YOUR_TOKEN
|
||||
|
||||
# Your bookmarks
|
||||
python twitter_cli.py https://x.com/i/bookmarks --limit 20 --json --auth-token YOUR_TOKEN
|
||||
```
|
||||
|
||||
### Search
|
||||
|
||||
```powershell
|
||||
# Search with filters
|
||||
python twitter_cli.py "https://x.com/search?q=from:username filter:media" --limit 50 --json --guest
|
||||
|
||||
# Date range search
|
||||
python twitter_cli.py "https://x.com/search?q=from:username since:2024-01-01 until:2024-12-31" --json --guest
|
||||
```
|
||||
|
||||
### Advanced Filtering
|
||||
|
||||
```powershell
|
||||
# Filter by date (2024 only)
|
||||
python twitter_cli.py https://x.com/username/media --json --metadata --set filter="date.year == 2024" --guest
|
||||
|
||||
# Filter by engagement (>1000 likes)
|
||||
python twitter_cli.py https://x.com/username/media --json --metadata --set filter="favorite_count > 1000" --guest
|
||||
|
||||
# Combine filters (photos from 2024 with >100 likes)
|
||||
python twitter_cli.py https://x.com/username/media --type photo --json --metadata --set filter="date.year == 2024 and favorite_count > 100" --guest
|
||||
```
|
||||
|
||||
### Text-Only Tweets
|
||||
|
||||
```powershell
|
||||
# Fetch tweets without media
|
||||
python twitter_cli.py https://x.com/username/tweets --text-tweets --json --metadata --guest
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Output Format
|
||||
|
||||
### JSON Structure
|
||||
|
||||
```json
|
||||
{
|
||||
"media": [
|
||||
{
|
||||
"url": "https://pbs.twimg.com/media/...",
|
||||
"tweet_id": 1234567890,
|
||||
"extension": "jpg",
|
||||
"width": 2048,
|
||||
"height": 1536,
|
||||
"type": "photo"
|
||||
}
|
||||
],
|
||||
"metadata": [
|
||||
{
|
||||
"tweet_id": 1234567890,
|
||||
"date": "2024-01-15T10:30:00+00:00",
|
||||
"author": {
|
||||
"id": 123456,
|
||||
"name": "username",
|
||||
"nick": "Display Name"
|
||||
},
|
||||
"content": "Tweet text content",
|
||||
"favorite_count": 150,
|
||||
"retweet_count": 25,
|
||||
"reply_count": 5,
|
||||
"view_count": 5000
|
||||
}
|
||||
],
|
||||
"cursor": "1/DAABCgABG7OZH1D___0KAAIW...",
|
||||
"total": 100,
|
||||
"completed": false
|
||||
}
|
||||
```
|
||||
|
||||
### Resume File Structure
|
||||
|
||||
When using `--output`, the file includes:
|
||||
- `url` — Original URL
|
||||
- `cursor` — Position for resume
|
||||
- `total` — Total items fetched
|
||||
- `completed` — Whether fetch is complete
|
||||
- `media` — All media items
|
||||
- `metadata` — All metadata (if requested)
|
||||
|
||||
---
|
||||
|
||||
## Pagination & Resume
|
||||
|
||||
### How It Works
|
||||
|
||||
- Twitter API returns ~100-200 items per batch
|
||||
- Cursor tracks position for next fetch
|
||||
- System ensures cursor availability before stopping
|
||||
- Resume continues from exact position (no duplicates)
|
||||
|
||||
### Resume Workflow
|
||||
|
||||
```powershell
|
||||
# Step 1: Initial fetch (saves state)
|
||||
python twitter_cli.py https://x.com/user/media --limit 100 --output data.json --progress --guest
|
||||
# Output: Saved 202 media to data.json [partial (can resume)]
|
||||
|
||||
# Step 2: Resume and fetch more
|
||||
python twitter_cli.py https://x.com/user/media --resume data.json --limit 100 --output data.json --guest
|
||||
# Output: Resuming from cursor, 202 media already fetched
|
||||
# Added 100 new media (total: 302)
|
||||
|
||||
# Step 3: Fetch all remaining
|
||||
python twitter_cli.py https://x.com/user/media --resume data.json --limit 0 --output data.json --guest
|
||||
# Output: Added 244 new media (total: 546) [completed]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Supported URL Patterns
|
||||
|
||||
### User Timelines
|
||||
- `https://x.com/username/media` — Media timeline
|
||||
- `https://x.com/username/timeline` — Full timeline
|
||||
- `https://x.com/username/tweets` — Tweets only
|
||||
- `https://x.com/username/with_replies` — With replies
|
||||
- `https://x.com/username/likes` — Liked tweets (requires auth)
|
||||
|
||||
### Private Content
|
||||
- `https://x.com/i/bookmarks` — Your bookmarks (requires auth)
|
||||
|
||||
### Search
|
||||
- `https://x.com/search?q=QUERY` — Search tweets
|
||||
- `https://x.com/hashtag/NAME` — Hashtag search
|
||||
|
||||
### Individual Tweet
|
||||
- `https://x.com/username/status/1234567890` — Single tweet
|
||||
|
||||
---
|
||||
|
||||
## Search Operators
|
||||
|
||||
Use in search URLs:
|
||||
|
||||
| Operator | Example | Description |
|
||||
|----------|---------|-------------|
|
||||
| `from:USER` | `from:username` | Tweets from user |
|
||||
| `to:USER` | `to:username` | Tweets mentioning user |
|
||||
| `filter:media` | `cats filter:media` | Only with media |
|
||||
| `filter:images` | `cats filter:images` | Only images |
|
||||
| `filter:videos` | `cats filter:videos` | Only videos |
|
||||
| `since:DATE` | `since:2024-01-01` | After date |
|
||||
| `until:DATE` | `until:2024-12-31` | Before date |
|
||||
| `-filter:retweets` | `cats -filter:retweets` | Exclude retweets |
|
||||
| `min_faves:N` | `min_faves:1000` | Min likes |
|
||||
| `min_retweets:N` | `min_retweets:100` | Min retweets |
|
||||
|
||||
---
|
||||
|
||||
## Authentication
|
||||
|
||||
### Getting Your Auth Token
|
||||
|
||||
1. Open Twitter/X in your browser
|
||||
2. Open Developer Tools (F12)
|
||||
3. Go to Application/Storage → Cookies
|
||||
4. Find cookie named `auth_token`
|
||||
5. Copy its value
|
||||
|
||||
### Security Notes
|
||||
|
||||
- Use a dummy/secondary account for scraping
|
||||
- Don't share your auth token
|
||||
- Excessive usage may cause account suspension
|
||||
- Guest mode has stricter rate limits
|
||||
|
||||
---
|
||||
|
||||
## Rate Limits
|
||||
|
||||
- **Guest mode**: Lower limits, may hit rate limit faster
|
||||
- **Authenticated**: Higher limits but still restricted
|
||||
- **Rate limit hit**: Script will wait and retry automatically
|
||||
- **Best practice**: Use `--limit` to control fetch size
|
||||
|
||||
---
|
||||
|
||||
## Tips & Tricks
|
||||
|
||||
### 1. Progressive Fetch with Resume
|
||||
```powershell
|
||||
# Fetch in batches to avoid long waits
|
||||
python twitter_cli.py URL --limit 200 --output data.json --progress --guest
|
||||
python twitter_cli.py URL --resume data.json --limit 200 --output data.json --guest
|
||||
```
|
||||
|
||||
### 2. Combine Type Filter with Custom Filter
|
||||
```powershell
|
||||
# Photos from 2024 with high engagement
|
||||
python twitter_cli.py URL --type photo --metadata --set filter="date.year == 2024 and favorite_count > 500" --json --guest
|
||||
```
|
||||
|
||||
### 3. Use Progress Flag for Long Fetches
|
||||
```powershell
|
||||
python twitter_cli.py URL --limit 1000 --progress --output data.json --guest
|
||||
```
|
||||
|
||||
### 4. Check Cursor Availability
|
||||
- If `completed: false` in output → can resume
|
||||
- If `completed: true` → all data fetched
|
||||
- If `cursor: null` → no more data available
|
||||
@@ -0,0 +1,309 @@
|
||||
"""Command-line helper for gallery-dl's Twitter extractor.
|
||||
|
||||
Features:
|
||||
- Cursor-based resume: Use --output to save progress, --resume to continue
|
||||
- Progress tracking: See fetch progress in real-time
|
||||
- Rate limit: Automatically handled by gallery-dl
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from twitter_common import ( # type: ignore
|
||||
TwitterRequest,
|
||||
coerce_literal,
|
||||
load_resume_state,
|
||||
merge_options,
|
||||
run_request_dict,
|
||||
save_state,
|
||||
)
|
||||
|
||||
DEFAULT_AUTH_TOKEN = ""
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Light-weight CLI wrapper around gallery-dl's Twitter extractor",
|
||||
)
|
||||
parser.add_argument("url", help="Any supported Twitter/X URL (timeline, media, likes, ...)")
|
||||
parser.add_argument(
|
||||
"--auth-token",
|
||||
default=DEFAULT_AUTH_TOKEN,
|
||||
help="Value of the auth_token cookie (leave empty to skip)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--guest",
|
||||
action="store_true",
|
||||
help="Force guest mode (do not send auth_token)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--retweets",
|
||||
choices=["skip", "include", "original"],
|
||||
default="skip",
|
||||
help="Control retweet handling (skip, include, or swap for originals)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-videos",
|
||||
action="store_true",
|
||||
help="Skip video/animated GIF variants",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--size",
|
||||
default="orig",
|
||||
help="Image size to request (orig, 4096x4096, large, ...)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--limit",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Stop after N media files (0 = all)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--json",
|
||||
action="store_true",
|
||||
help="Emit JSON instead of plain URLs",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--metadata",
|
||||
action="store_true",
|
||||
help="Include tweet metadata in output",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--text-tweets",
|
||||
action="store_true",
|
||||
help="Include text tweets (without media)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--type",
|
||||
choices=["photo", "video", "animated_gif", "all"],
|
||||
default="all",
|
||||
help="Filter by media type (photo, video, animated_gif, or all)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--verbose",
|
||||
"-v",
|
||||
action="store_true",
|
||||
help="Echo tweet metadata to stderr while crawling",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--set",
|
||||
metavar="KEY=VALUE",
|
||||
action="append",
|
||||
default=[],
|
||||
help="Add override for extractor.twitter.* options (repeatable)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
"-o",
|
||||
metavar="FILE",
|
||||
help="Save results to JSON file (includes cursor for resume)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--resume",
|
||||
"-r",
|
||||
metavar="FILE",
|
||||
help="Resume from previous JSON file (continues from saved cursor)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--cursor",
|
||||
metavar="CURSOR",
|
||||
help="Resume from specific cursor position",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--progress",
|
||||
action="store_true",
|
||||
help="Show progress during fetch",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def _retweets_value(mode: str):
|
||||
if mode == "original":
|
||||
return "original"
|
||||
if mode == "include":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _parse_overrides(entries: List[str]) -> Dict[str, object]:
|
||||
overrides: Dict[str, object] = {}
|
||||
for entry in entries:
|
||||
if "=" not in entry:
|
||||
raise ValueError(f"Format must be KEY=VALUE, got: {entry}")
|
||||
key, raw = entry.split("=", 1)
|
||||
overrides[key.strip()] = coerce_literal(raw.strip())
|
||||
return overrides
|
||||
|
||||
|
||||
def _progress_callback(count: int, cursor: Optional[str]) -> None:
|
||||
"""Print progress to stderr."""
|
||||
cursor_info = f" (cursor: {cursor[:20]}...)" if cursor and len(cursor) > 20 else ""
|
||||
cursor_info = f" (cursor: {cursor})" if cursor and len(cursor) <= 20 else cursor_info
|
||||
print(f"Fetching... {count} media{cursor_info}", file=sys.stderr)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
|
||||
# Handle resume from file
|
||||
resume_cursor: Optional[str] = None
|
||||
previous_media: List[Dict] = []
|
||||
previous_metadata: List[Dict] = []
|
||||
seen_urls: set = set()
|
||||
url = args.url
|
||||
|
||||
# Convert --type to filter expression
|
||||
type_filter = None
|
||||
if args.type != "all":
|
||||
type_filter = f"type == '{args.type}'"
|
||||
|
||||
if args.resume:
|
||||
state = load_resume_state(args.resume)
|
||||
if state:
|
||||
if state.get("completed"):
|
||||
print(f"Previous fetch already completed ({state.get('total', 0)} media)", file=sys.stderr)
|
||||
print("Use --output to start fresh or remove the file", file=sys.stderr)
|
||||
sys.exit(0)
|
||||
resume_cursor = state.get("cursor")
|
||||
previous_media = state.get("media", [])
|
||||
previous_metadata = state.get("metadata", [])
|
||||
# Track seen URLs to avoid duplicates
|
||||
seen_urls = {m.get("url") for m in previous_media if m.get("url")}
|
||||
url = state.get("url", args.url)
|
||||
|
||||
if resume_cursor:
|
||||
print(f"Resuming from cursor, {len(previous_media)} media already fetched", file=sys.stderr)
|
||||
else:
|
||||
print(f"No cursor available, will deduplicate {len(previous_media)} existing media", file=sys.stderr)
|
||||
else:
|
||||
print(f"Could not load resume file: {args.resume}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Manual cursor override
|
||||
if args.cursor:
|
||||
resume_cursor = args.cursor
|
||||
|
||||
include_videos = not args.no_videos
|
||||
auth_token = None if args.guest or not args.auth_token.strip() else args.auth_token
|
||||
|
||||
# Parse user overrides
|
||||
user_overrides = _parse_overrides(args.set)
|
||||
|
||||
# Add type filter if specified (merge with existing filter if any)
|
||||
if type_filter:
|
||||
existing_filter = user_overrides.get("filter")
|
||||
if existing_filter:
|
||||
# Combine with AND
|
||||
user_overrides["filter"] = f"({existing_filter}) and ({type_filter})"
|
||||
else:
|
||||
user_overrides["filter"] = type_filter
|
||||
|
||||
options = merge_options(
|
||||
{
|
||||
"auth_token": auth_token,
|
||||
"retweets": _retweets_value(args.retweets),
|
||||
"videos": include_videos,
|
||||
"size": args.size,
|
||||
"text-tweets": args.text_tweets,
|
||||
},
|
||||
user_overrides,
|
||||
)
|
||||
|
||||
request = TwitterRequest(
|
||||
url=url,
|
||||
limit=args.limit,
|
||||
metadata=(args.metadata or args.verbose),
|
||||
options=options,
|
||||
cursor=resume_cursor,
|
||||
)
|
||||
|
||||
progress_cb = _progress_callback if (args.progress or args.verbose) else None
|
||||
|
||||
# Pass seen_urls for deduplication if no cursor available
|
||||
skip_urls = seen_urls if (not resume_cursor and seen_urls) else None
|
||||
|
||||
try:
|
||||
result = run_request_dict(request, on_progress=progress_cb, skip_urls=skip_urls)
|
||||
except KeyboardInterrupt:
|
||||
print("\nInterrupted by user", file=sys.stderr)
|
||||
sys.exit(130)
|
||||
except Exception as exc:
|
||||
print(f"Error: {exc}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Merge with previous results if resuming
|
||||
if previous_media:
|
||||
if resume_cursor:
|
||||
# With cursor: filter duplicates that might occur at boundary
|
||||
new_media = [m for m in result["media"] if m.get("url") not in seen_urls]
|
||||
result["media"] = previous_media + new_media
|
||||
result["metadata"] = previous_metadata + result["metadata"]
|
||||
else:
|
||||
# Without cursor: results already deduplicated via skip_urls
|
||||
result["media"] = previous_media + result["media"]
|
||||
result["metadata"] = previous_metadata + result["metadata"]
|
||||
|
||||
result["total"] = len(result["media"])
|
||||
new_count = result["total"] - len(previous_media)
|
||||
|
||||
if new_count > 0:
|
||||
print(f"Added {new_count} new media (total: {result['total']})", file=sys.stderr)
|
||||
else:
|
||||
print(f"No new media found", file=sys.stderr)
|
||||
|
||||
media = result["media"]
|
||||
metadata = result["metadata"]
|
||||
|
||||
# Save to file if --output specified
|
||||
if args.output:
|
||||
save_state(args.output, result, url)
|
||||
status = "completed" if result.get("completed") else "partial (can resume)"
|
||||
print(f"Saved {len(media)} media to {args.output} [{status}]", file=sys.stderr)
|
||||
if not result.get("completed") and result.get("cursor"):
|
||||
print(f"Resume with: --resume {args.output}", file=sys.stderr)
|
||||
|
||||
if args.verbose:
|
||||
for entry in metadata:
|
||||
author = (entry.get("author") or {}).get("name")
|
||||
print(
|
||||
f"Processing tweet {entry.get('tweet_id')} / {author}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
if args.json:
|
||||
payload: Dict[str, object] = {
|
||||
"media": media,
|
||||
"total": len(media),
|
||||
"completed": result.get("completed", True),
|
||||
}
|
||||
if result.get("cursor"):
|
||||
payload["cursor"] = result["cursor"]
|
||||
if args.metadata:
|
||||
payload["metadata"] = metadata
|
||||
print(json.dumps(payload, indent=2, default=str))
|
||||
else:
|
||||
for item in media:
|
||||
print(item["url"])
|
||||
if args.metadata:
|
||||
print("\n# Metadata", file=sys.stderr)
|
||||
for entry in metadata:
|
||||
print(
|
||||
f"{entry['tweet_id']}: {entry['content']}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
# Print summary
|
||||
print(f"\nTotal: {len(media)} media", file=sys.stderr)
|
||||
if not result.get("completed") and result.get("cursor"):
|
||||
print(f"Cursor for resume: {result['cursor']}", file=sys.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,326 @@
|
||||
"""Shared abstraction for all Twitter gallery-dl examples.
|
||||
|
||||
This file provides utility functions that build gallery-dl configuration
|
||||
based on parameters in ``twitter-parameters.md`` and execute the Twitter
|
||||
extractor in a single run. All four interfaces (direct module, CLI, FastAPI, and
|
||||
Flask) rely on this module to ensure consistent behavior.
|
||||
|
||||
Features:
|
||||
- Cursor-based resume: Save progress and continue from where you left off
|
||||
- Progress tracking: Monitor fetch progress in real-time
|
||||
- Rate limit handling: Automatically handled by gallery-dl
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date, datetime
|
||||
from typing import Any, Callable, Dict, Iterable, List, MutableMapping, Optional
|
||||
|
||||
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
if REPO_ROOT not in sys.path:
|
||||
sys.path.append(REPO_ROOT)
|
||||
|
||||
from gallery_dl import config, extractor as extractor_mod # type: ignore
|
||||
from gallery_dl.extractor.common import Message # type: ignore
|
||||
|
||||
_CONFIG_LOCK = threading.Lock()
|
||||
|
||||
# Internal keys that should not be directly passed to extractor config.
|
||||
_INTERNAL_KEYS = {
|
||||
"url",
|
||||
"limit",
|
||||
"metadata",
|
||||
"options",
|
||||
"auth_token",
|
||||
"guest",
|
||||
"cookies",
|
||||
"cursor",
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class TwitterRequest:
|
||||
"""Standard request structure for all frontends."""
|
||||
|
||||
url: str
|
||||
options: Dict[str, Any] = field(default_factory=dict)
|
||||
limit: int = 0
|
||||
metadata: bool = False
|
||||
cursor: Optional[str] = None # Resume from this cursor position
|
||||
|
||||
|
||||
@dataclass
|
||||
class TwitterResult:
|
||||
"""Result structure with cursor for resume capability."""
|
||||
|
||||
media: List[Dict[str, Any]]
|
||||
metadata: List[Dict[str, Any]]
|
||||
cursor: Optional[str] = None # Cursor to resume from
|
||||
total: int = 0
|
||||
completed: bool = True # False if stopped before completion
|
||||
|
||||
|
||||
def _serialize_value(value: Any) -> Any:
|
||||
if isinstance(value, (datetime, date)):
|
||||
return value.isoformat()
|
||||
if isinstance(value, set):
|
||||
return sorted(value)
|
||||
return value
|
||||
|
||||
|
||||
def _extract_tweet_metadata(data: MutableMapping[str, Any]) -> Dict[str, Any]:
|
||||
keys = (
|
||||
"tweet_id",
|
||||
"retweet_id",
|
||||
"quote_id",
|
||||
"reply_id",
|
||||
"conversation_id",
|
||||
"date",
|
||||
"author",
|
||||
"content",
|
||||
"lang",
|
||||
"hashtags",
|
||||
"mentions",
|
||||
"sensitive",
|
||||
"sensitive_flags",
|
||||
"favorite_count",
|
||||
"retweet_count",
|
||||
"quote_count",
|
||||
"reply_count",
|
||||
"bookmark_count",
|
||||
"view_count",
|
||||
)
|
||||
meta = {key: _serialize_value(data.get(key)) for key in keys}
|
||||
if isinstance(meta["author"], dict):
|
||||
meta["author"] = {
|
||||
"id": meta["author"].get("id"),
|
||||
"name": meta["author"].get("name"),
|
||||
"nick": meta["author"].get("nick"),
|
||||
}
|
||||
return meta
|
||||
|
||||
|
||||
def _clean_file_metadata(meta: MutableMapping[str, Any]) -> Dict[str, Any]:
|
||||
return {key: _serialize_value(value) for key, value in meta.items()}
|
||||
|
||||
|
||||
def _apply_options(options: Dict[str, Any], cursor: Optional[str] = None) -> None:
|
||||
"""Reset config and apply all extractor options."""
|
||||
|
||||
config.clear()
|
||||
cookies = options.get("cookies")
|
||||
auth_token = options.get("auth_token")
|
||||
if auth_token:
|
||||
cookies = dict(cookies or {})
|
||||
cookies["auth_token"] = auth_token
|
||||
if cookies:
|
||||
config.set(("extractor", "twitter"), "cookies", cookies)
|
||||
|
||||
# Enable cursor tracking, or set specific cursor for resume
|
||||
if cursor:
|
||||
config.set(("extractor", "twitter"), "cursor", cursor)
|
||||
else:
|
||||
config.set(("extractor", "twitter"), "cursor", True)
|
||||
|
||||
for key, value in options.items():
|
||||
if key in _INTERNAL_KEYS or value is None:
|
||||
continue
|
||||
config.set(("extractor", "twitter"), key, value)
|
||||
|
||||
|
||||
def run_request(
|
||||
request: TwitterRequest,
|
||||
on_progress: Optional[Callable[[int, Optional[str]], None]] = None,
|
||||
skip_urls: Optional[set] = None,
|
||||
ensure_cursor: bool = True,
|
||||
) -> TwitterResult:
|
||||
"""Run Twitter extractor and return media & metadata results.
|
||||
|
||||
Args:
|
||||
request: The TwitterRequest configuration
|
||||
on_progress: Optional callback(count, cursor) called periodically during fetch
|
||||
skip_urls: Optional set of URLs to skip (for resume/deduplication)
|
||||
ensure_cursor: If True, continue fetching until cursor is available (for reliable resume)
|
||||
|
||||
Returns:
|
||||
TwitterResult with media, metadata, cursor for resume, and completion status
|
||||
"""
|
||||
skip_urls = skip_urls or set()
|
||||
|
||||
with _CONFIG_LOCK:
|
||||
_apply_options(request.options, request.cursor)
|
||||
extractor = extractor_mod.find(request.url)
|
||||
if extractor is None or extractor.category != "twitter":
|
||||
raise ValueError(f"URL not recognized by Twitter extractor: {request.url}")
|
||||
|
||||
media: List[Dict[str, Any]] = []
|
||||
metadata: List[Dict[str, Any]] = []
|
||||
collected = 0
|
||||
skipped = 0
|
||||
last_cursor: Optional[str] = None
|
||||
last_tweet_id: Optional[int] = None
|
||||
completed = True
|
||||
limit_reached = False
|
||||
|
||||
try:
|
||||
for message in extractor:
|
||||
mtype = message[0]
|
||||
if mtype is Message.Directory and request.metadata:
|
||||
metadata.append(_extract_tweet_metadata(message[1]))
|
||||
elif mtype is Message.Url:
|
||||
url = message[1]
|
||||
|
||||
# Skip if already seen
|
||||
if url in skip_urls:
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
file_meta = _clean_file_metadata(message[2])
|
||||
|
||||
# Apply client-side filter if specified
|
||||
# Note: gallery-dl's filter only works for downloads, not message iteration
|
||||
filter_expr = request.options.get("filter")
|
||||
if filter_expr:
|
||||
try:
|
||||
# Create evaluation context with file metadata
|
||||
eval_context = {**file_meta}
|
||||
# Add common functions that might be used in filters
|
||||
eval_context['datetime'] = datetime
|
||||
eval_context['date'] = date
|
||||
|
||||
# Evaluate filter expression
|
||||
if not eval(filter_expr, {"__builtins__": {}}, eval_context):
|
||||
continue # Skip this item
|
||||
except Exception as e:
|
||||
# If filter evaluation fails, include the item (fail-open)
|
||||
pass
|
||||
|
||||
media.append({"url": url, **file_meta})
|
||||
collected += 1
|
||||
|
||||
# Track last tweet_id for progress display
|
||||
if "tweet_id" in file_meta:
|
||||
last_tweet_id = file_meta["tweet_id"]
|
||||
|
||||
# Try to get cursor from extractor
|
||||
if hasattr(extractor, '_cursor') and extractor._cursor:
|
||||
last_cursor = extractor._cursor
|
||||
|
||||
# Report progress every 10 items
|
||||
if on_progress and collected % 10 == 0:
|
||||
on_progress(collected, last_cursor)
|
||||
|
||||
# Check if limit reached
|
||||
if request.limit and collected >= request.limit:
|
||||
if ensure_cursor and not last_cursor:
|
||||
# Continue until cursor is available for reliable resume
|
||||
limit_reached = True
|
||||
continue
|
||||
completed = False # Stopped due to limit
|
||||
break
|
||||
|
||||
# If we were waiting for cursor and now have it, stop
|
||||
if limit_reached and last_cursor:
|
||||
completed = False
|
||||
break
|
||||
|
||||
except KeyboardInterrupt:
|
||||
completed = False
|
||||
finally:
|
||||
# Get final cursor from extractor
|
||||
if hasattr(extractor, '_cursor') and extractor._cursor:
|
||||
last_cursor = extractor._cursor
|
||||
|
||||
return TwitterResult(
|
||||
media=media,
|
||||
metadata=metadata,
|
||||
cursor=last_cursor,
|
||||
total=collected,
|
||||
completed=completed,
|
||||
)
|
||||
|
||||
|
||||
def run_request_dict(
|
||||
request: TwitterRequest,
|
||||
on_progress: Optional[Callable[[int, Optional[str]], None]] = None,
|
||||
skip_urls: Optional[set] = None,
|
||||
ensure_cursor: bool = True,
|
||||
) -> Dict[str, Any]:
|
||||
"""Run request and return as dictionary (for JSON serialization)."""
|
||||
result = run_request(request, on_progress, skip_urls, ensure_cursor)
|
||||
return {
|
||||
"media": result.media,
|
||||
"metadata": result.metadata,
|
||||
"cursor": result.cursor,
|
||||
"total": result.total,
|
||||
"completed": result.completed,
|
||||
}
|
||||
|
||||
|
||||
def load_resume_state(filepath: str) -> Optional[Dict[str, Any]]:
|
||||
"""Load previous state from JSON file for resume."""
|
||||
if not os.path.exists(filepath):
|
||||
return None
|
||||
try:
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
return data
|
||||
except (json.JSONDecodeError, IOError):
|
||||
return None
|
||||
|
||||
|
||||
def save_state(filepath: str, result: Dict[str, Any], url: str) -> None:
|
||||
"""Save current state to JSON file."""
|
||||
state = {
|
||||
"url": url,
|
||||
"cursor": result.get("cursor"),
|
||||
"total": result.get("total", len(result.get("media", []))),
|
||||
"completed": result.get("completed", True),
|
||||
"media": result.get("media", []),
|
||||
"metadata": result.get("metadata", []),
|
||||
}
|
||||
with open(filepath, "w", encoding="utf-8") as f:
|
||||
json.dump(state, f, indent=2, default=str)
|
||||
|
||||
|
||||
def merge_options(*sources: Optional[Dict[str, Any]]) -> Dict[str, Any]:
|
||||
"""Merge multiple configuration dicts (None values ignored)."""
|
||||
|
||||
merged: Dict[str, Any] = {}
|
||||
for source in sources:
|
||||
if not source:
|
||||
continue
|
||||
for key, value in source.items():
|
||||
merged[key] = value
|
||||
return merged
|
||||
|
||||
|
||||
def coerce_literal(value: str) -> Any:
|
||||
"""Infer basic type from CLI argument string."""
|
||||
|
||||
lowered = value.lower()
|
||||
if lowered in {"true", "false"}:
|
||||
return lowered == "true"
|
||||
if lowered in {"null", "none"}:
|
||||
return None
|
||||
try:
|
||||
return int(value)
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
return float(value)
|
||||
except ValueError:
|
||||
pass
|
||||
if (value.startswith("[") and value.endswith("]")) or (
|
||||
value.startswith("{") and value.endswith("}")
|
||||
):
|
||||
try:
|
||||
return json.loads(value)
|
||||
except json.JSONDecodeError:
|
||||
return value
|
||||
return value
|
||||
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"log"
|
||||
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
|
||||
"github.com/wailsapp/wails/v2/pkg/options/windows"
|
||||
)
|
||||
|
||||
//go:embed all:frontend/dist
|
||||
var assets embed.FS
|
||||
|
||||
func main() {
|
||||
// Create an instance of the app structure
|
||||
app := NewApp()
|
||||
|
||||
// Create application with options
|
||||
err := wails.Run(&options.App{
|
||||
Title: "Twitter/X Media Batch Downloader",
|
||||
Width: 1024,
|
||||
Height: 600,
|
||||
MinWidth: 1024,
|
||||
MinHeight: 600,
|
||||
Frameless: true,
|
||||
AssetServer: &assetserver.Options{
|
||||
Assets: assets,
|
||||
},
|
||||
BackgroundColour: &options.RGBA{R: 0, G: 0, B: 0, A: 255},
|
||||
OnStartup: app.startup,
|
||||
OnShutdown: app.shutdown,
|
||||
DragAndDrop: &options.DragAndDrop{
|
||||
EnableFileDrop: true,
|
||||
DisableWebViewDrop: false,
|
||||
CSSDropProperty: "--wails-drop-target",
|
||||
CSSDropValue: "drop",
|
||||
},
|
||||
Bind: []interface{}{
|
||||
app,
|
||||
},
|
||||
Windows: &windows.Options{
|
||||
WebviewIsTransparent: false,
|
||||
WindowIsTranslucent: false,
|
||||
DisableWindowIcon: false,
|
||||
DisableFramelessWindowDecorations: false,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Error:", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
PyQt6==6.9.1
|
||||
requests
|
||||
aiohttp
|
||||
imageio-ffmpeg
|
||||
packaging
|
||||
gallery-dl
|
||||
pyqtdarktheme
|
||||
qtawesome
|
||||
@@ -1,7 +0,0 @@
|
||||
from ui.mainWindow import TwitterXMediaBatchDownloaderGUI
|
||||
from ui.dialogs import UpdateDialog
|
||||
|
||||
__all__ = [
|
||||
'TwitterXMediaBatchDownloaderGUI',
|
||||
'UpdateDialog'
|
||||
]
|
||||
@@ -1,33 +0,0 @@
|
||||
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)
|
||||
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 173 KiB |
-3139
File diff suppressed because it is too large.
Load diff
@@ -1,240 +0,0 @@
|
||||
import os
|
||||
import qtawesome as qta
|
||||
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_name, color=None):
|
||||
palette = QApplication.palette()
|
||||
text_color = palette.color(QPalette.ColorRole.Text)
|
||||
color_hex = text_color.name()
|
||||
return qta.icon(icon_name, color=color_hex)
|
||||
|
||||
def update_button_icons(self):
|
||||
self.update_button_icons_unix()
|
||||
|
||||
def update_button_icons_unix_dashboard(self):
|
||||
if not hasattr(self, 'import_btn'):
|
||||
return
|
||||
|
||||
self.import_btn.setIcon(self.create_colored_icon('fa6s.file-import'))
|
||||
self.export_btn.setIcon(self.create_colored_icon('fa6s.file-export'))
|
||||
self.download_selected_btn.setIcon(self.create_colored_icon('fa6s.download'))
|
||||
self.update_selected_btn.setIcon(self.create_colored_icon('fa6s.arrows-rotate'))
|
||||
self.delete_btn.setIcon(self.create_colored_icon('fa6s.trash'))
|
||||
|
||||
def update_button_icons_unix(self):
|
||||
self.update_button_icons_unix_dashboard()
|
||||
|
||||
if hasattr(self, 'stop_btn'):
|
||||
self.stop_btn.setIcon(self.create_colored_icon('fa6s.circle-xmark'))
|
||||
if hasattr(self, 'pause_resume_btn'):
|
||||
if self.pause_resume_btn.text() == ' Resume':
|
||||
self.pause_resume_btn.setIcon(self.create_colored_icon('fa6s.play'))
|
||||
else:
|
||||
self.pause_resume_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
if hasattr(self, 'stop_fetch_btn'):
|
||||
self.stop_fetch_btn.setIcon(self.create_colored_icon('fa6s.circle-xmark'))
|
||||
if hasattr(self, 'pause_fetch_btn'):
|
||||
if self.pause_fetch_btn.text() == ' Resume':
|
||||
self.pause_fetch_btn.setIcon(self.create_colored_icon('fa6s.play'))
|
||||
else:
|
||||
self.pause_fetch_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
|
||||
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'):
|
||||
if self.worker.is_paused:
|
||||
self.worker.resume()
|
||||
self.pause_resume_btn.setText(' Pause')
|
||||
self.pause_resume_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
self.timer.start(1000)
|
||||
else:
|
||||
self.worker.pause()
|
||||
self.pause_resume_btn.setText(' Resume')
|
||||
self.pause_resume_btn.setIcon(self.create_colored_icon('fa6s.play'))
|
||||
|
||||
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()
|
||||
|
||||
self.pause_fetch_btn.setText(' Resume')
|
||||
self.pause_fetch_btn.setIcon(self.create_colored_icon('fa6s.play'))
|
||||
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')
|
||||
self.pause_fetch_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
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':
|
||||
self.pause_fetch_btn.setText(' Pause')
|
||||
self.pause_fetch_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
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()
|
||||
if hasattr(self, 'pause_resume_btn'):
|
||||
self.pause_resume_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
|
||||
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.pause_resume_btn.setIcon(self.create_colored_icon('fa6s.pause'))
|
||||
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)
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"version": "3.9"
|
||||
"version": "4.0"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"$schema": "https://wails.io/schemas/config.v2.json",
|
||||
"name": "TwitterXMediaBatchDownloader",
|
||||
"outputfilename": "TwitterXMediaBatchDownloader",
|
||||
"frontend:install": "pnpm install",
|
||||
"frontend:build": "pnpm run build",
|
||||
"frontend:dev:watcher": "pnpm run dev",
|
||||
"frontend:dev:serverUrl": "auto",
|
||||
"author": {
|
||||
"name": "afkarxyz"
|
||||
},
|
||||
"info": {
|
||||
"productName": "Twitter/X Media Batch Downloader",
|
||||
"productVersion": "4.0"
|
||||
},
|
||||
"wailsjsdir": "./frontend",
|
||||
"assetdir": "./frontend/dist",
|
||||
"reloaddirs": "./frontend/src"
|
||||
}
|
||||
Reference in new issue
Block a user