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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
pub mod piped {
    use reqwest::{Client, Url};
    use serde::Deserialize;

    pub struct PipedClient {
        pub httpclient: Client,
        pub instance: String,
    }

    impl PipedClient {
        pub fn new(httpclient: Client, instance: String) -> PipedClient {
            PipedClient {
                httpclient: httpclient,
                instance: instance,
            }
        }

        pub async fn get_trending(&self) -> Result<Vec<RelatedStream>, Box<dyn std::error::Error>> {
            let resp = &self
                .httpclient
                .get(format!("{}/trending", &self.instance))
                .send()
                .await?
                .text()
                .await?;

            let streams: Vec<RelatedStream> = serde_json::from_str(resp.as_str()).unwrap();

            Ok(streams)
        }

        pub async fn get_channel_from_id(
            &self,
            id: String,
        ) -> Result<Channel, Box<dyn std::error::Error>> {
            let resp = &self
                .httpclient
                .get(format!("{}/channels/{}", &self.instance, id))
                .send()
                .await?
                .text()
                .await?;

            let channel: Channel = serde_json::from_str(resp.as_str()).unwrap();

            Ok(channel)
        }

        pub async fn get_channel_continuation(
            &self,
            id: String,
            nexturl: String,
            nextbody: String,
        ) -> Result<StreamsPage, Box<dyn std::error::Error>> {
            let mut url =
                Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id).as_str())
                    .unwrap();
            url.query_pairs_mut()
                .append_pair("url", nexturl.as_str())
                .append_pair("id", nextbody.as_str());

            let resp = &self
                .httpclient
                .get(url.as_str())
                .send()
                .await?
                .text()
                .await?;

            let streams: StreamsPage = serde_json::from_str(resp.as_str()).unwrap();

            Ok(streams)
        }

        pub async fn get_playlist_from_id(
            &self,
            id: String,
        ) -> Result<Playlist, Box<dyn std::error::Error>> {
            let resp = &self
                .httpclient
                .get(format!("{}/playlists/{}", &self.instance, id))
                .send()
                .await?
                .text()
                .await?;

            let playlist: Playlist = serde_json::from_str(resp.as_str()).unwrap();

            Ok(playlist)
        }

        pub async fn get_playlist_continuation(
            &self,
            id: String,
            nexturl: String,
            nextbody: String,
        ) -> Result<StreamsPage, Box<dyn std::error::Error>> {
            let mut url =
                Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id).as_str())
                    .unwrap();
            url.query_pairs_mut()
                .append_pair("url", nexturl.as_str())
                .append_pair("id", nextbody.as_str());

            let resp = &self
                .httpclient
                .get(url.as_str())
                .send()
                .await?
                .text()
                .await?;

            let streams: StreamsPage = serde_json::from_str(resp.as_str()).unwrap();

            Ok(streams)
        }
    }

    #[derive(Deserialize, Debug)]
    #[serde(rename_all = "camelCase")]
    pub struct Channel {
        pub id: String,
        pub name: String,
        pub avatar_url: String,
        pub banner_url: String,
        pub description: String,
        pub nextpage: Option<String>,
        pub nextbody: Option<String>,
        pub related_streams: Vec<RelatedStream>,
    }

    #[derive(Deserialize, Debug)]
    #[serde(rename_all = "camelCase")]
    pub struct Playlist {
        pub name: String,
        pub thumbnail_url: String,
        pub banner_url: Option<String>,
        pub uploader: String,
        pub uploader_url: String,
        pub uploader_avatar: String,
        pub videos: i32,
        pub nextpage: Option<String>,
        pub nextbody: Option<String>,
        pub related_streams: Vec<RelatedStream>,
    }

    #[derive(Deserialize, Debug)]
    #[serde(rename_all = "camelCase")]
    pub struct StreamsPage {
        pub nextpage: Option<String>,
        pub nextbody: Option<String>,
        pub related_streams: Vec<RelatedStream>,
    }

    #[derive(Deserialize, Debug)]
    #[serde(rename_all = "camelCase")]
    pub struct RelatedStream {
        pub url: String,
        pub title: String,
        pub thumbnail: String,
        pub uploader_name: String,
        pub uploader_url: String,
        pub uploaded_date: Option<String>,
        pub duration: i32,
        pub views: i64,
    }
}