10fd53af41
- Removed command line flag parsing for date range scraping in the scraper's main function. - Introduced configuration fields for one-time scraping and date range (FromDate, ToDate) in the ScraperConfig struct. - Updated the main function to utilize the new configuration fields, improving the clarity and maintainability of the scraping logic. - This change enhances the scraper's functionality by centralizing configuration management and reducing command line complexity.
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
"tm/cmd/scraper/bootstrap"
|
|
)
|
|
|
|
func main() {
|
|
// Load configuration
|
|
config, err := bootstrap.InitConfig()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Initialize logger
|
|
appLogger := bootstrap.InitLogger(config.Logging)
|
|
appLogger.Info("Starting TED scraper application", map[string]interface{}{
|
|
"version": "1.0.0",
|
|
})
|
|
|
|
// Initialize MongoDB connection
|
|
mongoManager := bootstrap.InitMongoDB(config.Database.MongoDB, appLogger)
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := mongoManager.Close(ctx); err != nil {
|
|
appLogger.Error("Failed to close MongoDB connection", map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
}()
|
|
|
|
// Initialize notification service
|
|
notificationService := bootstrap.InitNotificationService(config.Notification, appLogger)
|
|
|
|
// Check if one-time date range scraping is requested
|
|
if config.TED.OneTime {
|
|
if config.TED.FromDate == "" || config.TED.ToDate == "" {
|
|
appLogger.Error("Both TED_FROM_DATE and TED_TO_DATE are required for one-time scraping", map[string]interface{}{})
|
|
fmt.Println("Usage: ./scraper -onetime -from 12/01/2025 -to 13/09/2025")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Parse dates
|
|
from, err := time.Parse("02/01/2006", config.TED.FromDate)
|
|
if err != nil {
|
|
appLogger.Error("Invalid from date format", map[string]interface{}{
|
|
"from_date": config.TED.FromDate,
|
|
"error": err.Error(),
|
|
})
|
|
fmt.Println("Date format should be: DD/MM/YYYY (e.g., 12/01/2025)")
|
|
os.Exit(1)
|
|
}
|
|
|
|
to, err := time.Parse("02/01/2006", config.TED.ToDate)
|
|
if err != nil {
|
|
appLogger.Error("Invalid to date format", map[string]interface{}{
|
|
"to_date": config.TED.ToDate,
|
|
"error": err.Error(),
|
|
})
|
|
fmt.Println("Date format should be: DD/MM/YYYY (e.g., 13/09/2025)")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Run one-time scraping
|
|
bootstrap.RunOneTimeScraping(*config, mongoManager, appLogger, notificationService, &from, &to)
|
|
return
|
|
}
|
|
|
|
// Initialize TED scraper for scheduled mode
|
|
bootstrap.InitTEDScraper(*config, mongoManager, appLogger, notificationService)
|
|
|
|
// Set up signal handling for graceful shutdown
|
|
signalChan := make(chan os.Signal, 1)
|
|
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
// Wait for shutdown signal
|
|
<-signalChan
|
|
appLogger.Info("Received shutdown signal, stopping scheduler...", map[string]interface{}{})
|
|
|
|
appLogger.Info("TED scraper application stopped", map[string]interface{}{})
|
|
}
|