I've been making a bot for Reddit that finds reposts (based on image MD5 comparing) and posts a small comment on the thread (that part will be later, right now it downloads images and compares MD5). If the image is original content, it will add it to its list of hashes. The source code is here. Any suggestions as to what I can improve?
public static bool DownloadImage(string url, bool isDirect) // returns if repost
{ // example url: http://imgur.com/UndN77d
if (url.Contains('?'))
{
url = url.Split('?')[0]; // if it has a query at the end like ?1
}
string filename = null;
if (url.Contains("imgur"))
{ // is imgur, so use ImgurAPI class for it
string imgurID = url.Split('/')[url.Split('/').Length - 1];
Console.WriteLine("Processing imgur id {0}", imgurID);
if (!isDirect)
{ // the sole goal for this block is to get Vars.picToCompare
string details = Imgur.GetImageDetails(imgurID);
if (details == null)
{
return false;
}
Console.WriteLine("Got response: {0}", details); // json
JObject imgur = JObject.Parse(details);
if (imgur["success"].ToString() == "false")
{ // auth failed
Console.WriteLine("Imgur processing failed.. Printing JSON contents: ");
Console.WriteLine(details);
Console.ReadKey();
return false;
}
else
{
Console.WriteLine("Imgur details recieved successfully!");
filename = imgur["data"]["link"].ToString();
url = filename;
Console.WriteLine("Processed direct URL: " + filename);
filename = filename.Split('/')[filename.Split('/').Length - 1];
Console.WriteLine("Processed image filename: " + filename);
Vars.picToCompare = Path.Combine(Vars.picsFolder, filename);
}
}
else
{
Vars.picToCompare = Path.Combine(Vars.picsFolder, url.Split('/')[url.Split('/').Length - 1]); // should be filename
}
}
else
{ // for example mysite.com/blah.jpg
Vars.picToCompare = Path.Combine(Vars.picsFolder, url.Split('/')[url.Split('/').Length - 1]);
Console.WriteLine("Processing image url {0}", url);
Console.WriteLine("Processed image {0}", Vars.picToCompare);
}
bool isRepost = false;
using (WebClient client = new WebClient())
{
if (File.Exists(Vars.picToCompare))
File.Delete(Vars.picToCompare);
client.DownloadFile(url, Vars.picToCompare);
Console.WriteLine("Successfully downloaded image {0}", Vars.picToCompare);
}
isRepost = Processing.CompareHash(url);
if (isRepost)
Console.WriteLine("This is a repost!");
else
Console.WriteLine("This is OC. Adding to hashlist..");
//foreach (string hash in Vars.totalHashes)
//{
// Console.WriteLine("Written hash to file: " + hash);
//}
return isRepost;
}