• rust下载文件


    背景

    最近晚上给娃听故事

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

    动手

    1. 整理下载链接

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

    1. 编码
    • 解析json
    1. use serde::{Deserialize,Serialize};
    2. use serde_json;
    3. #[derive(Debug,Deserialize,Serialize)]
    4. struct Audio {
    5. title:String,
    6. src:String,
    7. }
    8. fn main() {
    9. println!("download mp4!");
    10. let json_urls = r#"[
    11. {"title":"第一辑 吃饭","src":"http://tcv.clewm.net"},
    12. {"title":"第一辑.大声回应","src":"http://tcv.clewm.net"},
    13. {"title":"第一辑.好朋友","src":"http://tcv.clewm.net"},
    14. {"title":"第一辑.拉粑粑","src":"http://tcv.clewm.net"},
    15. {"title":"第一辑.你好","src":"http://tcv.clewm.net"},
    16. {"title":"第一辑.尿床了","src":"http://tcv.clewm.net"},
    17. {"title":"第一辑.刷牙","src":"http://tcv.clewm.net"},
    18. {"title":"第一辑.谁哭了","src":"http://tcv.clewm.net"},
    19. {"title":"第一辑.睡觉","src":"http://tcv.clewm.net"},
    20. {"title":"第一辑.洗澡","src":"http://tcv.clewm.net"},
    21. {"title":"第二辑.对不起","src":"http://tcv.clewm.net"},
    22. {"title":"第二辑.多吃蔬菜","src":"http://tcv.clewm.net"},
    23. {"title":"第二辑.多吃水果","src":"http://tcv.clewm.net"},
    24. {"title":"第二辑.过生日","src":"http://tcv.clewm.net"},
    25. {"title":"第二辑.排好队","src":"http://tcv.clewm.net"},
    26. {"title":"第二辑.散步","src":"http://tcv.clewm.net"},
    27. {"title":"第二辑.收起来","src":"http://tcv.clewm.net"},
    28. {"title":"第二辑.我会穿鞋子","src":"http://tcv.clewm.net"},
    29. {"title":"第二辑.我会穿衣服","src":"http://tcv.clewm.net"},
    30. {"title":"第二辑.有危险","src":"http://tcv.clewm.net"},
    31. {"title":"第三辑.不赖床","src":"http://tcv.clewm.net"},
    32. {"title":"第三辑.插座上的洞洞","src":"http://tcv.clewm.net"},
    33. {"title":"第三辑.地上的东西是糖吗","src":"http://tcv.clewm.net"},
    34. {"title":"第三辑.好烫的水","src":"http://tcv.clewm.net"},
    35. {"title":"第三辑.客人来我家","src":"http://tcv.clewm.net"},
    36. {"title":"第三辑.妈妈不见了","src":"http://tcv.clewm.net"},
    37. {"title":"第三辑.勤洗手","src":"http://tcv.clewm.net"},
    38. {"title":"第三辑.讨厌的桌角","src":"http://tcv.clewm.net"},
    39. {"title":"第三辑.谢谢","src":"http://tcv.clewm.net"},
    40. {"title":"第三辑.站在椅子上","src":"http://tcv.clewm.net"}
    41. ]"#;
    42. let audios = serde_json::from_str::<Vec
    • 下载文件
    1. use std::fs::File;
    2. use std::path::Path;
    3. use std::io::prelude::*;
    4. use reqwest;
    5. async fn download(audio:Audio)->std::result::Result<(),Box<dyn std::error::Error>>{
    6. println!("downloading {}",audio.title);
    7. let client = reqwest::Client::new();
    8. let body = client.get(&audio.src).send().await.unwrap().bytes().await.unwrap();
    9. let title = String::from("./")+&audio.title+".mp4";
    10. let path = Path::new(&title);
    11. let mut file = File::create(&path).unwrap();
    12. let content = body.bytes();
    13. let data:std::result::Result<Vec<_>,_> = content.collect();
    14. file.write_all(&data.unwrap()).unwrap();
    15. println!("downloaded {}",audio.title);
    16. Ok(())
    17. }
    • 完整代码
    1. use serde::{Deserialize,Serialize};
    2. use serde_json;
    3. use std::fs::File;
    4. use std::path::Path;
    5. use std::io::prelude::*;
    6. use reqwest;
    7. #[derive(Debug,Deserialize,Serialize)]
    8. struct Audio {
    9. title:String,
    10. src:String,
    11. }
    12. async fn download(audio:Audio)->std::result::Result<(),Box<dyn std::error::Error>>{
    13. println!("downloading {}",audio.title);
    14. let client = reqwest::Client::new();
    15. let body = client.get(&audio.src).send().await.unwrap().bytes().await.unwrap();
    16. let title = String::from("./")+&audio.title+".mp4";
    17. let path = Path::new(&title);
    18. let mut file = File::create(&path).unwrap();
    19. let content = body.bytes();
    20. let data:std::result::Result<Vec<_>,_> = content.collect();
    21. file.write_all(&data.unwrap()).unwrap();
    22. println!("downloaded {}",audio.title);
    23. Ok(())
    24. }
    25. #[tokio::main]
    26. async fn main() {
    27. println!("download mp4!");
    28. let json_urls = r#"[
    29. {"title":"第一辑 吃饭","src":"http://tcv.clewm.net"},
    30. {"title":"第一辑.大声回应","src":"http://tcv.clewm.net"},
    31. {"title":"第一辑.好朋友","src":"http://tcv.clewm.net"},
    32. {"title":"第一辑.拉粑粑","src":"http://tcv.clewm.net"},
    33. {"title":"第一辑.你好","src":"http://tcv.clewm.net"},
    34. {"title":"第一辑.尿床了","src":"http://tcv.clewm.net"},
    35. {"title":"第一辑.刷牙","src":"http://tcv.clewm.net"},
    36. {"title":"第一辑.谁哭了","src":"http://tcv.clewm.net"},
    37. {"title":"第一辑.睡觉","src":"http://tcv.clewm.net"},
    38. {"title":"第一辑.洗澡","src":"http://tcv.clewm.net"},
    39. {"title":"第二辑.对不起","src":"http://tcv.clewm.net"},
    40. {"title":"第二辑.多吃蔬菜","src":"http://tcv.clewm.net"},
    41. {"title":"第二辑.多吃水果","src":"http://tcv.clewm.net"},
    42. {"title":"第二辑.过生日","src":"http://tcv.clewm.net"},
    43. {"title":"第二辑.排好队","src":"http://tcv.clewm.net"},
    44. {"title":"第二辑.散步","src":"http://tcv.clewm.net"},
    45. {"title":"第二辑.收起来","src":"http://tcv.clewm.net"},
    46. {"title":"第二辑.我会穿鞋子","src":"http://tcv.clewm.net"},
    47. {"title":"第二辑.我会穿衣服","src":"http://tcv.clewm.net"},
    48. {"title":"第二辑.有危险","src":"http://tcv.clewm.net"},
    49. {"title":"第三辑.不赖床","src":"http://tcv.clewm.net"},
    50. {"title":"第三辑.插座上的洞洞","src":"http://tcv.clewm.net"},
    51. {"title":"第三辑.地上的东西是糖吗","src":"http://tcv.clewm.net"},
    52. {"title":"第三辑.好烫的水","src":"http://tcv.clewm.net"},
    53. {"title":"第三辑.客人来我家","src":"http://tcv.clewm.net"},
    54. {"title":"第三辑.妈妈不见了","src":"http://tcv.clewm.net"},
    55. {"title":"第三辑.勤洗手","src":"http://tcv.clewm.net"},
    56. {"title":"第三辑.讨厌的桌角","src":"http://tcv.clewm.net"},
    57. {"title":"第三辑.谢谢","src":"http://tcv.clewm.net"},
    58. {"title":"第三辑.站在椅子上","src":"http://tcv.clewm.net"}
    59. ]"#;
    60. let audios = serde_json::from_str::<Vec
    61. for audio in audios {
    62. let _ = download(audio).await;
    63. }
    64. }
    • 结果

  • 相关阅读:
    【冰糖Python】Python 中的 assert 语句
    【附源码】计算机毕业设计JAVA政府人才机构在线考试系统2021
    对话框管理器第四章:对话框消息循环
    SpringBoot + 自定义注解 + AOP 高级玩法打造通用开关
    Linux下修改jar包中的配置文件application.conf
    Java01-JDK1.8下载安装教程(win11版)
    HTML5的基础知识的梳理及常用标签
    Windows、Linux应急响应大致流程
    Modbus协议
    洛谷P6075 子集选取
  • 原文地址:https://blog.csdn.net/hbh112233abc/article/details/136230931