Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Speed up processing by reusing cached nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
weespin committed Mar 14, 2022
1 parent 626a599 commit 21deb72
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions AcapellaDownloader/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public static class Utils
{
private const string _NonceEndpoint = "https://acapelavoices.acapela-group.com/index/getnonce/";
private const string _SynthesizerEndpoint = "http://www.acapela-group.com:8080/webservices/1-34-01-Mobility/Synthesizer";
public static string GetSoundLink(string text, string voiceid)
private static string _CachedNonce = "";
private static string _CachedEmail = "";
private static bool _LastFailed = false;
public static bool UpdateNonceToken()
{
HttpClient httpClient = new HttpClient();
Random random = new Random();
Expand All @@ -24,48 +27,64 @@ public static string GetSoundLink(string text, string voiceid)
{
fakeEmail.Append((char)(random.Next(1, 26) + 64));
}

fakeEmail.Append("@gmail.com");

var nonceRequestValues = new Dictionary<string, string>
{
{"json", "{\"googleid\":\"" + fakeEmail.ToString() + "\"}"}
{ "json", "{\"googleid\":\"" + fakeEmail.ToString() + "\"}" }

};

var nonceRequestContent = new FormUrlEncodedContent(nonceRequestValues);
var nonceResponse = httpClient.PostAsync(_NonceEndpoint, nonceRequestContent).Result.Content.ReadAsStringAsync().Result;
var nonceResponse = httpClient.PostAsync(_NonceEndpoint, nonceRequestContent).Result.Content
.ReadAsStringAsync().Result;
var nonceRegex = new Regex(@"^\{\""nonce\""\:\""(.+)\""\}$");
var nonceRegexMatch = nonceRegex.Match(nonceResponse);

if (nonceRegexMatch.Groups.Count > 1)
{
var synthesizerRequest = (HttpWebRequest)WebRequest.Create(_SynthesizerEndpoint);
var synthesizerRequestString = $"req_voice={voiceid}&cl_pwd=&cl_vers=1-30&req_echo=ON&cl_login=AcapelaGroup&req_comment=%7B%22nonce%22%3A%22{nonceRegexMatch.Groups[1]}%22%2C%22user%22%3A%22{fakeEmail}%22%7D&req_text={Uri.EscapeDataString(text)}&cl_env=ACAPELA_VOICES&prot_vers=2&cl_app=AcapelaGroup_WebDemo_Android";
var data = Encoding.ASCII.GetBytes(synthesizerRequestString);
synthesizerRequest.Method = "POST";
synthesizerRequest.ContentType = "application/x-www-form-urlencoded";
synthesizerRequest.ContentLength = data.Length;
_CachedNonce = nonceRegexMatch.Groups[1].Value;
_CachedEmail = fakeEmail.ToString();
_LastFailed = false;
return true;
}

using (var stream = synthesizerRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
return false;
}

var synthesizerResponseStream = synthesizerRequest.GetResponse().GetResponseStream();
if (synthesizerResponseStream == null)
{
return "";
}
var synthesizerResponseString = new StreamReader(synthesizerResponseStream).ReadToEnd();
var synthesizerRegex = new Regex("snd_url=(.+)&snd_size");
var synthesizerMatch = synthesizerRegex.Match(synthesizerResponseString);
public static string GetSoundLink(string text, string voiceid)
{
if (_CachedEmail == "" || _CachedNonce == "" || _LastFailed)
{
UpdateNonceToken();
}
var synthesizerRequest = (HttpWebRequest)WebRequest.Create(_SynthesizerEndpoint);
var synthesizerRequestString = $"req_voice={voiceid}&cl_pwd=&cl_vers=1-30&req_echo=ON&cl_login=AcapelaGroup&req_comment=%7B%22nonce%22%3A%22{_CachedNonce}%22%2C%22user%22%3A%22{_CachedEmail}%22%7D&req_text={Uri.EscapeDataString(text)}&cl_env=ACAPELA_VOICES&prot_vers=2&cl_app=AcapelaGroup_WebDemo_Android";
var data = Encoding.ASCII.GetBytes(synthesizerRequestString);
synthesizerRequest.Method = "POST";
synthesizerRequest.ContentType = "application/x-www-form-urlencoded";
synthesizerRequest.ContentLength = data.Length;

using (var stream = synthesizerRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}

if (synthesizerMatch.Success)
{
return synthesizerMatch.Groups[1].Value;
}
var synthesizerResponseStream = synthesizerRequest.GetResponse().GetResponseStream();
if (synthesizerResponseStream == null)
{
_LastFailed = true;
return "";
}
var synthesizerResponseString = new StreamReader(synthesizerResponseStream).ReadToEnd();
var synthesizerRegex = new Regex("snd_url=(.+)&snd_size");
var synthesizerMatch = synthesizerRegex.Match(synthesizerResponseString);

if (synthesizerMatch.Success)
{
return synthesizerMatch.Groups[1].Value;
}
_LastFailed = true;
return "";
}
}
Expand Down

0 comments on commit 21deb72

Please sign in to comment.