using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; namespace sharpknife { class AdLogItem { static string LINK_START = "href=\""; static string LINK_END = "\""; static string TIMER_START = "onclick=javascript:reloadpage("; static string TIMER_END = ")"; static string VALUE_START = "The ad above is worth "; static string VALUE_END = ""; public enum ValueType { Cents, Points } public ValueType Type { get; set; } public decimal Value { get; set; } public int Timer { get; set; } public string Link { get; set; } public string Body { get; set; } public AdLogItem(DateTime timestamp, ValueType type, decimal value, int timer, string link, string body) { Type = type; Value = value; Timer = timer; Link = link; Body = body; } public AdLogItem(DateTime timestamp, string html) { Type = html.IndexOf("point(s)", StringComparison.InvariantCultureIgnoreCase) >= 0 ? ValueType.Points : ValueType.Cents; int valueStart = html.IndexOf(VALUE_START, StringComparison.InvariantCultureIgnoreCase) + VALUE_START.Length; int valueEnd = html.IndexOf(VALUE_END, valueStart, StringComparison.InvariantCultureIgnoreCase); Value = decimal.Parse(html.Substring(valueStart, valueEnd - valueStart), CultureInfo.InvariantCulture.NumberFormat); // .Replace(".", ",")); int timerStart = html.IndexOf(TIMER_START, StringComparison.InvariantCultureIgnoreCase) + TIMER_START.Length; int timerEnd = html.IndexOf(TIMER_END, timerStart, StringComparison.InvariantCultureIgnoreCase); int timer = 0; int.TryParse(html.Substring(timerStart, timerEnd - timerStart), out timer); Timer = timer; if (html.IndexOf(LINK_START, StringComparison.InvariantCultureIgnoreCase) >= 0) { int linkStart = html.IndexOf(LINK_START, StringComparison.InvariantCultureIgnoreCase) + LINK_START.Length; int linkEnd = html.IndexOf(LINK_END, linkStart, StringComparison.InvariantCultureIgnoreCase); Link = html.Substring(linkStart, linkEnd - linkStart); if (Link.Contains('&')) { Link = Link.Substring(0, Link.IndexOf('&')); } } else { // TODO: throw malformedadexception throw new Exception("Malformed ad"); } Body = html; } public override string ToString() { return string.Format("Value: {0} {1}\nTimer: {2} seconds\nLink: {3}\nBody: {4}", Value, Type, Timer, Link, Body); } } }