Thursday, January 15, 2015

Traverse Posts on Blogger

After I showed in the previous post how to publish new posts on Blogger using C# and the Blogger Data API v2, I want to show today how one can scan through the already existing posts on one's blog.
We do the same preliminary work like in the previous post, meaning we include Google.GData.Client and create the Service object, with which we log on.
With the following feed query we then can access all published posts:

            FeedQuery query = new FeedQuery();
            query.Uri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");

            AtomFeed feed = null;
            feed = service.Query(query);

The following example program searches through the contents of all existing posts for the target expression "Test" and then returns the title of all matching posts:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Google.GData.Client;

namespace BloggerQueryPosts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Service service = new Service("blogger""");
            service.Credentials = new GDataCredentials(Email, Password);
            GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
            factory.AccountType = "GOOGLE";

            FeedQuery query = new FeedQuery();
            query.Uri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");

            AtomFeed feed = null;
            feed = service.Query(query);
            foreach (AtomEntry entry in feed.Entries)
            {
                if (entry.Content.Content.ToString().Contains("Test"))
                    MessageBox.Show("Match. " + entry.Title.Text);
            }
        }
    }
}

1 comment: