diff options
| author | John Conway <john.a.conway@gmail.com> | 2019-05-09 22:13:26 +0100 |
|---|---|---|
| committer | John Conway <john.a.conway@gmail.com> | 2019-05-09 22:13:26 +0100 |
| commit | 9fc335c6aebfcb83b8cf6be354f169a237920ad0 (patch) | |
| tree | e53cd4391c5dec0baab1fb91475d8cd418e25e28 /src | |
| parent | 7b132fd2d4afa0cf7e795398d93a3637bd9b8282 (diff) | |
| download | feed-nim-9fc335c6aebfcb83b8cf6be354f169a237920ad0.tar.gz feed-nim-9fc335c6aebfcb83b8cf6be354f169a237920ad0.zip | |
[RSS] Added Tests, fixes, rewrites
Diffstat (limited to 'src')
| -rw-r--r-- | src/feednim/atom.nim | 15 | ||||
| -rw-r--r-- | src/feednim/rss.nim | 50 |
2 files changed, 42 insertions, 23 deletions
diff --git a/src/feednim/atom.nim b/src/feednim/atom.nim index 431bd5e..a432844 100644 --- a/src/feednim/atom.nim +++ b/src/feednim/atom.nim @@ -12,7 +12,7 @@ import streams import sugar type - AtomCommon = ref object of RootObj + AtomCommon = ref object of RootObj # These properties aren't gathered yet xmlbase*: string xmllang*: string @@ -92,7 +92,7 @@ type # Promotes text node to the top of an AtomText object if caller expects a string -converter toString*(obj: AtomText): string = +converter atomToString*(obj: AtomText): string = return obj.text @@ -250,16 +250,13 @@ proc parseAtom* ( data: string ): Atom = else: atom.author = AtomAuthor() - # If there are no entries: - if node.child("entry") == nil: + + if node.child("entry") == nil: # If there are no entries: atom.entries = @[] return atom - # Otherwise, add the entries. - if node.child("entry") != nil: + if node.child("entry") != nil: # Otherwise, add the entries. atom.entries = map( node.findAll("entry"), parseEntry ) # Return the Atom data. - return atom - - + return atom
\ No newline at end of file diff --git a/src/feednim/rss.nim b/src/feednim/rss.nim index abd4883..91fc0a4 100644 --- a/src/feednim/rss.nim +++ b/src/feednim/rss.nim @@ -24,18 +24,24 @@ type webMaster*: string pubDate*: string lastBuildDate*: string - category*: seq[string] + categories*: seq[RSSCategory] generator*: string docs*: string cloud*: RSSCloud - ttl*: string + ttl*: int image*: RSSImage rating*: string textInput*: RSSTextInput - skipHours*: seq[string] + skipHours*: seq[int] skipDays*: seq[string] items*: seq[RSSItem] + RSSText = ref object of RootObj + text*: string + + RSSCategory = ref object of RSSText + domain*: string + RSSEnclosure* = object url*: string length*: string @@ -62,20 +68,36 @@ type name*: string link*: string + RSSSource* = ref object of RSSText + url*: string + RSSItem* = object title*: string link*: string description*: string author*: string - category*: seq[string] + categories*: seq[RSSCategory] comments*: string enclosure*: RSSEnclosure guid*: string pubDate*: string - sourceUrl*: string - sourceText*: string + source*: RSSSource + +converter rssToString*(obj: RSSText): string = + return obj.text + +func parseCategories( node: XmlNode ): seq[RSSCategory] = + var categories:seq[RSSCategory] + for cat_node in node.findAll("category"): + var category: RSSCategory = RSSCategory() + if cat_node.attr("domain") != "": category.domain = cat_node.attr("domain") + category.text = cat_node.innerText + categories.add(category) + if categories.len == 0: return @[] + return categories + -proc parseItem( node: XmlNode) : RSSItem = +func parseItem( node: XmlNode) : RSSItem = var item: RSSItem = RSSItem() if node.child("title") != nil: item.title = node.child("title").innerText @@ -86,8 +108,7 @@ proc parseItem( node: XmlNode) : RSSItem = for key in @["author", "dc:creator"]: if node.child(key) != nil: item.author = node.child(key).innerText - if node.child("category") != nil: - item.category = map(node.findAll("category"), (x: XmlNode) -> string => x.innerText) + if node.child("category") != nil: item.categories = node.parseCategories() if node.child("comments") != nil: item.comments = node.child("comments").innerText @@ -103,8 +124,9 @@ proc parseItem( node: XmlNode) : RSSItem = if node.child("pubDate") != nil: item.pubDate = node.child("pubDate").innerText if node.child("source") != nil: - item.sourceUrl = node.child("source").attr("url") - item.sourceText = node.child("source").innerText + item.source = RSSSource() + item.source.url = node.child("source").attr("url") + item.source.text = node.child("source").innerText return item @@ -140,7 +162,7 @@ proc parseRSS*(data: string): RSS = if channel.child("lastBuildDate") != nil: rss.lastBuildDate = channel.child("lastBuildDate").innerText - if channel.child("category") != nil: rss.category = map(channel.findAll("category"), (x: XmlNode) -> string => x.innerText) + if channel.child("category") != nil: rss.categories = channel.parseCategories() for key in @["generator", "dc:publisher"]: if channel.child(key) != nil: rss.generator = channel.child(key).innerText @@ -156,7 +178,7 @@ proc parseRSS*(data: string): RSS = cloud.protocol = channel.child("cloud").attr("protocol") rss.cloud = cloud - if channel.child("ttl") != nil: rss.ttl = channel.child("ttl").innerText + if channel.child("ttl") != nil: rss.ttl = channel.child("ttl").innerText.parseInt() if channel.child("image") != nil: var image: RSSImage = RSSImage() @@ -181,7 +203,7 @@ proc parseRSS*(data: string): RSS = rss.textInput = textInput if channel.child("skipHours") != nil: - rss.skipHours = map(channel.findAll("hour"), (x: XmlNode) -> string => x.innerText) + rss.skipHours = map(channel.findAll("hour"), (x: XmlNode) -> int => x.innerText.parseInt() ) if channel.child("skipDays") != nil: rss.skipDays = map(channel.findAll("day"), (x: XmlNode) -> string => x.innerText) |
