Show correct allowed regions when only blocked is present.

Closes #5
This commit is contained in:
Kavin 2023-02-11 11:24:31 +00:00
parent 423f776fa0
commit ef45853a00
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
1 changed files with 258 additions and 3 deletions

View File

@ -43,6 +43,253 @@ lazy_static!(
.unwrap();
);
const ALL_REGIONS: [&str; 244] = [
"AD",
"AE",
"AF",
"AG",
"AI",
"AL",
"AM",
"AO",
"AQ",
"AR",
"AS",
"AT",
"AU",
"AW",
"AX",
"AZ",
"BA",
"BB",
"BD",
"BE",
"BF",
"BG",
"BH",
"BI",
"BJ",
"BL",
"BM",
"BN",
"BO",
"BR",
"BS",
"BT",
"BV",
"BW",
"BY",
"BZ",
"CA",
"CC",
"CD",
"CF",
"CG",
"CH",
"CI",
"CK",
"CL",
"CM",
"CN",
"CO",
"CR",
"CU",
"CV",
"CX",
"CY",
"CZ",
"DE",
"DJ",
"DK",
"DM",
"DO",
"DZ",
"EC",
"EE",
"EG",
"EH",
"ER",
"ES",
"ET",
"FI",
"FJ",
"FK",
"FM",
"FO",
"FR",
"GA",
"GB",
"GD",
"GE",
"GF",
"GG",
"GH",
"GI",
"GL",
"GM",
"GN",
"GP",
"GQ",
"GR",
"GS",
"GT",
"GU",
"GW",
"GY",
"HK",
"HM",
"HN",
"HR",
"HT",
"HU",
"ID",
"IE",
"IL",
"IM",
"IN",
"IO",
"IQ",
"IR",
"IS",
"IT",
"JE",
"JM",
"JO",
"JP",
"KE",
"KG",
"KH",
"KI",
"KM",
"KN",
"KP",
"KR",
"KW",
"KY",
"KZ",
"LA",
"LB",
"LC",
"LI",
"LK",
"LR",
"LS",
"LT",
"LU",
"LV",
"LY",
"MA",
"MC",
"MD",
"ME",
"MG",
"MH",
"MK",
"ML",
"MM",
"MN",
"MO",
"MP",
"MQ",
"MR",
"MS",
"MT",
"MU",
"MV",
"MW",
"MX",
"MY",
"MZ",
"NA",
"NC",
"NE",
"NF",
"NG",
"NI",
"NL",
"NO",
"NP",
"NR",
"NU",
"NZ",
"OM",
"PA",
"PE",
"PF",
"PG",
"PH",
"PK",
"PL",
"PM",
"PN",
"PR",
"PS",
"PT",
"PW",
"PY",
"QA",
"RE",
"RO",
"RS",
"RU",
"RW",
"SA",
"SB",
"SC",
"SD",
"SE",
"SG",
"SH",
"SI",
"SJ",
"SK",
"SL",
"SM",
"SN",
"SO",
"SR",
"ST",
"SV",
"SY",
"SZ",
"TC",
"TD",
"TF",
"TG",
"TH",
"TJ",
"TK",
"TL",
"TM",
"TN",
"TO",
"TR",
"TT",
"TV",
"TW",
"TZ",
"UA",
"UG",
"UM",
"US",
"UY",
"UZ",
"VA",
"VC",
"VE",
"VG",
"VI",
"VN",
"VU",
"WF",
"WS",
"YE",
"YT",
"ZA",
"ZM",
"ZW"
];
#[async_recursion]
async fn fetch_restrictions(video_id: &str) -> RestrictionInfo {
let resp = CLIENT.get(format!("https://content-youtube.googleapis.com/youtube/v3/videos?id={}&part=contentDetails&key=AIzaSyAa8yy0GdcGPHdtD083HiGGx_S0vMPScDM", video_id))
@ -72,11 +319,19 @@ async fn fetch_restrictions(video_id: &str) -> RestrictionInfo {
let resp: serde_json::Value = serde_json::from_str(&resp).unwrap();
let region = resp["items"][0]["contentDetails"]["regionRestriction"]["allowed"].as_array();
let blocked = resp["items"][0]["contentDetails"]["regionRestriction"]["blocked"].as_array();
let allowed = resp["items"][0]["contentDetails"]["regionRestriction"]["allowed"].as_array();
let region = {
if let Some(region) = region {
let regions = region.iter().map(|x| x.as_str().unwrap().to_string()).collect();
if let Some(allowed) = allowed {
let regions = allowed.iter().map(|x| x.as_str().unwrap().to_string()).collect();
RestrictionInfo {
restricted: true,
regions: Some(regions),
}
} else if let Some(blocked) = blocked {
let blocked: Vec<String> = blocked.iter().map(|x| x.as_str().unwrap().to_string()).collect();
let regions = ALL_REGIONS.iter().filter(|x| !blocked.contains(&x.to_string())).map(|x| x.to_string()).collect();
RestrictionInfo {
restricted: true,
regions: Some(regions),