Java /Scala client - date query

65 views
Skip to first unread message

Виктор Параскив

unread,
Jul 24, 2016, 10:17:08 PM7/24/16
to Guardian Open Platform API Forum
Hi,
I'm trying to get yesterday's news in a query like this [scala code] :


import org.joda.time.DateTime

val startDate = DateTime.now().minusDays(1)
val endDate = DateTime.now().minusDays(1)
val fields = "firstPublicationDate,headline,trailText,body,byline,shortUrl"
val tags = "all"

val qSearch = SearchQuery()
 
.pageSize(200)
 
.page(1)
 
.showFields(fields)
 
.showTags(tags)
 
.orderBy("newest")
 
.useDate("published")
 
.fromDate(startDate)
 
.toDate(endDate)

the request 

qSearch.toString

gets me this :

/search?order-by=newest&page-size=200&to-date=2016-07-24T01%3A12%3A33.120%2B01%3A00&page=1&use-date=published&show-fields=firstPublicationDate%2Cheadline%2CtrailText%2Cbody%2Cbyline%2CshortUrl&show-tags=all&from-date=2016-07-24T01%3A12%3A33.120%2B01%3A00

What am I doing wrong?
The date is formatted wrong and the fields are all encoded wrong.

Mariot Chauvin

unread,
Jul 25, 2016, 4:56:34 AM7/25/16
to guardian...@googlegroups.com
Hi,

Rather than transforming query to a string. did you try using it with the client? 

 val client = new GuardianContentClient("your-api-key")
client.getResponse(qSearch)
map { response => println(response.total) }


Cheers,

Mariot

--
You received this message because you are subscribed to the Google Groups "Guardian Open Platform API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guardian-api-t...@googlegroups.com.
To post to this group, send email to guardian...@googlegroups.com.
Visit this group at https://groups.google.com/group/guardian-api-talk.
For more options, visit https://groups.google.com/d/optout.



This e-mail and all attachments are confidential and may also be privileged. If you are not the named recipient, please notify the sender and delete the e-mail and all attachments immediately. Do not disclose the contents to another person. You may not use the information for any purpose, or store, or copy, it in any way.  Guardian News & Media Limited is not liable for any computer viruses or other material transmitted with or as part of this e-mail. You should employ virus checking software.
 
Guardian News & Media Limited is a member of Guardian Media Group plc. Registered Office: PO Box 68164, Kings Place, 90 York Way, London, N1P 2AP.  Registered in England Number 908396


Виктор Параскив

unread,
Jul 25, 2016, 6:15:38 AM7/25/16
to Guardian Open Platform API Forum
Yes of course :)
This is how I print it the Query String. It also tells me there is a one page with zero results.

client.getResponse(qSearch).foreach(response => {
 response
.results.map(GNewsArticle(_)) |> resultProcessor

 
AppLogger(s"\tQuery ${qSearch.toString()} has ${response.pages + 1} pages of size ${response.pageSize} i.e. ${response.total}")
 
if (response.pages > 1)
 
(2 to response.pages)
 
.foreach(pi => client.getResponse(qSearch.page(pi)).foreach(r => r.results.map(GNewsArticle(_)) |> resultProcessor))
})

luke....@guardian.co.uk

unread,
Jul 25, 2016, 6:05:42 PM7/25/16
to Guardian Open Platform API Forum
Hi,

I think your dates in this instance are wrong, you are querying using a to and from date that are the same. 

Rather than: 

val startDate = DateTime.now().minusDays(1)
val endDate = DateTime.now().minusDays(1)

You want:

val startDate = DateTime.now().minusDays(1)..withTimeAtStartOfDay()
val endDate = DateTime.now().minusDays(1).millisOfDay().withMaximumValue()


Hope that helps,
Luke.
Reply all
Reply to author
Forward
0 new messages