From bc956cf37d7b592e774a62c143e347fde06d2f0e Mon Sep 17 00:00:00 2001 From: piqoni Date: Tue, 16 May 2023 22:59:17 +0200 Subject: [PATCH 1/2] Add show_images option --- config.go | 4 +++- feeds_writer.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 11 +++++++++- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 4c51de7..28cf43b 100644 --- a/config.go +++ b/config.go @@ -34,7 +34,8 @@ markdown_file_prefix: markdown_file_suffix: reading_time: false openai_api_key: -summary_feeds: ` +summary_feeds: +show_images: true` func parseOPML(xmlContent []byte) []RSS { o := Opml{} @@ -167,6 +168,7 @@ func bootstrapConfig() { instapaper = viper.GetBool("instapaper") reading_time = viper.GetBool("reading_time") + show_images = viper.GetBool("show_images") // Overwrite terminal_mode from config file only if its not set through -t flag if !terminal_mode { diff --git a/feeds_writer.go b/feeds_writer.go index 0ece6c0..6d25e6c 100644 --- a/feeds_writer.go +++ b/feeds_writer.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/PuerkitoBio/goquery" readability "github.com/go-shiori/go-readability" "github.com/mmcdole/gofeed" "github.com/savioxavier/termlink" @@ -24,6 +25,7 @@ var lat, lon float64 var instapaper bool var openaiApiKey string var reading_time bool +var show_images bool var myFeeds []RSS var db *sql.DB @@ -135,6 +137,50 @@ func writeToMarkdown(body string) { } } +func ExtractImageTagFromHTML(htmlText string) string { + doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlText)) + if err != nil { + return "" // Error occurred while parsing HTML + } + + imgTags := doc.Find("img") + + if imgTags.Length() == 0 { + return "" // No img tag found, return empty string + } + + firstImgTag := imgTags.First() + + width := firstImgTag.AttrOr("width", "") + height := firstImgTag.AttrOr("height", "") + + // If both width and height are present, calculate the aspect ratio and set the maximum width + if width != "" && height != "" { + widthInt := stringToInt(width) + heightInt := stringToInt(height) + + if widthInt > 0 && heightInt > 0 { + aspectRatio := float64(widthInt) / float64(heightInt) + maxWidth := 400 + + if widthInt > maxWidth { + widthInt = maxWidth + heightInt = int(float64(widthInt) / aspectRatio) + } + + firstImgTag.SetAttr("width", fmt.Sprintf("%d", widthInt)) + firstImgTag.SetAttr("height", fmt.Sprintf("%d", heightInt)) + } + } + + html, err := goquery.OuterHtml(firstImgTag) + if err != nil { + return "" // Error occurred while extracting the HTML of the img tag + } + + return html // Return the modified img tag +} + // Parses the feed URL and returns the feed object func parseFeed(fp *gofeed.Parser, url string, limit int) *gofeed.Feed { feed, err := fp.ParseURL(url) @@ -193,6 +239,13 @@ func generateFeedItems(feed *gofeed.Feed, rss *RSS) string { items += writeSummary(summary, true) } + if show_images && !terminal_mode { + img := ExtractImageTagFromHTML(item.Content) + if img != "" { + items += img + "\n" + } + } + // Add the item to the seen table if not seen today if !seen_today { addToSeenTable(item.Link, summary) diff --git a/utils.go b/utils.go index 5069d47..125b6c8 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,9 @@ package main -import "regexp" +import ( + "fmt" + "regexp" +) const regex = `<.*?>` @@ -9,3 +12,9 @@ func stripHtmlRegex(s string) string { r := regexp.MustCompile(regex) return r.ReplaceAllString(s, "") } + +func stringToInt(s string) int { + var n int + fmt.Sscanf(s, "%d", &n) + return n +} From e9933422eb504f38720f6866a470ae1963183602 Mon Sep 17 00:00:00 2001 From: piqoni Date: Tue, 16 May 2023 22:59:40 +0200 Subject: [PATCH 2/2] default false for now --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index 28cf43b..0f4e465 100644 --- a/config.go +++ b/config.go @@ -35,7 +35,7 @@ markdown_file_suffix: reading_time: false openai_api_key: summary_feeds: -show_images: true` +show_images: false` func parseOPML(xmlContent []byte) []RSS { o := Opml{}