public async void TwitterText() { /// <summary> /// Получение твитов из ленты пользователя (имя, тескт, никнейм) и сохранение в бд /// </summary> #region Tweets var tweets = twitter.GetTweets(); var twitList = new List <string>(); for (int i = 0; i < tweets.Count; i++) { var name = tweets[i].User.Name; var screenNameTweet = tweets[i].User.ScreenName; var text = tweets[i].Text; var dateTwit = tweets[i].CreatedDate; if (!string.IsNullOrEmpty(screenNameTweet) && !screenNameTweet.StartsWith("@")) { screenNameTweet = "@" + screenNameTweet; } twitList.Add(tweets[i].User.Name + "\n" + screenNameTweet + "\n" + text); using (var db = new TwitterAppContext()) // сохранение в бд { db.twitterTexts.Add(new TwitterTextDb { Text = text, Date = dateTwit }); db.users.Add(new UsersDb { UserName = name, ScreenName = screenNameTweet }); db.SaveChanges(); } } var twitTask = await Task.Run(() => twitList); //var DataBasedTweets = dataBasedHelper.Reader(); //twitterText.ItemsSource = DataBasedTweets; /// <summary> /// вывод (из бд) последних 10 сохраненных твитов пользователя по дате и по Id /// </summary> using (var db = new TwitterAppContext()) { List <string> listOut = new List <string>(); var tweetsOutputId = db.twitterTexts.Where(t => t.Id > 0).OrderByDescending(t => t.Id).Take(10); // выборка по Id var tweetsOutputDate = db.twitterTexts.Where(t => t.Date >= new DateTime(2019, 12, 30)).OrderByDescending(t => t.Date).Take(10); //выборка по дате foreach (var tweetText in tweetsOutputId) { listOut.Add(tweetText.Text + tweetText.UsersDb); } //var twitTaskDB = await Task.Run(() => listOut); //twitterText.ItemsSource = twitTaskDB; } }
private void insertDB(List <TwitterStatus> Tweets) { string lat; string lon; string loc; foreach (var tweet in Tweets) { try { lat = tweet.Location.Coordinates.Latitude.ToString(); lon = tweet.Location.Coordinates.Longitude.ToString(); } catch { lat = ""; lon = ""; } try { loc = tweet.Location.ToString(); } catch { loc = ""; } var user = TwitterDB.UsersTable.SingleOrDefault(u => u.ScreenName == tweet.User.ScreenName); var twt = TwitterDB.TweetsTable.SingleOrDefault(t => t.TweetID == tweet.Id); if (user == null) { var newUser = new UsersInfo { UsersInfoID = tweet.User.Id, ScreenName = tweet.User.ScreenName, ProfileImageUrl = tweet.User.ProfileImageUrl, Name = tweet.User.Name, TweetsCounter = 1, Description = tweet.User.Description, Location = tweet.User.Location, }; var newTweet = new TweetsInfo { TweetID = tweet.Id, TweetText = tweet.Text, CreationDate = tweet.CreatedDate, Longitude = lon, Latitude = lat, Location = loc, Useritem = newUser }; TwitterDB.UsersTable.Add(newUser); TwitterDB.TweetsTable.Add(newTweet); TwitterDB.SaveChanges(); } else { if (twt == null) { var newTweet = new TweetsInfo { TweetID = tweet.Id, TweetText = tweet.Text, CreationDate = tweet.CreatedDate, Longitude = lon, Latitude = lat, Location = loc, Useritem = user }; TwitterDB.TweetsTable.Add(newTweet); user.TweetsCounter++; TwitterDB.Entry(user).State = EntityState.Modified; TwitterDB.SaveChanges(); } } } }