2025-01-16 15:55:38 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2025-01-23 20:26:21 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
2025-01-16 15:55:38 +00:00
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
"github.com/Jaculabilis/intake/core"
|
2025-01-16 15:55:38 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2025-01-21 16:42:59 +00:00
|
|
|
var sourceFetchCmd = &cobra.Command{
|
|
|
|
Use: "fetch",
|
|
|
|
Short: "Fetch items for a source and update the feed",
|
2025-01-16 15:55:38 +00:00
|
|
|
Long: `Fetch items from a feed source using the configured "fetch" action.
|
|
|
|
Items returned by a successful fetch will be used to update the source.
|
2025-01-21 16:42:59 +00:00
|
|
|
A fetch is successful if all items output by the fetch are parsed successfully
|
|
|
|
and the exit code is 0. No changes will be made to the source if the fetch
|
2025-01-23 20:26:21 +00:00
|
|
|
does not succeed.
|
|
|
|
|
|
|
|
In a dry run, the items will be printed according to the chosen format.`,
|
2025-01-16 15:55:38 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2025-01-21 16:42:59 +00:00
|
|
|
sourceFetch()
|
2025-01-16 15:55:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
var sourceFetchSource string
|
|
|
|
var sourceFetchFormat string
|
|
|
|
var sourceFetchDryRun bool
|
2025-01-16 15:55:38 +00:00
|
|
|
|
|
|
|
func init() {
|
2025-01-21 16:42:59 +00:00
|
|
|
sourceCmd.AddCommand(sourceFetchCmd)
|
2025-01-16 15:55:38 +00:00
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
sourceFetchCmd.Flags().StringVarP(&sourceFetchSource, "source", "s", "", "Source name to fetch (required)")
|
2025-01-21 16:42:59 +00:00
|
|
|
sourceFetchCmd.MarkFlagRequired("source")
|
2025-01-16 15:55:38 +00:00
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
sourceFetchCmd.Flags().StringVarP(&sourceFetchFormat, "format", "f", "headlines", "Feed format for returned items.")
|
|
|
|
sourceFetchCmd.Flags().BoolVar(&sourceFetchDryRun, "dry-run", false, "Instead of updating the source, print the fetched items")
|
2025-01-16 15:55:38 +00:00
|
|
|
}
|
|
|
|
|
2025-01-21 16:42:59 +00:00
|
|
|
func sourceFetch() {
|
2025-01-23 20:26:21 +00:00
|
|
|
formatter, err := core.FormatAs(sourceFetchFormat)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: %v", err)
|
2025-01-16 15:55:38 +00:00
|
|
|
}
|
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
db := openAndMigrateDb()
|
2025-01-16 15:55:38 +00:00
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
argv, err := core.GetArgvForAction(db, sourceFetchSource, "fetch")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: failed to get fetch action: %v", err)
|
|
|
|
}
|
2025-01-16 15:55:38 +00:00
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
items, err := core.Execute(sourceFetchSource, argv, nil, "", time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sourceFetchDryRun {
|
|
|
|
log.Printf("Fetch returned %d items", len(items))
|
|
|
|
for _, item := range items {
|
|
|
|
fmt.Println(formatter(item))
|
2025-01-16 15:55:38 +00:00
|
|
|
}
|
2025-01-23 20:26:21 +00:00
|
|
|
return
|
|
|
|
}
|
2025-01-16 15:55:38 +00:00
|
|
|
|
2025-01-23 20:26:21 +00:00
|
|
|
added, deleted, err := core.UpdateWithFetchedItems(db, sourceFetchSource, items)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
log.Printf("%s added %d items, updated %d items, and deleted %d items", sourceFetchSource, added, len(items)-added, deleted)
|
2025-01-16 15:55:38 +00:00
|
|
|
}
|