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 ClixSenseStrategy : Strategy
{
// API Key
// edc5049bd035dd7f2ac78691a8ff1253
// API Secret
// 73963b0186372806ca3d5829fa2a2514
// API Status
// Active
public override bool CheckLogin(WebBrowserControl browser, PTCSource source)
{
string body = browser.Html;
return StringUtils.Contains(body, "You will not be compensated for clicking on these ads unless you are signed in.");
}
public override void Login(WebBrowserControl browser, PTCSource source)
{
IHTMLElementCollection inputFields = HtmlUtils.GetElementCollection(browser.Document, "input");
foreach (IHTMLInputElement element in inputFields)
{
if (element.name == "username")
{
element.value = source.Username;
}
else if (element.name == "password")
{
element.value = source.Password;
}
else if (element.type == "submit" && element.value == "Login")
{
((IHTMLElement)element).click();
return;
}
}
}
public override decimal GetBalance(WebBrowserControl browser, PTCSource source)
{
throw new NotImplementedException();
}
public override bool SolveCaptcha(WebBrowserControl browser, PTCSource source)
{
//
IHTMLElement div = HtmlUtils.GetElement(browser.Document, "cpics");
IHTMLElementCollection elements = ((IHTMLElementCollection)div.all).tags("img");
Bitmap[] images = new Bitmap[5];
int i = 0;
foreach (IHTMLElement element in elements)
{
IHTMLImgElement image = (IHTMLImgElement)element;
images[i] = ImageUtils.GetImage(image);
if (images[i] == null)
{
return false;
}
i++;
}
DirectoryInfo catsDirectory = new DirectoryInfo(string.Format(@"..\..\Images\Captcha\PetFinder\Cats"));
DirectoryInfo dogsDirectory = new DirectoryInfo(string.Format(@"..\..\Images\Captcha\PetFinder\Dogs"));
int cats = 0;
int dogs = 0;
CatsCaptchaConfirm.MatchType[] matches = new CatsCaptchaConfirm.MatchType[5];
i = 0;
foreach (Bitmap bitmap in images)
{
string tempImage = Path.GetTempFileName();
bitmap.Save(tempImage);
Bitmap left = new Bitmap(tempImage);
FileInfo[] files = null;
bool found = false;
#region Cats
files = catsDirectory.GetFiles();
found = false;
// Compare with cats
foreach (FileInfo file in files)
{
Bitmap right = new Bitmap(file.FullName);
Image reference = Bitmap.FromFile(file.FullName);
if (ImageUtils.CompareImages(left, right, 1, 1, left.Width, left.Height, 10))
{
matches[i] = CatsCaptchaConfirm.MatchType.Cat;
found = true;
cats++;
break;
}
right.Dispose();
}
#endregion
//if (found)
//{
// break;
//}
#region Dogs
files = dogsDirectory.GetFiles();
found = false;
foreach (FileInfo file in files)
{
Bitmap right = new Bitmap(file.FullName);
Image reference = Bitmap.FromFile(file.FullName);
if (ImageUtils.CompareImages(left, right, 1, 1, left.Width, left.Height, 10))
{
matches[i] = CatsCaptchaConfirm.MatchType.Dog;
found = true;
dogs++;
break;
}
right.Dispose();
}
#endregion
left.Dispose();
if (!found)
{
matches[i] = CatsCaptchaConfirm.MatchType.None;
}
i++;
}
CatsCaptchaConfirm dialog = new CatsCaptchaConfirm(false);
//UpsideDownCaptchaConfirm dialog = new UpsideDownCaptchaConfirm();
dialog.Image1 = (Bitmap)images[0];
dialog.Image2 = (Bitmap)images[1];
dialog.Image3 = (Bitmap)images[2];
dialog.Image4 = (Bitmap)images[3];
dialog.Image5 = (Bitmap)images[4];
dialog.Match1 = matches[0];
dialog.Match2 = matches[1];
dialog.Match3 = matches[2];
dialog.Match4 = matches[3];
dialog.Match5 = matches[4];
if (dialog.ShowDialog() == true)
{
//
i = 0;
foreach (IHTMLElement element in elements)
{
if (i == dialog.Match)
{
element.click();
return true;
}
i++;
}
}
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();
}
}
}