Advertisement
Guest User

FaceApp CLI

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