add tags and skipping tags

Amolith created

Change summary

main.go      | 50 ++++++++++++++++++++++++++++++++++++++------------
template.txt |  2 ++
2 files changed, 40 insertions(+), 12 deletions(-)

Detailed changes

main.go 🔗

@@ -26,11 +26,13 @@ var (
 	flagSensitive    *bool   = flag.BoolP("sensitive", "S", false, "Mark the post as sensitive (optional)")
 	flagAuthenticate *bool   = flag.BoolP("authenticate", "a", false, "Authenticate with the server")
 	flagInstance     *string = flag.StringP("instance", "i", "", "Instance to post to (required)")
+	flagTagsSkip     *string = flag.StringP("tagsskip", "T", "", "Particular tags to skip (optional)")
 )
 
 type post struct {
 	Title       string
 	Description string `json:"status"`
+	Tags        string
 	Image       string `json:"media_attachments"`
 	Link        string
 	Visibility  string `json:"visibility"`
@@ -44,24 +46,49 @@ func main() {
 
 	fp := gofeed.NewParser()
 	feed, _ := fp.ParseURL(*flagFeed)
-	description, _ := htmlToPlaintext(feed.Items[0].Description)
-	description = strings.ReplaceAll(description, "\n\n", " ")
-	description = strings.ReplaceAll(description, "\n", " ")
-	splitDesc := strings.Split(description, " ")
-	if len(splitDesc) > 50 {
-		description = strings.Join(splitDesc[:50], " ")
+
+	skippedTags := strings.Split(*flagTagsSkip, ",")
+	skippedTagsMaps := make(map[string]int)
+	for i := range skippedTags {
+		skippedTagsMaps[skippedTags[i]] = 0
+	}
+	var i int
+	tags := strings.Split(feed.Items[i].Categories[0], "/")
+	for i, tag := range tags {
+		if _, ok := skippedTagsMaps[tag]; ok {
+			i++
+			continue
+		}
+		break
 	}
 
 	post := post{
-		Title:       feed.Items[0].Title,
-		Description: description + "...",
-		Link:        feed.Items[0].Link,
+		Title: feed.Items[i].Title,
+		Link:  feed.Items[i].Link,
+	}
+
+	if feed.Items[i].Image != nil {
+		post.Image = feed.Items[i].Image.URL
 	}
 
-	if feed.Items[0].Image != nil {
-		post.Image = feed.Items[0].Image.URL
+	for i, tag := range tags {
+		tag = "#" + strings.ReplaceAll(tag, " ", "")
+		if i == 0 {
+			post.Tags = tag
+			continue
+		}
+		post.Tags += " " + tag
 	}
 
+	description, _ := htmlToPlaintext(feed.Items[i].Description)
+	description = strings.ReplaceAll(description, "\n\n", " ")
+	description = strings.ReplaceAll(description, "\n", " ")
+	splitDesc := strings.Split(description, " ")
+	if len(splitDesc) > 50 {
+		description = strings.Join(splitDesc[:50], " ")
+	}
+	post.Description = description + "..."
+
 	tFile, err := os.ReadFile(*flagTemplate)
 	if err != nil {
 		fmt.Println(err)
@@ -80,7 +107,6 @@ func main() {
 	post.Sensitive = *flagSensitive
 	post.Spoiler = *flagSpoiler
 	post.Visibility = *flagVisibility
-
 	postBytes, err := json.Marshal(post)
 	if err != nil {
 		fmt.Println(err)

template.txt 🔗

@@ -2,3 +2,5 @@
 {{ .Link }}
 
 {{ .Description }}
+
+{{ .Tags }}