Skip to content

Instantly share code, notes, and snippets.

@sheepla
Created December 14, 2024 14:05
Show Gist options
  • Save sheepla/7819a81a008c0a5b5df5232b2c06e805 to your computer and use it in GitHub Desktop.
Save sheepla/7819a81a008c0a5b5df5232b2c06e805 to your computer and use it in GitHub Desktop.
Fetch AUR page and extract PKGBILD and other filees diff info in Rust (reqwest + select-rs)
use eyre::OptionExt;
use reqwest::Url;
use select::{
document::Document,
predicate::{Class, Name, Predicate},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct DiffInfo {
pub summary: String,
pub content: String,
}
pub async fn fetch_pkgbuild_diffinfo(package_name: &str) -> eyre::Result<DiffInfo> {
let mut url = Url::parse("https://aur.archlinux.org/cgit/aur.git/diff/")?;
{
url.query_pairs_mut().append_pair("h", package_name);
}
let response = reqwest::get(url).await?;
let text = response.text().await?;
let document = Document::from(text.as_str());
Ok(DiffInfo {
summary: document
.find(Class("diffstat-summary"))
.next()
.map(|node| node.text())
.ok_or_eyre("summary node text was not found")?,
content: document
.find(
Class("diff").descendant(Name("tr").descendant(Name("td")).descendant(Name("div"))),
)
.map(|node| node.text())
.collect::<Vec<_>>()
.join("\n"),
})
}
@sheepla
Copy link
Author

sheepla commented Dec 15, 2024

Hmm, I want to parse by bytes stream from reqwest::Response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment