/*
Компиляция:
- Идём в C:\WINDOWS\Microsoft.NET\Framework, выбираем версию фреймфорка
выше или равную v3.5. Смотрим, чтобы в папке был csc.exe.
- Открываем консоль, пишем SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\версия
- Идём в папку с исходником.
- Говорим csc имяисходника.cs
- Говорим имяисходника.exe, дальше понятно.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
static class FaceAppCLI
{
public static void Main(string[] args)
{
if (args.Length != 3)
{
System.Console.WriteLine("Wrong args. Usage: <input file> <filter> <output file>");
System.Console.WriteLine("Filters: smile, smile_2, hot, old, young, female, male");
return;
}
string inputFile = args[0];
string filter = args[1];
string outputFile = args[2];
FaceAppClient client = new FaceAppClient();
try
{
string code = client.UploadImage(inputFile);
Image img = client.GetImage(code, filter);
img.Save(outputFile);
}
catch (WebException)
{
System.Console.WriteLine("Something went wrong!");
}
}
}
class FaceAppClient
{
private const string FA_BASE_URL = "https://n...content-available-to-author-only...p.io/api/v2.3/photos";
private const string FA_FILTER = "/{0}/filters/{1}?cropped={2}";
private const string FA_PREIMAGE = "--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
private const string FA_POSTIMAGE = "--{0}--";
private const string UA = "FaceApp/1.0.229 (Linux; Android 4.4)";
private string awselbCookie = string.Empty;
private string xFaceAppDeviceId = string.Empty;
private string guid = string.Empty;
public FaceAppClient()
{
Random random = new Random();
for (int i = 0; i < 4; i++)
{
this.xFaceAppDeviceId += Convert.ToString(random.Next(4096, 65535), 16);
}
this.guid = Guid.NewGuid().ToString();
}
public string UploadImage(string fileName)
{
string result;
try
{
List<byte> list = new List<byte>();
list.AddRange(Encoding.ASCII.GetBytes(string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n", this.guid)));
byte[] imageBytes = File.ReadAllBytes(fileName);
list.AddRange(imageBytes);
list.AddRange(Encoding.ASCII.GetBytes(string.Format("--{0}--", this.guid)));
imageBytes = list.ToArray();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://n...content-available-to-author-only...p.io/api/v2.3/photos");
httpWebRequest.Method = "POST";
httpWebRequest.UserAgent = "FaceApp/1.0.229 (Linux; Android 4.4)";
httpWebRequest.ContentType = string.Format("multipart/form-data; boundary={0}", this.guid);
httpWebRequest.Headers.Add("X-FaceApp-DeviceID", this.xFaceAppDeviceId);
httpWebRequest.KeepAlive = true;
httpWebRequest.SendChunked = true;
httpWebRequest.Timeout = 25000;
BinaryWriter binaryWriter = new BinaryWriter(httpWebRequest.GetRequestStream());
binaryWriter.Write(imageBytes);
binaryWriter.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string responseHeader = httpWebResponse.GetResponseHeader("Set-Cookie");
long contentLength = httpWebResponse.ContentLength;
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string text = streamReader.ReadToEnd();
if (contentLength != 29L || !text.Contains("code") || !responseHeader.StartsWith("AWSELB"))
{
throw new Exception("UploadImage() got the following answer from the server: " + text);
}
if (this.awselbCookie == string.Empty)
{
this.awselbCookie = responseHeader.Substring(7, responseHeader.IndexOf(';') - 7);
}
result = text.Substring(9, 18);
}
catch (Exception)
{
result = string.Empty;
}
return result;
}
public Image GetImage(string code, string filter)
{
string requestUriString = "https://n...content-available-to-author-only...p.io/api/v2.3/photos" + string.Format("/{0}/filters/{1}?cropped={2}", code, filter, (filter == "male" || filter == "female") ? "1" : "0");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
httpWebRequest.Headers.Add("X-FaceApp-DeviceID", this.xFaceAppDeviceId);
httpWebRequest.UserAgent = "FaceApp/1.0.229 (Linux; Android 4.4)";
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
return new Bitmap(httpWebResponse.GetResponseStream());
}
}