Benji
Benji

Reputation: 635

Using news api in bash and write to webpage

I'm writing a script that generates a webpage and I want it to display current news. I figured to use The Guardians API but I can't figure out how to make it work. Here is the link to The Guardian's API http://www.theguardian.com/open-platform/getting-started The code I have now is:

#!/bin/bash
news()
{
cat <<- _EOF_


_EOF_


write_page()
{
    cat <<- _EOF_
    <html>
        <head>
        </head>
        <body>
        $(news)
        </body>
    </html>
_EOF_
}
write_page > testNews.html

I figured I need to put some HTML text within the cat <<- _EOF_ and _EOF_ but I don't know how to make it work. I'm very new to working with API and bash

Upvotes: 0

Views: 68

Answers (1)

dwurf
dwurf

Reputation: 12749

You can fetch some current news using an API call like this:

curl "http://content.guardianapis.com/search?from-date=2013-11-23&to-date=2013-11-24&format=xml"

The problem is you will get the results in XML format, whereas you want HTML format. If you install xsltproc you could write a simple conversion script guardian.xsl like this:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <ol>
    <xsl:for-each select="response/results/content">
    <li>
        <ul>
          <li><xsl:value-of select="@web-title"/></li>
          <li><xsl:value-of select="@web-url"/></li>
        </ul>
    </li>
    </xsl:for-each>
    </ol>
</xsl:template>

</xsl:stylesheet>

Your command then becomes something like:

curl -s "http://content.guardianapis.com/search?from-date=2013-11-23&to-date=2013-11-24&format=xml" | xsltproc guardian.xsl -

Upvotes: 1

Related Questions