Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::error::Error;
- use std::fs::File;
- use std::io::{BufReader, Read, Write};
- use std::path::{MAIN_SEPARATOR, PathBuf};
- extern crate hyper;
- use hyper::client::Client;
- use hyper::client::response::Response;
- use hyper::header::UserAgent;
- extern crate rss;
- use rss::Channel;
- const EXTENSION: &'static str = "torrent";
- const USER_AGENT: &'static str = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19";
- const PATH: &'static str = "/tmp";
- const URLS: [&'static str; 1] = [
- "https://www.nyaa.se/?page=rss&term=horriblesubs+hibike+euphonium+s2+1080",
- ];
- fn get(client: &Client, url: &str) -> Result<Response, hyper::Error> {
- client.get(url).header(UserAgent(USER_AGENT.to_string())).send()
- }
- fn save(data: &[u8], name: &str) -> Result<(), Box<Error>> {
- let name = name.replace(MAIN_SEPARATOR, "-");
- let mut buf = PathBuf::from(PATH);
- buf.push(&name);
- buf.set_extension(EXTENSION);
- let path = buf.as_path();
- if !path.exists() {
- let mut file = File::create(path)?;
- file.write(data)?;
- }
- Ok(())
- }
- fn torrent(client: &Client, url: Option<String>, name: Option<String>) -> Result<(), Box<Error>> {
- let url = url.ok_or("no url")?;
- let name = name.ok_or("no name")?;
- let mut response = get(client, &url)?;
- let mut body = vec![];
- response.read_to_end(&mut body)?;
- save(&body, &name)
- }
- fn feed(client: &Client, url: &str) -> Result<(), Box<Error>> {
- let response = get(client, url)?;
- let reader = BufReader::new(response);
- let channel = Channel::read_from(reader)?;
- for item in channel.items {
- if let Err(e) = torrent(client, item.link.clone(), item.title.clone()) {
- println!("{} {}", item.link.unwrap_or("item".to_string()), e);
- }
- }
- Ok(())
- }
- fn main() {
- let client = Client::new();
- for url in URLS.iter() {
- if let Err(e) = feed(&client, url) {
- println!("{} {}", url, e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement