awasu.user
Posts: 81
Joined: Fri Jan 06, 2017 12:50 pm

Postby awasu.user » Wed Jul 26, 2017 8:18 pm

How use API to get unread articles from channel as JSON? I remove my previous code, but I try something like that:

Code: Select all

{%REPEAT% Channels-IfGroupingItems}
{%?GROUP-ITEMS-BY-CHANNEL%}
{
  "{%CHANNEL-METADATA% name}": {
    {%REPEAT% FeedItems} "{%id%}": {
      "title": "{%ITEM-METADATA% name!}",
      "published": "{%ITEM-METADATA% timestamp}",
      "url": "{%ITEM-METADATA% url encode=attr}",
       },

  }
}{%ENDIF%}{%/REPEAT%}{%/REPEAT%}


Problem is in place where I repeat feed item data with ",". After last feed is extra "," and make JSON break.

User avatar
support
Site Admin
Posts: 3021
Joined: Fri Feb 07, 2003 12:48 pm
Location: Melbourne, Australia
Contact:

Postby support » Wed Jul 26, 2017 10:43 pm

{%REPEAT%} has a parameter that lets you insert a string between each repeated item (precisely for this reason :| ) e.g. {%REPEAT% FeedItems insert=","} will insert the comma in-between each feed item, but not after the last one.

awasu.user
Posts: 81
Joined: Fri Jan 06, 2017 12:50 pm

Postby awasu.user » Thu Jul 27, 2017 8:14 am

I try modified template:

Code: Select all

{%REPEAT% Channels-IfGroupingItems insert=","}{%?GROUP-ITEMS-BY-CHANNEL%}{{%CHANNEL-METADATA% name}:[
    {%REPEAT% FeedItems insert=","}{
    "id": "{%ITEM-METADATA AwasuId%}",
    "title": {%ITEM-METADATA% name!},
    "published": {%ITEM-METADATA% timestamp},
    "url": "{%ITEM-METADATA% url encode=attr}",
    "state" :"(%ITEM-STATE%}"}{%/REPEAT%}
{%/REPEAT%}]}


Still is not working as I expected. I got errors:

Error 1. if title has ' " ' fx.

result from Awasu:

}, {
"id": "{%ITEM-METADATA AwasuId%}",
"title": "Sequential Peeling: A Model-Based Approach to Structure Identification and De-embedding",
"published": "Published: Yesterday 18:30",
"url": "http://spectrum.ieee.org/whitepaper/sequential-peeling-a-model-based-approach-to-structure-identification-and-de-embedding",
"state": "(%ITEM-STATE%}"
}, {
"id": "{%ITEM-METADATA AwasuId%}",
"title": "U.S. Defense Budget May Help Fund "
"Hacking for Defense"
" Classes at Universities"
,
"published": "Published: Tue Jul 25 18:00",
"url": "http://spectrum.ieee.org/view-from-the-valley/at-work/education/us-defense-budget-may-help-fund-university-hacking-for-defense-classes",
"state": "(%ITEM-STATE%}"
}, {


Error 2. I can not get documented info - internal ID for article:


The following pseudo-values can be used with the {%ITEM-METADATA%} template parameter.

Metadata ID
Description
AwasuId Returns Awasu's internal item ID.


I got in place number of ID as string only Awasu variable:

{
"id": "{%ITEM-METADATA AwasuId%}",
"title": "Wealthy People Give to Charity for Different Reasons Than the Rest of Us",
"published": "Published: Tue Jul 25 16:00",
"url": "https://www.scientificamerican.com/article/wealthy-people-give-to-charity-for-different-reasons-than-the-rest-of-us/",
"state": "(%ITEM-STATE%}


Error 3. Wrong code if channel has no new data, because is empty:

"state": "(%ITEM-STATE%}"
}, {
"Journal news | Royal Society": [

, {
"MIT News": [{
"id": "{%ITEM-METADATA AwasuId%}",
"title": "SproutsIO aims to power a “Personal Produce” movement",
"published": "Published: 05:59",
"url": "http://news.mit.edu/2017/sproutsio-smart-microgarden-personal-produce-0727",
"state": "(%ITEM-STATE%}"


Error 4. Last error is ridle:

}, {
"id": "{%ITEM-METADATA AwasuId%}",
"title": "Wealthy People Give to Charity for Different Reasons Than the Rest of Us",
"published": "Published: Tue Jul 25 16:00",
"url": "https://www.scientificamerican.com/article/wealthy-people-give-to-charity-for-different-reasons-than-the-rest-of-us/",
"state": "(%ITEM-STATE%}"
}]
}


From https://jsonlint.com is:

Error: Parse error on line 2059:
... }
-----------------------^
Expecting ',', ']', got 'EOF'


I try make template with valid JSON code as start pattern:

Code: Select all

{"employees":[
    { "firstName":"John", "lastName":"Doe" },
    { "firstName":"Anna", "lastName":"Smith" },
    { "firstName":"Peter", "lastName":"Jones" }
]}


Eh, at the end I can't make template working well to get JSON from channels data.

User avatar
support
Site Admin
Posts: 3021
Joined: Fri Feb 07, 2003 12:48 pm
Location: Melbourne, Australia
Contact:

Postby support » Thu Jul 27, 2017 10:21 am

awasu.user wrote:Error 1. if title has ' " ' fx.

The template parameter needs to be quoted in the template i.e.

Code: Select all

    "title": "{%ITEM-METADATA% name!}",

If the substituted value has quotes, Awasu will escape them for you. Note that the template output file must have a .JSON extension, so that Awasu knows it's generating JSON and needs to escape quotes.

awasu.user wrote:Error 2. I can not get documented info - internal ID for article:

The correct parameter is {%ITEM-METADATA% AwasuId} - note the position of the closing %. BTW, in the next build I'm going to send you, you will be able to use just {%ITEM-ID%}.

awasu.user wrote:Error 3. Wrong code if channel has no new data, because is empty

This should never happen :-) Send me the feed URL and the template you're using.

awasu.user wrote:Error 4. Last error is ridle

I think you've got an error in the structure of your template, that's causing malformed JSON to be generated. Fix the other things, then post the entire template here, and I'll take a look at it.

awasu.user
Posts: 81
Joined: Fri Jan 06, 2017 12:50 pm

Postby awasu.user » Thu Jul 27, 2017 12:07 pm

I fix what I can and full tempate is:

Code: Select all

{%REPEAT% Channels-IfGroupingItems insert=","}{%?GROUP-ITEMS-BY-CHANNEL%}{"{%CHANNEL-METADATA% name}":[
    {%REPEAT% FeedItems insert=","}{
    "id": "{%ITEM-METADATA% AwasuId}",
    "title": "{%ITEM-METADATA% name!}",
    "published": "{%ITEM-METADATA% timestamp}",
    "url": "{%ITEM-METADATA% url encode=attr}",
    "state" :"(%ITEM-STATE%}"}{%/REPEAT%}
{%/REPEAT%}]}


Full data I send to you by e-mail.

User avatar
support
Site Admin
Posts: 3021
Joined: Fri Feb 07, 2003 12:48 pm
Location: Melbourne, Australia
Contact:

Postby support » Thu Jul 27, 2017 1:08 pm

Give this a go:

Code: Select all

{
{%REPEAT% Channels-IfGroupingItems insert=","}
"{%CHANNEL-METADATA% name}": [
    {%REPEAT% FeedItems insert=","} {
        "id": "{%ITEM-METADATA% AwasuId}",
        "title": "{%ITEM-METADATA% name!}",
        "published": "{%ITEM-METADATA% timestamp}",
        "url": "{%ITEM-METADATA% url}",
        "state": "{%ITEM-STATE%}"
    } {%/REPEAT%}
]
{%/REPEAT%}
}


I mis-spoke earlier when I said the template file needs a .JSON extension, it's the extension of the output file that determines how Awasu encodes special characters (apologies) :oops:

awasu.user
Posts: 81
Joined: Fri Jan 06, 2017 12:50 pm

Postby awasu.user » Thu Jul 27, 2017 1:27 pm

I check. Now output JSON is valid. Thank you!


Return to “Awasu - General Discussion”

Who is online

Users browsing this forum: No registered users and 6 guests