From e6affb91d689161971d73a70f22400e32a14324a Mon Sep 17 00:00:00 2001 From: John Conway Date: Fri, 17 May 2019 15:36:59 +0100 Subject: Pack structure (capitalised FeedNim) --- FeedNim.nimble | 2 +- src/FeedNim/wrangler.nim | 85 +++++++++++++++++++++++++++++++++++++++++++++++ src/feednim/atom.nim | 4 +-- src/feednim/jsonfeed.nim | 2 +- tests/test_atom | Bin 707272 -> 707128 bytes tests/test_atom.nim | 4 +-- tests/test_jsonfeed | Bin 607872 -> 612136 bytes tests/test_rss | Bin 470960 -> 470952 bytes 8 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/FeedNim/wrangler.nim diff --git a/FeedNim.nimble b/FeedNim.nimble index 3a2ed49..1377e06 100644 --- a/FeedNim.nimble +++ b/FeedNim.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.0" +version = "0.2.1" author = "John Conway" description = "An Atom, RSS, and JSONfeed parser" license = "MIT" diff --git a/src/FeedNim/wrangler.nim b/src/FeedNim/wrangler.nim new file mode 100644 index 0000000..2afc99c --- /dev/null +++ b/src/FeedNim/wrangler.nim @@ -0,0 +1,85 @@ +import + strUtils + +import + atom, + jsonfeed, + rss + +type + Feed = ref object of JSONFeed + +func wrangleAtomItems( xml_feed:( Atom | Rss ) ):seq[JsonFeedItem] = + var items: seq[JsonFeedItem] = @[] + for atom_item in xml_feed.entries: + + var item = JsonFeedItem() + + item.author.name = atom_item.author.name + item.author.url = atom_item.author.uri + item.title = atom_item.title + + if atom_item.content == "": + item.content_text = atom_item.summary + else: + if atom_item.content.textType == "html" or atom_item.content.textType == "xhtml": + item.content_html = atom_item.content + else: item.content_text = atom_item.content + + item.date_published = atom_item.published + item.date_modified = atom_item.updated + + for category in atom_item.categories: + item.tags.add( category.term ) + + item.attachments[0].url = atom_item.link.href + item.attachments[0].mime_type = atom_item.link.linktype # WONT WORK! + item.attachments[0].title = atom_item.link.title + item.attachments[0].size_in_bytes = atom_item.link.length + + items.add( item ) + + return items + +func wrangleAtom( xml_feed: Atom ): Feed = + var feed = Feed() + + feed.author.name = xml_feed.author.name + feed.author.url = xml_feed.author.uri + feed.author.avatar = xml_feed.icon # Munged! + feed.title = xml_feed.title + feed.home_page_url = xml_feed.link.href # MAYBE NOT + feed.feed_url = xml_feed.link.href + feed.description = xml_feed.subtitle + feed.icon = xml_feed.icon + feed.favicon = xml_feed.icon + feed.items = xml_feed.wrangleAtomItems() + + return feed + +func wrangleRss( xml_feed: Rss ): Feed = + var feed = Feed() + + func rssAuthor( feild:string ): JSONFeedAuthor = + var author = JSONFeedAuthor() + var name = feild.split(" ")[1] # RSS author feilds look like this remember: + if name.len > 3: # joe@bloggs.com (Joe Bloggs) + author.name = name.substr[1 .. name.len()-2] + return author + + feed.author = rssAuthor( xml_feed.managingEditor ) # Munged! + feed.title = xml_feed.title + feed.home_page_url = xml_feed.link.link # MAYBE NOT + feed.feed_url = xml_feed.link.href + feed.description = xml_feed.description + feed.icon = xml_feed.image.url # Munged! + feed.items = xml_feed.wrangleAtomItems() + + return feed + + +proc wrangle*( xml_feed:( Atom | Rss ) ):Feed = + if xml_feed.kind == Atom: + return wrangleAtom( xml_feed ) + elif xml_feed.kind == Rss: + return wrangleRss( xml_feed ) \ No newline at end of file diff --git a/src/feednim/atom.nim b/src/feednim/atom.nim index bbdcbe9..ee60672 100644 --- a/src/feednim/atom.nim +++ b/src/feednim/atom.nim @@ -58,7 +58,7 @@ type linktype*: string hreflang*: string title*: string - length*: string + length*: int AtomEntry* = ref object of AtomCommon id*: string # Required Atom field @@ -137,7 +137,7 @@ func parseLink ( node: XmlNode ): AtomLink = if node.attr("type") != "": link.linktype = node.attr("type") if node.attr("hreflang") != "": link.hreflang = node.attr("hreflang") if node.attr("title") != "": link.title = node.attr("title") - if node.attr("length") != "": link.length = node.attr("length") + if node.attr("length") != "": link.length = node.attr("length").parseInt() return link func parseText ( node: XmlNode ): string = diff --git a/src/feednim/jsonfeed.nim b/src/feednim/jsonfeed.nim index e6562a2..e523778 100644 --- a/src/feednim/jsonfeed.nim +++ b/src/feednim/jsonfeed.nim @@ -10,7 +10,7 @@ import streams import sugar type - JSONFeed* = object + JSONFeed* = ref object of RootObj author*: JSONFeedAuthor version*: string title*: string diff --git a/tests/test_atom b/tests/test_atom index a8473ff..e1e300a 100755 Binary files a/tests/test_atom and b/tests/test_atom differ diff --git a/tests/test_atom.nim b/tests/test_atom.nim index 88b6916..3b8a4c7 100644 --- a/tests/test_atom.nim +++ b/tests/test_atom.nim @@ -45,7 +45,7 @@ test "Read Valid Atom Feed": check feed.link.linktype == "application/xml+atom" check feed.link.hreflang == "en-GB" check feed.link.title == "Bloggs's Planes Trains and Automobiles" - check feed.link.length == "1000000" + check feed.link.length == 1000000 check feed.logo == "http://joe.bloggs/logo.jpeg" check feed.rights == "Copyright Joe and Jane Bloggs" @@ -97,7 +97,7 @@ test "Read Valid Atom Feed": check feed.entries[1].link.linktype == "text/html" check feed.entries[1].link.hreflang == "en-GB" check feed.entries[1].link.title == "Trains!" - check feed.entries[1].link.length == "1000000" + check feed.entries[1].link.length == 1000000 check feed.entries[1].published == "2003-12-13T18:20:02Z" check feed.entries[1].rights == "Copyright Jane Bloggs" check feed.entries[1].summary == "Trains!" diff --git a/tests/test_jsonfeed b/tests/test_jsonfeed index 6779f58..6e809a1 100755 Binary files a/tests/test_jsonfeed and b/tests/test_jsonfeed differ diff --git a/tests/test_rss b/tests/test_rss index ed877b2..acdb45f 100755 Binary files a/tests/test_rss and b/tests/test_rss differ -- cgit v1.2.3