rust下载文件

背景

最近晚上给娃听故事

这页面不能连续播放,想着下载下来用默认播放器播放好了, 刚好最近在学rust,就用rust实现mp4下载.

动手

  1. 整理下载链接

由于链接可能涉及不必要的纠纷就先不明文列出来了

  1. 编码
  • 解析json
rust 复制代码
use serde::{Deserialize,Serialize};
use serde_json;


#[derive(Debug,Deserialize,Serialize)]
struct Audio {
    title:String,
    src:String,
}

fn main() {
    println!("download mp4!");
    let json_urls = r#"[
        {"title":"第一辑 吃饭","src":"http://tcv.clewm.net"},
        {"title":"第一辑.大声回应","src":"http://tcv.clewm.net"},
        {"title":"第一辑.好朋友","src":"http://tcv.clewm.net"},
        {"title":"第一辑.拉粑粑","src":"http://tcv.clewm.net"},
        {"title":"第一辑.你好","src":"http://tcv.clewm.net"},
        {"title":"第一辑.尿床了","src":"http://tcv.clewm.net"},
        {"title":"第一辑.刷牙","src":"http://tcv.clewm.net"},
        {"title":"第一辑.谁哭了","src":"http://tcv.clewm.net"},
        {"title":"第一辑.睡觉","src":"http://tcv.clewm.net"},
        {"title":"第一辑.洗澡","src":"http://tcv.clewm.net"},
        {"title":"第二辑.对不起","src":"http://tcv.clewm.net"},
        {"title":"第二辑.多吃蔬菜","src":"http://tcv.clewm.net"},
        {"title":"第二辑.多吃水果","src":"http://tcv.clewm.net"},
        {"title":"第二辑.过生日","src":"http://tcv.clewm.net"},
        {"title":"第二辑.排好队","src":"http://tcv.clewm.net"},
        {"title":"第二辑.散步","src":"http://tcv.clewm.net"},
        {"title":"第二辑.收起来","src":"http://tcv.clewm.net"},
        {"title":"第二辑.我会穿鞋子","src":"http://tcv.clewm.net"},
        {"title":"第二辑.我会穿衣服","src":"http://tcv.clewm.net"},
        {"title":"第二辑.有危险","src":"http://tcv.clewm.net"},
        {"title":"第三辑.不赖床","src":"http://tcv.clewm.net"},
        {"title":"第三辑.插座上的洞洞","src":"http://tcv.clewm.net"},
        {"title":"第三辑.地上的东西是糖吗","src":"http://tcv.clewm.net"},
        {"title":"第三辑.好烫的水","src":"http://tcv.clewm.net"},
        {"title":"第三辑.客人来我家","src":"http://tcv.clewm.net"},
        {"title":"第三辑.妈妈不见了","src":"http://tcv.clewm.net"},
        {"title":"第三辑.勤洗手","src":"http://tcv.clewm.net"},
        {"title":"第三辑.讨厌的桌角","src":"http://tcv.clewm.net"},
        {"title":"第三辑.谢谢","src":"http://tcv.clewm.net"},
        {"title":"第三辑.站在椅子上","src":"http://tcv.clewm.net"}
    ]"#;

    let audios = serde_json::from_str::<Vec<Audio>>(json_urls).unwrap();
  • 下载文件
rust 复制代码
use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
use reqwest;


async fn download(audio:Audio)->std::result::Result<(),Box<dyn std::error::Error>>{
    println!("downloading {}",audio.title);
    let client = reqwest::Client::new();
    let body = client.get(&audio.src).send().await.unwrap().bytes().await.unwrap();
    let title = String::from("./")+&audio.title+".mp4";
    let path = Path::new(&title);
    let mut file = File::create(&path).unwrap();
    let content = body.bytes();
    let data:std::result::Result<Vec<_>,_> = content.collect();
    file.write_all(&data.unwrap()).unwrap();

    println!("downloaded {}",audio.title);
    Ok(())
}
  • 完整代码
rust 复制代码
use serde::{Deserialize,Serialize};
use serde_json;

use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
use reqwest;

#[derive(Debug,Deserialize,Serialize)]
struct Audio {
    title:String,
    src:String,
}


async fn download(audio:Audio)->std::result::Result<(),Box<dyn std::error::Error>>{
    println!("downloading {}",audio.title);
    let client = reqwest::Client::new();
    let body = client.get(&audio.src).send().await.unwrap().bytes().await.unwrap();
    let title = String::from("./")+&audio.title+".mp4";
    let path = Path::new(&title);
    let mut file = File::create(&path).unwrap();
    let content = body.bytes();
    let data:std::result::Result<Vec<_>,_> = content.collect();
    file.write_all(&data.unwrap()).unwrap();

    println!("downloaded {}",audio.title);
    Ok(())
}
#[tokio::main]
async fn main() {
    println!("download mp4!");
    let json_urls = r#"[
        {"title":"第一辑 吃饭","src":"http://tcv.clewm.net"},
        {"title":"第一辑.大声回应","src":"http://tcv.clewm.net"},
        {"title":"第一辑.好朋友","src":"http://tcv.clewm.net"},
        {"title":"第一辑.拉粑粑","src":"http://tcv.clewm.net"},
        {"title":"第一辑.你好","src":"http://tcv.clewm.net"},
        {"title":"第一辑.尿床了","src":"http://tcv.clewm.net"},
        {"title":"第一辑.刷牙","src":"http://tcv.clewm.net"},
        {"title":"第一辑.谁哭了","src":"http://tcv.clewm.net"},
        {"title":"第一辑.睡觉","src":"http://tcv.clewm.net"},
        {"title":"第一辑.洗澡","src":"http://tcv.clewm.net"},
        {"title":"第二辑.对不起","src":"http://tcv.clewm.net"},
        {"title":"第二辑.多吃蔬菜","src":"http://tcv.clewm.net"},
        {"title":"第二辑.多吃水果","src":"http://tcv.clewm.net"},
        {"title":"第二辑.过生日","src":"http://tcv.clewm.net"},
        {"title":"第二辑.排好队","src":"http://tcv.clewm.net"},
        {"title":"第二辑.散步","src":"http://tcv.clewm.net"},
        {"title":"第二辑.收起来","src":"http://tcv.clewm.net"},
        {"title":"第二辑.我会穿鞋子","src":"http://tcv.clewm.net"},
        {"title":"第二辑.我会穿衣服","src":"http://tcv.clewm.net"},
        {"title":"第二辑.有危险","src":"http://tcv.clewm.net"},
        {"title":"第三辑.不赖床","src":"http://tcv.clewm.net"},
        {"title":"第三辑.插座上的洞洞","src":"http://tcv.clewm.net"},
        {"title":"第三辑.地上的东西是糖吗","src":"http://tcv.clewm.net"},
        {"title":"第三辑.好烫的水","src":"http://tcv.clewm.net"},
        {"title":"第三辑.客人来我家","src":"http://tcv.clewm.net"},
        {"title":"第三辑.妈妈不见了","src":"http://tcv.clewm.net"},
        {"title":"第三辑.勤洗手","src":"http://tcv.clewm.net"},
        {"title":"第三辑.讨厌的桌角","src":"http://tcv.clewm.net"},
        {"title":"第三辑.谢谢","src":"http://tcv.clewm.net"},
        {"title":"第三辑.站在椅子上","src":"http://tcv.clewm.net"}
    ]"#;

    let audios = serde_json::from_str::<Vec<Audio>>(json_urls).unwrap();

    for audio in audios {
        let _ = download(audio).await;
    }
}
  • 结果

相关推荐
喵叔哟8 分钟前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构
尘浮生14 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
hopetomorrow28 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
郑祎亦37 分钟前
Spring Boot 项目 myblog 整理
spring boot·后端·java-ee·maven·mybatis
小牛itbull37 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
请叫我欧皇i1 小时前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落1 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
本当迷ya1 小时前
💖2025年不会Stream流被同事排挤了┭┮﹏┭┮(强烈建议实操)
后端·程序员
GIS瞧葩菜1 小时前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
chnming19871 小时前
STL关联式容器之set
开发语言·c++