Skip to content

Commit

Permalink
Upload commit
Browse files Browse the repository at this point in the history
Uploading the whole working project
  • Loading branch information
ThomasMiz authored Mar 6, 2019
1 parent 608b41c commit 259a743
Show file tree
Hide file tree
Showing 93 changed files with 93,933 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ImageCreator/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
23 changes: 23 additions & 0 deletions ImageCreator/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace ImageCreator
{
/// <summary>
/// Contains all the program constants such as API Keys, tokens and secrets.
/// </summary>
static class Constants
{
//mustache man :{

internal const String WORDNIK_API_KEY = "";

internal static String TWITTER_CUSTOMER_KEY = "";
internal static String TWITTER_CUSTOMER_KEY_SECRET = "";

internal static String TWITTER_ACCESS_TOKEN = "";
internal static String TWITTER_ACCESS_TOKEN_SECRET = "";

internal static String CSE_API_KEY = "";
internal static String CSE_CX = "";
}
}
135 changes: 135 additions & 0 deletions ImageCreator/CseManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using Google.Apis.Customsearch.v1;
using Google.Apis.Customsearch.v1.Data;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.Net;

namespace ImageCreator
{
class CseManager : IDisposable
{
private CustomsearchService service;

public CseManager()
{
service = new CustomsearchService(new BaseClientService.Initializer() { ApiKey = Constants.CSE_API_KEY });
#if LOG_DATA
DataLogger.Log("[CseManager] Initiated CustomsearchService", LoggingMode.Success);
#endif
}

/// <summary>
/// Tries to search for images in google of a specified query and returns the first 10. Returns whether successfull
/// </summary>
/// <param name="query"></param>
/// <param name="tries"></param>
/// <returns></returns>
public bool GetImagesFor(String query, int tries, out List<ImageData> result)
{
#if LOG_DATA
DataLogger.Log(String.Concat("[CseManager] Getting images for query \"", query, "\""), LoggingMode.Message);
#endif
for (int t = 0; t < tries; t++)
{
try
{
#if LOG_DATA
DataLogger.Log(String.Concat("[CseManager] Attempting to get images (", t, ")"), LoggingMode.Message);
#endif
CseResource.ListRequest req = service.Cse.List(query);
req.Cx = Constants.CSE_CX;
req.SearchType = CseResource.ListRequest.SearchTypeEnum.Image;
req.FileType = "png, jpg, jpeg, bmp";
req.Rights = "cc_publicdomain, cc_attribute, cc_sharealike, cc_noncommercial";
req.Safe = CseResource.ListRequest.SafeEnum.Active;
Search s = req.Execute();

#if LOG_DATA
DataLogger.Log("[CseManager] Recieved images. Processing them into data list", LoggingMode.Message);
#endif
result = new List<ImageData>(10);
for (int i = 0; i < s.Items.Count; i++)
{
Result r = s.Items[i];
if (r.Image.Width.HasValue && r.Image.Height.HasValue && EndsWithProperImageFormat(r.Link))
result.Add(new ImageData(r));
}
#if LOG_DATA
DataLogger.Log(String.Concat("[CseManager] Done processing images into data list. The ", result.Count, " results are:"), LoggingMode.Success);
for (int i = 0; i < result.Count; i++)
DataLogger.Log(String.Concat("Width=", result[i].Width, " Height=", result[i].Height, " Extension: ", result[i].Extension, " Link: \"", result[i].Link, "\""), LoggingMode.RawData);
DataLogger.Log("[CseManager] [END OF IMAGE DATA LIST]", LoggingMode.Message);
#endif
return true;
}
catch
{

}
}

result = null;
return false;
}

private static bool EndsWithProperImageFormat(String link)
{
return link.EndsWith(".png") || link.EndsWith(".jpg") || link.EndsWith(".jpeg") || link.EndsWith(".bmp");
}

public void Dispose()
{
service.Dispose();
}
}

class ImageData
{
private int _width, _height;
private String _link, _displayLink, _ext;

public int Width { get { return _width; } }
public int Height { get { return _height; } }

public String Extension { get { return _ext; } }
public String DisplayLink { get { return _displayLink; } }
public String Link { get { return _link; } }

public ImageData(Result r)
{
_width = r.Image.Width.Value;
_height = r.Image.Height.Value;

_link = r.Link;
_displayLink = r.DisplayLink;

_ext = _link.Substring(_link.LastIndexOf('.'));
}

/// <summary>
/// Tries to download an image and store it in the given filename a specified amount of times. Returns whether successfull
/// </summary>
/// <param name="fileName"></param>
/// <param name="tries"></param>
public bool TryDownloadAs(String fileName, int tries)
{
for (int t = 0; t < tries; t++)
{
try
{
using (WebClient client = new WebClient())
{
client.DownloadFile(_link, fileName);
return true;
}
}
catch
{

}
}
return false;
}
}
}
100 changes: 100 additions & 0 deletions ImageCreator/DataLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#if LOG_DATA
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageCreator
{
class DataLogger
{
const string LOG_FOLDER = "ImageCreator_logs";

private static String[] preStrings;
private static String file;
private static StreamWriter stream;
private static FileStream fileStream;

public static void Init()
{
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

preStrings = new string[5];
preStrings[(int)LoggingMode.Success] = "[OK]";
preStrings[(int)LoggingMode.Message] = "[MSG]";
preStrings[(int)LoggingMode.RawData] = "[DATA]";
preStrings[(int)LoggingMode.Warning] = "[WARNING]";
preStrings[(int)LoggingMode.Error] = "[ERROR]";

DateTime now = DateTime.Now;
if (!Directory.Exists(LOG_FOLDER))
Directory.CreateDirectory(LOG_FOLDER);
file = String.Concat(LOG_FOLDER, "/", now.Day, "-", now.Month, "-", now.Year, " ", now.Hour, ".", now.Minute, ".", now.Second, ".log");
while (File.Exists(file))
file = file.Insert(file.Length - 4, "0");
fileStream = File.Create(file);
stream = new StreamWriter(fileStream, Encoding.Default);//new FileStream(file, FileMode.Open, FileAccess.Write, FileShare.Read);
}

public static void End()
{
stream.Flush();
fileStream.Flush();
stream.Dispose();
fileStream.Dispose();
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log("[DataLogger] An exception has passed unhandled. Program is likely terminating", LoggingMode.Error);
}

private static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
Log("[DataLogger] Exception happned. Logging data: ", LoggingMode.Warning);
Log("Message: " + e.Exception.Message, LoggingMode.RawData);
Log("HResult: " + e.Exception.HResult, LoggingMode.RawData);
Log("Source: " + e.Exception.Source, LoggingMode.RawData);
Log("StackTrace: " + e.Exception.StackTrace, LoggingMode.RawData);
Log("[DataLogger] Note that while this exception did happen, this message does NOT mean it wasn't handled.", LoggingMode.Warning);
Log("[DataLogger] [END OF EXCEPTION MESSAGE]", LoggingMode.Warning);
}

public static void Log(String log, LoggingMode mode)
{
/*switch (mode)
{
case LoggingMode.Success:
Console.ForegroundColor = ConsoleColor.Green;
break;
case LoggingMode.Message:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LoggingMode.RawData:
Console.ForegroundColor = ConsoleColor.White;
break;
case LoggingMode.Error:
case LoggingMode.Warning:
Console.ForegroundColor = ConsoleColor.Red;
break;
}*/

DateTime now = DateTime.Now;
String s = String.Concat(preStrings[(int)mode], " [", now.Day, "-", now.Month, "-", now.Year, "] [", now.Hour, ":", now.Minute, ":", now.Second, "] ", log);
stream.WriteLine(s);
//Console.WriteLine(s);
}
}

enum LoggingMode
{
Success = 0, Message = 1, RawData = 2, Warning = 3, Error = 4
}
}
#endif
Binary file added ImageCreator/Hammock.ClientProfile.dll
Binary file not shown.
Loading

0 comments on commit 259a743

Please sign in to comment.