Created
September 22, 2011 12:17
-
-
Save niik/1234641 to your computer and use it in GitHub Desktop.
Event dump example
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
/* | |
* Copyright (c) 2011 Markus Olsson, Tickster AB | |
* var mail = string.Join(".", new string[] {"markus", "olsson"}) + string.Concat('@', "tickster.com"); | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
* software and associated documentation files (the "Software"), to deal in the Software without | |
* restriction, including without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | |
* Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
using System; | |
using System.Net; | |
using Newtonsoft.Json.Linq; | |
namespace DumpApiExample | |
{ | |
// Retrieve latest event api dump of upcoming events from tickster.com public data api (developer.tickster.com) | |
// Requires Json.Net. Written for .net 4 but the dynamic bits are not at all necessary and you can rewrite it | |
// to run under .net 2.0 by simply not casting to dynamic. | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string apiKey = "xxx"; | |
var wc = new GZipWebClient { BaseAddress = "http://www.tickster.com/sv/api/0.2/" }; | |
dynamic apiResponse = JObject.Parse(wc.DownloadString("events/dump/upcoming?key=" + apiKey)); | |
if (GetLastProcessedDumpId() == (string)apiResponse.id) | |
{ | |
// The currently published dump file has already been processed locally; | |
// terminate and try again later | |
return; | |
} | |
dynamic dump = JObject.Parse(wc.DownloadString((string)apiResponse.uri)); | |
Console.WriteLine("Retrieved dump file containing {0} events", dump.count); | |
foreach (var ev in dump.events) | |
{ | |
// Merge/update with your local data store | |
Console.WriteLine("{0}: {1}", ev.id, ev.name); | |
} | |
Console.ReadLine(); | |
} | |
private static string GetLastProcessedDumpId() | |
{ | |
// Retrieve the id in from own data store | |
return string.Empty; | |
} | |
private static void SaveProcessedDumpId(string id) | |
{ | |
// Store the id in your own data store | |
} | |
// See http://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient | |
private class GZipWebClient : WebClient | |
{ | |
protected override WebRequest GetWebRequest(Uri address) | |
{ | |
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); | |
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
return request; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment