|
| 1 | +// |
| 2 | +// rss2 is a golang package for working with RSS 2 feeds and documents. |
| 3 | +// |
| 4 | +// @author R. S. Doiel, <rsdoiel@caltech.edu> |
| 5 | +// |
| 6 | +// Copyright (c) 2016, Caltech |
| 7 | +// All rights not granted herein are expressly reserved by Caltech. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 14 | +// |
| 15 | +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | +// |
| 19 | +package rss2 |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/xml" |
| 23 | + "fmt" |
| 24 | + "html/template" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | +) |
| 28 | + |
| 29 | +type RSS2 struct { |
| 30 | + XMLName xml.Name `xml:"rss" json:"-"` |
| 31 | + Version string `xml:"version,attr" json:"version"` |
| 32 | + // Required |
| 33 | + Title string `xml:"channel>title" json:"title"` |
| 34 | + Link string `xml:"channel>link" json:"link"` |
| 35 | + Description string `xml:"channel>description" json:"description"` |
| 36 | + // Optional |
| 37 | + PubDate string `xml:"channel>pubDate" json:"pubDate,omitempty"` |
| 38 | + ItemList []Item `xml:"channel>item" json:"item,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +type Item struct { |
| 42 | + // Optional according to Dave Winer |
| 43 | + Title string `xml:"title" json:"title,omitempty"` |
| 44 | + // Required |
| 45 | + Link string `xml:"link" json:"link"` |
| 46 | + // Optional |
| 47 | + Description template.HTML `xml:"description" json:"description,omitempty"` |
| 48 | + Content template.HTML `xml:"encoded" json:"encoded,omitempty"` |
| 49 | + PubDate string `xml:"pubDate" json:"pubDate,omitempty"` |
| 50 | + Comments string `xml:"comments" json:"comments,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +// Parse return an RSS2 document as a RSS2 structure. |
| 54 | +func Parse(buf []byte) (*RSS2, error) { |
| 55 | + data := new(RSS2) |
| 56 | + err := xml.Unmarshal(buf, &data) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return data, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (r *RSS2) channel(dataPath string) (map[string]interface{}, error) { |
| 64 | + results := make(map[string]interface{}) |
| 65 | + switch { |
| 66 | + case strings.Compare(dataPath, ".channel") == 0: |
| 67 | + // package and return all the channel fields |
| 68 | + results[".title"] = r.Title |
| 69 | + results[".link"] = r.Link |
| 70 | + results[".description"] = r.Description |
| 71 | + if r.PubDate != "" { |
| 72 | + results[".pubDate"] = r.PubDate |
| 73 | + } |
| 74 | + case strings.HasSuffix(dataPath, ".title"): |
| 75 | + results[".title"] = r.Title |
| 76 | + case strings.HasSuffix(dataPath, ".link"): |
| 77 | + results[".link"] = r.Link |
| 78 | + case strings.HasSuffix(dataPath, ".description"): |
| 79 | + results[".description"] = r.Description |
| 80 | + case strings.HasSuffix(dataPath, ".pubDate"): |
| 81 | + results[".pubDate"] = r.PubDate |
| 82 | + default: |
| 83 | + return nil, fmt.Errorf("Unknown data path %s", dataPath) |
| 84 | + } |
| 85 | + return results, nil |
| 86 | +} |
| 87 | + |
| 88 | +type rangeExpression struct { |
| 89 | + first int |
| 90 | + last int |
| 91 | +} |
| 92 | + |
| 93 | +func getRange(listLength int, exp string) *rangeExpression { |
| 94 | + rexp := new(rangeExpression) |
| 95 | + rexp.first = 0 |
| 96 | + rexp.last = listLength - 1 |
| 97 | + |
| 98 | + if strings.Contains(exp, "-") == true { |
| 99 | + nums := strings.SplitN(exp, "-", 2) |
| 100 | + i, err := strconv.Atoi(nums[0]) |
| 101 | + if err == nil { |
| 102 | + rexp.first = i |
| 103 | + } |
| 104 | + i, err = strconv.Atoi(nums[1]) |
| 105 | + if err == nil { |
| 106 | + rexp.last = i |
| 107 | + } |
| 108 | + } else { |
| 109 | + i, err := strconv.Atoi(exp) |
| 110 | + if err == nil { |
| 111 | + rexp.first = i |
| 112 | + rexp.last = i |
| 113 | + } |
| 114 | + } |
| 115 | + return rexp |
| 116 | +} |
| 117 | + |
| 118 | +func (rexp *rangeExpression) inRange(val int) bool { |
| 119 | + if val >= rexp.first && val <= rexp.last { |
| 120 | + return true |
| 121 | + } |
| 122 | + return false |
| 123 | +} |
| 124 | + |
| 125 | +func (r *RSS2) items(dataPath string) (map[string]interface{}, error) { |
| 126 | + rexp := new(rangeExpression) |
| 127 | + rexp.first = 0 |
| 128 | + rexp.last = len(r.ItemList) - 1 |
| 129 | + |
| 130 | + // Get the range expression so we know when to add it to results. |
| 131 | + s := strings.Index(dataPath, "[") |
| 132 | + e := strings.Index(dataPath, "]") |
| 133 | + if s >= 0 && e >= 0 { |
| 134 | + rexp = getRange(len(r.ItemList), dataPath[s:e]) |
| 135 | + } |
| 136 | + |
| 137 | + results := make(map[string]interface{}) |
| 138 | + switch { |
| 139 | + case strings.HasSuffix(dataPath, ".title") == true: |
| 140 | + vals := []string{} |
| 141 | + for i, item := range r.ItemList { |
| 142 | + if rexp.inRange(i) == true { |
| 143 | + vals = append(vals, item.Title) |
| 144 | + } |
| 145 | + } |
| 146 | + results["title"] = vals |
| 147 | + case strings.HasSuffix(dataPath, ".link") == true: |
| 148 | + vals := []string{} |
| 149 | + for i, item := range r.ItemList { |
| 150 | + if rexp.inRange(i) == true { |
| 151 | + vals = append(vals, item.Link) |
| 152 | + } |
| 153 | + } |
| 154 | + results["link"] = vals |
| 155 | + /* |
| 156 | + case strings.HasSuffix(dataPath, ".description") == true: |
| 157 | + vals := []string{} |
| 158 | + for i, item := range r.ItemList { |
| 159 | + if rexp.inRange(i) == true { |
| 160 | + vals = append(vals, item.Description) |
| 161 | + } |
| 162 | + } |
| 163 | + results["description"] = vals |
| 164 | + case strings.HasSuffix(dataPath, ".content") == true: |
| 165 | + vals := []string{} |
| 166 | + for i, item := range r.ItemList { |
| 167 | + if rexp.inRange(i) == true { |
| 168 | + vals = append(vals, item.Content) |
| 169 | + } |
| 170 | + } |
| 171 | + results["content"] = vals |
| 172 | + */ |
| 173 | + case strings.HasSuffix(dataPath, ".pubDate") == true: |
| 174 | + vals := []string{} |
| 175 | + for i, item := range r.ItemList { |
| 176 | + if rexp.inRange(i) == true { |
| 177 | + vals = append(vals, item.PubDate) |
| 178 | + } |
| 179 | + } |
| 180 | + results["pubDate"] = vals |
| 181 | + case strings.HasSuffix(dataPath, ".comments") == true: |
| 182 | + vals := []string{} |
| 183 | + for i, item := range r.ItemList { |
| 184 | + if rexp.inRange(i) == true { |
| 185 | + vals = append(vals, item.Comments) |
| 186 | + } |
| 187 | + } |
| 188 | + results["comments"] = vals |
| 189 | + } |
| 190 | + return results, nil |
| 191 | +} |
| 192 | + |
| 193 | +// Filter given an RSS2 document return all the entries matching so we |
| 194 | +// can apply return each of the data paths requested. |
| 195 | +// e.g. .version, .channel.title, .channel.link, .item[].link, |
| 196 | +// .item[].guid, .item[].title, .item[].description |
| 197 | +func (r *RSS2) Filter(dataPaths []string) (map[string]interface{}, error) { |
| 198 | + var ( |
| 199 | + err error |
| 200 | + data map[string]interface{} |
| 201 | + ) |
| 202 | + result := make(map[string]interface{}) |
| 203 | + for _, dataPath := range dataPaths { |
| 204 | + switch { |
| 205 | + case strings.Compare(dataPath, ".version") == 0: |
| 206 | + result["version"] = r.Version |
| 207 | + case strings.HasPrefix(dataPath, ".channel"): |
| 208 | + data, err = r.channel(dataPath) |
| 209 | + // Merge data into results keyed' by path |
| 210 | + for _, val := range data { |
| 211 | + result[dataPath] = val |
| 212 | + } |
| 213 | + case strings.HasPrefix(dataPath, ".item[]"): |
| 214 | + data, err = r.items(dataPath) |
| 215 | + // Merge data into results keyed' by path |
| 216 | + for _, val := range data { |
| 217 | + result[dataPath] = val |
| 218 | + } |
| 219 | + default: |
| 220 | + return nil, fmt.Errorf("path %q not found", dataPath) |
| 221 | + } |
| 222 | + } |
| 223 | + if result == nil { |
| 224 | + return nil, fmt.Errorf("No data paths found") |
| 225 | + } |
| 226 | + return result, err |
| 227 | +} |
0 commit comments