public List<Tweet> GetAllTweets() { MongoDatabase database = server.GetDatabase("Tweets"); MongoCollection<Mongo_Tweet> _lstMongoTweets = database.GetCollection<Mongo_Tweet>("Tweets"); List<Tweet> _lstTweets = new List<Tweet> { }; foreach (Mongo_Tweet _mongotweet in _lstMongoTweets.FindAll()) { Tweet _Tweet = new Tweet(); _Tweet.Id= _mongotweet.Id.ToString(); _Tweet.TweetFrom = _mongotweet.TweetFrom; _Tweet.TweetText = _mongotweet.TweetText; _Tweet.TweetSource = _mongotweet.TweetSource; _Tweet.TweetSource = _mongotweet.TweetTimezone; _lstTweets.Add(_Tweet); } return _lstTweets; }
public string GetAndSaveUserTweets(string ScreenName) { WebClient _twitter = new WebClient(); string _TweetsString = ""; try { _TweetsString = _twitter.DownloadString(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + ScreenName + "")); } catch (Exception ex) { if (ex.Message.Contains("Not Found")) { return "Screen name not found"; } else { return ex.Message; } } XElement _xmlTweets = XElement.Parse(_TweetsString); IEnumerable<XElement> _lstXMlTweets = _xmlTweets.Descendants("status"); int _RetrievedTweetsCount= 0; string StatusString = ""; if (_lstXMlTweets.Count<XElement>() != 0) { _RetrievedTweetsCount = _lstXMlTweets.Count<XElement>(); List<Tweet> _lstTweets = new List<Tweet>(); foreach (XElement _RetrievedTweet in _lstXMlTweets) { Tweet _tweet = new Tweet(); _tweet.TweetFrom = _RetrievedTweet.Element("user").Element("name").Value; _tweet.TweetText = _RetrievedTweet.Element("text").Value; if (_RetrievedTweet.Element("user").Element("created_at").Value != "") { string[] _CreatedAtParts = _RetrievedTweet.Element("user").Element("created_at").Value.Split(' '); string day = ""; string month = ""; string year = ""; day = _CreatedAtParts[2]; year = _CreatedAtParts[5]; switch (_CreatedAtParts[1]) { case "Jan": month = "1"; break; case "Feb": month = "2"; break; case "Mar": month = "3"; break; case "Apr": month = "4"; break; case "May": month = "5"; break; case "Jun": month = "6"; break; case "Jul": month = "7"; break; case "Aug": month = "8"; break; case "Sep": month = "9"; break; case "Oct": month = "10"; break; case "Nov": month = "11"; break; case "Dec": month = "12"; break; } _tweet.TweetTime = month + "/" + day + "/" + year; } if (_RetrievedTweet.Element("source").Value.Contains("<")) { _tweet.TweetSource = _RetrievedTweet.Element("source").Value.Substring(_RetrievedTweet.Element("source").Value.IndexOf(">") + 1, _RetrievedTweet.Element("source").Value.LastIndexOf("<") - _RetrievedTweet.Element("source").Value.IndexOf(">") - 1); } else { _tweet.TweetSource = _RetrievedTweet.Element("source").Value; } _tweet.TweetTimezone = _RetrievedTweet.Element("user").Element("time_zone").Value; _lstTweets.Add(_tweet); } if (_lstTweets.Count > 0) { try { AddTweets(_lstTweets); StatusString = "(" + _RetrievedTweetsCount + ") tweets saved successfully"; } catch { //Error can be handled here using Log4Net, Email notifications ...etc StatusString = "Faild to save tweets"; } } else { StatusString = "No tweets saved"; } } else { StatusString = "No tweets retrieved"; } return StatusString; }
public List<Tweet> GetTweetsByKey(string KeyName, string KeyValue) { MongoDatabase database = server.GetDatabase("Tweets"); MongoCollection<Mongo_Tweet> _MongoTweets = database.GetCollection<Mongo_Tweet>("Tweets"); IMongoQuery query = Query.EQ( KeyName, KeyValue); MongoCursor<Mongo_Tweet> _lstMongoTweets = _MongoTweets.Find(query); List<Tweet> _lstTweets = new List<Tweet> { }; foreach (Mongo_Tweet _mongotweet in _lstMongoTweets) { Tweet _Tweet = new Tweet(); _Tweet.Id = _mongotweet.Id.ToString(); _Tweet.TweetFrom = _mongotweet.TweetFrom; _Tweet.TweetText = _mongotweet.TweetText; _Tweet.TweetSource = _mongotweet.TweetSource; _Tweet.TweetTimezone = _mongotweet.TweetTimezone; _lstTweets.Add(_Tweet); } return _lstTweets; }