rediffusion
11/7/2017 - 11:00 PM

deepart-postget

Что такое HTTP - https://ru.wikipedia.org/wiki/HTTP Список кодов состояния HTTP - https://ru.wikipedia.org/wiki/%D0%A1%D0%BF%D0%B8%D1%81%D0%BE%D0%BA_%D0%BA%D0%BE%D0%B4%D0%BE%D0%B2_%D1%81%D0%BE%D1%81%D1%82%D0%BE%D1%8F%D0%BD%D0%B8%D1%8F_HTTP Статья "Простым языком об HTTP" на Хабрахабре - https://habrahabr.ru/post/215117/

string strFilePath = project.Path + "source.jpg";;
if (!File.Exists(strFilePath)) {
    project.SendErrorToLog(String.Format("Файл {0} не найден в системе", strFilePath));
	return "error";
}
string strFileName = new FileInfo(strFilePath).Name;

//формируем перечень доступных стилей
string strStyle = project.Variables["cfg_style"].Value; //заданный в настройках стиль. Если не указан - будет выбран случайный
string strAvailStyles = ZennoPoster.HttpGet("http://turbo.deepart.io/styles/", "", "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly, 30000, MaxRedirectCount: 5);
List<string> lstAvailStyles = new Regex(@"\d+(?=\ http)").Matches(strAvailStyles).Cast<Match>().Select(match => match.Value).ToList();
if (strStyle==String.Empty) {
	strStyle = lstAvailStyles[new Random().Next(0, lstAvailStyles.Count)];
}else{
	if (!lstAvailStyles.Contains(strStyle)) {
		project.SendErrorToLog(String.Format("Стиль {0} не входит в число допустимых", strStyle));
		return "error";		
	}
}

//формируем контент запроса (да, знаю, можно шаблон поместить в файл, и здесь только подставлять данные - так сделал для наглядности)
string strContent = String.Format("---boundary" + "\r\n" +
	@"Content-Disposition: form-data; name=""input_image""; filename=""{0}""" + "\r\n" +
	"Content-Type: image/png\r\n\r\n" +
	@"{1}" + "\r\n" +
	"---boundary" + "\r\n" +
	@"Content-Disposition: form-data; name=""style""" + "\r\n\r\n" +
	"{2}" + "\r\n" +
	"---boundary--", strFileName, strFilePath, strStyle);

//Выполняем POST-запрос (отправляем данные)
string strPostResult = ZennoPoster.HttpPost("http://turbo.deepart.io/api/post/", 
	strContent, "multipart/form-data", "", 
	"utf-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly);

//Ждём в цикле появления ссылки на скачивание
string strDownloadLink = String.Empty;
for (int i=0; i<100; i++) {
	strDownloadLink = ZennoPoster.HttpGet(String.Format("http://turbo.deepart.io/get/output/{0}/", strPostResult), "", "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly, 30000);
		
	if (strDownloadLink!=String.Empty) {
		//Скачиваем результат
		string strDownloadResult = ZennoPoster.HttpGet(strDownloadLink, "", "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.File, 30000, DownloadPath: project.Path);
		if (File.Exists(strDownloadResult)) {
			project.SendInfoToLog (String.Format("Картинка успешно сохранена под именем {0}", strDownloadResult));
			return "oki";
		}else{
			project.SendErrorToLog (String.Format("Ошибка при сохранении файла: {0}", strDownloadResult));
			return "error";
		}
	}
	Thread.Sleep(150);
}


//string strResultLink = ZennoPoster.HttpGet(strAPIQuery, "", "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly, 30000, MaxRedirectCount: 5);
//return strResultLink;