feat: add change detection module

This commit is contained in:
Shashank S 2024-05-08 11:47:25 +02:00
parent e204029d3c
commit 7adf624e95
6 changed files with 167 additions and 1 deletions

View File

@ -22,6 +22,7 @@ var (
RedditCardsHorizontalTemplate = compileTemplate("reddit-horizontal-cards.html", "widget-base.html") RedditCardsHorizontalTemplate = compileTemplate("reddit-horizontal-cards.html", "widget-base.html")
RedditCardsVerticalTemplate = compileTemplate("reddit-vertical-cards.html", "widget-base.html") RedditCardsVerticalTemplate = compileTemplate("reddit-vertical-cards.html", "widget-base.html")
ReleasesTemplate = compileTemplate("releases.html", "widget-base.html") ReleasesTemplate = compileTemplate("releases.html", "widget-base.html")
ChangesTemplate = compileTemplate("changes.html", "widget-base.html")
VideosTemplate = compileTemplate("videos.html", "widget-base.html") VideosTemplate = compileTemplate("videos.html", "widget-base.html")
StocksTemplate = compileTemplate("stocks.html", "widget-base.html") StocksTemplate = compileTemplate("stocks.html", "widget-base.html")
RSSListTemplate = compileTemplate("rss-list.html", "widget-base.html") RSSListTemplate = compileTemplate("rss-list.html", "widget-base.html")

View File

@ -0,0 +1,17 @@
{{ template "widget-base.html" . }}
{{ define "widget-content" }}
<ul class="list list-gap-14 list-collapsible">
{{ range $i, $watch := .ChangeDetections }}
<li {{ if shouldCollapse $i $.CollapseAfter }}class="list-collapsible-item" style="--animation-delay: {{ itemAnimationDelay $i $.CollapseAfter }};"{{ end }}>
<a class="size-h4 block text-truncate color-primary-if-not-visited" href="{{ $watch.Url }}" target="_blank" rel="noreferrer">{{ .Name }}</a>
<ul class="list-horizontal-text">
<li title="{{ $watch.LastChanged | formatTime }}" {{ dynamicRelativeTimeAttrs $watch.LastChanged }}>{{ $watch.LastChanged | relativeTime }}</li>
</ul>
</li>
{{ end }}
</ul>
{{ if gt (len .ChangeDetections) $.CollapseAfter }}
<label class="list-collapsible-label"><input type="checkbox" autocomplete="off" class="list-collapsible-input"></label>
{{ end }}
{{ end }}

View File

@ -0,0 +1,79 @@
package feed
import (
"fmt"
"log/slog"
"net/http"
"time"
)
type changeDetectionResponseJson struct {
Name string `json:"title"`
Url string `json:"url"`
LastChanged int `json:"last_changed"`
}
func parseLastChangeTime(t int) time.Time {
parsedTime := time.Unix(int64(t), 0)
return parsedTime
}
func FetchLatestDetectedChanges(watches []string, token string) (ChangeWatches, error) {
changeWatches := make(ChangeWatches, 0, len(watches))
if len(watches) == 0 {
return changeWatches, nil
}
requests := make([]*http.Request, len(watches))
for i, repository := range watches {
request, _ := http.NewRequest("GET", fmt.Sprintf("https://changedetection.knhash.in/api/v1/watch/%s", repository), nil)
if token != "" {
request.Header.Add("x-api-key", token)
}
requests[i] = request
}
task := decodeJsonFromRequestTask[changeDetectionResponseJson](defaultClient)
job := newJob(task, requests).withWorkers(15)
responses, errs, err := workerPoolDo(job)
if err != nil {
return nil, err
}
var failed int
for i := range responses {
if errs[i] != nil {
failed++
slog.Error("Failed to fetch or parse change detections", "error", errs[i], "url", requests[i].URL)
continue
}
watch := responses[i]
changeWatches = append(changeWatches, ChangeWatch{
Name: watch.Name,
Url: watch.Url,
LastChanged: parseLastChangeTime(watch.LastChanged),
})
}
if len(changeWatches) == 0 {
return nil, ErrNoContent
}
changeWatches.SortByNewest()
if failed > 0 {
return changeWatches, fmt.Errorf("%w: could not get %d watches", ErrPartialContent, failed)
}
return changeWatches, nil
}

View File

@ -48,6 +48,14 @@ type AppRelease struct {
type AppReleases []AppRelease type AppReleases []AppRelease
type ChangeWatch struct {
Name string
Url string
LastChanged time.Time
}
type ChangeWatches []ChangeWatch
type Video struct { type Video struct {
ThumbnailUrl string ThumbnailUrl string
Title string Title string
@ -200,6 +208,14 @@ func (r AppReleases) SortByNewest() AppReleases {
return r return r
} }
func (r ChangeWatches) SortByNewest() ChangeWatches {
sort.Slice(r, func(i, j int) bool {
return r[i].LastChanged.After(r[j].LastChanged)
})
return r
}
func (v Videos) SortByNewest() Videos { func (v Videos) SortByNewest() Videos {
sort.Slice(v, func(i, j int) bool { sort.Slice(v, func(i, j int) bool {
return v[i].TimePosted.After(v[j].TimePosted) return v[i].TimePosted.After(v[j].TimePosted)

View File

@ -0,0 +1,51 @@
package widget
import (
"context"
"html/template"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
type ChangeDetections struct {
widgetBase `yaml:",inline"`
ChangeDetections feed.ChangeWatches `yaml:"-"`
Watches []string `yaml:"watches"`
Token OptionalEnvString `yaml:"token"`
Limit int `yaml:"limit"`
CollapseAfter int `yaml:"collapse-after"`
}
func (widget *ChangeDetections) Initialize() error {
widget.withTitle("Changes").withCacheDuration(2 * time.Hour)
if widget.Limit <= 0 {
widget.Limit = 10
}
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
widget.CollapseAfter = 5
}
return nil
}
func (widget *ChangeDetections) Update(ctx context.Context) {
watches, err := feed.FetchLatestDetectedChanges(widget.Watches, string(widget.Token))
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
if len(watches) > widget.Limit {
watches = watches[:widget.Limit]
}
widget.ChangeDetections = watches
}
func (widget *ChangeDetections) Render() template.HTML {
return widget.render(widget, assets.ChangesTemplate)
}

View File

@ -43,8 +43,10 @@ func New(widgetType string) (Widget, error) {
return &TwitchGames{}, nil return &TwitchGames{}, nil
case "twitch-channels": case "twitch-channels":
return &TwitchChannels{}, nil return &TwitchChannels{}, nil
case "changes":
return &ChangeDetections{}, nil
default: default:
return nil, fmt.Errorf("unknown widget type: %s", widgetType) return nil, fmt.Errorf("unknown widget type: %s found", widgetType)
} }
} }