User:Riviera/Podcast rss: Difference between revisions
No edit summary |
No edit summary |
||
Line 100: | Line 100: | ||
$ cat open.txt close.txt | sort -n > sorted.txt && cat sorted.txt | less | $ cat open.txt close.txt | sort -n > sorted.txt && cat sorted.txt | less | ||
== Fourth Attempt == | |||
I refined the regular expression for the <tt>grep</tt> command. It now matches every opening and closing tag. Also, it's necessary to pass the global option to the sed substitution command. The second sed command prepends whitespace to every opening tag. This improves the structure of the output | |||
<pre> | |||
$ sed 's/<\\//\n<\\//g' rss >rss-out | |||
$ sed "s/<\([[:alpha:]][^>]*>\)/\n<\1/g" rss-out > rss-out-out | |||
$ grep -Eon "<[[:alpha:]]([^>]*>)" rss-out-out > open.txt | |||
$ grep -Eon "</([^>]*>)" rss-out-out > close.txt | |||
$ cat open.txt close.txt | sort -n > sorted.txt | |||
$ grep -Eo "<([^>]*>)" sorted.txt | less > skeleton.xml | |||
$ chromium sorted.xml | |||
</pre> | |||
The webpage complains about unmatched horizontal rule tags. I deleted all of these using the query replace function in Emacs. The webpage subsequently complained about <tt><nowiki><br></nowiki></tt> and <tt><nowiki><br /></nowiki></tt> tags, so I deleted all of these too. The result is what I wanted. It views well in the browser; I am able to collapse tags for an abbreviated overview of the document. |
Revision as of 22:51, 4 October 2023
Podcasts, RSS feeds, grep
On Monday 2nd, we discussed podcasts as opposed to radio. That radio is live whereas podcasts are prerecorded and that modes of engaging with podcasts and radio differ. Podcasts are built upon Really Simple Syndication (RSS). In other words, in terms of code, there's little difference between the XML for an blog feed and the XML for a podcast feed. The following command combines a regular expression with the grep command to retrieve a list of all the XML tags in RSS feed for the Call Me Mother podcast.
This command prints results such as
and
These tags also appear in RSS feeds for written blogs. However, the command also prints results such as
and
Editing
What I want to produce is a list of the tags only. At the moment, I'm not interested in what's in between the tags. Ideally I'd like to use built in commands to generate a text which contains an outline of tags along the lines of the following listing.
1 <tag> 2 <subtag> 3 </subtag> 4 </tag>
First Attempt
Can this be achieved by making two files, open.txt and close.txt? The first file should contain all the opening tags and the latter all the closing tags.
$ grep -Eon "</[[:alpha:]]+>" rss > close.txt
It should then be possible to cat both files and sort the result by line number producing the desired outcome.
Second Attempt
Whilst reading through the output of the first attempt (sorted.txt), I discovered that
- the </guid> tags had no correlating opening tag
- That the closing tags were sorted before the opening tags
Problem 1: Some information was missing
I had already deliberately omitted acast and itunes tags. In doing so I worked on the assumption that there were no other relevant, colon-separated tags in the rss file. Fortunately, retrieving the additional data was a quick fix:
$ grep -Eon "<[[:alpha:]]+>|<[[:alpha:]]+ [^z-A]*>" rss > open.txt
Figuring out a way to sort the file such that the closing tags followed the opening tags was another matter. The general outline was there, but if I could sort out the details this could become a template for a podcast RSS generator. And that could potentially be useful in relation to Worm's sonic archive.
Problem 2: Adding whitespace with sed
The following command prepends a new line to each instance of '</' in the rss file.
sed 's/<\\//\n<\\//' rss >rss-out;
Now it's time to write the open.txt and close.txt files.
$ grep -Eon "<[[:alpha:]]+>|<[[:alpha:]]+ [^z-A]*>" rss-out > open.txt $ grep -Eon "</[[:alpha:]]+>>" rss-out > close.txt
Placing a newline before each closing tag increases the line number on which each closing tag appears. Now it's time to concatenate open.txt and close.txt and view the output.
$ cat open.txt close.txt | sort -n > sorted.txt && cat sorted.txt | less
Third Attempt
On closer inspection of the output of sorted.txt I noticed a further lack of information. Various closing anchor tags had no corresponding opening tag. I therefore had to write a regular expression capable of matching the additional data in these tags. This might make the output of sorted.txt a little less readable, Although there may be a way to tidy up this information. For completeness, it would also make sense to include the acast and itunes tags.
$ grep -Eon "<[[:alpha:]]+>|<[[:alpha:]]+ [^z-A]*>|<[[:alpha:]]+ [[:alpha:]]+([=\":/'.;_ ]|[[:alnum:]])*>|<itunes:[[:alpha:]]+>|<acast:[[:alpha:]]+>" rss-out > open.txt $ grep -Eon "</[[:alpha:]]+>|</itunes:[[:alpha:]]+>|</acast:[[:alpha:]]+>" rss-out > close.txt
Then, as above
$ cat open.txt close.txt | sort -n > sorted.txt && cat sorted.txt | less
Fourth Attempt
I refined the regular expression for the grep command. It now matches every opening and closing tag. Also, it's necessary to pass the global option to the sed substitution command. The second sed command prepends whitespace to every opening tag. This improves the structure of the output
$ sed 's/<\\//\n<\\//g' rss >rss-out $ sed "s/<\([[:alpha:]][^>]*>\)/\n<\1/g" rss-out > rss-out-out $ grep -Eon "<[[:alpha:]]([^>]*>)" rss-out-out > open.txt $ grep -Eon "</([^>]*>)" rss-out-out > close.txt $ cat open.txt close.txt | sort -n > sorted.txt $ grep -Eo "<([^>]*>)" sorted.txt | less > skeleton.xml $ chromium sorted.xml
The webpage complains about unmatched horizontal rule tags. I deleted all of these using the query replace function in Emacs. The webpage subsequently complained about <br> and <br /> tags, so I deleted all of these too. The result is what I wanted. It views well in the browser; I am able to collapse tags for an abbreviated overview of the document.