/// <summary> /// Check parse streamed JSON line as normal (not direct-message) status /// </summary> /// <param name="graph">JSON object graph</param> /// <param name="handler">stream handler</param> /// <returns></returns> internal static bool ParseStreamLineAsStatus(dynamic graph, IStreamHandler handler) { if (!graph.text()) { return(false); } handler.OnStatus(new TwitterStatus(graph)); return(true); }
/// <summary> /// Check parse streamed JSON line as normal (not direct-message) status /// </summary> /// <param name="graph">JSON object graph</param> /// <param name="handler">stream handler</param> /// <returns></returns> internal static bool ParseStreamLineAsStatus(JsonValue graph, IStreamHandler handler) { if (!graph.ContainsKey("text")) { return(false); } handler.OnStatus(new TwitterStatus(graph)); return(true); }
/// <summary> /// Parse streamed JSON line (which is not a status) /// </summary> /// <param name="graph">JSON object graph</param> /// <param name="handler">result handler</param> internal static void ParseNotStatusStreamLine(dynamic graph, IStreamHandler handler) { try { // element.foo() -> element.IsDefined("foo") // direct message if (graph.direct_message()) { handler.OnStatus(new TwitterStatus(graph.direct_message)); return; } // delete if (graph.delete()) { if (graph.delete.status()) { handler.OnMessage(new StreamDelete( Int64.Parse(graph.delete.status.id_str), Int64.Parse(graph.delete.status.user_id_str), graph.delete.timestamp_ms)); return; } if (graph.delete.direct_message()) { handler.OnMessage(new StreamDelete( Int64.Parse(graph.delete.status.id_str), Int64.Parse(graph.delete.direct_message.user_id.ToString()), graph.delete.timestamp_ms)); return; } } // scrub_geo if (graph.scrub_geo()) { handler.OnMessage(new StreamScrubGeo( Int64.Parse(graph.scrub_geo.user_id_str), Int64.Parse(graph.scrub_geo.up_to_status_id_str), graph.scrub_geo.timestamp_ms)); return; } // limit if (graph.limit()) { handler.OnMessage(new StreamLimit( (long)graph.limit.track, graph.limit.timestamp_ms)); return; } // withheld if (graph.status_withheld()) { handler.OnMessage(new StreamWithheld( Int64.Parse(graph.status_withheld.user_id), Int64.Parse(graph.status_withheld.id), graph.status_withheld.withheld_in_countries, graph.status_withheld.timestamp_ms)); return; } if (graph.user_withheld()) { handler.OnMessage(new StreamWithheld( Int64.Parse(graph.user_withheld.id), graph.user_withheld.withheld_in_countries, graph.user_withheld.timestamp_ms)); return; } // disconnect if (graph.disconnect()) { handler.OnMessage(new StreamDisconnect( (DisconnectCode)graph.disconnect.code, graph.disconnect.stream_name, graph.disconnect.reason, graph.disconnect.timestamp_ms)); return; } // stall warning if (graph.warning()) { if (graph.warning.code == "FALLING_BEHIND") { handler.OnMessage(new StreamStallWarning( graph.warning.code, graph.warning.message, graph.warning.percent_full, graph.warning.timestamp_ms)); return; } } // user update if (graph.IsDefined("event")) // 'event' is the reserved word... { var ev = ((string)graph["event"]).ToLower(); if (ev == "user_update") { // parse user_update only in generic streams. handler.OnMessage(new StreamUserEvent( new TwitterUser(graph.source), new TwitterUser(graph.target), ev, ((string)graph.created_at).ParseTwitterDateTime())); return; } // unknown event... handler.OnMessage(new StreamUnknownMessage("event: " + ev, graph.ToString())); } if (graph.IsObject()) { // unknown... foreach (KeyValuePair <string, dynamic> item in graph) { handler.OnMessage(new StreamUnknownMessage(item.Key, item.Value.ToString())); return; } } // unknown event-type... handler.OnMessage(new StreamUnknownMessage(null, graph.Value.ToString())); } catch (Exception ex) { handler.OnException(new StreamParseException( "Stream graph parse failed.", graph.ToString(), ex)); } }
private static void DispatchStreamingElements(dynamic element, IStreamHandler handler) { var type = "initialize"; try { // element.foo() -> element.IsDefined("foo") if (element.text()) { // standard status receiving type = "status"; handler.OnStatus(new TwitterStatus(element)); return; } if (element.direct_message()) { // direct message type = "message"; handler.OnStatus(new TwitterStatus(element.direct_message)); return; } if (element.delete()) { type = "delete"; // status or message is deleted if (element.delete.status()) { // status is deleted handler.OnDeleted(new StreamDelete { Id = Int64.Parse(element.delete.status.id_str), UserId = Int64.Parse(element.delete.status.user_id_str) }); } if (element.delete.direct_message()) { // message is deleted handler.OnDeleted(new StreamDelete { Id = Int64.Parse(element.delete.direct_message.id_str), // UserId = Int64.Parse(element.delete.status.user_id_str) // user_id_str field is not exist. UserId = Int64.Parse(element.delete.direct_message.user_id.ToString()) }); } return; } if (element.scrub_geo()) { type = "geolocation"; // TODO: Not implemented.(Location deletion notices) return; } if (element.limit()) { type = "tracklimit"; handler.OnTrackLimit(new StreamTrackLimit { UndeliveredCount = (long)element.limit.track }); return; } if (element.status_withheld() || element.user_withheld()) { type = "withheld"; // TODO: Not implemented.(???) return; } if (element.disconnect()) { type = "discon"; handler.OnDisconnect(new StreamDisconnect { Code = (DisconnectCode)element.disconnect.code, Reason = element.disconnect.reason, StreamName = element.disconnect.stream_name }); return; } if (element.warning()) { type = "warning"; // TODO: Not implemented.(stall warning) return; } if (element.friends()) { type = "friends"; handler.OnEnumerationReceived(new StreamEnumeration { Friends = (long[])element.friends }); return; } if (element.IsDefined("event")) { type = "event"; string ev = ((string)element["event"]).ToLower(); type = "event:" + ev; switch (ev) { case "favorite": case "unfavorite": handler.OnStatusActivity(new StreamStatusActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamStatusActivity.ToEnumEvent(ev), EventRawString = ev, Status = new TwitterStatus(element.target_object), CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; case "block": case "unblock": case "follow": case "unfollow": case "user_update": handler.OnUserActivity(new StreamUserActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamUserActivity.ToEnumEvent(ev), EventRawString = ev, CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; case "list_created": case "list_destroyed": case "list_updated": case "list_member_added": case "list_member_removed": case "list_user_subscribed": case "list_user_unsubscribed": handler.OnListActivity(new StreamListActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamListActivity.ToEnumEvent(ev), EventRawString = ev, List = new TwitterList(element.target_object), CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; default: handler.OnExceptionThrownDuringParsing(new Exception("Unknown event: " + ev + " / " + element.ToString())); return; } } handler.OnExceptionThrownDuringParsing(new Exception("Unknown data: " + element.ToString())); } catch (Exception ex) { string elemstr = element.ToString(); System.Diagnostics.Debug.WriteLine("!exception thrown!" + Environment.NewLine + elemstr); handler.OnExceptionThrownDuringParsing(new Exception("type:" + type, ex)); } }
/// <summary> /// Parse streamed JSON line (which is not a status) /// </summary> /// <param name="graph">JSON object graph</param> /// <param name="handler">result handler</param> internal static void ParseNotStatusStreamLine(JsonValue graph, IStreamHandler handler) { try { // element.foo() -> element.IsDefined("foo") // direct message if (graph.TryGetValue("direct_message", out var directMessage)) { handler.OnStatus(new TwitterStatus(directMessage)); return; } // delete if (graph.TryGetValue("delete", out var delete)) { var timestamp = GetTimestamp(delete); if (delete.TryGetValue("status", out var delstatus)) { handler.OnMessage(new StreamDelete( Int64.Parse(delstatus["id_str"].AsString()), Int64.Parse(delstatus["user_id_str"].AsString()), timestamp)); return; } if (delete.TryGetValue("direct_message", out var delmsg)) { handler.OnMessage(new StreamDelete( Int64.Parse(delmsg["id_str"].AsString()), Int64.Parse(delmsg["user_id"].AsString()), timestamp)); return; } } // scrub_geo if (graph.TryGetValue("scrub_geo", out var scrubGeo)) { handler.OnMessage(new StreamScrubGeo( Int64.Parse(scrubGeo["user_id_str"].AsString()), Int64.Parse(scrubGeo["up_to_status_id_str"].AsString()), GetTimestamp(scrubGeo))); return; } // limit if (graph.TryGetValue("limit", out var limit)) { handler.OnMessage(new StreamLimit( limit["track"].AsLong(), GetTimestamp(limit))); return; } // withheld if (graph.TryGetValue("status_withheld", out var statusWithheld)) { handler.OnMessage(new StreamWithheld( statusWithheld["user_id"].AsLong(), statusWithheld["id"].AsLong(), ((JsonArray)statusWithheld["withheld_in_countries"]).Select(s => s.AsString()).ToArray(), GetTimestamp(statusWithheld))); return; } if (graph.TryGetValue("user_withheld", out var userWithheld)) { handler.OnMessage(new StreamWithheld( userWithheld["id"].AsLong(), ((JsonArray)statusWithheld["withheld_in_countries"]).Select(s => s.AsString()).ToArray(), GetTimestamp(statusWithheld))); return; } // disconnect if (graph.TryGetValue("disconnect", out var disconnect)) { handler.OnMessage(new StreamDisconnect( (DisconnectCode)disconnect["code"].AsLong(), disconnect["stream_name"].AsString(), disconnect["reason"].AsString(), GetTimestamp(disconnect))); return; } // stall warning if (graph.TryGetValue("warning", out var warning)) { var timestamp = GetTimestamp(warning); var code = warning["code"].AsString(); if (code == "FALLING_BEHIND") { handler.OnMessage(new StreamStallWarning( code, warning["message"].AsString(), (int)warning["percent_full"].AsLong(), timestamp)); return; } } // user update if (graph.TryGetValue("event", out var @event)) { var ev = @event.AsString().ToLower(); if (ev == StreamUserEvent.UserUpdateEventKey) { // parse user_update only in generic streams. handler.OnMessage(new StreamUserEvent( new TwitterUser(graph["source"]), new TwitterUser(graph["target"]), ev, graph["created_at"].AsString().ParseTwitterDateTime())); return; } // unknown event... handler.OnMessage(new StreamUnknownMessage("event: " + ev, graph.ToString())); } // unknown event-type... handler.OnMessage(new StreamUnknownMessage(null, graph.ToString())); } catch (Exception ex) { handler.OnException(new StreamParseException( "Stream graph parse failed.", graph.ToString(), ex)); } }
private static void DispatchStreamingElements(dynamic element, IStreamHandler handler) { if (element.text()) { // standard status receiving handler.OnStatus(new TwitterStatus(element)); } // element.foo() -> element.IsDefined("foo") if (element.delete()) { // delete handler handler.OnDeleted(new StreamDelete { Id = Int64.Parse(element.delete.status.id_str), UserId = Int64.Parse(element.delete.status.user_id_str) }); return; } if (element.scrub_geo()) { // TODO: Not implemented.(Location deletion notices) return; } if (element.limit()) { handler.OnTrackLimit(new StreamTrackLimit { UndeliveredCount = (long)element.limit.track }); return; } if (element.status_withheld() || element.user_withheld()) { // TODO: Not implemented.(???) return; } if (element.disconnect()) { handler.OnDisconnect(new StreamDisconnect { Code = (DisconnectCode)element.disconnect.code, Reason = element.disconnect.reason, StreamName = element.disconnect.stream_name }); } if (element.warning()) { // TODO: Not implemented.(stall warning) return; } if (element.friends()) { handler.OnEnumerationReceived(new StreamEnumeration { Friends = (long[])element.friends }); } if (element.IsDefined("event")) { string ev = ((string)element["event"]).ToLower(); switch (ev) { case "favorite": case "unfavorite": handler.OnStatusActivity(new StreamStatusActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamStatusActivity.ToEnumEvent(ev), EventRawString = ev, Status = new TwitterStatus(element.target_object), CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; case "block": case "unblock": case "follow": case "unfollow": case "user_update": handler.OnUserActivity(new StreamUserActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamUserActivity.ToEnumEvent(ev), EventRawString = ev, CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; case "list_created": case "list_destroyed": case "list_updated": case "list_member_added": case "list_member_removed": case "list_user_subscribed": case "list_user_unsubscribed": handler.OnListActivity(new StreamListActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamListActivity.ToEnumEvent(ev), EventRawString = ev, List = new TwitterList(element.target_object), CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); break; } } }
private static void DispatchStreamingElements(dynamic element, IStreamHandler handler) { var type = "initialize"; try { // element.foo() -> element.IsDefined("foo") if (element.text()) { // standard status receiving type = "status"; handler.OnStatus(new TwitterStatus(element)); return; } if (element.direct_message()) { // direct message type = "message"; handler.OnStatus(new TwitterStatus(element.direct_message)); return; } if (element.delete()) { type = "delete"; // status or message is deleted if (element.delete.status()) { // status is deleted handler.OnDeleted(new StreamDelete { Id = Int64.Parse(element.delete.status.id_str), UserId = Int64.Parse(element.delete.status.user_id_str) }); } if (element.delete.direct_message()) { // message is deleted handler.OnDeleted(new StreamDelete { Id = Int64.Parse(element.delete.direct_message.id_str), // UserId = Int64.Parse(element.delete.status.user_id_str) // user_id_str field is not exist. UserId = Int64.Parse(element.delete.direct_message.user_id.ToString()) }); } return; } if (element.scrub_geo()) { type = "geolocation"; // TODO: Not implemented.(Location deletion notices) return; } if (element.limit()) { type = "tracklimit"; handler.OnTrackLimit(new StreamTrackLimit { UndeliveredCount = (long)element.limit.track }); return; } if (element.status_withheld() || element.user_withheld()) { type = "withheld"; // TODO: Not implemented.(???) return; } if (element.disconnect()) { type = "discon"; handler.OnDisconnect(new StreamDisconnect { Code = (DisconnectCode)element.disconnect.code, Reason = element.disconnect.reason, StreamName = element.disconnect.stream_name }); return; } if (element.warning()) { type = "warning"; // TODO: Not implemented.(stall warning) return; } if (element.friends()) { type = "friends"; handler.OnEnumerationReceived(new StreamEnumeration { Friends = (long[])element.friends }); return; } if (element.IsDefined("event")) { type = "event"; string ev = ((string)element["event"]).ToLower(); type = "event:" + ev; switch (ev) { case "favorite": case "unfavorite": handler.OnStatusActivity(new StreamStatusActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamStatusActivity.ToEnumEvent(ev), EventRawString = ev, Status = new TwitterStatus(element.target_object), CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; case "block": case "unblock": case "follow": case "unfollow": case "user_update": handler.OnUserActivity(new StreamUserActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamUserActivity.ToEnumEvent(ev), EventRawString = ev, CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; case "list_created": case "list_destroyed": case "list_updated": case "list_member_added": case "list_member_removed": case "list_user_subscribed": case "list_user_unsubscribed": handler.OnListActivity(new StreamListActivity { Target = new TwitterUser(element.target), Source = new TwitterUser(element.source), Event = StreamListActivity.ToEnumEvent(ev), EventRawString = ev, List = new TwitterList(element.target_object), CreatedAt = ((string)element.created_at).ParseDateTime(ParsingExtension.TwitterDateTimeFormat), }); return; default: handler.OnExceptionThrownDuringParsing(new Exception("Unknown event: " + ev + " / " + element.ToString())); return; } } handler.OnExceptionThrownDuringParsing(new Exception("Unknown data: " + element.ToString())); } catch (Exception ex) { string elemstr = element.ToString(); System.Diagnostics.Debug.WriteLine("!exception thrown!" + Environment.NewLine + elemstr); handler.OnExceptionThrownDuringParsing(new Exception("type:" + type, ex)); } }
/// <summary> /// Parse streamed JSON line (which is not a status) /// </summary> /// <param name="graph">JSON object graph</param> /// <param name="handler">result handler</param> internal static void ParseNotStatusStreamLine(JsonValue graph, IStreamHandler handler) { try { // element.foo() -> element.IsDefined("foo") // direct message JsonValue directMessage; if (graph.TryGetValue("direct_message", out directMessage)) { handler.OnStatus(new TwitterStatus(graph["direct_message"])); return; } // delete JsonValue delete; if (graph.TryGetValue("delete", out delete)) { var timestamp = GetTimestamp(delete); JsonValue status; if (delete.TryGetValue("status", out status)) { handler.OnMessage(new StreamDelete( Int64.Parse(status["id_str"].AsString()), Int64.Parse(status["user_id_str"].AsString()), timestamp)); return; } if (delete.TryGetValue("direct_message", out directMessage)) { handler.OnMessage(new StreamDelete( Int64.Parse(directMessage["id_str"].AsString()), Int64.Parse(directMessage["user_id"].AsString()), timestamp)); return; } } // scrub_geo JsonValue scrubGeo; if (graph.TryGetValue("scrub_geo", out scrubGeo)) { handler.OnMessage(new StreamScrubGeo( Int64.Parse(scrubGeo["user_id_str"].AsString()), Int64.Parse(scrubGeo["up_to_status_id_str"].AsString()), GetTimestamp(scrubGeo))); return; } // limit JsonValue limit; if (graph.TryGetValue("limit", out limit)) { handler.OnMessage(new StreamLimit( limit["track"].AsLong(), GetTimestamp(limit))); return; } // withheld JsonValue statusWithheld; if (graph.TryGetValue("status_withheld", out statusWithheld)) { handler.OnMessage(new StreamWithheld( statusWithheld["user_id"].AsLong(), statusWithheld["id"].AsLong(), ((JsonArray)statusWithheld["withheld_in_countries"]).Select(s => s.AsString()).ToArray(), GetTimestamp(statusWithheld))); return; } JsonValue userWithheld; if (graph.TryGetValue("user_withheld", out userWithheld)) { handler.OnMessage(new StreamWithheld( userWithheld["id"].AsLong(), ((JsonArray)statusWithheld["withheld_in_countries"]).Select(s => s.AsString()).ToArray(), GetTimestamp(statusWithheld))); return; } // disconnect JsonValue disconnect; if (graph.TryGetValue("disconnect", out disconnect)) { handler.OnMessage(new StreamDisconnect( (DisconnectCode)disconnect["code"].AsLong(), disconnect["stream_name"].AsString(), disconnect["reason"].AsString(), GetTimestamp(disconnect))); return; } // stall warning JsonValue warning; if (graph.TryGetValue("warning", out warning)) { var timestamp = GetTimestamp(warning); var code = warning["code"].AsString(); if (code == "FALLING_BEHIND") { handler.OnMessage(new StreamStallWarning( code, warning["message"].AsString(), (int)warning["percent_full"].AsLong(), timestamp)); return; } } // user update JsonValue @event; if (graph.TryGetValue("event", out @event)) { var ev = @event.AsString().ToLower(); if (ev == "user_update") { // parse user_update only in generic streams. handler.OnMessage(new StreamUserEvent( new TwitterUser(graph["source"]), new TwitterUser(graph["target"]), ev, graph["created_at"].AsString().ParseTwitterDateTime())); return; } // unknown event... handler.OnMessage(new StreamUnknownMessage("event: " + ev, graph.ToString())); } // unknown event-type... handler.OnMessage(new StreamUnknownMessage(null, graph.ToString())); } catch (Exception ex) { handler.OnException(new StreamParseException( "Stream graph parse failed.", graph.ToString(), ex)); } }
/// <summary> /// Check parse streamed JSON line as normal (not direct-message) status /// </summary> /// <param name="graph">JSON object graph</param> /// <param name="handler">stream handler</param> /// <returns></returns> internal static bool ParseStreamLineAsStatus(JsonValue graph, IStreamHandler handler) { if (!graph.ContainsKey("text")) return false; handler.OnStatus(new TwitterStatus(graph)); return true; }