Created
September 19, 2011 08:19
-
-
Save prabirshrestha/1226146 to your computer and use it in GitHub Desktop.
Twitter Streaming Api with FluentHttp.Core, SimpleJson and Async/Await
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // install-package FluentHttp.Core | |
| // install-package SimpleJson | |
| // add as conditional symbol | |
| // FLUENTHTTP_CORE_TPL;FLUENTHTTP_CORE_UTILS;FLUENTHTTP_HTTPBASIC_AUTHENTICATION;SIMPLE_JSON_DYNAMIC;SIMPLE_JSON_REFLECTIONEMIT | |
| namespace ConsoleApplication1 | |
| { | |
| using System; | |
| using System.IO; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using FluentHttp; | |
| using SimpleJson; | |
| class Program | |
| { | |
| static string _twitterUsername = ""; | |
| static string _twitterPassword = ""; | |
| static void Main(string[] args) | |
| { | |
| var cts = new CancellationTokenSource(); | |
| Console.CancelKeyPress += ((o, e) => cts.Cancel()); | |
| StartTwitterStreamingApi(cts.Token).Wait(); | |
| } | |
| private static async Task StartTwitterStreamingApi(CancellationToken ct) | |
| { | |
| var httpHelper = new HttpHelper("http://stream.twitter.com/1/statuses/sample.json"); | |
| httpHelper.AuthenticateUsingHttpBasicAuthentication(_twitterUsername, _twitterPassword); | |
| using (var responseStream = await httpHelper.OpenReadTaskAsync(ct)) | |
| { | |
| using (var reader = new StreamReader(responseStream)) | |
| { | |
| while (true) | |
| { | |
| dynamic json = SimpleJson.DeserializeObject(await reader.ReadLineAsync()); | |
| string text = json.text; | |
| Console.WriteLine(text); | |
| Console.WriteLine(); | |
| } | |
| } | |
| } | |
| } | |
| private static void StartTwitterStreamingApiSync() | |
| { | |
| var httpHelper = new HttpHelper("http://stream.twitter.com/1/statuses/sample.json"); | |
| httpHelper.AuthenticateUsingHttpBasicAuthentication(_twitterUsername, _twitterPassword); | |
| using (var responseStream = httpHelper.OpenRead()) | |
| { | |
| using (var reader = new StreamReader(responseStream)) | |
| { | |
| while (true) | |
| { | |
| dynamic json = SimpleJson.DeserializeObject(reader.ReadLine()); | |
| string text = json.text; | |
| Console.WriteLine(text); | |
| Console.WriteLine(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment