Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Journal Comment Eater

Journal Comment Eater

Daerax
Journal · · 3 min read
2,379 13
I found some free time hiding beneath my bed so I spent some 3 and a half hours making a little application which sits in your taskbar and tells you if you got new comments in your game dev journal. It was fun, Linq is my favourite part of the otherwise mediocre C#. It does this by html scraping the respective page. Every 20 minutes it checks if there have been new comments since the last check. If there has it tries to find out which posts got the comments and pops out a ballon telling you which. If there have been new entries since the last check it only checks the old entries. All this was done quite easily and not many lines of code thanks to the html agility pack and Xlinq.

Every 20 mins it checks if there was a new post at a url which is pasted in a textbox. If the textbox is empty it checks for a file located in the same directory as it for the url, if not it uses Gaiidens journal as the default.

    if (textBoxUrl.Text == "")            {                if (File.Exists(infFilePath))                {                    var urlFromFile = System.IO.File.ReadAllText(infFilePath); textBoxUrl.Text = urlFromFile;                    return (urlFromFile == "") ? "https://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=251283" : urlFromFile;                }                else                {                    textBoxUrl.Text = "https://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=251283";                    return textBoxUrl.Text ;}            }            else                return textBoxUrl.Text;


It then checks if there have been any new comments since the last check.

var currentCheck = (currentCheckInit.Count > checkCompare.Count) && checkCompare.Count>0  ?                                     currentCheckInit.Skip(currentCheckInit.Count - checkCompare.Count).ToList()                                     : currentCheckInit;            var sb = new StringBuilder();            if (currentCheck.Sum(p => p.CommentCount) != checkCompare.Sum(p => p.CommentCount))            {                for (int i = 0; i < checkCompare.Count; i++)                {                    if (currentCheck.IsComment && currentCheck.CommentCount != checkCompare.CommentCount)                    {                        var msg = "At " + DateTime.Now.ToShortTimeString() + " Found "                             + (currentCheck.CommentCount - checkCompare.CommentCount)                             + " New Comment(s) in thread, " + checkCompare[i - 1].Title;                        listBoxLog.Items.Add(msg);                        sb.AppendLine(msg);                        newComment = true;                        notifyIcon1.Text = "New Comment(s) found";                    }                }            }


You can also save the last query results to disk. Then later on you may download from the site and check against disk so the program doesnt have to run continuosly to be useful.

     private void buttonSaveState_Click(object sender, EventArgs e)        {            var sb = new StringBuilder();            oldCheck.ForEach(entry => sb.AppendLine (entry.CommentCount + "|"+entry.IsComment + "|" +entry.Title));            File.WriteAllText(statePath, sb.ToString());                    }        private void buttonCompareStates_Click(object sender, EventArgs e)        {            var urlFromFile = System.IO.File.ReadAllText(infFilePath);            if ((urlFromFile != textBoxUrl.Text)  && textBoxUrl.Text != "")                MessageBox.Show("Warning Url found in file and in textbox do not match. This *may* cause discrapncies.",                                  "Are you Sure you know what you are doing?",                                  MessageBoxButtons.OK, MessageBoxIcon.Warning);            var tmpCheck = new List();            var dat = File.ReadAllLines(statePath).ToList ();            dat.ForEach(item => {   var s = item.Split('|');                                    var newE = new JournalEntry();                                    newE.CommentCount = int.Parse(s[0]);                                    newE.IsComment = bool.Parse(s[1]);                                    newE.Title = s[2];                                                    tmpCheck.Add(newE); });            var currentCheck = util.PollGDNEt(CheckUrlOptions());            inited = true;            DoComparison(currentCheck, tmpCheck);        }


The part that scrapes the page is here

public static List PollGDNEt(string url)        {            HtmlWeb page = new HtmlWeb();                                    HtmlDocument doc = page.Load(url);                        var xdoc = doc.ToXDocument();            var queryResults = from element in xdoc.Descendants()                    where element.HasAttributes                      && element.Name.LocalName == "span"                    && (element.FirstAttribute.Value == "regularfont" || (element.FirstAttribute.Value == "smallfont" && element.Value.Contains("Comments")))                    select new {Title = element.Value,                                 IsComment = element.FirstAttribute.Value == "smallfont",                                 Count = element.FirstAttribute.Value == "smallfont" ?                                        int.Parse( Regex.Match( element.Value, @"\d+").Value  ) : 0  };           var Entries = new List();           foreach (var result in queryResults)           {               var entry = new JournalEntry ();                entry.CommentCount = result.Count; entry.IsComment = result.IsComment ; entry.Title = result.Title ;               Entries.Add(entry);           }           return Entries;        }


Full Source is here (svn) or source rar'd. And app is here (requires .NET 3.5). Some stats in order of # comments found on page:
User       |  Total comments on Page   |   Avg Comments Per Post  |   Total Posts-------------------------------------------------------------------------------------------------------TrapperZoid      20                                       4                                 5Drew             19                                       1.27                             15Talestyn         16                                       1.07                             15Ben              16                                       2                                 8Ravuya           16                                       1.14                            14Me               6                                        1.2                              5


Also Mike P if I you read this and I could get the source for your line counter project, would be cool.

Discussion

Loading comments...