Skip to main content

scrapr_bindings/
protocols.rs

1use crate::request::*;
2use pyo3::prelude::*;
3
4#[pyfunction]
5pub fn fetch_http(host: &str, path: &str) -> PyResult<String> {
6    scrapr_core::fetch_http(host, path)
7        .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
8}
9
10#[pyfunction]
11pub fn fetch_http_with_options(
12    host: &str,
13    path: &str,
14    options: RequestOptions,
15) -> PyResult<String> {
16    let core_options = scrapr_core::RequestOptions {
17        headers: options.headers,
18        cookies: options.cookies,
19        query: options.query,
20    };
21
22    scrapr_core::fetch_http_with_options(host, path, core_options)
23        .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
24}
25
26#[pyfunction]
27pub fn extract_attribute(text: &str, tag: &str, attr: &str) -> PyResult<Vec<String>> {
28    scrapr_core::extract_attribute(text, tag, attr)
29        .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
30}
31
32#[pyfunction]
33pub fn extract_links(text: &str) -> PyResult<Vec<String>> {
34    extract_attribute(text, "a", "href")
35}
36
37// pub fn fetch_http_(host: &str, path: &str) -> PyResult<String> {
38//     let mut stream = TcpStream::connect((host, 80))
39//         .map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))?;
40
41//     stream.set_read_timeout(Some(std::time::Duration::from_secs(5)))
42//         .map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))?;
43//     stream.set_write_timeout(Some(std::time::Duration::from_secs(5)))
44//         .map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))?;
45
46//     let request = format!("GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n");
47
48//     stream.write_all(request.as_bytes())
49//         .map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))?;
50
51//     let mut response = String::new();
52//     stream.read_to_string(&mut response)
53//         .map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))?;
54
55//     // Check HTTP status
56//     let status_line = response.lines().next().unwrap_or_default();
57//     if !status_line.contains("200 OK") {
58//         return Err(PyErr::new::<pyo3::exceptions::PyException, _>(format!("HTTP error: {}", status_line)));
59//     }
60
61//     if let Some(body) = response.split("\r\n\r\n").nth(1) {
62//         Ok(body.to_string())
63//     } else {
64//         Err(PyErr::new::<pyo3::exceptions::PyException, _>("No body found"))
65//     }
66// }
67
68#[pyfunction]
69pub fn extract_tag(text: &str, tag: &str) -> PyResult<Vec<String>> {
70    scrapr_core::extract_tag(text, tag)
71        .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
72}
73
74#[pyfunction]
75pub fn fetch_https(host: &str, path: &str) -> PyResult<String> {
76    scrapr_core::fetch_https(host, path)
77        .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
78}
79
80#[pyfunction]
81pub fn fetch_https_with_options(
82    host: &str,
83    path: &str,
84    options: RequestOptions,
85) -> PyResult<String> {
86    let core_options = scrapr_core::RequestOptions {
87        headers: options.headers,
88        cookies: options.cookies,
89        query: options.query,
90    };
91
92    scrapr_core::fetch_https_with_options(host, path, core_options)
93        .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
94}