continuous

Open full view…

Messing around with the free postcode lottery website

sirhenrygaylord
Tue, 27 Dec 2016 15:17:09 GMT

Using only the app on my iPhone I wrote a script that gets the latest winning postcode image and decodes it with an online OCR service :) using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using System.Text.RegularExpressions; async Task<string> GetResponseText(Func<HttpClient, Task<HttpResponseMessage>> requestFunc) { using(var client = new HttpClient()) { using (var response = await requestFunc(client)) { using (var content = response.Content) { return await content.ReadAsStringAsync(); } } } } string GetTagAttribute(string html, string tagName, string findAttributeName, string findAttributeValue, string valueAttributeName) { return Regex.Match(html, "<" + tagName + ".*?" + findAttributeName + ".*?=.*?\"" + findAttributeValue + "\".*?" + valueAttributeName + ".*?=.*?\"(.+?)\"").Groups[1].Value; } string GetTagData(string html, string tagName, string findAttributeName, string findAttributeValue) { return Regex.Match(html, "<" + tagName + ".*?" + findAttributeName + ".*?=.*?\"" + findAttributeValue + "\".*?>(.+?)</" + tagName + ".*?>").Groups[1].Value; } async Task<string> GetUrl(string url) { return await GetResponseText(client => client.GetAsync(new Uri(url))); } void OutputDictionary(Dictionary<string, string> d) { foreach (var kp in d) { Console.WriteLine($"{kp.Key} = {kp.Value}"); } } async Task<string> PostForm(string url, Dictionary<string, string> formData) { var formContent = new FormUrlEncodedContent(formData); return await GetResponseText(client => client.PostAsync(new Uri(url), formContent)); } string ReadFormFieldValue(string html, string fieldName) { return Regex.Match(html, @"name.*?\=""" + fieldName + @"""[ ]value\=""(?<value>[^""]*)").Groups["value"].Value; } /* Go */ Console.WriteLine(""); Console.WriteLine("* Getting todays postcode image details ..."); var webPageHtml = await PostForm("http://freepostcodelottery.com/", new Dictionary<string, string>() { { "register-ticket", "*** YOUR POSTCODE ***" }, { "register-email", "*** YOUR EMAIL ADDRESS ***" }, { "login", "" } }); var imageUrl = $"http://freepostcodelottery.com{GetTagAttribute(webPageHtml, "img", "alt", "The current winning postcode", "src")}"; /*var nextDraw = GetTagAttribute(webPageHtml, "abbr", "class", "timeago", "title");*/ Console.WriteLine(""); Console.WriteLine($"Postcode image = {imageUrl}"); /*Console.WriteLine($"Next draw = {nextDraw}");*/ Console.WriteLine(""); Console.WriteLine("* Decoding postcode image ..."); webPageHtml = await PostForm("http://www.newocr.com/", new Dictionary<string, string>() { { "url", imageUrl }, { "preview", "1" } }); var convertParams = new Dictionary<string, string>(); convertParams["u"] = ReadFormFieldValue(webPageHtml, "u"); convertParams["x1"] = ReadFormFieldValue(webPageHtml, "x1"); convertParams["x2"] = ReadFormFieldValue(webPageHtml, "x2"); convertParams["y1"] = ReadFormFieldValue(webPageHtml, "y1"); convertParams["y2"] = ReadFormFieldValue(webPageHtml, "y2"); convertParams["r"] = "0"; convertParams["psm"] = "6"; convertParams["l3"] = ""; convertParams["l2[]"] = "eng"; convertParams["ocr"] = "1"; Console.WriteLine(""); OutputDictionary(convertParams); webPageHtml = await PostForm("http://www.newocr.com/", convertParams); /*var postcode = Regex.Match(webPageHtml, "<textarea id=\"ocr-result\".*?>(.+?)</textarea>").Groups[1].Value;*/ var postcode = GetTagData(webPageHtml, "textarea", "id", "ocr-result"); Console.WriteLine(""); Console.WriteLine($">>> Postcode = {postcode} <<<");

sirhenrygaylord
Tue, 27 Dec 2016 15:18:25 GMT

Hmmm some characters have gone walkabout in the listing

sirhenrygaylord
Tue, 27 Dec 2016 15:19:46 GMT

If anyone is interested I can send the .csx script file ... I wouldn't normally have posted anything but I was surprised the app could do it