예제 #1
0
파일: Program.cs 프로젝트: mxamber/DMPeek
        static void item_load_Click(object sender, EventArgs e)
        {
            MenuItem mItem  = sender as MenuItem;
            Form     form   = mItem.GetMainMenu().GetForm();
            Panel    cpanel = (Panel)form.Controls["cpanel"];

            if (cpanel == null)
            {
                return;
            }

            OpenFileDialog dia = new OpenFileDialog();

            dia.Title  = "Select JSON File";
            dia.Filter = "JSON files|*.json";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                Stream       fstream = dia.OpenFile();
                StreamReader sreader = new StreamReader(fstream);
                String       content = sreader.ReadToEnd();
                sreader.Close();

                DmConversation convo = Twitter.ParseConversation(content);
                if (convo == null)
                {
                    MessageBox.Show("The file you chose doesn't contain any conversation data.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                cpanel.BackColor = Color.LightGray;
                Console.Clear();

                while (cpanel.Controls.Count > 0)
                {
                    cpanel.Controls.Remove(cpanel.Controls[0]);
                }

                foreach (Message msg in convo.messages)
                {
                    MColor color;
                    if (msg.sender.id == convo.userTwo.id)
                    {
                        color = MColor.Blue;
                    }
                    else
                    {
                        color = MColor.Grey;
                    }

                    Console.WriteLine("[{0}] {1}", msg.date.ToShortTimeString(), msg.text);
                    Graphing.AddMessage(msg.text, msg.date, cpanel, color, true);
                }

                cpanel.BackColor = Color.White;
            }
        }
예제 #2
0
        public static DmConversation ParseConversation(String input)
        {
            // is there even anything in { } brackets?
            // if no json -> return empty convo
            Regex json_regex = new Regex("(?'json'{.*})", RegexOptions.Singleline);
            Match json_match = json_regex.Match(input);

            if (!json_match.Groups["json"].Success)
            {
                return(null);
            }

            // check for conversation id
            // if no conversation id -> return empty convo
            String json     = json_match.Groups["json"].Value;
            Regex  id_regex = new Regex("\"conversationId\"\\s*:\\s*\"(?'one'\\d*)-(?'two'\\d*)\"");
            Match  id_match = id_regex.Match(json);

            if (!id_match.Success || !id_match.Groups["one"].Success || !id_match.Groups["two"].Success)
            {
                return(null);
            }

            long id_one = Int64.Parse(id_match.Groups["one"].Value);
            long id_two = Int64.Parse(id_match.Groups["two"].Value);

            // find msg array
            Regex msg_ar_regex = new Regex("\"messages\"\\s*:\\s*\\[\\s*{.*}\\s*\\]", RegexOptions.Singleline);
            Match msg_ar_match = msg_ar_regex.Match(json);

            if (!msg_ar_match.Success)
            {
                return(null);
            }


            // {\s*"messageCreate"[^}]*"recipientId"\s*:\s*"(?'recip'\d*)"[^}]*"text"\s*:\s*"(?'text'[^"]*)"[^}]*"senderId"\s*:\s*"(?'sender'\d*)"[^}]*"id"\s*:\s*"(?'id'\d*)"[^}]*"createdAt"\s*:\s*"(?'date'[^"]*)"[^}]*}
            // check for entire messageCreate objects, gather them in a match collection, return null if none are found
            Regex           msg_regex   = new Regex("{\\s*\"messageCreate\"[^}]*\"recipientId\"\\s*:\\s*\"(?'recip'\\d*)\"[^}]*\"text\"\\s*:\\s*\"(?'text'[^\"]*)\"[^}]*\"senderId\"\\s*:\\s*\"(?'sender'\\d*)\"[^}]*\"id\"\\s*:\\s*\"(?'id'\\d*)\"[^}]*\"createdAt\"\\s*:\\s*\"(?'date'[^\"]*)\"[^}]*}", RegexOptions.Singleline);
            MatchCollection msg_matches = msg_regex.Matches(msg_ar_match.Value);

            if (msg_matches.Count == 0)
            {
                return(null);
            }

            DmConversation convo = new DmConversation(new User(id_one), new User(id_two));


            for (int i = msg_matches.Count - 1; i > -1; i--)
            {
                Match    current   = msg_matches[i];
                String   text      = current.Groups["text"].Value;
                long     sender    = Int64.Parse(current.Groups["sender"].Value);
                long     recipient = Int64.Parse(current.Groups["recip"].Value);
                long     id        = Int64.Parse(current.Groups["id"].Value);
                DateTime sent      = new DateTime(1970, 01, 01, 12, 00, 00);
                DateTime.TryParse(current.Groups["date"].Value, out sent);
                Message msg = new Message(text, id, new User(sender), new User(recipient), sent);
                convo.messages.Add(msg);
            }


            return(convo);
        }