mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-12-02 02:51:29 +01:00
v4.4
This commit is contained in:
1 parent
8cbf7c979b
commit
4794fac099
80 files changed
+7500
-10133
No files matched your search
+29
-77
@@ -15,32 +15,28 @@ import (
|
||||
)
|
||||
|
||||
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)
|
||||
OriginalFilename string `json:"original_filename,omitempty"` // Original Twitter media filename (15 char alphanumeric)
|
||||
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"`
|
||||
OriginalFilename string `json:"original_filename,omitempty"`
|
||||
}
|
||||
|
||||
// DownloadMediaFiles downloads media files from URLs to the output directory (legacy)
|
||||
func DownloadMediaFiles(urls []string, outputDir string, customProxy 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)
|
||||
}
|
||||
|
||||
// Create HTTP client with proxy support
|
||||
client, err := CreateHTTPClient(customProxy, 60*time.Second)
|
||||
if err != nil {
|
||||
// If proxy setup fails, use default client without proxy
|
||||
|
||||
client = &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
@@ -50,7 +46,6 @@ func DownloadMediaFiles(urls []string, outputDir string, customProxy string) (do
|
||||
filename := extractFilename(mediaURL)
|
||||
outputPath := filepath.Join(outputDir, filename)
|
||||
|
||||
// Skip if file already exists
|
||||
if _, err := os.Stat(outputPath); err == nil {
|
||||
downloaded++
|
||||
continue
|
||||
@@ -66,21 +61,16 @@ func DownloadMediaFiles(urls []string, outputDir string, customProxy string) (do
|
||||
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
|
||||
}
|
||||
|
||||
// DownloadMediaWithMetadataProgressAndStatus downloads media files with progress and per-item status callbacks
|
||||
// Returns: downloaded count, skipped count, failed count, error
|
||||
func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir string, username string, progress ProgressCallback, itemStatus ItemStatusCallback, ctx context.Context, customProxy string) (downloaded int, skipped int, failed int, err error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
@@ -91,24 +81,20 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
|
||||
// Prepare all tasks first (sequential to handle tweet media count)
|
||||
// For bookmarks and likes, each item may have different username, so we track per username
|
||||
tweetMediaCount := make(map[string]map[int64]int) // username -> tweet_id -> count
|
||||
tweetMediaCount := make(map[string]map[int64]int)
|
||||
tasks := make([]downloadTask, 0, total)
|
||||
|
||||
for i, item := range items {
|
||||
// Use item.Username if available (for bookmarks/likes with different authors), otherwise use provided username
|
||||
|
||||
itemUsername := item.Username
|
||||
if itemUsername == "" {
|
||||
itemUsername = username
|
||||
}
|
||||
|
||||
// Initialize tweet media count for this username if needed
|
||||
if tweetMediaCount[itemUsername] == nil {
|
||||
tweetMediaCount[itemUsername] = make(map[int64]int)
|
||||
}
|
||||
|
||||
// Determine subfolder based on type
|
||||
var subfolder string
|
||||
switch item.Type {
|
||||
case "photo":
|
||||
@@ -123,29 +109,23 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
subfolder = "other"
|
||||
}
|
||||
|
||||
// Create base directory for this username
|
||||
baseDir := filepath.Join(outputDir, itemUsername)
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 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 username and tweet_id
|
||||
tweetMediaCount[itemUsername][item.TweetID]++
|
||||
mediaIndex := tweetMediaCount[itemUsername][item.TweetID]
|
||||
|
||||
// Create filename: {username}_{timestamp}_{tweet_id}_{index}.{ext}
|
||||
filename := fmt.Sprintf("%s_%s_%d_%02d%s", itemUsername, timestamp, item.TweetID, mediaIndex, ext)
|
||||
outputPath := filepath.Join(typeDir, filename)
|
||||
|
||||
@@ -156,27 +136,23 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
})
|
||||
}
|
||||
|
||||
// Counters for parallel downloads
|
||||
var downloadedCount int64
|
||||
var skippedCount 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)
|
||||
}
|
||||
|
||||
// Create HTTP client once for all workers (shared client is more efficient)
|
||||
var sharedClient *http.Client
|
||||
client, err := CreateHTTPClient(customProxy, 60*time.Second)
|
||||
if err != nil {
|
||||
// If proxy setup fails, use default client without proxy
|
||||
|
||||
sharedClient = &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
@@ -191,7 +167,7 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
client := sharedClient
|
||||
|
||||
for task := range taskChan {
|
||||
// Check for cancellation
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
@@ -199,17 +175,17 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
}
|
||||
|
||||
var status string
|
||||
// Skip if file already exists
|
||||
|
||||
if _, err := os.Stat(task.outputPath); err == nil {
|
||||
status = "skipped"
|
||||
// Emit status immediately for skipped files
|
||||
|
||||
if itemStatus != nil {
|
||||
itemStatus(task.item.TweetID, task.index, status)
|
||||
}
|
||||
atomic.AddInt64(&skippedCount, 1)
|
||||
continue // Skip to next task
|
||||
continue
|
||||
} 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"
|
||||
@@ -221,30 +197,23 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
atomic.AddInt64(&failedCount, 1)
|
||||
status = "failed"
|
||||
} else {
|
||||
// Embed metadata after successful download
|
||||
|
||||
tweetURL := fmt.Sprintf("https://x.com/i/status/%d", task.item.TweetID)
|
||||
// Always extract original filename from URL (simpler approach)
|
||||
|
||||
originalFilename := ExtractOriginalFilename(task.item.URL)
|
||||
|
||||
// For debugging: if original filename is still empty for video, it means it's not in the URL
|
||||
// This is acceptable - video URLs from Twitter may not contain original filename
|
||||
|
||||
// Embed metadata (non-fatal: if it fails, file is still downloaded)
|
||||
|
||||
if err := EmbedMetadata(task.outputPath, task.item.Content, tweetURL, originalFilename); err != nil {
|
||||
// Log error but don't fail the download
|
||||
// Metadata embedding is optional
|
||||
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
@@ -253,7 +222,6 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
}()
|
||||
}
|
||||
|
||||
// Send tasks to workers
|
||||
for _, task := range tasks {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -265,13 +233,11 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str
|
||||
}
|
||||
close(taskChan)
|
||||
|
||||
// Wait for all workers to finish
|
||||
wg.Wait()
|
||||
|
||||
return int(downloadedCount), int(skippedCount), 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 {
|
||||
@@ -298,14 +264,13 @@ func downloadFileWithContext(ctx context.Context, client *http.Client, url, outp
|
||||
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",
|
||||
"2006-01-02T15:04:05+00:00",
|
||||
"2006-01-02T15:04:05-07:00",
|
||||
time.RFC3339,
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
"2006-01-02T15:04:05Z",
|
||||
"2006-01-02 15:04:05",
|
||||
@@ -318,35 +283,30 @@ func formatTimestamp(dateStr string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
return ".mp4"
|
||||
case "text":
|
||||
return ".txt"
|
||||
default:
|
||||
@@ -354,28 +314,23 @@ func getExtension(mediaURL string, mediaType string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
||||
@@ -383,12 +338,10 @@ func extractFilename(mediaURL string) string {
|
||||
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())
|
||||
}
|
||||
@@ -396,7 +349,6 @@ func extractFilename(mediaURL string) string {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user