Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- faceapp.cs
- How to compile FaceApp CLI:
- - Go to C:\WINDOWS\Microsoft.NET\Framework, look for framework version
- above or equal to v3.5. There should be also csc.exe.
- - Open terminal, copy SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\--your-version--
- - Go to the folder with this source file (faceapp.cs)
- - enter csc faceapp.cs
- - Enter faceapp.exe, for example "faceapp.exe female photo.jpg)
- */
- 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://node-01.faceapp.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://node-01.faceapp.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://node-01.faceapp.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());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement