10

I want to get the most current questions from Stack Overflow using the Stacky C# library for the Stack Exchange API.

I took the example code and tried to run it but it hangs when it comes to returning data from the Stack Exchange website.

StackyClient client = new StackyClient("0.9", "", Sites.StackOverflow, 
            new UrlClient(), new JsonProtocol());

var o = new QuestionOptions();
o.FromDate = DateTime.Now.AddMinutes(-10.0);
o.ToDate = DateTime.Now;
o.IncludeAnswers = false;
o.IncludeBody = false;
o.IncludeComments = false;
o.SortBy = QuestionSort.Creation;
o.SortDirection = SortDirection.Descending;

IPagedList<Question> l = client.GetQuestions(o); <--- program hangs here 4ever

What am I doing wrong?

I also saw that I can register my application to get an API Key. But that is not necessary to make it run in the first place, is it?

Edit

If I remove the lines

o.FromDate = DateTime.Now.AddMinutes(-10.0);
o.ToDate = DateTime.Now;

it works and returns all questions. Also if I add the line

o.Max = 50;

instead, then it does not work either.

Edit 2

Now it works - rebooted my computer.
BTW I used that code in the end

var o = new QuestionOptions();
o.FromDate = DateTime.UtcNow.AddMinutes(-20);
o.IncludeAnswers = false;
o.IncludeBody = false;
o.IncludeComments = false;
o.SortBy = QuestionSort.Creation;
o.SortDirection = SortDirection.Descending;

IPagedList<Question> l = client.GetQuestions(o);

And

o.Max

expects an Unix Epoch time, not a number of maximum posts.

Community
  • 1
  • 1
juergen d
  • 201,996
  • 37
  • 293
  • 362

2 Answers2

4

Try changing the version specified in the StackyClient constructor from "0.9" to "1.1". I get a JSON parse error on the client.GetQuestions(o) line when the version is "0.9", but it runs fine with "1.1".

WarrenG
  • 3,084
  • 16
  • 10
  • Then it does not hang any more but the result does not contain any questions either. – juergen d Apr 29 '13 at 11:06
  • Which version of the Stacky library are you using? I downloaded the binary for v1.1 from codeplex [here](http://stacky.codeplex.com/releases/view/60851). Other than changing the version and adding a loop to print question titles to a console window, my code is identical to yours, and it retrieves questions every time I run it. – WarrenG Apr 29 '13 at 15:53
  • I downloaded that version and also tried compiling the source code myself. – juergen d Apr 29 '13 at 16:25
  • What time zone are you in? Perhaps your requested from/to dates are in the future relative to SO's servers. – WarrenG Apr 29 '13 at 16:33
  • Argentinian time zone. But I tried a date in the past days and it does not work either. – juergen d Apr 29 '13 at 16:38
  • 1
    I created my own UrlClient class to intercept and print the `Uri` the client is sending and got [this link](http://api.stackoverflow.com/1.1/questions/?key=&fromdate=1367228893&todate=1367229493&sort=creation&order=desc) Try pasting it into a browser and see if you get a response. I get the expected json response if I view it in Chrome. – WarrenG Apr 29 '13 at 17:03
0

Using the latest Stacky code from bitbucket there is no longer a QuestionOptions parameter to GetQuestions. Also using version 0.9 of the API causes Stacky to crash, but according to this version 1.x is deprecated, so maybe 0.9 is removed?

  StackyClient client = new StackyClient("2.1", Sites.StackOverflow,
        new UrlClient(), new JsonProtocol());
  //var o = new QuestionOptions();
  //o.FromDate = DateTime.Now.AddMinutes(-10.0);
  //o.ToDate = DateTime.Now;
  //o.IncludeAnswers = false;
  //o.IncludeBody = false;
  //o.IncludeComments = false;
  //o.SortBy = QuestionSort.Creation;
  //o.SortDirection = SortDirection.Descending;
  QuestionSort sort = QuestionSort.Creation;
  SortDirection sortDir = SortDirection.Descending;
  int page = 1;
  int pageSize = 100;
  DateTime fromDate = DateTime.Now.AddMinutes(-10.0);
  DateTime toDate = DateTime.Now;
  IPagedList<Question> l = client.GetQuestions(sort, sortDir, page, pageSize, fromDate, toDate);
  foreach (var question in l)
  {
    Console.WriteLine(question.Title);
  }

Or, just remove the date and see if you get any results.

  IPagedList<Question> l = client.GetQuestions(sort, sortDir, page, pageSize);//, fromDate, toDate);
  foreach (var question in l)
  {
    Console.WriteLine(question.Title);
  }
Ken Wilcox
  • 843
  • 7
  • 9