0

Currently, I'm working on adding a "previous blog" and "next blog" link to an ASP.net C# application that feeds its blog from a customer's blog hosted on blogspot. Once I grab a specific blog, I then want to get its immediately previous blog and immediately next blog. To get the previous blog isn't hard.

// THIS IS MY CALL TO GET THE PREVIOUS BLOG    
FeedQuery prevQuery = new FeedQuery();
prevQuery.Uri = new Uri(String.Format("{0}", blogCred.feedUri));
    // {0} This is automatically retrieved from the database [ https://www.blogger.com/feeds/XXXXXXXXXXXXXXXXXX/posts/default/ ]
prevQuery.MaxPublication = entry.Published.AddSeconds(-1);
prevQuery.NumberToRetrieve = 1;
AtomFeed prevFeed = googleService.Query(prevQuery);
AtomEntry prevEntry = prevFeed.Entries.FirstOrDefault();
prevBlog(prevEntry);

This gets me the result I want. The problem is getting the next blog.

// THIS IS MY CALL TO GET THE NEXT BLOG   
FeedQuery nextQuery = new FeedQuery();
nextQuery.Uri = new Uri(String.Format("{0}", blogCred.feedUri)); 
    // {0} This is automatically retrieved from the database [ https://www.blogger.com/feeds/XXXXXXXXXXXXXXXXXX/posts/default/ ]
nextQuery.MinPublication = entry.Published.AddSeconds(1);
nextQuery.NumberToRetrieve = 1;
AtomFeed nextFeed = googleService.Query(nextQuery);
AtomEntry nextEntry = nextFeed.Entries.FirstOrDefault();
nextBlog(nextEntry);

The problem I'm having is that I can't get Blogger to order through these posts in a descending order. It keeps sorting through them in an ascending order and grabbing the latest blog that's been posted, rather than the next post. I don't want to grab the FULL xml file with all the blog posts and grabbing the last item. I want to get the last item in a similar fashion to this LINQ to SQL call:

var query = from f in nextEntry
            where f.date > desiredDate
            orderby date descending
            select f;

If anyone has any ideas or anything I'm missing - hopefully something as simple as getting the "next post" link from somewhere in the entry that's spat back from Google's response.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Laki Politis
  • 147
  • 9

1 Answers1

0

Kiquenet, I can't post the full solution, but I can post the snippet of code you'll need to make this happen.

FeedQuery nextQuery = new FeedQuery();
nextQuery.Uri = new Uri(String.Format("{0}", blogCred.feedUri));
DateTime dateToFind = entry.Published.AddSeconds(1);
int numberOfDates = (int)(DateTime.Now - dateToFind).TotalDays;
nextQuery.MinPublication = dateToFind;
nextQuery.NumberToRetrieve = 1;
AtomFeed nextFeed = null;
AtomEntry nextEntry = null;
for (int i = 0; i < numberOfDates; )
{
    nextQuery.MaxPublication = dateToFind.AddDays(i);
    nextFeed = googleService.Query(nextQuery);
    if (nextFeed.TotalResults >= 1)
    {
        nextEntry = nextFeed.Entries.FirstOrDefault();
        break;
    }
    i = i + 10;
}

So what this does for you is it sees if there are any posts 10 days after the last post you just grabbed. If there aren't any posts in that timespan, add 10 more days. It will continue to search by increments of 10 until it reach todays date.the number of dates between the dateToFind and Now.

Hopefully, this helps you out. It's what I'm currently - and have been - using.

Laki Politis
  • 147
  • 9