|
| 1 | +// Main command logic, flag parsing, and orchestration |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/hashicorp/go-retryablehttp" |
| 14 | + |
| 15 | + "github.com/xtruder/ffbookmarks-to-markdown/internal/bookmarks" |
| 16 | + "github.com/xtruder/ffbookmarks-to-markdown/internal/firefox" |
| 17 | + "github.com/xtruder/ffbookmarks-to-markdown/internal/llm" |
| 18 | + "github.com/xtruder/ffbookmarks-to-markdown/internal/markdown" |
| 19 | + "github.com/xtruder/ffbookmarks-to-markdown/internal/web" |
| 20 | + "github.com/xtruder/ffbookmarks-to-markdown/internal/x" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + // Command line flags |
| 25 | + baseFolder string |
| 26 | + outputDir string |
| 27 | + listBookmarks bool |
| 28 | + verbose bool |
| 29 | + ignoreFolders string |
| 30 | + screenshotAPI string |
| 31 | + llmAPIKey string |
| 32 | + llmBaseURL string |
| 33 | + llmModel string |
| 34 | +) |
| 35 | + |
| 36 | +func main() { |
| 37 | + // Define command line flags |
| 38 | + flag.StringVar(&baseFolder, "folder", "toolbar", "Base folder name to sync from Firefox bookmarks") |
| 39 | + flag.StringVar(&outputDir, "output", "bookmarks", "Output directory for markdown files") |
| 40 | + flag.BoolVar(&listBookmarks, "list", false, "List all available bookmarks") |
| 41 | + flag.BoolVar(&verbose, "verbose", false, "Enable verbose logging") |
| 42 | + flag.StringVar(&ignoreFolders, "ignore", "", "Comma-separated list of folder names to ignore") |
| 43 | + flag.StringVar(&screenshotAPI, "screenshot-api", "https://gowitness.cloud.x-truder.net", "Screenshot API base URL") |
| 44 | + flag.StringVar(&llmAPIKey, "llm-key", "", "API key for LLM service") |
| 45 | + flag.StringVar(&llmBaseURL, "llm-url", "https://generativelanguage.googleapis.com/v1beta/openai/", "Base URL for LLM service") |
| 46 | + flag.StringVar(&llmModel, "llm-model", "gemini-2.0-flash", "Model to use for LLM service") |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + // Get API key from environment if not provided |
| 50 | + if llmAPIKey == "" { |
| 51 | + llmAPIKey = os.Getenv("GEMINI_API_KEY") |
| 52 | + } |
| 53 | + |
| 54 | + // Initialize logger |
| 55 | + logLevel := slog.LevelInfo |
| 56 | + if verbose { |
| 57 | + logLevel = slog.LevelDebug |
| 58 | + } |
| 59 | + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
| 60 | + Level: logLevel, |
| 61 | + })) |
| 62 | + slog.SetDefault(logger) |
| 63 | + |
| 64 | + // Initialize HTTP client |
| 65 | + client := retryablehttp.NewClient() |
| 66 | + client.RetryMax = 3 |
| 67 | + client.Logger = nil // Disable retryable client logging |
| 68 | + |
| 69 | + homeDir, err := os.UserHomeDir() |
| 70 | + if err != nil { |
| 71 | + slog.Error("failed to get home directory", "error", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + |
| 75 | + cacheDir := filepath.Join(homeDir, ".cache", "ffbookmarks-to-markdown") |
| 76 | + |
| 77 | + // Initialize cache |
| 78 | + cache, err := x.NewFileCache(cacheDir) |
| 79 | + if err != nil { |
| 80 | + slog.Warn("failed to initialize cache", "error", err) |
| 81 | + } |
| 82 | + |
| 83 | + llmClient, err := llm.NewOpenAIClient(llmAPIKey, llmBaseURL, llmModel, client.StandardClient(), cache) |
| 84 | + if err != nil { |
| 85 | + slog.Error("failed to initialize LLM client", "error", err) |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + |
| 89 | + // Initialize services |
| 90 | + ffFetcher := firefox.NewFirefoxFetcher() |
| 91 | + contentService := web.NewContentService(client.StandardClient(), web.FetchOptions{ |
| 92 | + BaseURL: "https://md.dhr.wtf", |
| 93 | + ContentCleaner: llmClient, |
| 94 | + Cache: cache, |
| 95 | + }) |
| 96 | + screenshotService := web.NewScreenshotService(client.StandardClient(), screenshotAPI) |
| 97 | + |
| 98 | + // Get Firefox bookmarkRoot |
| 99 | + bookmarkRoot, err := ffFetcher.GetBookmarks() |
| 100 | + if err != nil { |
| 101 | + slog.Error("failed to get Firefox bookmarks", "error", err) |
| 102 | + os.Exit(1) |
| 103 | + } |
| 104 | + |
| 105 | + // Find target folder |
| 106 | + targetFolder := bookmarkRoot.Path(baseFolder) |
| 107 | + if targetFolder == nil { |
| 108 | + fmt.Printf("Folder '%s' not found in bookmarks\n", baseFolder) |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + |
| 112 | + // Parse ignored folders |
| 113 | + var ignoredFoldersList []string |
| 114 | + if ignoreFolders != "" { |
| 115 | + ignoredFoldersList = strings.Split(ignoreFolders, ",") |
| 116 | + } |
| 117 | + |
| 118 | + // Collect new URLs for screenshots |
| 119 | + allBookmarks := x.Filter2( |
| 120 | + targetFolder.All(), |
| 121 | + func(path string, v *bookmarks.Bookmark) bool { |
| 122 | + for _, ignorePath := range ignoredFoldersList { |
| 123 | + if strings.HasPrefix(path, ignorePath) { |
| 124 | + return false |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return v.Type == "bookmark" && !v.Deleted |
| 129 | + }, |
| 130 | + ) |
| 131 | + |
| 132 | + if listBookmarks { |
| 133 | + for path := range allBookmarks { |
| 134 | + fmt.Println(path) |
| 135 | + } |
| 136 | + |
| 137 | + os.Exit(0) |
| 138 | + } |
| 139 | + |
| 140 | + // Get existing screenshots |
| 141 | + screenshots, err := screenshotService.GetExistingScreenshots() |
| 142 | + if err != nil { |
| 143 | + slog.Error("failed to get existing screenshots", "error", err) |
| 144 | + os.Exit(1) |
| 145 | + } |
| 146 | + |
| 147 | + mdCache, err := markdown.BuildCache(outputDir) |
| 148 | + if err != nil { |
| 149 | + slog.Error("failed to build markdown cache", "error", err) |
| 150 | + os.Exit(1) |
| 151 | + } |
| 152 | + |
| 153 | + newURLs := mdCache.CollectNewURLs(x.Values(allBookmarks)) |
| 154 | + |
| 155 | + // Filter URLs that need screenshots |
| 156 | + var urlsToScreenshot []string |
| 157 | + for _, u := range newURLs { |
| 158 | + if !screenshots[u] { |
| 159 | + urlsToScreenshot = append(urlsToScreenshot, u) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Submit new screenshots |
| 164 | + if len(urlsToScreenshot) > 0 { |
| 165 | + slog.Info("submitting batch screenshot request", |
| 166 | + "total", len(newURLs), |
| 167 | + "new", len(urlsToScreenshot), |
| 168 | + "cached", len(newURLs)-len(urlsToScreenshot)) |
| 169 | + if err := screenshotService.SubmitScreenshots(urlsToScreenshot); err != nil { |
| 170 | + slog.Error("failed to submit screenshots", "error", err) |
| 171 | + } |
| 172 | + } else { |
| 173 | + slog.Info("no new screenshots needed", |
| 174 | + "total", len(newURLs), |
| 175 | + "cached", len(newURLs)) |
| 176 | + } |
| 177 | + |
| 178 | + // Process bookmarks |
| 179 | + mdProcessor := markdown.NewProcessor( |
| 180 | + markdown.ProcessorOptions{ |
| 181 | + OutputDir: outputDir, |
| 182 | + IgnoredFolders: ignoredFoldersList, |
| 183 | + }, |
| 184 | + contentService, |
| 185 | + screenshotService, |
| 186 | + mdCache, |
| 187 | + ) |
| 188 | + |
| 189 | + // Process bookmarks and create indexes |
| 190 | + if err := mdProcessor.ProcessBookmarks(*targetFolder, ""); err != nil { |
| 191 | + slog.Error("failed to process bookmarks", "error", err) |
| 192 | + os.Exit(1) |
| 193 | + } |
| 194 | + |
| 195 | + if err := mdProcessor.CreateYearIndexes(x.Values(allBookmarks)); err != nil { |
| 196 | + slog.Error("failed to create year indexes", "error", err) |
| 197 | + os.Exit(1) |
| 198 | + } |
| 199 | +} |
0 commit comments