From d6eab32bb6b0669b96be77976481d04902e19b6a Mon Sep 17 00:00:00 2001 From: Lucidiot Date: Sun, 15 Dec 2019 13:17:56 +0100 Subject: [PATCH] Support XML namespaces in json2xml for thumbnails in danish feeds --- bin/json2xml | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-- feedgen.sh | 6 +++--- jq/denmark.jq | 3 +-- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/bin/json2xml b/bin/json2xml index fa18f4d..8fc123d 100755 --- a/bin/json2xml +++ b/bin/json2xml @@ -1,6 +1,60 @@ #!/usr/bin/env python3 -import fileinput +import argparse import json import xmltodict +import sys -print(xmltodict.unparse(json.loads('\n'.join(fileinput.input())))) + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + 'infile', + type=argparse.FileType('r'), + nargs='?', + default=sys.stdin, + ) + parser.add_argument( + 'outfile', + type=argparse.FileType('w'), + nargs='?', + default=sys.stdout, + ) + parser.add_argument( + '-s', + '--short-empty', + help='Shorten empty elements to instead of ', + action='store_true', + default=False, + ) + parser.add_argument( + '-p', + '--pretty', + help='Indent XML with tabs and newlines.', + action='store_true', + default=False, + ) + parser.add_argument( + '-ns', + '--namespace', + help='Add a namespace abbreviation.\n' + 'Use the empty string as a name to set a default namespace.\n' + 'xmltodict will expect the URL in your JSON objects:\n' + '{"http://my/ns:tag": "something"}', + action='append', + nargs=2, + default=[], + metavar=('URL', 'NAME'), + ) + + args = parser.parse_args() + + args.outfile.write(xmltodict.unparse( + json.load(args.infile), + pretty=args.pretty, + namespaces=dict(args.namespace), + short_empty_elements=args.short_empty, + )) + + +if __name__ == '__main__': + main() diff --git a/feedgen.sh b/feedgen.sh index 915195b..9159a5b 100755 --- a/feedgen.sh +++ b/feedgen.sh @@ -74,7 +74,7 @@ curl -s 'https://en.havarikommissionen.dk/aviation-archive/' \ --arg title 'AIBD Aviation' \ --arg description 'Accident Investigation Board Denmark aviation accident reports' \ --arg link 'https://en.havarikommissionen.dk/aviation-archive/' \ - | json2xml > $DIR/feeds/aibd/aviation.xml.new \ + | json2xml -s -ns 'http://search.yahoo.com/mrss/' media > $DIR/feeds/aibd/aviation.xml.new \ && mv $DIR/feeds/aibd/aviation.xml.new $DIR/feeds/aibd/aviation.xml echo Building AIBD Rail feed to $DIR/feeds/aibd/rail.xml @@ -84,7 +84,7 @@ curl -s 'https://en.havarikommissionen.dk/railway-archive/' \ --arg title 'AIBD Rail' \ --arg description 'Accident Investigation Board Denmark railroad accident reports' \ --arg link 'https://en.havarikommissionen.dk/railway-archive/' \ - | json2xml > $DIR/feeds/aibd/rail.xml.new \ + | json2xml -s -ns 'http://search.yahoo.com/mrss/' media > $DIR/feeds/aibd/rail.xml.new \ && mv $DIR/feeds/aibd/rail.xml.new $DIR/feeds/aibd/rail.xml echo Building DMAIB feed to $DIR/feeds/dmaib.xml @@ -94,5 +94,5 @@ curl -s 'https://dmaib.com/reports/?categorizations=9510' \ --arg title 'DMAIB' \ --arg description 'Danish Maritime Accident Investigation Board accident reports' \ --arg link 'https://dmaib.com/reports/?categorizations=9510' \ - | json2xml > $DIR/feeds/dmaib.xml.new \ + | json2xml -s -ns 'http://search.yahoo.com/mrss/' media > $DIR/feeds/dmaib.xml.new \ && mv $DIR/feeds/dmaib.xml.new $DIR/feeds/dmaib.xml diff --git a/jq/denmark.jq b/jq/denmark.jq index cba075b..f775e84 100644 --- a/jq/denmark.jq +++ b/jq/denmark.jq @@ -12,7 +12,6 @@ { "rss": { "@version": "2.0", - "@xmlns:media": "http://search.yahoo.com/mrss/", "channel": { "title": $title, "description": $description, @@ -28,7 +27,7 @@ "link": .["data-url"], "pubDate": (first(.children[-1].children[] | select(.class == "date")).text[-10:] | strptime("%d-%m-%Y") | mktime | strftime("%a, %d %b %Y %T %z")), "category": [.children[-1].children[] | select(.class == "labels") | .children[].text], - "media:thumbnail": ( + "http://search.yahoo.com/mrss/:thumbnail": ( if .children[0].class == "media-left" then { "@url": .children[0].children[0].src } else empty end