Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. /*
  2.     Компиляция:
  3.     - Идём в C:\WINDOWS\Microsoft.NET\Framework, выбираем версию фреймфорка
  4.       выше или равную v3.5. Смотрим, чтобы в папке был csc.exe.
  5.     - Открываем консоль, пишем SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\версия
  6.     - Идём в папку с исходником.
  7.     - Говорим csc имяисходника.cs
  8.     - Говорим имяисходника.exe, дальше понятно.
  9. */
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Drawing;
  14. using System.IO;
  15. using System.Net;
  16. using System.Text;
  17.  
  18. static class FaceAppCLI
  19. {
  20.     public static void Main(string[] args)
  21.     {
  22.         if (args.Length != 3)
  23.         {
  24.             System.Console.WriteLine("Wrong args. Usage: <input file> <filter> <output file>");
  25.             System.Console.WriteLine("Filters: smile, smile_2, hot, old, young, female, male");
  26.             return;
  27.         }
  28.         string inputFile = args[0];
  29.         string filter = args[1];
  30.         string outputFile = args[2];
  31.        
  32.         FaceAppClient client = new FaceAppClient();
  33.         try
  34.         {
  35.             string code = client.UploadImage(inputFile);
  36.             Image img = client.GetImage(code, filter);
  37.             img.Save(outputFile);
  38.         }
  39.         catch (WebException)
  40.         {
  41.             System.Console.WriteLine("Something went wrong!");
  42.         }
  43.     }
  44. }
  45.  
  46. class FaceAppClient
  47. {
  48.     private const string FA_BASE_URL = "https://node-01.faceapp.io/api/v2.3/photos";
  49.  
  50.     private const string FA_FILTER = "/{0}/filters/{1}?cropped={2}";
  51.  
  52.     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";
  53.  
  54.     private const string FA_POSTIMAGE = "--{0}--";
  55.  
  56.     private const string UA = "FaceApp/1.0.229 (Linux; Android 4.4)";
  57.  
  58.     private string awselbCookie = string.Empty;
  59.  
  60.     private string xFaceAppDeviceId = string.Empty;
  61.  
  62.     private string guid = string.Empty;
  63.  
  64.     public FaceAppClient()
  65.     {
  66.         Random random = new Random();
  67.         for (int i = 0; i < 4; i++)
  68.         {
  69.             this.xFaceAppDeviceId += Convert.ToString(random.Next(4096, 65535), 16);
  70.         }
  71.         this.guid = Guid.NewGuid().ToString();
  72.     }
  73.  
  74.     public string UploadImage(string fileName)
  75.     {
  76.         string result;
  77.         try
  78.         {
  79.             List<byte> list = new List<byte>();
  80.             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)));
  81.             byte[] imageBytes = File.ReadAllBytes(fileName);
  82.             list.AddRange(imageBytes);
  83.             list.AddRange(Encoding.ASCII.GetBytes(string.Format("--{0}--", this.guid)));
  84.             imageBytes = list.ToArray();
  85.             HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://node-01.faceapp.io/api/v2.3/photos");
  86.             httpWebRequest.Method = "POST";
  87.             httpWebRequest.UserAgent = "FaceApp/1.0.229 (Linux; Android 4.4)";
  88.             httpWebRequest.ContentType = string.Format("multipart/form-data; boundary={0}", this.guid);
  89.             httpWebRequest.Headers.Add("X-FaceApp-DeviceID", this.xFaceAppDeviceId);
  90.             httpWebRequest.KeepAlive = true;
  91.             httpWebRequest.SendChunked = true;
  92.             httpWebRequest.Timeout = 25000;
  93.             BinaryWriter binaryWriter = new BinaryWriter(httpWebRequest.GetRequestStream());
  94.             binaryWriter.Write(imageBytes);
  95.             binaryWriter.Close();
  96.             HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  97.             string responseHeader = httpWebResponse.GetResponseHeader("Set-Cookie");
  98.             long contentLength = httpWebResponse.ContentLength;
  99.             StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
  100.             string text = streamReader.ReadToEnd();
  101.             if (contentLength != 29L || !text.Contains("code") || !responseHeader.StartsWith("AWSELB"))
  102.             {
  103.                 throw new Exception("UploadImage() got the following answer from the server: " + text);
  104.             }
  105.             if (this.awselbCookie == string.Empty)
  106.             {
  107.                 this.awselbCookie = responseHeader.Substring(7, responseHeader.IndexOf(';') - 7);
  108.             }
  109.             result = text.Substring(9, 18);
  110.         }
  111.         catch (Exception)
  112.         {
  113.             result = string.Empty;
  114.         }
  115.         return result;
  116.     }
  117.  
  118.     public Image GetImage(string code, string filter)
  119.     {
  120.         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");
  121.         HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
  122.         httpWebRequest.Headers.Add("X-FaceApp-DeviceID", this.xFaceAppDeviceId);
  123.         httpWebRequest.UserAgent = "FaceApp/1.0.229 (Linux; Android 4.4)";
  124.         HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  125.         return new Bitmap(httpWebResponse.GetResponseStream());
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement