Skip to content

Commit

Permalink
Merge pull request #32 from piqoni/add_images
Browse files Browse the repository at this point in the history
Add show_images option to prevew article image (Reddit)
  • Loading branch information
piqoni authored May 16, 2023
2 parents 6f79e1e + e993342 commit e1a3eab
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ markdown_file_prefix:
markdown_file_suffix:
reading_time: false
openai_api_key:
summary_feeds: `
summary_feeds:
show_images: false`

func parseOPML(xmlContent []byte) []RSS {
o := Opml{}
Expand Down Expand Up @@ -166,6 +167,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 {
Expand Down
53 changes: 53 additions & 0 deletions feeds_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "regexp"
import (
"fmt"
"regexp"
)

const regex = `<.*?>`

Expand All @@ -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
}

0 comments on commit e1a3eab

Please sign in to comment.