366 lines
9.9 KiB
Rust
366 lines
9.9 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::fmt::Write;
|
|
use std::path::Path;
|
|
|
|
use reqwest::header;
|
|
use reqwest::Client;
|
|
use serde::Deserialize;
|
|
use serde_with::serde_as;
|
|
use serde_with::VecSkipError;
|
|
|
|
use crate::util::Text;
|
|
|
|
#[serde_as]
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct LanguageMenu {
|
|
#[serde_as(as = "VecSkipError<_>")]
|
|
actions: Vec<ActionWrap>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct ActionWrap {
|
|
open_popup_action: OpenPopupAction,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct OpenPopupAction {
|
|
popup: Popup,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Popup {
|
|
multi_page_menu_renderer: MultiPageMenuRenderer<MenuSectionRenderer>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct MultiPageMenuRenderer<T> {
|
|
sections: Vec<MenuSectionRendererWrap<T>>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct MenuSectionRendererWrap<T> {
|
|
multi_page_menu_section_renderer: T,
|
|
}
|
|
|
|
#[serde_as]
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct MenuSectionRenderer {
|
|
#[serde_as(as = "VecSkipError<_>")]
|
|
items: Vec<CompactLinkRendererWrap>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct CompactLinkRendererWrap {
|
|
compact_link_renderer: CompactLinkRenderer,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct CompactLinkRenderer {
|
|
icon: Icon,
|
|
service_endpoint: ServiceEndpoint<MenuAction>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Icon {
|
|
pub icon_type: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct ServiceEndpoint<T> {
|
|
signal_service_endpoint: SignalServiceEndpoint<T>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct SignalServiceEndpoint<T> {
|
|
actions: Vec<T>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct MenuAction {
|
|
get_multi_page_menu_action: MultiPageMenuAction,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct MultiPageMenuAction {
|
|
menu: Menu,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Menu {
|
|
multi_page_menu_renderer: MultiPageMenuRenderer<ItemSectionRenderer>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct ItemSectionRenderer {
|
|
items: Vec<LanguageItemWrap>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct LanguageItemWrap {
|
|
compact_link_renderer: LanguageItem,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct LanguageItem {
|
|
title: Text,
|
|
service_endpoint: ServiceEndpoint<LanguageCountryAction>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct LanguageCountryAction {
|
|
#[serde(alias = "selectCountryCommand")]
|
|
select_language_command: LanguageCountryCommand,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
struct LanguageCountryCommand {
|
|
#[serde(alias = "gl")]
|
|
hl: String,
|
|
}
|
|
|
|
pub async fn generate_locales(project_root: &Path) {
|
|
let (languages, countries) = get_locales().await;
|
|
|
|
let code_head = r#"// This file is automatically generated. DO NOT EDIT.
|
|
|
|
//! Languages and countries
|
|
|
|
use std::{fmt::Display, str::FromStr};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
"#;
|
|
|
|
let code_foot = r#"impl Display for Language {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.write_str(
|
|
&serde_json::to_string(self).map_or("".to_owned(), |s| s[1..s.len() - 1].to_owned()),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Display for Country {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.write_str(
|
|
&serde_json::to_string(self).map_or("".to_owned(), |s| s[1..s.len() - 1].to_owned()),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl FromStr for Language {
|
|
type Err = serde_json::Error;
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
serde_json::from_str(&format!("\"{}\"", s))
|
|
}
|
|
}
|
|
|
|
impl FromStr for Country {
|
|
type Err = serde_json::Error;
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
serde_json::from_str(&format!("\"{}\"", s))
|
|
}
|
|
}
|
|
"#;
|
|
|
|
let mut code_langs = r#"/// Available languages
|
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum Language {
|
|
"#
|
|
.to_owned();
|
|
|
|
let mut code_countries = r#"/// Available countries
|
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
#[serde(rename_all = "UPPERCASE")]
|
|
pub enum Country {
|
|
"#
|
|
.to_owned();
|
|
|
|
let mut code_lang_array = format!(
|
|
"/// Array of all available languages\npub const LANGUAGES: [Language; {}] = [\n",
|
|
languages.len()
|
|
);
|
|
let mut code_country_array = format!(
|
|
"/// Array of all available countries\npub const COUNTRIES: [Country; {}] = [\n",
|
|
countries.len()
|
|
);
|
|
|
|
let mut code_lang_names = r#"impl Language {
|
|
/// Get the native name of the language
|
|
///
|
|
/// Examples: "English (US)", "Deutsch", "中文 (简体)"
|
|
pub fn name(&self) -> &str {
|
|
match self {
|
|
"#
|
|
.to_owned();
|
|
let mut code_country_names = r#"impl Country {
|
|
/// Get the English name of the country
|
|
///
|
|
/// Examples: "United States", "Germany"
|
|
pub fn name(&self) -> &str {
|
|
match self {
|
|
"#
|
|
.to_owned();
|
|
|
|
languages.iter().for_each(|(c, n)| {
|
|
let enum_name = c
|
|
.split('-')
|
|
.map(|c| {
|
|
format!(
|
|
"{}{}",
|
|
c[0..1].to_owned().to_uppercase(),
|
|
c[1..].to_owned().to_lowercase()
|
|
)
|
|
})
|
|
.collect::<String>();
|
|
|
|
// Language enum
|
|
let _ = write!(code_langs, " /// {}\n ", n);
|
|
if c.contains('-') {
|
|
let _ = write!(code_langs, "#[serde(rename = \"{}\")]\n ", c);
|
|
}
|
|
code_langs += &enum_name;
|
|
code_langs += ",\n";
|
|
|
|
// Language array
|
|
let _ = writeln!(code_lang_array, " Language::{},", enum_name);
|
|
|
|
// Language names
|
|
let _ = writeln!(
|
|
code_lang_names,
|
|
" Language::{} => \"{}\",",
|
|
enum_name, n
|
|
);
|
|
});
|
|
code_langs += "}\n";
|
|
|
|
countries.iter().for_each(|(c, n)| {
|
|
let enum_name = c[0..1].to_owned().to_uppercase() + &c[1..].to_owned().to_lowercase();
|
|
|
|
// Country enum
|
|
let _ = writeln!(code_countries, " /// {}", n);
|
|
let _ = writeln!(code_countries, " {},", enum_name);
|
|
|
|
// Country array
|
|
let _ = writeln!(code_country_array, " Country::{},", enum_name);
|
|
|
|
// Country names
|
|
let _ = writeln!(
|
|
code_country_names,
|
|
" Country::{} => \"{}\",",
|
|
enum_name, n
|
|
);
|
|
});
|
|
code_countries += "}\n";
|
|
|
|
code_lang_array += "];\n";
|
|
code_country_array += "];\n";
|
|
code_lang_names += " }\n }\n}\n";
|
|
code_country_names += " }\n }\n}\n";
|
|
|
|
let code = format!(
|
|
"{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
|
|
code_head,
|
|
code_langs,
|
|
code_countries,
|
|
code_lang_array,
|
|
code_country_array,
|
|
code_lang_names,
|
|
code_country_names,
|
|
code_foot
|
|
);
|
|
|
|
let mut target_path = project_root.to_path_buf();
|
|
target_path.push("src/param/locale.rs");
|
|
std::fs::write(target_path, code).unwrap();
|
|
}
|
|
|
|
async fn get_locales() -> (BTreeMap<String, String>, BTreeMap<String, String>) {
|
|
let client = Client::new();
|
|
let resp = client
|
|
.post("https://www.youtube.com/youtubei/v1/account/account_menu?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false")
|
|
.header(header::CONTENT_TYPE, "application/json")
|
|
.body(
|
|
r##"{"context":{"client":{"clientName":"WEB","clientVersion":"2.20220914.06.00","platform":"DESKTOP","originalUrl":"https://www.youtube.com/","hl":"en","gl":"US"},"request":{"internalExperimentFlags":[],"useSsl":true},"user":{"lockedSafetyMode":false}}}"##
|
|
)
|
|
.send().await
|
|
.unwrap()
|
|
.error_for_status()
|
|
.unwrap();
|
|
|
|
let language_menu = resp.json::<LanguageMenu>().await.unwrap();
|
|
|
|
let lm_section = &language_menu.actions[0]
|
|
.open_popup_action
|
|
.popup
|
|
.multi_page_menu_renderer
|
|
.sections
|
|
.iter()
|
|
.find(|s| s.multi_page_menu_section_renderer.items.len() >= 2)
|
|
.unwrap();
|
|
|
|
let lang_section = lm_section
|
|
.multi_page_menu_section_renderer
|
|
.items
|
|
.iter()
|
|
.find(|s| s.compact_link_renderer.icon.icon_type == "TRANSLATE")
|
|
.unwrap();
|
|
|
|
let country_section = lm_section
|
|
.multi_page_menu_section_renderer
|
|
.items
|
|
.iter()
|
|
.find(|s| s.compact_link_renderer.icon.icon_type == "LANGUAGE")
|
|
.unwrap();
|
|
|
|
let languages = map_language_section(lang_section);
|
|
let countries = map_language_section(country_section);
|
|
|
|
(languages, countries)
|
|
}
|
|
|
|
fn map_language_section(section: &CompactLinkRendererWrap) -> BTreeMap<String, String> {
|
|
section
|
|
.compact_link_renderer
|
|
.service_endpoint
|
|
.signal_service_endpoint
|
|
.actions[0]
|
|
.get_multi_page_menu_action
|
|
.menu
|
|
.multi_page_menu_renderer
|
|
.sections[0]
|
|
.multi_page_menu_section_renderer
|
|
.items
|
|
.iter()
|
|
.map(|i| {
|
|
(
|
|
i.compact_link_renderer
|
|
.service_endpoint
|
|
.signal_service_endpoint
|
|
.actions[0]
|
|
.select_language_command
|
|
.hl
|
|
.to_owned(),
|
|
i.compact_link_renderer.title.simple_text.to_owned(),
|
|
)
|
|
})
|
|
.collect()
|
|
}
|