Giter Club home page Giter Club logo

data_downloader's Introduction

data_downloader

data_downloader crate data_downloader documentation MIT/Apache-2 licensed

This crate provides a simple way to download files. In particular this crate aims to make it easy to download and cache files that do not change over time, for example reference image files, ML models, example audio files or common password lists.

use data_downloader::{get, DownloadRequest};
use std::collections::HashSet;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define where to get the file from
    let rfc_link = &DownloadRequest {
        url: "https://www.rfc-editor.org/rfc/rfc2068.txt",
        sha256_hash: &hex_literal::hex!(
            "D6C4E471389F2D309AB1F90881576542C742F95B115336A346447D052E0477CF"
        ),
    };

    // Get the binary contents of the file
    let rfc: Vec<u8> = get(rfc_link)?;

    // Convert the file to a String
    let as_text = String::from_utf8(rfc)?;
    assert!(as_text.contains("The Hypertext Transfer Protocol (HTTP) is an application-level"));
    assert!(as_text.contains("protocol"));


    // There are also some handy built-in files 
    let rockyou_txt = get(data_downloader::files::misc::ROCKYOU_TXT)?;
    let pws: HashSet<&[u8]> = rockyou_txt.split(|e| *e == b'\n').collect();
    assert!(pws.contains(&b"hello".as_slice()));
    assert!(pws.contains(&b"goodpassword".as_slice()));
    assert!(!pws.contains(&b"correcthorsebatterystaple".as_slice()));

    Ok(())
}

Have a look at the docs for more examples.

Alternatives

If you need to download files that might change over time or where you do not know the SHA-256 in advance consider using cached-path.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

data_downloader's People

Contributors

tillarnold avatar

Stargazers

 avatar

Watchers

 avatar  avatar

data_downloader's Issues

Add expected_size

  • Add an expected_size: Optional<u64> size to DownloadRequests
    • If the download is bigger than that fail
    • If it is None no upper limit

Expand API

  • Add a feature to unpack all files in a zip
    • add an example for this
  • Add delete
  • Use multiple levels of directories
    • Create a TempFile struct, that on drop deletes itself and tries to clean up the parent directories if they are empty

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.