this can help figuring out which feed is broken when fetching for example
This commit is contained in:
parent
894e66a6de
commit
a23318e25e
3 changed files with 28 additions and 15 deletions
19
src/rss.nim
19
src/rss.nim
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[algorithm, json, xmltree, times, options, sequtils]
|
import std/[algorithm, json, xmltree, times, options, sequtils, strformat]
|
||||||
import pkg/[mummy, rssatom, puppy, pretty]
|
import pkg/[mummy, rssatom, puppy, pretty]
|
||||||
|
import ./[shared]
|
||||||
|
|
||||||
const
|
const
|
||||||
atomTimeFormat* {.strdefine.} = "yyyy-MM-dd'T'HH:mm:ss'Z'"
|
atomTimeFormat* {.strdefine.} = "yyyy-MM-dd'T'HH:mm:ss'Z'"
|
||||||
|
|
@ -32,7 +33,9 @@ proc getTime(r: RssItem): DateTime =
|
||||||
proc cmpRssItem(x, y: RssItem): int =
|
proc cmpRssItem(x, y: RssItem): int =
|
||||||
cmp(x.getTime(), y.getTime())
|
cmp(x.getTime(), y.getTime())
|
||||||
|
|
||||||
proc mixRssFeeds*(feeds: seq[RssFeed], name, id, link, authorName, description:string): RSS =
|
proc mixRssFeeds*(
|
||||||
|
feeds: seq[RssFeed], name, id, link, authorName, description: string
|
||||||
|
): RSS =
|
||||||
result = RSS()
|
result = RSS()
|
||||||
result.id = name.some()
|
result.id = name.some()
|
||||||
result.title = id.some()
|
result.title = id.some()
|
||||||
|
|
@ -41,18 +44,22 @@ proc mixRssFeeds*(feeds: seq[RssFeed], name, id, link, authorName, description:s
|
||||||
result.description = description.some()
|
result.description = description.some()
|
||||||
var entries: seq[RSS]
|
var entries: seq[RSS]
|
||||||
for feed in feeds:
|
for feed in feeds:
|
||||||
|
relDebug:
|
||||||
|
echo &"[feed {name}] fetching {feed.url}"
|
||||||
|
let fetched = fetch(feed.url)
|
||||||
case feed.kind
|
case feed.kind
|
||||||
of Atom:
|
of Atom:
|
||||||
entries.add parseAtom(fetch(feed.url))
|
entries.add parseAtom(fetched)
|
||||||
of RSSv2:
|
of RSSv2:
|
||||||
entries.add parseRss(fetch(feed.url))
|
entries.add parseRss(fetched)
|
||||||
|
|
||||||
entries[^1].items = entries[^1].items.mapIt(
|
entries[^1].items = entries[^1].items.mapIt(
|
||||||
block:
|
block:
|
||||||
var item = it
|
var item = it
|
||||||
if feed.timeFormat.len != 0 and item.pubDate.isSome():
|
if feed.timeFormat.len != 0 and item.pubDate.isSome():
|
||||||
# if the IDE screams at you here, ignore it
|
# if the IDE screams at you here, ignore it
|
||||||
item.pubdate = some(parse(item.pubDate.get(), feed.timeFormat).format(atomTimeFormat))
|
item.pubdate =
|
||||||
|
some(parse(item.pubDate.get(), feed.timeFormat).format(atomTimeFormat))
|
||||||
else:
|
else:
|
||||||
item.pubdate = some(getRssv2Time(item).format(atomTimeFormat))
|
item.pubdate = some(getRssv2Time(item).format(atomTimeFormat))
|
||||||
item
|
item
|
||||||
|
|
@ -63,7 +70,7 @@ proc mixRssFeeds*(feeds: seq[RssFeed], name, id, link, authorName, description:s
|
||||||
block:
|
block:
|
||||||
var item = it
|
var item = it
|
||||||
if item.title.isSome():
|
if item.title.isSome():
|
||||||
item.title = some(feed.titlePrefix&item.title.get())
|
item.title = some(feed.titlePrefix & item.title.get())
|
||||||
item
|
item
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@ proc atomHandler(r: Request) {.gcsafe, thread.} =
|
||||||
headers["Content-Type"] = "application/atom+xml"
|
headers["Content-Type"] = "application/atom+xml"
|
||||||
|
|
||||||
var cfg = configs[name]
|
var cfg = configs[name]
|
||||||
let mixed = mixRssFeeds(cfg, "rssmix: "&name, name, "/atom/"&name, "rssmix", "mix of various rss feeds")
|
let mixed =
|
||||||
|
mixRssFeeds(cfg, name, name, "/atom/" & name, "rssmix", "mix of various rss feeds")
|
||||||
|
|
||||||
r.respond(200, headers, body = $assembleAtom(mixed))
|
r.respond(200, headers, body = $assembleAtom(mixed))
|
||||||
|
|
||||||
|
|
@ -45,11 +46,11 @@ proc stop() =
|
||||||
server.close()
|
server.close()
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
proc addfeeds(name:string, feeds:seq[RssFeed])=
|
proc addfeeds(name: string, feeds: seq[RssFeed]) =
|
||||||
if configs.contains(name): raise newException(KeyError, "duplicate config: "&name)
|
if configs.contains(name):
|
||||||
|
raise newException(KeyError, "duplicate config: " & name)
|
||||||
configs[name] = feeds
|
configs[name] = feeds
|
||||||
|
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
block:
|
block:
|
||||||
var cfgs = commandLineParams()
|
var cfgs = commandLineParams()
|
||||||
|
|
@ -63,16 +64,15 @@ proc main() =
|
||||||
let cfg = readFile(cfg).parseConfig()
|
let cfg = readFile(cfg).parseConfig()
|
||||||
addfeeds(cfg.name, cfg.feeds)
|
addfeeds(cfg.name, cfg.feeds)
|
||||||
elif dirExists(cfg):
|
elif dirExists(cfg):
|
||||||
for f in walkFiles(cfg/"*"):
|
for f in walkFiles(cfg / "*"):
|
||||||
let cfg = readFile(f).parseConfig()
|
let cfg = readFile(f).parseConfig()
|
||||||
addfeeds(cfg.name, cfg.feeds)
|
addfeeds(cfg.name, cfg.feeds)
|
||||||
if configs.len == 0: quit("no configs found.", QuitFailure)
|
if configs.len == 0:
|
||||||
|
quit("no configs found.", QuitFailure)
|
||||||
echo "registered feeds:"
|
echo "registered feeds:"
|
||||||
for key, value in configs.pairs:
|
for key, value in configs.pairs:
|
||||||
echo " - ", key, " (", value.len, " sub-feeds)"
|
echo " - ", key, " (", value.len, " sub-feeds)"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var router: Router
|
var router: Router
|
||||||
router.get("/atom/@name", atomHandler)
|
router.get("/atom/@name", atomHandler)
|
||||||
|
|
||||||
|
|
|
||||||
6
src/shared.nim
Normal file
6
src/shared.nim
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
import std/[os]
|
||||||
|
export existsEnv
|
||||||
|
|
||||||
|
template relDebug*(body) =
|
||||||
|
if existsEnv("RSSMIX_DEBUG"):
|
||||||
|
body
|
||||||
Loading…
Add table
Add a link
Reference in a new issue