internal static string LinkifiedText(TwitterEntityCollection entities, string text)
        {
            if (entities == null || entities.Count == 0)
            {
                return(text);
            }

            string linkedText = text;

            var entitiesSorted = entities.OrderBy(e => e.StartIndex).Reverse();

            foreach (TwitterEntity entity in entitiesSorted)
            {
                if (entity is TwitterHashTagEntity)
                {
                    TwitterHashTagEntity tagEntity = (TwitterHashTagEntity)entity;

                    linkedText = string.Format(
                        "{0}<a href=\"https://twitter.com/search?q=%23{1}\">{1}</a>{2}",
                        linkedText.Substring(0, entity.StartIndex),
                        tagEntity.Text,
                        linkedText.Substring(entity.EndIndex));
                }

                if (entity is TwitterUrlEntity)
                {
                    TwitterUrlEntity urlEntity = (TwitterUrlEntity)entity;

                    linkedText = string.Format(
                        "{0}<a href=\"{1}\">{1}</a>{2}",
                        linkedText.Substring(0, entity.StartIndex),
                        urlEntity.Url,
                        linkedText.Substring(entity.EndIndex));
                }

                if (entity is TwitterMentionEntity)
                {
                    TwitterMentionEntity mentionEntity = (TwitterMentionEntity)entity;

                    linkedText = string.Format(
                        "{0}<a href=\"https://twitter.com/{1}\">@{1}</a>{2}",
                        linkedText.Substring(0, entity.StartIndex),
                        mentionEntity.ScreenName,
                        linkedText.Substring(entity.EndIndex));
                }
            }

            return(linkedText);
        }
        internal static string LinkifiedText(TwitterEntityCollection entities, string text)
        {
            if (entities == null || entities.Count == 0)
            {
                return text;
            }

            string linkedText = text;

            var entitiesSorted = entities.OrderBy(e => e.StartIndex).Reverse();

            foreach (TwitterEntity entity in entitiesSorted)
            {
                if (entity is TwitterHashTagEntity)
                {
                    TwitterHashTagEntity tagEntity = (TwitterHashTagEntity)entity;

                    linkedText = string.Format(
                        "{0}<a href=\"http://twitter.com/search?q=%23{1}\">{1}</a>{2}",
                        linkedText.Substring(0, entity.StartIndex),
                        tagEntity.Text,
                        linkedText.Substring(entity.EndIndex));
                }

                if (entity is TwitterUrlEntity)
                {
                    TwitterUrlEntity urlEntity = (TwitterUrlEntity)entity;

                    linkedText = string.Format(
                        "{0}<a href=\"{1}\">{1}</a>{2}",
                        linkedText.Substring(0, entity.StartIndex),
                        urlEntity.Url,
                        linkedText.Substring(entity.EndIndex));
                }

                if (entity is TwitterMentionEntity)
                {
                    TwitterMentionEntity mentionEntity = (TwitterMentionEntity)entity;

                    linkedText = string.Format(
                        "{0}<a href=\"http://twitter.com/{1}\">@{1}</a>{2}",
                        linkedText.Substring(0, entity.StartIndex),
                        mentionEntity.ScreenName,
                        linkedText.Substring(entity.EndIndex));
                }
            }

            return linkedText;
        }        
示例#3
0
            /// <summary>
            /// Writes the JSON representation of the object.
            /// </summary>
            /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
            /// <param name="value">The value.</param>
            /// <param name="serializer">The calling serializer.</param>
            /// <remarks>This is a best attempt to recreate the structure created by the Twitter API.</remarks>
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                TwitterEntityCollection entities = (TwitterEntityCollection)value;

                writer.WriteStartObject();
                {
                    WriteEntity(writer, entities.OfType <TwitterHashTagEntity>().ToList(), "hashtags", (w, e) =>
                    {
                        w.WritePropertyName("text");
                        w.WriteValue(e.Text);
                    });

                    WriteEntity(writer, entities.OfType <TwitterMentionEntity>().ToList(), "user_mentions", (w, e) =>
                    {
                        w.WritePropertyName("screen_name");
                        w.WriteValue(e.ScreenName);

                        w.WritePropertyName("name");
                        w.WriteValue(e.Name);

                        w.WritePropertyName("id");
                        w.WriteValue(e.UserId);
                    });

                    WriteEntity(writer, entities.OfType <TwitterUrlEntity>().ToList(), "urls", (w, e) =>
                    {
                        w.WritePropertyName("url");
                        w.WriteValue(e.Url);

                        w.WritePropertyName("display_url");
                        w.WriteValue(e.DisplayUrl);

                        w.WritePropertyName("expanded_url");
                        w.WriteValue(e.ExpandedUrl);
                    });

                    WriteEntity(writer, entities.OfType <TwitterMediaEntity>().ToList(), "media", WriteMediaEntity);

                    writer.WriteEndObject();
                }
            }
示例#4
0
            /// <summary>
            /// Reads the JSON representation of the object.
            /// </summary>
            /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
            /// <param name="objectType">Type of the object.</param>
            /// <param name="existingValue">The existing value of object being read.</param>
            /// <param name="serializer">The calling serializer.</param>
            /// <returns>The object value.</returns>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                TwitterEntityCollection result = existingValue as TwitterEntityCollection;

                if (result == null)
                {
                    result = new TwitterEntityCollection();
                }

                int           startDepth = reader.Depth;
                string        entityType = string.Empty;
                TwitterEntity entity     = null;

                try
                {
                    while (reader.Read() && reader.Depth >= startDepth)
                    {
                        if (reader.Depth == startDepth && reader.TokenType == JsonToken.EndObject)
                        {
                            break;
                        }

                        if (reader.TokenType == JsonToken.PropertyName && reader.Depth == startDepth + 1)
                        {
                            entityType = (string)reader.Value;
                            continue;
                        }

                        switch (entityType)
                        {
                        case "urls":
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                entity = new TwitterUrlEntity();
                            }

                            TwitterUrlEntity tue = entity as TwitterUrlEntity;
                            if (tue != null)
                            {
                                ReadFieldValue(reader, "url", entity, () => tue.Url);
                                ReadFieldValue(reader, "display_url", entity, () => tue.DisplayUrl);
                                ReadFieldValue(reader, "expanded_url", entity, () => tue.ExpandedUrl);
                            }
                            break;

                        case "user_mentions":
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                entity = new TwitterMentionEntity();
                            }

                            TwitterMentionEntity tme = entity as TwitterMentionEntity;
                            if (tme != null)
                            {
                                ReadFieldValue(reader, "screen_name", entity, () => tme.ScreenName);
                                ReadFieldValue(reader, "name", entity, () => tme.Name);
                                ReadFieldValue(reader, "id", entity, () => tme.UserId);
                            }
                            break;

                        case "hashtags":
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                entity = new TwitterHashTagEntity();
                            }

                            TwitterHashTagEntity the = entity as TwitterHashTagEntity;
                            if (the != null)
                            {
                                ReadFieldValue(reader, "text", entity, () => the.Text);
                            }
                            break;

                        case "media":
                            // Move to object start and parse the entity
                            reader.Read();
                            entity = parseMediaEntity(reader);

                            break;
                        }

                        // Read the indicies (for all entities except Media)
                        if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "indices" && entity != null)
                        {
                            reader.Read();
                            reader.Read();
                            entity.StartIndex = Convert.ToInt32((long)reader.Value);
                            reader.Read();
                            entity.EndIndex = Convert.ToInt32((long)reader.Value);
                        }

                        if ((reader.TokenType == JsonToken.EndObject && entity != null) || entity is TwitterMediaEntity)
                        {
                            result.Add(entity);
                            entity = null;
                        }
                    }
                }
                catch { }

                return(result);
            }