feat: add the newly added video to playlists if any

This commit is contained in:
VC
2023-10-14 22:45:08 +02:00
parent 8d4d683f55
commit ebc42f915d
5 changed files with 178 additions and 9 deletions

View File

@@ -87,6 +87,71 @@ impl Default for YoutubeUploadParamsStatus {
}
}
#[derive(Serialize, Debug)]
struct YoutubePlaylistItemsParams {
snippet: YoutubePlaylistItemsParamsSnippet,
}
#[derive(Serialize, Debug)]
struct YoutubePlaylistItemsParamsSnippet {
#[serde(rename = "playlistId")]
playlist_id: String,
position: u16,
#[serde(rename = "resourceId")]
resource_id: YoutubePlaylistItemsParamsSnippetResourceId,
}
impl Default for YoutubePlaylistItemsParamsSnippet {
fn default() -> Self {
YoutubePlaylistItemsParamsSnippet {
playlist_id: "".to_string(),
position: 0,
resource_id: YoutubePlaylistItemsParamsSnippetResourceId {
..Default::default()
},
}
}
}
#[derive(Serialize, Debug)]
struct YoutubePlaylistItemsParamsSnippetResourceId {
kind: String,
#[serde(rename = "videoId")]
video_id: String,
}
impl Default for YoutubePlaylistItemsParamsSnippetResourceId {
fn default() -> Self {
YoutubePlaylistItemsParamsSnippetResourceId {
kind: "youtube#video".to_string(),
video_id: "".to_string(),
}
}
}
#[derive(Deserialize, Debug)]
struct YoutubeVideos {
id: String,
}
#[derive(Deserialize, Debug)]
struct YoutubePlaylistListResponse {
#[serde(rename = "nextPageToken")]
next_page_token: Option<String>,
items: Vec<YoutubePlaylistListResponseItem>,
}
#[derive(Deserialize, Debug)]
struct YoutubePlaylistListResponseItem {
id: String,
snippet: YoutubePlaylistListResponseItemSnippet,
}
#[derive(Deserialize, Debug)]
struct YoutubePlaylistListResponseItemSnippet {
title: String,
}
/// This function makes the registration process a little bit easier
#[tokio::main]
pub async fn register(config: &YoutubeConfig) -> Result<(), Box<dyn Error>> {
@@ -148,6 +213,88 @@ async fn refresh_token(config: &YoutubeConfig) -> Result<String, reqwest::Error>
.cloned()
}
/// This function takes a list of playlists keyword and returns a list of playlist ID
async fn get_playlist_ids(
config: &YoutubeConfig,
pl: &[String],
) -> Result<Vec<String>, Box<dyn Error>> {
let mut page_token = String::new();
let mut playlists: Vec<String> = vec![];
let access_token = refresh_token(config).await?;
let client = Client::new();
while let Ok(local_pl) = client
.get(&format!(
"https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&pageToken={}",
page_token
))
.header("Authorization", format!("Bearer {}", access_token))
.send()
.await?
.json::<YoutubePlaylistListResponse>()
.await
{
playlists.append(
&mut local_pl
.items
.iter()
.filter_map(|s| pl.contains(&s.snippet.title).then_some(s.id.clone()))
.collect(),
);
// if nextPageToken is present, continue the loop
match local_pl.next_page_token {
None => break,
Some(a) => page_token = a.clone(),
}
}
Ok(playlists)
}
/// This function adds the video id to the corresponding named playlist(s)
pub async fn add_video_to_playlists(
config: &YoutubeConfig,
v: &str,
pl: &[String],
) -> Result<(), Box<dyn Error>> {
let access_token = refresh_token(config).await?;
let playlists_ids = get_playlist_ids(config, pl).await?;
for pl_id in playlists_ids {
let yt_pl_upload_params = YoutubePlaylistItemsParams {
snippet: YoutubePlaylistItemsParamsSnippet {
playlist_id: pl_id.clone(),
resource_id: YoutubePlaylistItemsParamsSnippetResourceId {
video_id: v.to_string(),
..Default::default()
},
..Default::default()
},
};
let client = Client::new();
let res = client
.post("https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet")
.header("Authorization", format!("Bearer {}", access_token))
.json(&yt_pl_upload_params)
.send()
.await?;
if !res.status().is_success() {
return Err(TootubeError::new(&format!(
"Something went wrong when trying to add the video to a playlist: {}",
res.text().await?
))
.into());
}
}
Ok(())
}
/// This function creates a resumable YT upload, putting all the parameters in
pub async fn create_resumable_upload(
config: &YoutubeConfig,
vid: &PeerTubeVideo,
@@ -171,7 +318,6 @@ pub async fn create_resumable_upload(
};
let client = Client::new();
let res = client.post("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus")
.header("Authorization", format!("Bearer {}", access_token))
.json(&upload_params)
@@ -189,6 +335,7 @@ pub async fn create_resumable_upload(
}
}
/// This takes the PT stream for download, connects it to YT stream for upload
pub async fn now_kiss<'a>(
stream: impl Stream<Item = Result<Bytes, reqwest::Error>>
+ std::marker::Send
@@ -196,7 +343,7 @@ pub async fn now_kiss<'a>(
+ 'a + 'static,
r_url: &'a str,
config: &'a YoutubeConfig,
) -> Result<(), Box<dyn Error>> {
) -> Result<String, Box<dyn Error>> {
// Get access token
let access_token = refresh_token(config).await?;
@@ -211,7 +358,8 @@ pub async fn now_kiss<'a>(
.await?;
if res.status().is_success() {
Ok(())
let yt_videos: YoutubeVideos = res.json().await?;
Ok(yt_videos.id)
} else {
Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text().await?)).into())
}