pineapple_data/
request.rs

1// Copyright (c) 2025, Tom Ouellette
2// Licensed under the BSD 3-Clause License
3
4use anyhow::{Context, Result, anyhow};
5use kdam::BarExt;
6use reqwest::{Client, redirect::Policy};
7use scraper::{Html, Selector};
8use std::path::Path;
9use tokio::fs::File;
10use tokio::io::AsyncWriteExt;
11
12use pineapple_core::ut::track::{progress_bar, progress_log};
13
14/// Download a file from Google drive
15///
16/// # Arguments
17///
18/// * `file_id` - Unique google drive file identifier
19/// * `output_dir` - Directory to download file to
20/// * `filename` - Filename of downloaded file
21/// * `silent` - Turn off download messages
22#[tokio::main]
23pub async fn download_file(
24    file_id: &str,
25    output_dir: &Path,
26    filename: &str,
27    silent: bool,
28) -> Result<()> {
29    let client = create_http_client()?;
30    let initial_url = format!("https://drive.google.com/uc?id={}&export=download", file_id);
31    let download_url = handle_virus_scan_warning(&client, &initial_url).await?;
32    download_file_with_progress(&client, &download_url, output_dir, filename, silent).await?;
33    if !silent {
34        println!();
35    }
36    progress_log("Complete", !silent);
37    Ok(())
38}
39
40fn create_http_client() -> Result<Client> {
41    Client::builder()
42        .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
43        .cookie_store(true)
44        .redirect(Policy::limited(10))
45        .build()
46        .context("Failed to create HTTP client")
47}
48
49/// Allows for passing Google drive virus scan warning
50async fn handle_virus_scan_warning(client: &Client, url: &str) -> Result<String> {
51    let resp = client
52        .get(url)
53        .send()
54        .await
55        .context("Failed to send initial request")?;
56
57    let body = resp.text().await.context("Failed to get response body")?;
58
59    if body.contains("Google Drive can't scan this file for viruses") {
60        extract_download_url(&body)
61    } else {
62        Ok(url.to_string())
63    }
64}
65
66fn extract_download_url(body: &str) -> Result<String> {
67    let document = Html::parse_document(body);
68    let form_selector = Selector::parse("form#download-form").unwrap();
69    let input_selector = Selector::parse("input[name]").unwrap();
70
71    let form = document
72        .select(&form_selector)
73        .next()
74        .ok_or_else(|| anyhow!("Download form not found in the HTML"))?;
75
76    let action = form
77        .value()
78        .attr("action")
79        .ok_or_else(|| anyhow!("No form action found"))?;
80
81    let params: Vec<(String, String)> = form
82        .select(&input_selector)
83        .filter_map(
84            |input| match (input.value().attr("name"), input.value().attr("value")) {
85                (Some(name), Some(value)) => Some((name.to_string(), value.to_string())),
86                _ => None,
87            },
88        )
89        .collect();
90
91    let query_string = params
92        .into_iter()
93        .map(|(name, value)| format!("{}={}", name, value))
94        .collect::<Vec<_>>()
95        .join("&");
96
97    Ok(format!("{}?{}", action, query_string))
98}
99
100async fn download_file_with_progress(
101    client: &Client,
102    url: &str,
103    output_dir: &Path,
104    filename: &str,
105    silent: bool,
106) -> Result<()> {
107    let mut resp = client
108        .get(url)
109        .send()
110        .await
111        .context("Failed to send download request")?;
112
113    let total_size = resp.content_length().unwrap_or(0);
114    let mut pb = progress_bar(
115        total_size as usize,
116        format!("Downloading {}", filename).as_str(),
117        !silent,
118    );
119
120    let total_gigabytes = total_size as f64 / 1e9;
121
122    if !silent {
123        progress_log(
124            format!("Starting {} download ({:.2} GB)", filename, total_gigabytes).as_str(),
125            !silent,
126        );
127    }
128
129    if total_gigabytes == 0.0 {
130        println!("Download could not be started. Please check connection and try again.");
131        std::process::exit(1);
132    }
133
134    tokio::fs::create_dir_all(output_dir)
135        .await
136        .context("Failed to create output directory")?;
137    let filepath = output_dir.join(filename);
138    let mut file = File::create(&filepath)
139        .await
140        .context("Failed to create output file")?;
141
142    while let Some(chunk) = resp.chunk().await.context("Failed to read chunk")? {
143        file.write_all(&chunk)
144            .await
145            .context("Failed to write chunk to file")?;
146
147        if !silent {
148            pb.update(chunk.len())?;
149        }
150    }
151
152    Ok(())
153}