1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use super::Quality;
use reqwest;
use select::document::Document;
use select::predicate::{Class, Name};

const API_TEMPLATE: &'static str =
    "https://horriblesubs.info/api.php?method=getshows&type=show&showid=";

pub struct Fetcher<'a> {
    url: &'a str,
    quality: &'a Quality,
}

impl<'a> Fetcher<'a> {
    pub fn new(url: &'a str, quality: &'a Quality) -> Fetcher<'a> {
        Fetcher { url, quality }
    }

    pub fn get_show_id(&self) -> usize {
        println!("getting show id from {}", self.url);
        let resp = reqwest::get(self.url).unwrap();
        assert!(resp.status().is_success());
        let doc = Document::from_read(resp).unwrap();
        let scripts = doc.find(Name("script"));
        let id_code = scripts
            .filter(|n| n.attr("type").is_some())
            .filter(|n| n.attr("type").unwrap() == "text/javascript")
            .filter_map(|n| {
                if n.text().contains("var hs_showid =") {
                    Some(n.text())
                } else {
                    None
                }
            })
            .next()
            .unwrap();

        let id = id_code
            .trim_left_matches("var hs_showid =")
            .trim_right_matches(";")
            .trim();
        println!("got hs id: {}", id);

        id.parse::<usize>().unwrap()
    }

    pub fn get_mag_links(&self, id: usize) -> Vec<String> {
        let url = format!("{}{}&nextid=", API_TEMPLATE, id);
        let mut links = Vec::new();
        let mut next_id = 0;
        loop {
            let next_url = format!("{}{}", url, next_id);
            println!("fetching mag links from {}", next_url);

            let resp = reqwest::get(&next_url).unwrap();
            assert!(resp.status().is_success());

            let document = Document::from_read(resp).unwrap();

            let quality_selector = match self.quality {
                Quality::P480 => "link-480",
                Quality::P720 => "link-720p",
                Quality::P1080 => "link-1080p",
            };

            if let Some(body) = document.find(Name("body")).next() {
                if body.text() == "DONE" {
                    println!("DONE");
                    break;
                }
            }

            for node in document.find(Class("rls-links-container")) {
                if let Some(link_container) = node.find(Class(quality_selector)).next() {
                    if let Some(link_span) = link_container.find(Class("hs-magnet-link")).next() {
                        if let Some(link) = link_span.find(Name("a")).next() {
                            links.push(link.attr("href").unwrap().to_string());
                        }
                    }
                }
            }

            next_id += 1;
        }
        links
    }
}