Provides utility methods for parsing JSON received from Twitter.
Пример #1
0
        GetEntities
        (
            Dictionary <String, Object> entityValueDictionary,
            String entityName,
            String entityChildName,
            Boolean convertToLowerCase
        )
        {
            Debug.Assert(entityValueDictionary != null);
            Debug.Assert(!String.IsNullOrEmpty(entityName));
            Debug.Assert(!String.IsNullOrEmpty(entityChildName));

            List <String> entities = new List <String>();
            Object        entitiesAsObject;

            if (
                entityValueDictionary.TryGetValue(entityName, out entitiesAsObject)
                &&
                entitiesAsObject is Object[]
                )
            {
                foreach (Object entityAsObject in ( Object [] )entitiesAsObject)
                {
                    String childValue;

                    if (
                        entityAsObject is Dictionary <String, Object>
                        &&
                        TwitterJsonUtil.TryGetJsonValueFromDictionary(
                            (Dictionary <String, Object>)entityAsObject,
                            entityChildName, out childValue)
                        )
                    {
                        if (convertToLowerCase)
                        {
                            childValue = childValue.ToLower();
                        }

                        entities.Add(childValue);
                    }
                }
            }

            return(entities.ToArray());
        }
Пример #2
0
        GetTwitterResponseAsString
        (
            Stream oStream
        )
        {
            Debug.Assert(oStream != null);
            AssertValid();

            String sResponse = new StreamReader(oStream).ReadToEnd();

            if (!TwitterJsonUtil.IsValidJson(sResponse))
            {
                // Twitter occasionally sends a partial response with truncated
                // JSON.  That would lead to an ArgumentException with the message
                // "Unterminated string passed in" if parsing were attempted.

                throw new InvalidJsonException(
                          "The text received from Twitter is not valid JSON.");
            }

            return(sResponse);
        }
Пример #3
0
        TryGetQueryParametersForNextSearchPage
        (
            Dictionary <String, Object> oResponseDictionary,
            out String sQueryParametersForNextPage
        )
        {
            Debug.Assert(oResponseDictionary != null);
            AssertValid();

            sQueryParametersForNextPage = null;

            Dictionary <String, Object> oSearchMetadataDictionary =
                (Dictionary <String, Object>)
                oResponseDictionary["search_metadata"];

            return(
                oSearchMetadataDictionary != null
                &&
                TwitterJsonUtil.TryGetJsonValueFromDictionary(
                    oSearchMetadataDictionary, "next_results",
                    out sQueryParametersForNextPage)
                );
        }
Пример #4
0
        EnumerateJsonValues
        (
            String url,
            String jsonName,
            Int32 maximumValues,
            Boolean skipMostPage1Errors,
            RequestStatistics requestStatistics,
            ReportProgressHandler reportProgressHandler,
            CheckCancellationPendingHandler checkCancellationPendingHandler
        )
        {
            // Note:
            //
            // The logic in this method is similar to the logic in
            // EnumerateSearchStatuses().  In fact, at one time all enumeration was
            // done through this EnumerateJsonValues() method.
            // EnumerateSearchStatuses() was created only when version 1.1 of the
            // Twitter API introduced yet another paging scheme, one that differs
            // from the cursor scheme that this method handles.
            //
            // A possible work item is to recombine the two methods into one,
            // possibly by using a delegate to handle the different paging schemes.

            Debug.Assert(!String.IsNullOrEmpty(url));
            Debug.Assert(maximumValues > 0);
            Debug.Assert(requestStatistics != null);
            AssertValid();

            Int32  iPage              = 1;
            String sCursor            = null;
            Int32  iObjectsEnumerated = 0;

            while (true)
            {
                if (iPage > 1)
                {
                    HttpSocialNetworkUtil.ReportProgress(reportProgressHandler,

                                                         "Getting page {0}."
                                                         ,
                                                         iPage
                                                         );
                }

                String sUrlWithCursor = AppendCursorToUrl(url, sCursor);

                Dictionary <String, Object> oValueDictionary = null;
                Object [] aoObjectsThisPage;

                try
                {
                    Object oDeserializedTwitterResponse =
                        (new JavaScriptSerializer()).DeserializeObject(
                            GetTwitterResponseAsString(sUrlWithCursor,
                                                       requestStatistics, reportProgressHandler,
                                                       checkCancellationPendingHandler));

                    Object oObjectsThisPageAsObject;

                    if (jsonName == null)
                    {
                        // The top level of the Json response contains an array of
                        // objects this method will enumerate.

                        oObjectsThisPageAsObject = oDeserializedTwitterResponse;
                    }
                    else
                    {
                        // The top level of the Json response contains a set of
                        // name/value pairs.  The value for the specified name is
                        // the array of objects this method will enumerate.

                        oValueDictionary = (Dictionary <String, Object>)
                                           oDeserializedTwitterResponse;

                        oObjectsThisPageAsObject = oValueDictionary[jsonName];
                    }

                    aoObjectsThisPage = ( Object [] )oObjectsThisPageAsObject;
                }
                catch (Exception oException)
                {
                    // Rethrow the exception if appropriate.

                    TwitterUtil.OnExceptionWhileEnumeratingJsonValues(
                        oException, iPage, skipMostPage1Errors);

                    // Otherwise, just halt the enumeration.

                    yield break;
                }

                Int32 iObjectsThisPage = aoObjectsThisPage.Length;

                if (iObjectsThisPage == 0)
                {
                    yield break;
                }

                for (Int32 i = 0; i < iObjectsThisPage; i++)
                {
                    yield return(aoObjectsThisPage[i]);

                    iObjectsEnumerated++;

                    if (iObjectsEnumerated == maximumValues)
                    {
                        yield break;
                    }
                }

                iPage++;

                // When the top level of the Json response contains a set of
                // name/value pairs, a next_cursor_str value of "0" means "end of
                // data."

                if (
                    oValueDictionary == null
                    ||
                    !TwitterJsonUtil.TryGetJsonValueFromDictionary(
                        oValueDictionary, "next_cursor_str", out sCursor)
                    ||
                    sCursor == "0"
                    )
                {
                    yield break;
                }

                // Get the next page...
            }
        }
Пример #5
0
        TryParseStatus
        (
            Dictionary <String, Object> statusValueDictionary,
            out Int64 statusID,
            out DateTime statusDateUtc,
            out String screenName,
            out String text,
            out String rawStatusJson,
            out Dictionary <String, Object> userValueDictionary
        )
        {
            Debug.Assert(statusValueDictionary != null);

            statusID            = Int64.MinValue;
            statusDateUtc       = DateTime.MinValue;
            screenName          = null;
            text                = null;
            rawStatusJson       = null;
            userValueDictionary = null;

            String statusDateUtcString;

            if (
                !TwitterJsonUtil.TryGetJsonValueFromDictionary(
                    statusValueDictionary, "id", out statusID)
                ||
                !TwitterJsonUtil.TryGetJsonValueFromDictionary(
                    statusValueDictionary, "created_at", out statusDateUtcString)
                ||
                !TwitterDateParser.TryParseTwitterDate(statusDateUtcString,
                                                       out statusDateUtc)
                ||
                !TwitterJsonUtil.TryGetJsonValueFromDictionary(
                    statusValueDictionary, "text", out text)
                )
            {
                return(false);
            }

            const String UserKeyName = "user";

            if (!statusValueDictionary.ContainsKey(UserKeyName))
            {
                // This has actually happened--Twitter occasionally sends a
                // status without user information.

                return(false);
            }

            userValueDictionary =
                (Dictionary <String, Object>)statusValueDictionary[UserKeyName];

            if (!TwitterJsonUtil.TryGetJsonValueFromDictionary(
                    userValueDictionary, "screen_name", out screenName))
            {
                return(false);
            }

            rawStatusJson = ValueDictionaryToRawJson(statusValueDictionary);

            return(true);
        }