repositories

percival

Follow the trail a handle leaves across the web: username search across 5,700+ sites, in Rust

clonegit clone https://git.daemon-sec.xyz/percival.git
categories.rsRust162 lines4.2 KiBraw download
//! Category normalisation across databases.

/// Canonical categories, roughly ordered by how interesting they are in a report.
pub const CANONICAL: &[&str] = &[
    "social",
    "coding",
    "gaming",
    "forum",
    "blog",
    "art",
    "images",
    "video",
    "music",
    "business",
    "finance",
    "crypto",
    "shopping",
    "dating",
    "health",
    "hobby",
    "news",
    "political",
    "tech",
    "science",
    "education",
    "wiki",
    "security",
    "darknet",
    "archived",
    "search",
    "nsfw",
    "misc",
];

/// Maigret tag → canonical category, in priority order (first match wins).
const TAG_TO_CATEGORY: &[(&str, &str)] = &[
    ("porn", "nsfw"),
    ("erotic", "nsfw"),
    ("webcam", "nsfw"),
    ("social", "social"),
    ("messaging", "social"),
    ("mastodon", "social"),
    ("lemmy", "social"),
    ("geosocial", "social"),
    ("coding", "coding"),
    ("hacking", "security"),
    ("cybercriminal", "darknet"),
    ("tor", "darknet"),
    ("i2p", "darknet"),
    ("gaming", "gaming"),
    ("gambling", "gaming"),
    ("forum", "forum"),
    ("discussion", "forum"),
    ("q&a", "forum"),
    ("blog", "blog"),
    ("writing", "blog"),
    ("art", "art"),
    ("design", "art"),
    ("3d", "art"),
    ("photo", "images"),
    ("video", "video"),
    ("streaming", "video"),
    ("movies", "video"),
    ("music", "music"),
    ("business", "business"),
    ("professional", "business"),
    ("career", "business"),
    ("freelance", "business"),
    ("tasks", "business"),
    ("networking", "business"),
    ("finance", "finance"),
    ("fintech", "finance"),
    ("trading", "finance"),
    ("stock", "finance"),
    ("crypto", "crypto"),
    ("nft", "crypto"),
    ("shopping", "shopping"),
    ("classified", "shopping"),
    ("fashion", "shopping"),
    ("dating", "dating"),
    ("medicine", "health"),
    ("psychology", "health"),
    ("parenting", "health"),
    ("news", "news"),
    ("tech", "tech"),
    ("apps", "tech"),
    ("llm", "tech"),
    ("science", "science"),
    ("research", "science"),
    ("education", "education"),
    ("books", "education"),
    ("reading", "education"),
    ("documents", "education"),
    ("wiki", "wiki"),
    ("archive", "archived"),
    ("hobby", "hobby"),
    ("anime", "hobby"),
    ("cooking", "hobby"),
    ("sport", "hobby"),
    ("auto", "hobby"),
    ("travel", "hobby"),
    ("firearms", "hobby"),
    ("military", "hobby"),
    ("esoteric", "hobby"),
    ("religion", "hobby"),
    ("maps", "misc"),
    ("links", "misc"),
    ("sharing", "misc"),
    ("torrent", "misc"),
    ("bookmarks", "misc"),
    ("review", "misc"),
];

/// Derive a canonical category from a list of free-form tags.
pub fn from_tags(tags: &[String]) -> String {
    let lower: Vec<String> = tags.iter().map(|t| t.trim().to_ascii_lowercase()).collect();
    for (tag, cat) in TAG_TO_CATEGORY {
        if lower.iter().any(|t| t == tag) {
            return (*cat).to_string();
        }
    }
    "misc".to_string()
}

/// Normalise a WhatsMyName category label.
pub fn normalize(cat: &str) -> String {
    let c = cat.trim().to_ascii_lowercase();
    match c.as_str() {
        "xx nsfw xx" | "nsfw" | "adult" | "porn" => "nsfw".to_string(),
        "" => "misc".to_string(),
        "photo" | "photos" | "image" => "images".to_string(),
        "code" | "developer" | "dev" => "coding".to_string(),
        "games" | "game" => "gaming".to_string(),
        "forums" => "forum".to_string(),
        "archive" => "archived".to_string(),
        other => other.to_string(),
    }
}

/// Whether a tag is a two-letter country code (Maigret uses these).
pub fn is_country_tag(tag: &str) -> bool {
    tag.len() == 2 && tag.chars().all(|c| c.is_ascii_lowercase()) && !matches!(tag, "3d")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn maps_tags() {
        assert_eq!(
            from_tags(&["ru".into(), "forum".into(), "gaming".into()]),
            "gaming"
        );
        assert_eq!(from_tags(&["us".into()]), "misc");
        assert_eq!(from_tags(&["porn".into(), "social".into()]), "nsfw");
        assert_eq!(normalize("xx NSFW xx"), "nsfw");
        assert_eq!(normalize("Social"), "social");
        assert!(is_country_tag("ru"));
        assert!(!is_country_tag("art"));
    }
}