using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if WIN32
using MSHTML;
#else
using mshtml;
#endif
using sharpknife.Utils;
using System.Globalization;
using sharpknife.Controls;
using System.Drawing;
using System.IO;
using sharpknife.Data;
namespace sharpknife.Strategies
{
class JillsClickCornerStrategy : Strategy
{
private string CAPTCHA = "Please select the security code from the dropdown box that matches the one below";
public override bool CheckLogin(WebBrowserControl browser, PTCSource source)
{
//Hello Guest, please login or register.
return StringUtils.Contains(browser.Html, "Hello Guest");
}
public override void Login(WebBrowserControl browser, PTCSource source)
{
throw new NotImplementedException();
}
public override decimal GetBalance(WebBrowserControl browser, PTCSource source)
{
//
Your account balance after all transactions:
IHTMLElementCollection elements = HtmlUtils.GetElementCollection(browser.Document, "td");
int count = elements.length;
foreach (IHTMLElement element in elements)
{
if (!string.IsNullOrEmpty(element.innerText) &&
element.innerText.IndexOf("Cash Balance", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
IHTMLElementCollection innerTables = ((IHTMLElementCollection)element.all).tags("table");
if (innerTables.length == 0)
{
IHTMLTable table = (IHTMLTable)element.parentElement.parentElement.parentElement;
int rowIndex = 0;
int columnIndex = 2;
if (((IHTMLElement)table).innerHTML.IndexOf("Current Earnings", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
rowIndex++;
}
IHTMLTableRow row = (IHTMLTableRow)browser.GetElementAt(table.rows, rowIndex);
IHTMLTableCell cell = (IHTMLTableCell)browser.GetElementAt(row.cells, columnIndex);
string text = ((IHTMLElement)cell).innerText;
string value = ((IHTMLElement)cell).innerText.Trim();
return decimal.Parse(value, CultureInfo.InvariantCulture.NumberFormat);
}
}
}
throw new Exception();
}
public override bool SolveCaptcha(WebBrowserControl browser, PTCSource source)
{
//string html = HtmlUtils.GetFrameContent(browser.Document);
string html = string.Empty;
FramesCollection frames = browser.Document.frames;
IHTMLWindow2 timerFrame = null;
for (int i = 0; i < frames.length; i++)
{
object refIndex = i;
IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref refIndex);
try
{
string name = frame.name;
}
catch (Exception ex)
{
LogItem item = new LogItem(ex.ToString());
item.Save();
return false;
}
if (frame.name == "timer")
{
timerFrame = frame;
if (frame.document != null && frame.document.body != null)
{
html = frame.document.body.innerHTML;
}
break;
}
}
if (StringUtils.Contains(html, CAPTCHA))
{
//
IHTMLElementCollection elements = HtmlUtils.GetElementCollection(timerFrame.document, "img");
string imageUrl = string.Empty;
Bitmap bitmap = null;
foreach (IHTMLElement element in elements)
{
IHTMLImgElement image = (IHTMLImgElement)element;
if (StringUtils.Contains(image.src, "www.jillsclickcorner.com/security/image.php?"))
{
imageUrl = image.src;
bitmap = ImageUtils.GetImage(image);
break;
}
}
string captcha = ImageUtils.ParseStringCaptcha(bitmap);
StringCaptchaConfirm dialog = new StringCaptchaConfirm()
{
Captcha = captcha,
CaptchaImage = bitmap
};
int i = 0;
//
IHTMLElementCollection options = HtmlUtils.GetElementCollection(timerFrame.document, "option");
foreach (IHTMLElement element in options)
{
dialog.Options[i] = element.innerText;
i++;
}
bool dialogResult = dialog.ShowDialog().Value;
if (!dialog.IsMatch)
{
string path = string.Format(@"..\..\Images\Captcha\Strings");
bitmap.Save(Path.Combine(path, string.Format("{0}.png", Guid.NewGuid())));
}
bitmap.Dispose();
if (dialogResult == true)
{
//
foreach (IHTMLElement element in options)
{
if (element.innerText == dialog.Captcha)
{
IHTMLOptionElement option = (IHTMLOptionElement)element;
option.selected = true;
option.form.submit();
return true;
}
}
}
}
else
{
return true;
}
return false;
}
public override Ad ParseMail(Mail mail)
{
throw new NotImplementedException();
}
public override bool ParseAds(WebBrowserControl browser, PTCSource source)
{
throw new NotImplementedException();
}
public override decimal GetReferrals(WebBrowserControl browser, PTCSource source)
{
throw new NotImplementedException();
}
}
}