File under stupid quick API hacks.
I’ve been taking an even closer look at our API over the past few days for various reasons and in the process threw this together.
http://www.whatisthelatestguardianheadline.com/
Which does pretty much what it says on the tin, aside the fact that the API lags a few minutes behind content going live on the site. The indexes for the API are re-fed every few minutes, the very latest stories are of course over here (probably).
The code is very basic, it’s also on github if you want to pull apart/copy my hastily constructing python.
In essence though it does this API call…
content.guardianapis.com/search?page-size=5 &order-by=newest &format=json
Which gets the most recent 5 headlines from the site, it’s also an example of our “Keyless” API, which as the name suggests can be called without needed to bother with a key. Making it ideal for throwaway things like this.
A quick look at the fragment returned shows us we have this to play with…
-
-
response: {
- status: “ok”
- userTier: “free”
- total: 1239301
- startIndex: 1
- pageSize: 1
- currentPage: 1
- pages: 1239301
- orderBy: “newest”
-
-
results: [
-
-
{
- id: "society/2010/nov/10/yaseer-ahmed-bolton-council-mosques-clore-social-leadership"
- sectionId: "society"
- sectionName: "Society"
- webPublicationDate: "2010-11-10T06:45:01Z"
- webTitle: "Leading questions: Yaseer Ahmed, Bolton Council of Mosques"
- webUrl: "http://www.guardian.co.uk/society/2010/nov/10/yaseer-ahmed-bolton-council-mosques-clore-social-leadership"
- apiUrl: "http://content.guardianapis.com/society/2010/nov/10/yaseer-ahmed-bolton-council-mosques-clore-social-leadership"
}
]
-
}
}
We get the headline, URLs, section and publication date. We can also see the API has as of this moment just over 1.2 million articles in.
The backend code grabs that and shoves it into the database, doing only a tiny bit extra to work out how long ago it first saw the article and the colour to use. All with some extra random caching for no particular reason other than making everything a possible further 60 seconds out of date :)
Front end Ajax loads new (or not-new as the case may be) headlines once a minute. So in theory just leaving it should give you the latest(ish) headlines throughout the day.
I did go back and forth a bit on wether I should make a specific iPad style sheet and so on but decided that was probably too much effort for what it essentially is. I may get round to wrapping it up as a PhoneGap app at some point but only to learn more about PhoneGap.
Idea generally kicked off by, but unrelated to BERG’s Media Surfaces: The Journey, big font and a single page inspired by the many other single serving sites out there. On a black background because everyone complains at me (understandably) when I put Life and Style yellow on a white background, because I’m not particularly keen on the shade of orange they use.
