2025-10-31 21:35:53 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
2025-11-02 01:26:52 +00:00
|
|
|
"github.com/Snider/Borg/pkg/ui"
|
2025-11-01 19:03:04 +00:00
|
|
|
"github.com/Snider/Borg/pkg/website"
|
2025-10-31 21:35:53 +00:00
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// collectWebsiteCmd represents the collect website command
|
|
|
|
|
var collectWebsiteCmd = &cobra.Command{
|
|
|
|
|
Use: "website [url]",
|
|
|
|
|
Short: "Collect a single website",
|
|
|
|
|
Long: `Collect a single website and store it in a DataNode.`,
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
websiteURL := args[0]
|
|
|
|
|
outputFile, _ := cmd.Flags().GetString("output")
|
|
|
|
|
depth, _ := cmd.Flags().GetInt("depth")
|
|
|
|
|
|
2025-11-02 01:26:52 +00:00
|
|
|
bar := ui.NewProgressBar(-1, "Crawling website")
|
|
|
|
|
defer bar.Finish()
|
|
|
|
|
|
|
|
|
|
dn, err := website.DownloadAndPackageWebsite(websiteURL, depth, bar)
|
2025-10-31 21:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error downloading and packaging website: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
websiteData, err := dn.ToTar()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error converting website to bytes: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.WriteFile(outputFile, websiteData, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error writing website to file: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Website saved to %s\n", outputFile)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
collectCmd.AddCommand(collectWebsiteCmd)
|
|
|
|
|
collectWebsiteCmd.PersistentFlags().String("output", "website.dat", "Output file for the DataNode")
|
|
|
|
|
collectWebsiteCmd.PersistentFlags().Int("depth", 2, "Recursion depth for downloading")
|
|
|
|
|
}
|