From ef45853a003c98840f7ad75f3d3e48d74f76823e Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sat, 11 Feb 2023 11:24:31 +0000 Subject: [PATCH] Show correct allowed regions when only blocked is present. Closes #5 --- src/main.rs | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 258 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index a239bb1..0ecedb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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),