Something went wrong. Try again.
This string has no description
Something went wrong. Try again.
3.3 kB · 67 lines · Created 2w ago Nushell
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768def main [--lookup(-l), --add(-a)] { const current_path = path self | path dirname ls $current_path | where name ends-with '.opml' | collect {|files| if ($files | length) > 1 { $files | input list | collect {|input| $input.name } } else { $files.0.name } | collect {|file| let opml_file = open $file let new_opml_file = $'new_($file)' let lookup_rss = $"($current_path)/lookup-rss.nu"
'<?xml version="1.0" encoding="UTF-8"?>' | save -f $new_opml_file
({tag: opml attributes: {version: "2.0"} content : [ ({tag: head content: (($opml_file | query xml '//head/*' --output-string-value --output-names | update cells {|| | str trim } string_value | select local_name string_value | transpose -r -d | collect {|x| if $lookup or $add { $x | update dateModified {(date now | date to-timezone UTC | format date "%a, %d %b %Y %H:%M:%S GMT")} } else { $x } }) | items {|key, value| {tag: $key content: [$value]} }) } | transpose -r | transpose -d tag content) {tag: body content : ( 1..(($opml_file | query xml 'count(//body/*)') | into int) | each {|num|
let tag = { tag: 'outline' attributes: ( $opml_file | query xml $'//outline[($num)]/@*' --output-string-value --output-names | select local_name string_value | transpose -r -d | select -o text description htmlUrl language title type version xmlUrl | record where value != null ) } | to xml -s let tag_attributes = ($tag | from xml).attributes
if $lookup { $tag_attributes.xmlUrl | collect {|xml_url| if ($xml_url | is-not-empty) { (nu $lookup_rss $xml_url) } else { (nu $lookup_rss $tag_attributes.htmlUrl) } | collect {|| from xml } } } else { $tag | from xml } } | collect {|x| if $add { $x | append (nu $lookup_rss (input 'url: ') | from xml)} else { $x } } | uniq-by attributes ) } ] } | to xml -s -p -i 3) | save -a $new_opml_file open $new_opml_file | collect | save -f $file rm $new_opml_file } }}