Inheritance: PropertyChangedBase, ITwitterItem, ITimeTaged
Exemplo n.º 1
0
 public static bool HasRelationshipTo(this Status item,Status second)
 {
     if (second.InReplyToStatusID==item.ID||item.InReplyToStatusID==second.ID)
     {
         return true;
     }
     else
     {
         if (item.InReplyToStatus!=null)
         {
             return item.InReplyToStatus.HasRelationshipTo(second);
         }
         else if (second.InReplyToStatus!=null)
         {
             return second.InReplyToStatus.HasRelationshipTo(item);
         }
         return false;
     }
 }
Exemplo n.º 2
0
 public bool TryGetValue(ulong key, out Status value)
 {
     WeakReference t;
     var result = _cache.TryGetValue(key, out t);
     if (!result)
     {
         value = null;
         return false;
     }
     if (t.IsAlive)
     {
         value = (Status)t.Target;
         return true;
     }
     else
     {
         t = null;
         _cache.TryRemove(key, out t);
         value = null;
         return false;
     }
 }
Exemplo n.º 3
0
 public bool TryAdd(ulong key, Status value)
 {
     var newwr = new WeakReference(value);
     _cache.AddOrUpdate(key, newwr, (k, wr1) => wr1.IsAlive ? wr1 : newwr);
     return true;
 }
Exemplo n.º 4
0
 public bool Favorite(Status status)
 {
     for (int i = 0; i < RetryCount; i++)
     {
         if (status.Favorited ? DeleteFavorite(status.ID) : CreateFavorite(status.ID))
         {
             return true;
         }
         System.Threading.Thread.Sleep(1000);
     }
     return false;
 }
Exemplo n.º 5
0
        public void ChirpUserStream()
        {
            if ((_thread!=null)&&(_thread.IsAlive))
            {
                try
                {
                    _thread.Abort();
                    _thread = null;
                }
                catch
                {
                }
            }
            _thread = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        DateTime time;
                        using (var reader = new StreamReader(FetchStream(OAuthBase.HttpVerbs.Get, "https://userstream.twitter.com/2/user.json", null, out time)))
                        {
                            _failureCount = 0;

                            while (!reader.EndOfStream)
                            {
                                var line = reader.ReadLine();

                                if (string.IsNullOrWhiteSpace(line))
                                {
                                    continue;
                                }

                                using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
                                {
                                    var element = XElement.Load(jsonReader);

                                    if (element.Element("delete") != null)
                                    {
                                        var status = new Status
                                        {
                                            ID = (ulong)element.Element("delete").Element("status").Element("id")
                                        };

                                        UserStreamUpdated(this, new StatusEventArgs(status)
                                        {
                                            Action = StatusAction.Deleted
                                        });
                                    }
                                    else if (element.Element("user") != null)
                                    {
                                        var status = new Status
                                        {
                                            ID = (ulong)element.Element("id"),
                                            CreatedAt = DateTime.ParseExact(element.Element("created_at").Value, "ddd MMM dd HH:mm:ss zzzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo),
                                            Text = element.Element("text").Value,
                                            Source = element.Element("source").Value,
                                            Favorited = (bool)element.Element("favorited"),
                                            Sender = new User
                                            {
                                                ID = (int)element.Element("user").Element("id"),
                                                ScreenName = element.Element("user").Element("screen_name").Value,
                                                Name = element.Element("user").Element("name").Value,
                                                ImageUrl = element.Element("user").Element("profile_image_url").Value,
                                                Description = element.Element("user").Element("description").Value,
                                                Protected = (bool)element.Element("user").Element("protected"),
                                                Location = element.Element("user").Element("location").Value,
                                                Verified = (bool)element.Element("user").Element("verified"),
                                                FavouritesCount = (int)element.Element("user").Element("favourites_count"),
                                                Followers = (int)element.Element("user").Element("followers_count"),
                                                Friends = (int)element.Element("user").Element("friends_count"),
                                                StatusesCount = (int)element.Element("user").Element("statuses_count"),
                                            },
                                        };

                                        if (element.Element("retweeted_status") != null)
                                        {
                                            status.ReTweetedStatus = new Status
                                            {
                                                ID = (ulong)element.Element("retweeted_status").Element("id"),
                                                CreatedAt = DateTime.ParseExact(element.Element("retweeted_status").Element("created_at").Value, "ddd MMM dd HH:mm:ss zzzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo),
                                                Text = element.Element("retweeted_status").Element("text").Value,
                                                Source = element.Element("retweeted_status").Element("source").Value,
                                                Favorited = (bool)element.Element("retweeted_status").Element("favorited"),
                                                Sender = new User
                                                {
                                                    ID = (int)element.Element("retweeted_status").Element("user").Element("id"),
                                                    ScreenName = element.Element("retweeted_status").Element("user").Element("screen_name").Value,
                                                    Name = element.Element("retweeted_status").Element("user").Element("name").Value,
                                                    ImageUrl = element.Element("retweeted_status").Element("user").Element("profile_image_url").Value,
                                                    Description = element.Element("retweeted_status").Element("user").Element("description").Value,
                                                    Protected = (bool)element.Element("retweeted_status").Element("user").Element("protected"),
                                                    Location = element.Element("retweeted_status").Element("user").Element("location").Value,
                                                    Verified = (bool)element.Element("retweeted_status").Element("user").Element("verified"),
                                                    FavouritesCount = (int)element.Element("retweeted_status").Element("user").Element("favourites_count"),
                                                    Followers = (int)element.Element("retweeted_status").Element("user").Element("followers_count"),
                                                    Friends = (int)element.Element("retweeted_status").Element("user").Element("friends_count"),
                                                    StatusesCount = (int)element.Element("retweeted_status").Element("user").Element("statuses_count"),
                                                },
                                            };
                                        }

                                        if (!string.IsNullOrEmpty(element.Element("in_reply_to_status_id").Value))
                                        {
                                            status.InReplyToStatusID = (ulong)element.Element("in_reply_to_status_id");
                                        }

                                        if (!string.IsNullOrEmpty(element.Element("in_reply_to_user_id").Value))
                                        {
                                            status.InReplyToUserID = (int)element.Element("in_reply_to_user_id");
                                        }

                                        status.IsAuthor = status.Sender.ID == LoginedUser.ID;
                                        status.IsMention = status.InReplyToUserID == LoginedUser.ID;
                                        if (true)//Properties.Settings.Default.GlobalFilter.Count != 0 ? Properties.Settings.Default.GlobalFilter.AsParallel().All((filter) => filter.Process(status)) : true)
                                        {
                                            UserStreamUpdated(this, new StatusEventArgs(status)
                                                                    {
                                                                        Action = StatusAction.Update
                                                                    });
                                        }

                                    }
                                    else if (element.Element("event") != null)
                                    {
                                        if (element.Element("event").Value == "favorite")
                                        {
                                            var status = new Status
                                            {
                                                ID = (ulong)element.Element("target_object").Element("id"),
                                                Sender = new User
                                                {
                                                    ID = (int)element.Element("source").Element("id")
                                                }
                                            };

                                            UserStreamUpdated(this, new StatusEventArgs(status) { Action = StatusAction.Favorited });
                                        }
                                        else if (element.Element("event").Value == "unfavorite")
                                        {
                                            var status = new Status
                                            {
                                                ID = (ulong)element.Element("target_object").Element("id"),
                                                Sender = new User { ID = (int)element.Element("source").Element("id") }
                                            };

                                            UserStreamUpdated(this, new StatusEventArgs(status) { Action = StatusAction.Unfavorited });
                                        }
                                    }
                                }
                            }
                        }
                        _failureCount = 1;
                    }
                    catch (ThreadAbortException)
                    {
                        return;
                    }
                    catch
                    {
                        _failureCount++;

                        if (_failureCount > 10)
                        {
                            _failureCount = 10;
                        }
                    }
                    finally
                    {
                        Thread.SpinWait(1000 * (int)Math.Pow(_failureCount, _failureCount));
                    }
                }
            });

            _thread.IsBackground = true;

            _thread.Start();
        }