Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreshRSS widget #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions internal/feed/rss.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package feed

import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"fmt"
"html"
"io"
"log/slog"
"net/http"
"net/url"
"regexp"
"sort"
Expand All @@ -25,6 +30,29 @@ type RSSFeedItem struct {
PublishedAt time.Time
}

type FreshRssFeedsGroups struct {
Group_id int
Feed_ids string
}

type FreshRssFeed struct {
Id int
Favicon_id int
Title string
Url string
Site_url string
Is_spark int
Last_updated_on_time int
}

type FreshRSSFeedsAPI struct {
Api_version uint
Auth uint
Last_refreshed_on_time int
Feeds []FreshRssFeed
Feeds_groups []FreshRssFeedsGroups
}

// doesn't cover all cases but works the vast majority of the time
var htmlTagsWithAttributesPattern = regexp.MustCompile(`<\/?[a-zA-Z0-9-]+ *(?:[a-zA-Z-]+=(?:"|').*?(?:"|') ?)* *\/?>`)
var sequentialWhitespacePattern = regexp.MustCompile(`\s+`)
Expand Down Expand Up @@ -195,3 +223,53 @@ func GetItemsFromRSSFeeds(requests []RSSFeedRequest) (RSSFeedItems, error) {

return entries, nil
}

func GetItemsFromFreshRssFeeds(freshrssUrl string, freshrssUser string, freshrsspass string) (RSSFeedItems, error) {
var p FreshRSSFeedsAPI
var feedReqs []RSSFeedRequest
var param = url.Values{}

user_credentials := []byte(fmt.Sprintf("%v:%v", freshrssUser, freshrsspass))
api_key := fmt.Sprintf("%x", md5.Sum(user_credentials))

param.Set("api_key", api_key)
param.Set("feeds", "")
var payload = bytes.NewBufferString(param.Encode())

requestURL := fmt.Sprintf("%v/api/fever.php?api", freshrssUrl)
req, err := http.NewRequest(http.MethodPost, requestURL, payload)

if err != nil {
return nil, fmt.Errorf("could not create freshRss request: %v ", err)
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := http.Client{
Timeout: 10 * time.Second,
}

res, err := client.Do(req)
if err != nil || res.StatusCode != 200 {
return nil, fmt.Errorf("could not connect to freshRss instance: %v", err)
}

resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read freshRss response body: %v", err)
}

errr := json.Unmarshal(resBody, &p)
if errr != nil {
return nil, fmt.Errorf("could not unmarshal freshrss response body: %v", errr)
}

for i := range p.Feeds {
var feedReq RSSFeedRequest
feedReq.Url = p.Feeds[i].Url
feedReq.Title = p.Feeds[i].Title
feedReqs = append(feedReqs, feedReq)
}

return GetItemsFromRSSFeeds(feedReqs)
}
76 changes: 76 additions & 0 deletions internal/widget/freshrss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package widget

import (
"context"
"html/template"
"time"

"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)

type FreshRSS struct {
widgetBase `yaml:",inline"`
FeedRequests []feed.RSSFeedRequest `yaml:"feeds"`
Style string `yaml:"style"`
ThumbnailHeight float64 `yaml:"thumbnail-height"`
CardHeight float64 `yaml:"card-height"`
Items feed.RSSFeedItems `yaml:"-"`
Limit int `yaml:"limit"`
CollapseAfter int `yaml:"collapse-after"`
FreshRSSUrl string `yaml:"freshrss-url"`
FreshRSSUser string `yaml:"freshrss-user"`
FreshRSSApiPass string `yaml:"freshrss-api-pass"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing from string to OptionalEnvString should allow it to use environment variables

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a similar request has been made in the past. If the maintainer wants to do it here as well I will change it.

}

func (widget *FreshRSS) Initialize() error {
widget.withTitle("FreshRSS Feed").withCacheDuration(1 * time.Hour)

if widget.Limit <= 0 {
widget.Limit = 25
}

if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
widget.CollapseAfter = 5
}

if widget.ThumbnailHeight < 0 {
widget.ThumbnailHeight = 0
}

if widget.CardHeight < 0 {
widget.CardHeight = 0
}

return nil
}

func (widget *FreshRSS) Update(ctx context.Context) {

var items feed.RSSFeedItems
var err error

items, err = feed.GetItemsFromFreshRssFeeds(widget.FreshRSSUrl, widget.FreshRSSUser, widget.FreshRSSApiPass)

if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}

if len(items) > widget.Limit {
items = items[:widget.Limit]
}

widget.Items = items
}

func (widget *FreshRSS) Render() template.HTML {
if widget.Style == "horizontal-cards" {
return widget.render(widget, assets.RSSHorizontalCardsTemplate)
}

if widget.Style == "horizontal-cards-2" {
return widget.render(widget, assets.RSSHorizontalCards2Template)
}

return widget.render(widget, assets.RSSListTemplate)
}
2 changes: 2 additions & 0 deletions internal/widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func New(widgetType string) (Widget, error) {
return &Reddit{}, nil
case "rss":
return &RSS{}, nil
case "freshrss":
return &FreshRSS{}, nil
case "monitor":
return &Monitor{}, nil
case "twitch-top-games":
Expand Down