🦀
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

extracted parse_pdses

+76 -75
+76 -75
pds_list/src/main.rs
··· 1 - use std::collections::HashMap; 1 + use std::{collections::HashMap, panic, str::Lines}; 2 2 3 3 const JSON_URL: &str = "https://raw.githubusercontent.com/mary-ext/atproto-scraping/refs/heads/trunk/state.json"; 4 4 ··· 6 6 "atompds", "bluepds", "cirrus", "cocoon", "dnproto", "millipds", "pegasus", "rustproto", "tranquil" 7 7 ]; 8 8 9 - fn main() { 10 - let data = match minreq::get(JSON_URL).send().and_then(|res| res.as_str().map(str::to_owned)) { 11 - Ok(data) => data, 12 - Err(err) => { 13 - println!("Error downloading json: {}", err); 14 - return; 15 - } 16 - }; 17 - 9 + fn parse_pdses(mut lines_iter: Lines) -> (HashMap<String, Vec<String>>, Vec<(String, String)>) { 18 10 let mut pds_hosts: HashMap<String, Vec<String>> = HashMap::new(); 19 11 let mut other: Vec<(String, String)> = Vec::new(); 20 - let mut lines_iter = data.lines(); 21 12 22 - 'main: loop { 23 - let line = lines_iter.next(); 24 - 25 - if line.is_none() { 26 - break; 27 - } 13 + loop { 14 + let line = lines_iter.next().expect("PDSes block ended unexpectedly!"); 15 + let line = line.trim_end().to_string(); 28 16 29 - let line = line.unwrap().trim_end().to_string(); 17 + if line.starts_with("\t\t\"http") { 18 + let host = line.trim() 19 + .trim_start_matches("\"https://") 20 + .trim_end_matches("\": {") 21 + .trim_end_matches("/"); 30 22 31 - if line == "\t\"pdses\": {" { 32 - // parsing pdses section 23 + let mut version: Option<String> = None; 24 + let mut errored = false; 33 25 34 26 loop { 35 - let line = lines_iter.next(); 36 - 37 - if line.is_none() { 38 - break; 39 - } 40 - 41 - let line = line.unwrap().trim_end().to_string(); 27 + let line = lines_iter.next().expect("Host block ended unexpectedly!"); 42 28 43 - if line.starts_with("\t\t\"http") { 44 - let host = line.trim() 45 - .trim_start_matches("\"https://") 46 - .trim_end_matches("\": {") 47 - .trim_end_matches("/"); 29 + let line = line.trim_end().to_string(); 48 30 49 - let mut version: Option<String> = None; 50 - let mut errored = false; 31 + if line.starts_with("\t\t\t\"version\": \"") { 32 + let value = line.split('"').nth(3).expect("Invalid version line"); 51 33 52 - loop { 53 - let line = lines_iter.next().expect("Host block ended unexpectedly!"); 34 + if !(value.starts_with("0.") || value.starts_with("git-")) { 35 + version = Some(value.to_string()); 36 + } 37 + } else if line.contains("\"errorAt\":") { 38 + errored = true; 39 + } else if line == "\t\t}," || line == "\t\t}" { 40 + break; 41 + } else { 42 + // other host field 43 + } 44 + } 54 45 55 - let line = line.trim_end().to_string(); 46 + if !errored { 47 + if let Some(version) = version { 48 + let mut matched = false; 56 49 57 - if line.starts_with("\t\t\t\"version\": \"") { 58 - let value = line.split('"').nth(3).expect("Invalid version line"); 50 + for pds in PDS_TYPES { 51 + if version.contains(pds) { 52 + pds_hosts 53 + .entry(pds.to_string()) 54 + .or_default() 55 + .push(host.to_string()); 59 56 60 - if !(value.starts_with("0.") || value.starts_with("git-")) { 61 - version = Some(value.to_string()); 62 - } 63 - } else if line.contains("\"errorAt\":") { 64 - errored = true; 65 - } else if line == "\t\t}," || line == "\t\t}" { 57 + matched = true; 66 58 break; 67 - } else { 68 - // other host field 69 59 } 70 60 } 71 61 72 - if !errored { 73 - if let Some(version) = version { 74 - let mut matched = false; 75 - 76 - for pds in PDS_TYPES { 77 - if version.contains(pds) { 78 - pds_hosts 79 - .entry(pds.to_string()) 80 - .or_default() 81 - .push(host.to_string()); 82 - 83 - matched = true; 84 - break; 85 - } 86 - } 87 - 88 - if !matched { 89 - other.push((host.to_string(), version.to_string())); 90 - } 91 - } 62 + if !matched { 63 + other.push((host.to_string(), version.to_string())); 92 64 } 93 - } else if line == "\t}," { 94 - // end of pdses 95 - break 'main; 96 - } else { 97 - println!("Unexpected line: {}", line); 98 - return; 99 65 } 100 66 } 67 + } else if line == "\t}," { 68 + // end of pdses 69 + break; 70 + } else { 71 + panic!("Unexpected line: {}", line); 101 72 } 102 73 } 103 74 75 + return (pds_hosts, other); 76 + } 77 + 78 + fn main() { 79 + let data = match minreq::get(JSON_URL).send().and_then(|res| res.as_str().map(str::to_owned)) { 80 + Ok(data) => data, 81 + Err(err) => { 82 + println!("Error downloading json: {}", err); 83 + return; 84 + } 85 + }; 86 + 87 + let mut lines_iter = data.lines(); 88 + 89 + let (pds_hosts, other) = loop { 90 + let line = lines_iter.next().expect("End of file reached without finding PDSes section"); 91 + 92 + let line = line.trim_end().to_string(); 93 + 94 + if line == "\t\"pdses\": {" { 95 + // parsing pdses section 96 + let (pds_hosts, other) = parse_pdses(lines_iter); 97 + break (pds_hosts, other); 98 + } else { 99 + // keep looking 100 + } 101 + }; 102 + 104 103 for pds in PDS_TYPES { 105 104 println!("{pds}:"); 106 105 107 - for host in pds_hosts.entry(pds.to_string()).or_default() { 108 - println!(" {host}"); 106 + if let Some(list) = pds_hosts.get(pds) { 107 + for host in list { 108 + println!(" {host}"); 109 + } 109 110 } 110 111 111 112 println!();