|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/internetarchive/Zeno/internal/pkg/config" |
| 12 | + "github.com/internetarchive/Zeno/internal/pkg/controler" |
| 13 | + "github.com/internetarchive/Zeno/internal/pkg/ui" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +var getListCmd = &cobra.Command{ |
| 18 | + Use: "list [FILE|URL...]", |
| 19 | + Short: "Archive URLs from text file(s)", |
| 20 | + Long: `Archive URLs from one or more text files or URLs. |
| 21 | +Each file should contain one URL per line. |
| 22 | +Remote files (starting with http:// or https://) are supported. |
| 23 | +Empty lines and lines starting with # are ignored.`, |
| 24 | + Args: cobra.MinimumNArgs(1), |
| 25 | + PreRunE: func(_ *cobra.Command, args []string) error { |
| 26 | + if cfg == nil { |
| 27 | + return fmt.Errorf("viper config is nil") |
| 28 | + } |
| 29 | + |
| 30 | + return nil |
| 31 | + }, |
| 32 | + RunE: func(_ *cobra.Command, args []string) error { |
| 33 | + // Read URLs from all provided files |
| 34 | + for _, file := range args { |
| 35 | + var urls []string |
| 36 | + var err error |
| 37 | + |
| 38 | + if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") { |
| 39 | + urls, err = readRemoteURLList(file) |
| 40 | + } else { |
| 41 | + urls, err = readLocalURLList(file) |
| 42 | + } |
| 43 | + |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("error reading file %s: %w", file, err) |
| 46 | + } |
| 47 | + |
| 48 | + // Add URLs to config |
| 49 | + config.Get().InputSeeds = append(config.Get().InputSeeds, urls...) |
| 50 | + } |
| 51 | + |
| 52 | + if len(config.Get().InputSeeds) == 0 { |
| 53 | + return fmt.Errorf("no URLs found in provided files") |
| 54 | + } |
| 55 | + |
| 56 | + err := config.GenerateCrawlConfig() |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + if cfg.PyroscopeAddress != "" { |
| 62 | + err = startPyroscope() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if cfg.SentryDSN != "" { |
| 69 | + err = startSentry() |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + controler.Start() |
| 76 | + if config.Get().TUI { |
| 77 | + tui := ui.New() |
| 78 | + err := tui.Start() |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("error starting TUI: %w", err) |
| 81 | + } |
| 82 | + } else { |
| 83 | + controler.WatchSignals() |
| 84 | + } |
| 85 | + return nil |
| 86 | + }, |
| 87 | +} |
| 88 | + |
| 89 | +// readLocalURLList reads URLs from a local file |
| 90 | +func readLocalURLList(file string) (urls []string, err error) { |
| 91 | + f, err := os.Open(file) |
| 92 | + if err != nil { |
| 93 | + return urls, err |
| 94 | + } |
| 95 | + defer f.Close() |
| 96 | + |
| 97 | + scanner := bufio.NewScanner(f) |
| 98 | + for scanner.Scan() { |
| 99 | + line := strings.TrimSpace(scanner.Text()) |
| 100 | + if line == "" || strings.HasPrefix(line, "#") { |
| 101 | + continue |
| 102 | + } |
| 103 | + urls = append(urls, line) |
| 104 | + } |
| 105 | + |
| 106 | + return urls, scanner.Err() |
| 107 | +} |
| 108 | + |
| 109 | +// readRemoteURLList reads URLs from a remote file (http/https) |
| 110 | +func readRemoteURLList(URL string) (urls []string, err error) { |
| 111 | + httpClient := &http.Client{ |
| 112 | + Timeout: time.Second * 30, |
| 113 | + } |
| 114 | + |
| 115 | + req, err := http.NewRequest(http.MethodGet, URL, nil) |
| 116 | + if err != nil { |
| 117 | + return urls, err |
| 118 | + } |
| 119 | + |
| 120 | + // Set user agent, use default if not configured |
| 121 | + userAgent := config.Get().UserAgent |
| 122 | + if userAgent == "" { |
| 123 | + userAgent = "Mozilla/5.0 (compatible; Zeno)" |
| 124 | + } |
| 125 | + req.Header.Set("User-Agent", userAgent) |
| 126 | + |
| 127 | + resp, err := httpClient.Do(req) |
| 128 | + if err != nil { |
| 129 | + return urls, err |
| 130 | + } |
| 131 | + |
| 132 | + defer resp.Body.Close() |
| 133 | + |
| 134 | + if resp.StatusCode != http.StatusOK { |
| 135 | + return urls, fmt.Errorf("failed to download URL list: %s", resp.Status) |
| 136 | + } |
| 137 | + |
| 138 | + // Read file line by line |
| 139 | + scanner := bufio.NewScanner(resp.Body) |
| 140 | + for scanner.Scan() { |
| 141 | + line := strings.TrimSpace(scanner.Text()) |
| 142 | + if line == "" || strings.HasPrefix(line, "#") { |
| 143 | + continue |
| 144 | + } |
| 145 | + urls = append(urls, line) |
| 146 | + } |
| 147 | + return urls, scanner.Err() |
| 148 | +} |
0 commit comments