-
Notifications
You must be signed in to change notification settings - Fork 1
/
TikTok.cs
229 lines (206 loc) · 7.24 KB
/
TikTok.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Web;
using Serilog;
namespace AbevBot
{
public static class TikTok
{
// Dictionary of <key, value>
// key: TikTok internal voice name
// value: Voice name used on stream and tts messages
private static readonly Dictionary<string, string> Voices = new()
{
// English
{"en_uk_001", "uk1"},
{"en_uk_003", "uk3"},
{"en_au_001", "au1"},
{"en_au_002", "au2"},
{"en_us_001", "us1"},
{"en_us_002", "us2"},
{"en_us_006", "us6"},
{"en_us_007", "us7"},
{"en_us_009", "us9"},
{"en_us_010", "us10"},
{"en_female_emotional", "Emotional"},
{"en_female_samc", "Empathetic"},
{"en_male_cody", "Serious"},
{"en_male_narration", "Old"},
{"en_male_funny", "Funny"},
{"en_male_jarvis", "Jarvis"},
{"en_male_santa_narration", "Santa"},
{"en_female_betty", "Betty"},
{"en_female_makeup", "Makeup"},
{"en_female_richgirl", "Richgirl"},
{"en_male_cupid", "Cupid"},
{"en_female_shenna", "Shenna"},
{"en_male_ghosthost", "GhostHost"},
{"en_female_grandma", "Grandma"},
{"en_male_ukneighbor", "ukneighbor"},
{"en_male_wizard", "Wizard"},
{"en_male_trevor", "Trevor"},
{"en_male_deadpool", "Deadpool"},
{"en_male_ukbutler", "ukbutler"},
{"en_male_petercullen", "Petercullen"},
{"en_male_pirate", "Pirate"},
{"en_male_santa", "Santa2"},
{"en_male_santa_effect", "Santa3"},
{"en_female_pansino", "Pansino"},
{"en_male_grinch", "Trickster"},
{"en_us_ghostface", "Ghost"},
{"en_us_chewbacca", "Chewbacca"},
{"en_us_c3po", "C3PO"},
{"en_us_stormtrooper", "Storm"},
{"en_us_stitch", "Stitch"},
{"en_us_rocket", "Rocket"},
{"en_female_madam_leota", "Leota"},
{"en_female_ht_f08_halloween", "Opera"},
{"en_male_m03_lobby", "Sing"},
{"en_female_f08_salut_damour", "Sing2"},
{"en_female_f08_warmy_breeze", "Sing3"},
{"en_male_m03_sunshine_soon", "Sing4"},
{"en_female_ht_f08_glorious", "Sing5"},
{"en_male_sing_funny_it_goes_up", "Sing6"},
{"en_male_m2_xhxs_m03_silly", "Sing7"},
{"en_female_ht_f08_wonderful_world", "Sing8"},
{"en_male_sing_deep_jingle", "Sing9"},
{"en_male_m03_classical", "Sing10"},
{"en_male_m2_xhxs_m03_christmas", "Sing11"},
{"en_female_ht_f08_newyear", "Sing12"},
{"en_male_sing_funny_thanksgiving", "Sing13"},
{"en_female_f08_twinkle", "Sing14"},
// French
{"fr_001", "fr1"},
{"fr_002", "fr2"},
// German
{"de_001", "de1"},
{"de_002", "de2"},
// Indonesian
{"id_male_darma", "Darma"},
{"id_female_icha", "Icha"},
{"id_female_noor", "Noor"},
{"id_male_putra", "Putra"},
// Italian
{"it_male_m18", "itm18"},
// Japanese
{"jp_001", "jp1"},
{"jp_003", "jp3"},
{"jp_005", "jp5"},
{"jp_006", "jp6"},
{"jp_male_osada", "Osada"},
{"jp_male_matsuo", "Matsuo"},
{"jp_female_machikoriiita", "Machikoriiita"},
{"jp_male_matsudake", "Matsudake"},
{"jp_male_shuichiro", "Shuichiro"},
{"jp_female_rei", "Rei"},
{"jp_male_hikakin", "Hikakin"},
{"jp_female_yagishaki", "YagiShaki"},
// Korean
{"kr_002", "kr2"},
{"kr_003", "kr3"},
{"kr_004", "kr4"},
// Portuguese
{"br_001", "br1"},
{"br_003", "br3"},
{"br_004", "br4"},
{"br_005", "br5"},
{"pt_female_lhays", "Lhays"},
{"pt_female_laizza", "Laizza"},
{"pt_male_transformer", "OptimusPrimePt"},
// Spanish
{"es_002", "es2"},
{"es_male_m3", "esm3"},
{"es_female_f6", "esf6"},
{"es_female_fp1", "esfp1"},
{"es_mx_002", "esm2"},
{"es_mx_male_transformer", "OptimusPrimeMx"},
{"es_mx_female_supermom", "SuperMom"},
{"id_001", "id1"}
};
public static string GetVoice(string name)
{
if (name is null || name.Length == 0) return string.Empty;
if (Secret.Data[Secret.Keys.TikTokSessionID].Length == 0) return string.Empty;
string voiceName = name.ToLower();
var voice = Voices.GetEnumerator();
while (voice.MoveNext())
{
if (voice.Current.Value.ToLower().Equals(voiceName))
{
return voice.Current.Key;
}
}
return string.Empty;
}
public static string GetVoices()
{
StringBuilder sb = new();
var voice = Voices.GetEnumerator();
bool first = true;
while (voice.MoveNext())
{
if (!first) sb.Append(", ");
sb.Append(voice.Current.Value);
first = false;
}
return sb.ToString();
}
/// <summary> Appends all available voices to provided StringBuilder. </summary>
/// <param name="sb">StringBuilder to append voices to.</param>
/// <param name="charactersPerLine">Insert line break after provided amout of characters (<= 0 - don't insert line breaks).</param>
public static void AppendVoices(ref StringBuilder sb, int charactersPerLine = -1)
{
var voice = Voices.GetEnumerator();
bool first = true;
int lineLength = 0;
while (voice.MoveNext())
{
if (!first)
{
sb.Append(", ");
lineLength += 2;
}
if (charactersPerLine > 0 && lineLength > charactersPerLine)
{
sb.AppendLine();
lineLength = 0;
}
sb.Append(voice.Current.Value);
lineLength += voice.Current.Value.Length;
first = false;
}
sb.AppendLine();
}
public static Stream GetTTS(string text, string voice)
{
if (Secret.Data[Secret.Keys.TikTokSessionID].Length == 0) return null;
if (text is null || text.Length == 0) return null;
string t = HttpUtility.UrlEncode(text);
// string url = $"https://api16-normal-v6.tiktokv.com/media/api/text/speech/invoke/?text_speaker={voice}&req_text={text}&speaker_map_type=0&aid=1233";
string url = $"https://api16-normal-c-useast2a.tiktokv.com/media/api/text/speech/invoke/?text_speaker={voice}&req_text={t}&speaker_map_type=0&aid=1233";
using HttpRequestMessage request = new(HttpMethod.Post, url);
request.Headers.Add("User-Agent", "com.zhiliaoapp.musically/2022600030 (Linux; U; Android 7.1.2; es_ES; SM-G988N; Build/NRD90M;tt-ok/3.12.13.1)");
request.Headers.Add("Cookie", $"sessionid={Secret.Data[Secret.Keys.TikTokSessionID]}");
try
{
string resp = Notifications.Client.Send(request).Content.ReadAsStringAsync().Result;
var result = TikTokTTSResponse.Deserialize(resp);
if (result?.StatusCode != 0)
{
Log.Warning("TikTok TTS request status: {status}, error: {msg}", result?.StatusCode, result?.StatusMessage);
return null;
}
else if (result?.Data?.Duration?.Length is null || string.IsNullOrEmpty(result?.Data?.VStr))
{
Log.Warning("TikTok TTS request returned sound with length 0.");
return null;
}
return new MemoryStream(Convert.FromBase64String(result.Data.VStr));
}
catch (Exception ex) { Log.Error("TikTok TTS request failed. {ex}", ex); return null; }
}
}
}