Esempio n. 1
0
        TestFormatDuration()
        {
            TimeSpan oDuration = new TimeSpan(0);

            Assert.AreEqual("0-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 2
0
        TestFormatDuration2()
        {
            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(0, 0, 1);

            Assert.AreEqual("0-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 3
0
        TestFormatDuration14()
        {
            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(123, 19, 44);

            Assert.AreEqual("5-day, 3-hour, 19-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 4
0
        TestFormatDuration11()
        {
            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(25, 59, 48);

            Assert.AreEqual("1-day, 1-hour, 59-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 5
0
        TestFormatDuration13()
        {
            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(26, 12, 14);

            Assert.AreEqual("1-day, 2-hour, 12-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 6
0
        TestFormatDuration6()
        {
            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(23, 43, 59);

            Assert.AreEqual("23-hour, 43-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 7
0
        TestFormatDuration7()
        {
            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(24, 0, 0);

            Assert.AreEqual("1-day, 0-hour, 0-minute",
                            DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 8
0
        TestFormatDurationBad()
        {
            // Negative duration.

            // Hours, minutes, seconds.
            TimeSpan oDuration = new TimeSpan(-123, -19, -44);

            DateTimeUtil2.FormatDuration(oDuration);
        }
Esempio n. 9
0
        FormatDuration
        (
            DateTime startOfPeriodUtc,
            DateTime endOfPeriodUtc
        )
        {
            AssertValid();

            TimeSpan oDuration = endOfPeriodUtc - startOfPeriodUtc;

            return(oDuration.Ticks < 0 ? String.Empty :
                   DateTimeUtil2.FormatDuration(oDuration));
        }
Esempio n. 10
0
        OnNetworkObtained
        (
            XmlDocument oGraphMLXmlDocument,
            RequestStatistics oRequestStatistics,
            String sNetworkDescription,
            String sNetworkTitle,
            String sPartialFileName
        )
        {
            Debug.Assert(oGraphMLXmlDocument != null);
            Debug.Assert(oRequestStatistics != null);
            Debug.Assert(!String.IsNullOrEmpty(sNetworkDescription));
            Debug.Assert(!String.IsNullOrEmpty(sNetworkTitle));
            Debug.Assert(!String.IsNullOrEmpty(sPartialFileName));
            AssertValid();

            XmlNode oGraphXmlNode = XmlUtil2.SelectRequiredSingleNode(
                oGraphMLXmlDocument, "g:graphml/g:graph",

                GraphMLXmlDocument.CreateXmlNamespaceManager(
                    oGraphMLXmlDocument, "g")
                );

            XmlUtil2.SetAttributes(oGraphXmlNode,
                                   "description", sNetworkDescription);

            XmlUtil2.SetAttributes(oGraphXmlNode,
                                   "suggestedTitle", sNetworkTitle);

            String sSuggestedFileNameNoExtension = String.Format(
                "{0} NodeXL {1}"
                ,
                DateTimeUtil2.ToCultureInvariantFileName(
                    oRequestStatistics.StartTimeUtc),

                sPartialFileName
                );

            XmlUtil2.SetAttributes(oGraphXmlNode,
                                   "suggestedFileNameNoExtension", sSuggestedFileNameNoExtension);

            if (oRequestStatistics.UnexpectedExceptions > 0)
            {
                // The network is partial.

                throw new PartialNetworkException(oGraphMLXmlDocument,
                                                  oRequestStatistics);
            }
        }
Esempio n. 11
0
        ParseTwitterDate
        (
            String twitterDate
        )
        {
            Debug.Assert(twitterDate != null);

            DateTime oParsedTwitterDate;

            if (!TryParseTwitterDate(twitterDate, out oParsedTwitterDate))
            {
                return(twitterDate);
            }

            return(DateTimeUtil2.ToCultureInvariantString(oParsedTwitterDate));
        }
        AppendStartTimeRelationshipDateUtcGraphMLAttributeValue
        (
            GraphMLXmlDocument oGraphMLXmlDocument,
            XmlNode oEdgeXmlNode,
            RequestStatistics oRequestStatistics
        )
        {
            Debug.Assert(oGraphMLXmlDocument != null);
            Debug.Assert(oEdgeXmlNode != null);
            Debug.Assert(oRequestStatistics != null);
            AssertValid();

            oGraphMLXmlDocument.AppendGraphMLAttributeValue(oEdgeXmlNode,

                                                            TwitterGraphMLUtil.EdgeRelationshipDateUtcID,

                                                            DateTimeUtil2.ToCultureInvariantString(
                                                                oRequestStatistics.StartTimeUtc)
                                                            );
        }
Esempio n. 13
0
        GetUserNetworkRecursive
        (
            String sUserID,
            String sScreenName,
            WhatToInclude eWhatToInclude,
            Boolean bIncludeContactsThisCall,
            NetworkLevel eNetworkLevel,
            Int32 iMaximumPerRequest,
            String sApiKey,
            Int32 iRecursionLevel,
            GraphMLXmlDocument oGraphMLXmlDocument,
            Dictionary <String, XmlNode> oUserIDDictionary,
            RequestStatistics oRequestStatistics
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(sUserID));
            Debug.Assert(!String.IsNullOrEmpty(sScreenName));

            Debug.Assert(eNetworkLevel == NetworkLevel.One ||
                         eNetworkLevel == NetworkLevel.OnePointFive ||
                         eNetworkLevel == NetworkLevel.Two);

            Debug.Assert(iMaximumPerRequest > 0);
            Debug.Assert(!String.IsNullOrEmpty(sApiKey));
            Debug.Assert(iRecursionLevel == 1 || iRecursionLevel == 2);
            Debug.Assert(oGraphMLXmlDocument != null);
            Debug.Assert(oUserIDDictionary != null);
            Debug.Assert(oRequestStatistics != null);
            AssertValid();

            /*
             * Here is what this method should do, based on the eNetworkLevel and
             * iRecursionLevel parameters.
             *
             *      eNetworkLevel
             *
             |One               | OnePointFive      | Two
             *  ---|------------------| ------------------| -----------------
             * i   1  |Add all vertices. | Add all vertices. | Add all vertices.
             * R      |                  |                   |
             * e      |Add all edges.    | Add all edges.    | Add all edges.
             * c      |                  |                   |
             * u      |Do not recurse.   | Recurse.          | Recurse.
             * r      |                  |                   |
             * s   ---|------------------|-------------------|------------------
             * i   2  |Impossible.       | Do not add        | Add all vertices.
             * o      |                  | vertices.         |
             * n      |                  |                   |
             * L      |                  | Add edges only if | Add all edges.
             * e      |                  | vertices are      |
             * v      |                  | already included. |
             * e      |                  |                   |
             * l      |                  | Do not recurse.   | Do not recurse.
             |                  |                   |
             |  ---|------------------|-------------------|------------------
             */

            Boolean bNeedToRecurse = GetNeedToRecurse(eNetworkLevel,
                                                      iRecursionLevel);

            Boolean bNeedToAppendVertices = GetNeedToAppendVertices(eNetworkLevel,
                                                                    iRecursionLevel);

            List <String> oUserIDsToRecurse = new List <String>();

            ReportProgressForContactsOrCommenters(sScreenName,
                                                  bIncludeContactsThisCall);

            Boolean bThisUserAppended = false;

            foreach (XmlNode oChildXmlNode in GetContactsOrCommentersEnumerator(
                         sUserID, bIncludeContactsThisCall, iMaximumPerRequest,
                         oGraphMLXmlDocument, sApiKey, oRequestStatistics))
            {
                String sOtherScreenName, sOtherUserID;

                if (
                    !XmlUtil2.TrySelectSingleNodeAsString(oChildXmlNode,
                                                          bIncludeContactsThisCall ? "@username" : "@authorname",
                                                          null, out sOtherScreenName)
                    ||
                    !XmlUtil2.TrySelectSingleNodeAsString(oChildXmlNode,
                                                          bIncludeContactsThisCall ? "@nsid" : "@author",
                                                          null, out sOtherUserID)
                    )
                {
                    continue;
                }

                if (!bThisUserAppended)
                {
                    // Append a vertex node for this request's user.
                    //
                    // This used to be done after the foreach loop, which avoided
                    // the need for a "bThisUserAppended" flag.  That caused the
                    // following bug: If a YouTube error occurred within
                    // EnumerateXmlNodes() after some edges had been added, and the
                    // user decided to import the resulting partial network, the
                    // GraphML might contain edges that referenced "this user"
                    // without containing a vertex for "this user."  That is an
                    // illegal state for GraphML, which the ExcelTemplate project
                    // caught and reported as an error.

                    TryAppendVertexXmlNode(sUserID, sScreenName,
                                           oGraphMLXmlDocument, oUserIDDictionary);

                    bThisUserAppended = true;
                }

                if (bNeedToAppendVertices)
                {
                    if (
                        TryAppendVertexXmlNode(sOtherUserID, sOtherScreenName,
                                               oGraphMLXmlDocument, oUserIDDictionary)
                        &&
                        bNeedToRecurse
                        )
                    {
                        oUserIDsToRecurse.Add(sOtherUserID);
                    }
                }

                if (bNeedToAppendVertices ||
                    oUserIDDictionary.ContainsKey(sOtherUserID))
                {
                    // Append an edge node and optional attributes.

                    XmlNode oEdgeXmlNode;

                    if (bIncludeContactsThisCall)
                    {
                        oEdgeXmlNode = AppendEdgeXmlNode(oGraphMLXmlDocument,
                                                         sScreenName, sOtherScreenName, "Contact");
                    }
                    else
                    {
                        // (Note the swapping of screen names in the commenter
                        // case.)

                        oEdgeXmlNode = AppendEdgeXmlNode(oGraphMLXmlDocument,
                                                         sOtherScreenName, sScreenName, "Commenter");

                        UInt32 uCommentDateUtc;

                        if (XmlUtil2.TrySelectSingleNodeAsUInt32(oChildXmlNode,
                                                                 "@datecreate", null, out uCommentDateUtc))
                        {
                            DateTime oCommentDateUtc =
                                DateTimeUtil2.UnixTimestampToDateTimeUtc(
                                    uCommentDateUtc);

                            oGraphMLXmlDocument.AppendGraphMLAttributeValue(
                                oEdgeXmlNode, CommentDateUtcID,

                                ExcelDateTimeUtil.DateTimeToStringLocale1033(
                                    oCommentDateUtc, ExcelColumnFormat.DateAndTime)
                                );
                        }

                        AppendStringGraphMLAttributeValue(oChildXmlNode,
                                                          "@permalink", null, oGraphMLXmlDocument, oEdgeXmlNode,
                                                          CommentUrlID);
                    }
                }
            }

            if (bNeedToRecurse)
            {
                foreach (String sUserIDToRecurse in oUserIDsToRecurse)
                {
                    XmlNode oVertexXmlNode = oUserIDDictionary[sUserIDToRecurse];

                    String sScreenNameToRecurse = GetScreenNameFromVertexXmlNode(
                        oVertexXmlNode);

                    GetUserNetworkRecursive(sUserIDToRecurse, sScreenNameToRecurse,
                                            eWhatToInclude, bIncludeContactsThisCall, eNetworkLevel,
                                            iMaximumPerRequest, sApiKey, 2, oGraphMLXmlDocument,
                                            oUserIDDictionary, oRequestStatistics);
                }
            }
        }
Esempio n. 14
0
        TrySaveWorkbookIfNeverSaved
        (
            Microsoft.Office.Interop.Excel.Workbook oWorkbook,
            String sFolderToSaveWorkbookTo
        )
        {
            Debug.Assert(oWorkbook != null);

            // The Workbook.Path is an empty string until the workbook is saved.

            if (String.IsNullOrEmpty(oWorkbook.Path))
            {
                if (String.IsNullOrEmpty(sFolderToSaveWorkbookTo))
                {
                    sFolderToSaveWorkbookTo = Environment.GetFolderPath(
                        Environment.SpecialFolder.MyDocuments);
                }

                String sFileNameNoExtension;

                // Use a suggested file name if available; otherwise use a file
                // name based on the current time.

                if ((new PerWorkbookSettings(oWorkbook)).GraphHistory
                    .TryGetValue(
                        GraphHistoryKeys.ImportSuggestedFileNameNoExtension,
                        out sFileNameNoExtension))
                {
                    if (sFileNameNoExtension.Length >
                        MaximumImportSuggestedFileNameNoExtension)
                    {
                        sFileNameNoExtension = sFileNameNoExtension.Substring(
                            0, MaximumImportSuggestedFileNameNoExtension);
                    }

                    sFileNameNoExtension = FileUtil.ReplaceIllegalFileNameChars(
                        sFileNameNoExtension, " ");
                }
                else
                {
                    sFileNameNoExtension =
                        DateTimeUtil2.ToCultureInvariantFileName(DateTime.Now)
                        + " NodeXL";
                }

                try
                {
                    ExcelUtil.SaveWorkbookAs(oWorkbook,
                                             Path.Combine(sFolderToSaveWorkbookTo,
                                                          sFileNameNoExtension));
                }
                catch (COMException)
                {
                    FormUtil.ShowWarning(
                        "The workbook can't be saved, probably because the folder"
                        + " where the workbook should be saved does not exist.  To"
                        + " fix this, go to NodeXL, Graph, Automate and change the"
                        + " Options for \"Save workbook to a new file if it has"
                        + " never been saved\"."
                        );

                    return(false);
                }
            }

            return(true);
        }
        AddTweetDateRangeToNetworkDescription
        (
            XmlDocument graphMLXmlDocument,
            NetworkDescriber networkDescriber
        )
        {
            Debug.Assert(graphMLXmlDocument != null);
            Debug.Assert(networkDescriber != null);

            XmlNamespaceManager oXmlNamespaceManager = new XmlNamespaceManager(
                graphMLXmlDocument.NameTable);

            oXmlNamespaceManager.AddNamespace("g",
                                              GraphMLXmlDocument.GraphMLNamespaceUri);

            DateTime oMinimumRelationshipDateUtc = DateTime.MaxValue;
            DateTime oMaximumRelationshipDateUtc = DateTime.MinValue;

            // Loop through the graph's edges.

            foreach (XmlNode oEdgeXmlNode in
                     graphMLXmlDocument.DocumentElement.SelectNodes(
                         "g:graph/g:edge", oXmlNamespaceManager))
            {
                // Get the value of the edge's "relationship" GraphML-Attribute.

                String sRelationship;

                if (!TryGetEdgeGraphMLAttributeValue(oEdgeXmlNode,
                                                     NodeXLGraphMLUtil.EdgeRelationshipID, oXmlNamespaceManager,
                                                     out sRelationship))
                {
                    continue;
                }

                switch (sRelationship)
                {
                case TwitterGraphMLUtil.RepliesToRelationship:
                case TwitterGraphMLUtil.MentionsRelationship:
                case TwitterGraphMLUtil.NonRepliesToNonMentionsRelationship:

                    // Get the value of the edge's "relationship date"
                    // GraphML-Attribute.

                    String sRelationshipDateUtc;

                    if (!TryGetEdgeGraphMLAttributeValue(oEdgeXmlNode,
                                                         TwitterGraphMLUtil.EdgeRelationshipDateUtcID,
                                                         oXmlNamespaceManager, out sRelationshipDateUtc))
                    {
                        break;
                    }

                    DateTime oRelationshipDateUtc;

                    try
                    {
                        // Note that the relationship date may be in an
                        // unrecognized format.

                        oRelationshipDateUtc =
                            DateTimeUtil2.FromCultureInvariantString(
                                sRelationshipDateUtc);
                    }
                    catch (FormatException)
                    {
                        break;
                    }

                    if (oRelationshipDateUtc < oMinimumRelationshipDateUtc)
                    {
                        oMinimumRelationshipDateUtc = oRelationshipDateUtc;
                    }

                    if (oRelationshipDateUtc > oMaximumRelationshipDateUtc)
                    {
                        oMaximumRelationshipDateUtc = oRelationshipDateUtc;
                    }

                    break;

                default:

                    break;
                }
            }

            if (oMinimumRelationshipDateUtc != DateTime.MaxValue)
            {
                networkDescriber.AddSentence(
                    "The tweets in the network were tweeted over the {0} period"
                    + " from {1} to {2}."
                    ,
                    networkDescriber.FormatDuration(oMinimumRelationshipDateUtc,
                                                    oMaximumRelationshipDateUtc),

                    networkDescriber.FormatEventTime(oMinimumRelationshipDateUtc),
                    networkDescriber.FormatEventTime(oMaximumRelationshipDateUtc)
                    );
            }
        }