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
use std::fmt::Debug;
use serde::{Serialize, Deserialize};
use crate::responses::*;
use crate::{TwitchApi, Result};

#[derive(Debug, Serialize, Deserialize)]
pub struct HelixVideo {
    pub id: String,
    pub user_id: String,
    pub user_name: String,
    pub title: String,
    pub description: String,
    pub created_at: String,
    pub published_at: String,
    pub url: String,
    pub thumbnail_url: String,
    pub viewable: String,
    pub view_count: i32,
    pub language: String,
    pub r#type: String,
    pub duration: String
}

impl super::traits::HelixModel for HelixVideo {}

pub async fn get(twitch_api: &TwitchApi, video_ids: &Vec<String>) -> Result<HelixResponse<HelixVideo>> {
    let mut data: Vec<(&str, String)> = vec![];

    for video_id in video_ids {
        data.push(("id", String::from(video_id)));
    }

    Ok(
        serde_json::from_str(
            &twitch_api.get(String::from("https://api.twitch.tv/helix/videos"), &data)
                .await?
                .text()
                .await?[..]
        )?
    )
}

pub async fn get_from_game(twitch_api: &TwitchApi, game_id: String, first: i32, after: Option<String>, before: Option<String>, language: Option<String>, period: Option<String>,
                           sort: Option<String>, r#type: Option<String>) -> Result<HelixPaginatedResponse<HelixVideo>> {
    let mut data: Vec<(&str, String)> = vec![
        ("game_id", String::from(&game_id[..])),
        ("first", first.to_string())
    ];

    if let Some(value) = after {
        data.push(("after", value));
    }

    if let Some(value) = before {
        data.push(("before", value));
    }

    if let Some(value) = language {
        data.push(("language", value));
    }

    if let Some(value) = period {
        data.push(("period", value));
    }

    if let Some(value) = sort {
        data.push(("sort", value));
    }

    if let Some(value) = r#type {
        data.push(("type", value));
    }

    Ok(
        serde_json::from_str(
            &twitch_api.get(String::from("https://api.twitch.tv/helix/videos"), &data)
                .await?
                .text()
                .await?[..]
        )?
    )
}

pub async fn get_from_user(twitch_api: &TwitchApi, user_id: String, first: i32, after: Option<String>, before: Option<String>, language: Option<String>, period: Option<String>,
                           sort: Option<String>, r#type: Option<String>) -> Result<HelixPaginatedResponse<HelixVideo>> {
    let mut data: Vec<(&str, String)> = vec![
        ("user_id", String::from(&user_id[..])),
        ("first", first.to_string())
    ];

    if let Some(value) = after {
        data.push(("after", value));
    }

    if let Some(value) = before {
        data.push(("before", value));
    }

    if let Some(value) = language {
        data.push(("language", value));
    }

    if let Some(value) = period {
        data.push(("period", value));
    }

    if let Some(value) = sort {
        data.push(("sort", value));
    }

    if let Some(value) = r#type {
        data.push(("type", value));
    }

    Ok(
        serde_json::from_str(
            &twitch_api.get(String::from("https://api.twitch.tv/helix/videos"), &data)
                .await?
                .text()
                .await?[..]
        )?
    )
}