public int Add(TwitImage twitImage)
 {
     _twitsContext.Add(twitImage);
     return(_twitsContext.SaveChanges());
 }
예제 #2
0
        public async Task checkForPhotosInTwitsByText(string textToSearchFor)
        {
            try
            {
                var stream = Tweetinvi.Stream.CreateFilteredStream(
                    new TwitterCredentials("CY238Vi51lLvDLw8xbhzh2JCu",
                                           "B5Pik3mQ6GebCc214MwXOUmfYQxGUB2rCW7JBV1V7k4jtSx1ht",
                                           "935800346601033729-Dv0QUWtYx73R341AC5jSvqnLFXWI8p3",
                                           "GcsQzEvCuE4ytHp3dx1lfru5L0eEwwdI0HoVOfenhO2Gw"));

                stream.AddTrack(textToSearchFor);

                stream.MatchingTweetReceived += (sender, args) =>
                {
                    var newTwit = new Twitt()
                    {
                        Text       = args.Tweet.FullText,
                        AddingDate = args.Tweet.CreatedAt,
                        Track      = textToSearchFor
                    };

                    var entities = args.Tweet.Entities;
                    if (entities != null && entities.Medias != null && entities.Medias.Any(e => e.MediaType == "photo"))
                    {
                        newTwit.HasImage = true;

                        foreach (var item in entities.Medias.Where(e => e.MediaType == "photo").Select(a => a.MediaURL).ToList())
                        {
                            newTwit.Images.Add(new TwitImage()
                            {
                                Url = item, TwittId = newTwit.Id, Track = newTwit.Track, AddingDate = newTwit.AddingDate
                            });
                        }
                    }

                    _twitsContext.Add(newTwit);
                    _twitsContext.SaveChanges();
                };

                await stream.StartStreamMatchingAnyConditionAsync();

                #region old

                //var client = new HttpClient();

                ////HttpRequestMessage objRequest = new HttpRequestMessage(HttpMethod.Get,
                ////  "https://api.twitter.com/1.1/search/tweets.json?q=%" + textToSearchFor + "&count=" + twittsCount);
                //HttpRequestMessage objRequest = new HttpRequestMessage(System.Net.Http.HttpMethod.Post,
                //    "https://stream.twitter.com/1.1/statuses/filter.json?track=twitter");

                //objRequest.Headers.Add("track", "twitter");
                //objRequest.Headers.Add(HttpRequestHeader.Authorization.ToString(), "Bearer " + accessToken);
                //HttpResponseMessage response = await client.SendAsync(objRequest);

                //if (!response.IsSuccessStatusCode)
                //{ }

                //HttpContent responseContent = response.Content;

                //List<string> photos = new List<string>();
                //using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
                //{

                //    string result = await reader.ReadToEndAsync();
                //    var stuffasds = JsonConvert.DeserializeObject<TwitterApiResponse>(result);


                //    photos = stuffasds.statuses.Where(x => x.entities.media != null)
                //       .SelectMany(x => x.entities.media.Where(a => a.type == "photo")).Select(w => w.media_url).ToList();

                //}
                //return photos;
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }