Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get blog posts by label in blogger api

I using blogger api in my android app to integrate blogger content with it by using the REST APIs, as a JSON's objects.

I need to retrieve/filter posts by a label. In most blogs the link of blog's label it usually is

https://abtallaldigital.blogspot.com/search/label/Food
https://abtallaldigital.blogspot.com/search/label/Technology

I read all API documentation and I see it's deal with Blogs, Posts, Comments, Pages, Users but there's nothing to handle labels/categories in it.

There's a class BloggerAPI in the app that's used to retrieve blogs

package abtallaldigital.blogspot.com.dummyapp;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Url;

public class BloggerAPI {

    public static final String BASE_URL =
            "https://www.googleapis.com/blogger/v3/blogs/2399953/posts/";
    public static final String KEY = "THE-KEY";

    public static PostService postService = null;

    public static PostService getService() {

        if (postService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            postService = retrofit.create(PostService.class);
        }

        return postService;
    }

    public interface PostService {
        @GET
        Call<PostList> getPostList(@Url String url);
    }
}

It is used thus

  private void getData(){

    String url = BloggerAPI.BASE_URL + "?key=" + BloggerAPI.KEY;

    if(token != ""){
        url = url+ "&pageToken="+token;
    }
    if(token == null){
        return;
    }

   final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
    postList.enqueue(new Callback<PostList>() {
        @Override
        public void onResponse(Call<PostList> call, Response<PostList> response) {
            PostList list = response.body();
            token = list.getNextPageToken();
            items.addAll(list.getItems());
            adapter.notifyDataSetChanged();
            Toast.makeText(MainActivity.this, "Sucess", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call<PostList> call, Throwable t) {
            Toast.makeText(MainActivity.this,"Error occured",Toast.LENGTH_LONG).show();
            Log.i(TAG, "onFailure: "+t.toString());
        }
    });

}

I googling for how to get a link of RSS Feed for any label and I found this result

https://example.blogspot.com/feeds/posts/default/-/label/?alt=rss

this will gets the blog posts of any label with replacing the word "label" in the link

like image 985
Dr Mido Avatar asked Mar 04 '23 17:03

Dr Mido


1 Answers

Search query (q=) option

If you want to get posts with a specific label you could use this query:

GET https://www.googleapis.com/blogger/v3/blogs/2399953/posts/search?q=label:label1&key=THE-KEY

If you want to get posts that are labelled 'this' as well as 'that' (if one of them does not match, it's not a match) you could use:

GET https://www.googleapis.com/blogger/v3/blogs/2399953/posts/search?q=label:this+label:that&key=THE-KEY

Use quotes around labels with spaces:

GET https://www.googleapis.com/blogger/v3/blogs/15045980/posts/search?q=label%3A%22James+Whittaker%22&key=THE-KEY

Probably a better option to get posts by label

Source: https://developers.google.com/blogger/docs/3.0/reference/posts/list

HTTP-request:

GET https://www.googleapis.com/blogger/v3/blogs/blogId/posts

Amongst the optional parameters there is the labels parameter:

labels string Comma-separated list of labels to search for.

Example HTTP-request (using the Google Testing Blog's blogId):

GET https://www.googleapis.com/blogger/v3/blogs/15045980/posts?labels=James+Whittaker&key=THE-KEY

Note that quotes around the label should not be included using this option.

To fetch the next page of posts store the given nextPageToken, as mentioned in the comments, and put it in the pageToken-parameter, like:

GET https://www.googleapis.com/blogger/v3/blogs/15045980/posts?labels=James+Whittaker&pageToken=CgkIChiAjpb65CUQ3KqWBw&key=THE-KEY

like image 135
Wieger Avatar answered Mar 17 '23 05:03

Wieger