/* * static void Main(string[] args) * { * while (true) * { * * List<string> tweetstrings = File.ReadAllLines(string.Format("C:\\Users\\tstapleton\\Desktop\\Sunday22\\TweetFile{0}.tweet", fileNumber)).ToList(); * List<string> resultStrings = new List<string>(); * * try * { * * List<Tweet> tweets = new List<Tweet>(); * * foreach (var tweet in tweetstrings) * { * tweets.Add(JsonConvert.DeserializeObject<Tweet>(tweet)); * } * * List<Task> taskList = new List<Task>(); * * int count = 0; * foreach (var tweet in tweets) * { * count++; * taskList.Add(GetBlockInfo(tweet)); * if (count % 1000 == 0) * { * Task.WhenAll(taskList).Wait(); * * Console.WriteLine("1000"); * * while (!ResultList.IsEmpty) * { * TweetWithBlock current; * ResultList.TryDequeue(out current); * resultStrings.Add(JsonConvert.SerializeObject(current)); * } * * File.AppendAllLines( * String.Format( * "C:\\Users\\tstapleton\\Desktop\\locationresults\\TweetFile{0}.tweet", * fileNumber), * resultStrings); * * taskList.Clear(); * count = 0; * resultStrings.Clear(); * * } * } * * Task.WhenAll(taskList).Wait(); * * } * catch (Exception) * { * * } * finally * { * File.AppendAllLines( * "C:\\Users\\tstapleton\\Desktop\\locationresults\\TweetFile0.tweet", * resultStrings); * resultStrings.Clear(); * } * fileNumber++; * } * } * */ public static async Task GetBlockInfoAsync(Tweet tweet, int count) { HttpClient client = new HttpClient(); string url = "http://data.fcc.gov/api/block/2010/find?latitude={0}&longitude={1}&format=json"; url = string.Format(url, tweet.Latitude, tweet.Longitude); var result = await client.GetAsync(url); FccResponse location = JsonConvert.DeserializeObject <FccResponse>(await result.Content.ReadAsStringAsync()); /* * ResultList.Enqueue( * new TweetWithBlock() * { * Id = tweet.Id, * Latitude = tweet.Latitude, * LocationData = location, * Longitude = tweet.Longitude, * Text = tweet.Text, * TweetId = tweet.TweetId * }); */ Console.WriteLine(count); }
public static async Task GetBlockInfo(Tweet tweet, int count) { string cacheResult; cache.TryGetValue(new Tuple <double, double>(tweet.Latitude, tweet.Longitude), out cacheResult); if (!string.IsNullOrWhiteSpace(cacheResult)) { Console.WriteLine("cache hit!!!"); FinalResults.Add(JsonConvert.SerializeObject( new TweetWithBlock() { Id = tweet.Id, Latitude = tweet.Latitude, BlockGroup = cacheResult, Longitude = tweet.Longitude, Text = tweet.Text, TweetId = tweet.TweetId })); return; } HttpClient client = new HttpClient(); string url = "http://data.fcc.gov/api/block/2010/find?latitude={0}&longitude={1}&format=json&showall=false"; url = string.Format(url, tweet.Latitude, tweet.Longitude); HttpResponseMessage result; try { result = await client.GetAsync(url); } catch (Exception e) { Console.WriteLine(e.Message); return; } Console.WriteLine(count); FccResponse location = JsonConvert.DeserializeObject <FccResponse>(result.Content.ReadAsStringAsync().Result); cache.TryAdd(new Tuple <double, double>(tweet.Latitude, tweet.Longitude), location.Block.FIPS); FinalResults.Add(JsonConvert.SerializeObject( new TweetWithBlock() { Id = tweet.Id, Latitude = tweet.Latitude, BlockGroup = location.Block.FIPS, Longitude = tweet.Longitude, Text = tweet.Text, TweetId = tweet.TweetId })); }