DeepMindApi

 avatar
unknown
csharp
4 years ago
8.9 kB
6
Indexable
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    public class DeepMindApi
    {
        string url_service;
        string suffix_image_service;
        string api_key;
        string username_patner;
        string password_patner;

        public class DeepResponseRaw
        {
            public int error_code;
            public string error;
            public string answer;
        }

        public DeepMindApi(string url_service,string suffiximg, string api_key, string username_patner, string password_patner)
        {
            this.url_service = url_service;
            this.api_key = api_key;
            this.username_patner = username_patner;
            this.password_patner = password_patner;
            this.suffix_image_service = suffiximg;
        }

        /*
         Esempi di risposta
         POSITIVA
            response_code = 200
            steps, direction
        NEGATIVE
            CONTROLLI LATO CLIENT 
                response_code = 900
                response_message = Descrizione Errore
            CONTROLLI LATO SERVICE ( SOLVED BY AI )
                response_code >= 400 <= 500
                response_message = DESCRIZIONE ERRORE ( NON AUTORIZZATO, CREDITO FINITO, ERRORI NELLA RICHIESTA)
        */
        public CaptchaResolutorResponse SolveCaptcha(byte[] img_input,string captcha_id,int imgNumber)
        {
            CaptchaResolutorResponse res = new CaptchaResolutorResponse();
            if (img_input != null)
            {
                string base64_img = Convert.ToBase64String(img_input);
                if (base64_img != null && base64_img.Length > 0)
                {
                    string json = null;
                    using (var client = new WebClient())
                    {
                        json = encode_params_solve_captcha(this.api_key,base64_img,imgNumber,captcha_id);
                        client.Headers.Add("content-type", "application/json");
                        try
                        {
                            string url = this.url_service + "" + this.suffix_image_service;
                            
                            string response = Encoding.ASCII.GetString(client.UploadData(url, "POST", Encoding.UTF8.GetBytes(json)));
                            
                            DeepResponseRaw raw = JsonConvert.DeserializeObject<DeepResponseRaw>(response);


                            if (raw != null)
                            {
                                if(raw.answer!=null && raw.answer.Length > 0)
                                {
                                    var isNumeric = float.TryParse(raw.answer, out _);
                                    if (isNumeric == true)
                                    {
                                        raw.answer = raw.answer.Replace(".", ",");
                                        var angle = (int)Convert.ToDouble(raw.answer);
                                        res.Steps = (int)angle / 40;
                                        if(res.Steps < (9-res.Steps))
                                            res.Direction = "RIGHT";
                                        else
                                        {
                                            res.Direction = "LEFT";
                                            res.Steps = (9-res.Steps);
                                        }
                                            
                                    }
                                }
                                else
                                {
                                    if(raw.error_code!=null && raw.error != null)
                                    {
                                        res.Response_code= raw.error_code;
                                        res.Message = raw.error;
                                    }
                                    else
                                    {
                                        res.Response_code = 900;
                                        res.Message = "NO RESULTS";
                                    }
                                }
                            }
                            else
                            {
                                res.Response_code = 900;
                                res.Message = "NO RESULTS";
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("{0} Exception caught.", ex);
                            if (ex.GetType().Name == "WebException")
                            {
                                WebException we = (WebException)ex;
                                HttpWebResponse response = (System.Net.HttpWebResponse)we.Response;
                                res.Response_code = (int)response.StatusCode;
                                res.Message = response.StatusDescription;

                            }
                            else
                            {
                                res.Response_code = 402;
                                res.Message = "BAD RESPONSE";
                            }

                        }
                    }
                }
                else
                {
                    res.Response_code = 900;
                    res.Message = "BASE 64 IMAGE CORRUPTED";
                }
            }
            else
            {
                res.Response_code = 900;
                res.Message = "NO IMAGE";
            }

            return res;
        }

        public void ValidateResolution(int status, string captcha_id, int n_imges)
        {
            CaptchaResolutorResponse res = new CaptchaResolutorResponse();
            if (status != null && captcha_id!=null && n_imges!=null)
            {
                
                    string json = null;
                    using (var client = new WebClient())
                    {
                        json = encode_params_validate_resolution(this.api_key,status+"",n_imges,captcha_id);
                        client.Headers.Add("content-type", "application/json");
                        try
                        {
                            string url = this.url_service;
                            Encoding.ASCII.GetString(client.UploadData(url, "POST", Encoding.UTF8.GetBytes(json)));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("{0} Exception caught.", ex);
                        }
                    }
                
            }
        }

        public string encode_params_solve_captcha(string api_key, string base64_image, int img_number, string captcha_id)
        {
            string json = null;
            if (api_key != null && img_number != null && base64_image != null && captcha_id != null)
            {
                Dictionary<string,object> valuePairs = new Dictionary<string, object>
                {
                    { "api_key", api_key },
                    { "action", "solve" },
                    { "user", this.username_patner },
                    { "pass", this.password_patner },
                    { "image", base64_image },
                    { "captchaId", captcha_id },
                    { "imgNumber", img_number },
                    { "game", 1 },
                    { "variant", "wbgs" }
                };
                json = JsonConvert.SerializeObject(valuePairs, Formatting.Indented);
            }
            return json;
        }

        public string encode_params_validate_resolution(string api_key, string status, int n_images, string captcha_id)
        {
            string json = null;
            if (api_key != null && status != null && n_images != null && captcha_id != null)
            {
                Dictionary<string, object> valuePairs = new Dictionary<string, object>
                {
                    { "api_key", api_key },
                    { "action", "result" },
                    { "user", this.username_patner },
                    { "pass", this.password_patner },
                    { "captchaId", captcha_id },
                    { "status", status },
                    { "images", n_images }
                };
                json = JsonConvert.SerializeObject(valuePairs, Formatting.Indented);
            }
            return json;
        }
    }
}
Editor is loading...