~asimpson/podcast-filter

ref: e3a7896c064f64fd7751d6864ad6c44a259f7ec5 podcast-filter/main.go -rw-r--r-- 3.7 KiB
e3a7896c — Adam Simpson feat: add negate query param 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main

import (
	"encoding/xml"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

const index = `
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
		<meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>podcastfilter.com</title>
  </head>
  <body>
    <h1>Welcome to podcastfilter.com</h1>

    <p>To use podcastfilter you need to supply two query params (plus an optional third):</p>

    <ul>
      <li><code>query=SOME TEXT</code></li>
      <li><code>feed=https://example.com/somefeed.xml</code></li>
      <li><code>negate=true</code></li>
    </ul>
    <p>The final URL should look like this: <code>https://podcastfilter.com/?feed=https://example.com/feed.xml&query=Hi&20Bye&negate=true</code></p>

    <p>podcastfilter will then return a new RSS feed you can subscribe to that only includes items whose descriptions contain the query
		(or if negate is true will return a feed of items that don't match the query).</p>
  </body>
</html>
`

type rss struct {
	XMLName     xml.Name `xml:"rss"`
	Version     string   `xml:"version,attr"`
	Title       string   `xml:"channel>title"`
	Link        string   `xml:"channel>link"`
	Language    string   `xml:"channel>language"`
	Copyright   string   `xml:"channel>copyright"`
	Description string   `xml:"channel>description"`
	Image       image    `xml:"channel>image"`
	Items       []item   `xml:"channel>item"`
}

type image struct {
	URL   string `xml:"url"`
	Title string `xml:"title"`
	Link  string `xml:"link"`
}

type item struct {
	Title       string    `xml:"title"`
	Description string    `xml:"description"`
	PubDate     string    `xml:"pubDate"`
	GUID        guid      `xml:"guid"`
	Enclosure   enclosure `xml:"enclosure"`
}

type guid struct {
	IsPermaLink string `xml:"isPermaLink,attr"`
	Text        string `xml:",cdata"`
}

type enclosure struct {
	URL    string `xml:"url,attr"`
	Length string `xml:"length,attr"`
	Type   string `xml:"type,attr"`
}

func downloadAndParse(feedURL string, query string, negate string) string {
	resp, err := http.Get(feedURL)
	shouldNegate := negate == "true"

	if err != nil {
		fmt.Println("http err: ", err)
	}

	xmlData, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	feed := rss{}
	xmlErr := xml.Unmarshal(xmlData, &feed)

	if xmlErr != nil {
		fmt.Println("xml err: ", xmlErr)
	}

	matchedItems := []item{}

	for i := 0; i < len(feed.Items); i++ {
		var item = feed.Items[i]
		matches := strings.Contains(item.Description, query)

		if matches && !shouldNegate {
			matchedItems = append(matchedItems, item)
		}

		if !matches && shouldNegate {
			matchedItems = append(matchedItems, item)
		}
	}

	xmlOutput := &rss{
		Version:     feed.Version,
		Title:       "Filtered Feed: " + feed.Title,
		Copyright:   feed.Copyright,
		Description: "Filtered Feed: " + feed.Description,
		Image:       feed.Image,
		Language:    feed.Language,
		Link:        "https://podcastfilter.com/?feed=" + feedURL + url.PathEscape("&query=") + url.PathEscape(query),
		Items:       matchedItems,
	}

	output, xmlOutputErr := xml.MarshalIndent(xmlOutput, "", "")
	if xmlOutputErr != nil {
		fmt.Println(xmlOutputErr)
	}

	return xml.Header + string(output)
}

func main() {
	handler := func(w http.ResponseWriter, req *http.Request) {
		query := req.URL.Query()
		feedParam := query.Get("feed")

		if feedParam == "" {
			w.Header().Set("Content-Type", "text/html")
			io.WriteString(w, index)
		} else {
			w.Header().Set("Content-Type", "application/rss+xml")
			feed := downloadAndParse(query.Get("feed"), query.Get("query"), query.Get("negate"))
			io.WriteString(w, string(feed))
		}
	}

	http.HandleFunc("/", handler)

	err := http.ListenAndServe(":8005", nil)

	if err != nil {
		fmt.Println("http server err: ", err)
	}
}