~reesmichael1/roman

f1f93c24c7385f2ce089dab5bc426822eb3e2382 — Michael Rees 6 months ago c7a2477 threads
Try with using the parallel statement
4 files changed, 27 insertions(+), 12 deletions(-)

M src/romanpkg/feeds.nim
M src/romanpkg/htmlextractor.nim
M src/romanpkg/main.nim
M src/romanpkg/posts.nim
M src/romanpkg/feeds.nim => src/romanpkg/feeds.nim +9 -5
@@ 50,16 50,20 @@ proc displayFeed*(feed: var Feed) {.raises: [RomanError, InterruptError].} =
    raise newException(RomanError, "could not set terminal style: " & e.msg)


proc getFeed*(sub: Subscription): Feed {.raises: [RomanError].} =
proc getFeed*(sub: Subscription): ref Feed {.raises: [RomanError], gcsafe.} =
  try:
    let rssFeed = FeedNim.getRSS(sub.url)
    var feed: ref Feed
    new feed
    if sub.name.len > 0:
      result.title = sub.name
      feed.title = sub.name
    else:
      result.title = rssFeed.title
    result.posts = map(rssFeed.items,
      feed.title = rssFeed.title
    feed.posts = map(rssFeed.items,
      proc (i: RSSItem): Post = postFromRSSItem(i))
    result.updateUnread()
    feed[].updateUnread()
    result = feed
    # result.updateUnread()
  except ValueError:
    raise newException(RomanError, sub.url & " is not a valid URL")
  except:

M src/romanpkg/htmlextractor.nim => src/romanpkg/htmlextractor.nim +5 -4
@@ 8,10 8,11 @@ import errors
from config import conf


proc extractBody*(body: string): string {.raises: [RomanError].} =
proc extractBody*(body: string): string {.raises: [RomanError], gcsafe.} =
  try:
    let width = min(conf.postWidth, terminalWidth())
    let html2text = pyImport("html2text").HTML2Text(bodywidth = width)
    result = html2text.handle(body).to(string)
    # let width = min(conf.postWidth, terminalWidth())
    # let html2text = pyImport("html2text").HTML2Text(bodywidth = width)
    # result = html2text.handle(body).to(string)
    result = body
  except:
    raise newException(RomanError, "could not use Python module html2text")

M src/romanpkg/main.nim => src/romanpkg/main.nim +12 -2
@@ 3,6 3,7 @@ import options
import sequtils
import tables
import threadpool
{.experimental: "parallel".}

import errors
import feeds


@@ 39,10 40,19 @@ proc runMainPath() {.raises: [RomanError, InterruptError].} =
      "Use --subscribe [url] to add some."
    return
  elif subs.len == 1:
    feed = getFeed(subs[0])
    feed = getFeed(subs[0])[]
    feeds = @[feed]
  else:
    feeds = map(subs, getFeed)
    var flowFeeds: seq[FlowVar[ref Feed]]
    for sub in subs:
      parallel:
        flowFeeds.add(spawn getFeed(sub))
        # feeds.add(feed)

    sync()
    feeds = map(flowFeeds, proc(f: FlowVar[ref Feed]): Feed = (^f)[])
    # blockUntilAll(flowFeeds)
    # feeds = map(subs, getFeed)

  while true:
    if feeds.len == 1:

M src/romanpkg/posts.nim => src/romanpkg/posts.nim +1 -1
@@ 59,7 59,7 @@ proc displayPost*(p: Post) {.raises: [RomanError].} =
    raise newException(RomanError, "could not write to the terminal: " & msg)


proc postFromRSSItem*(item: RSSItem): Post {.raises: [RomanError].} =
proc postFromRSSItem*(item: RSSItem): Post {.raises: [RomanError], gcsafe.} =
  result.title = item.title
  result.content = extractBody(item.description)
  result.guid = item.guid