Compare commits
No commits in common. "469991d20b8b4a30efaf6e07216b015ee7ae6768" and "3066753c69efd14f2fd388d4c79ffead6e24c21c" have entirely different histories.
469991d20b
...
3066753c69
|
@ -1,19 +0,0 @@
|
||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="Run all tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
|
|
||||||
<option name="command" value="test" />
|
|
||||||
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
|
|
||||||
<option name="channel" value="DEFAULT" />
|
|
||||||
<option name="requiredFeatures" value="true" />
|
|
||||||
<option name="allFeatures" value="false" />
|
|
||||||
<option name="emulateTerminal" value="false" />
|
|
||||||
<option name="withSudo" value="false" />
|
|
||||||
<option name="buildTarget" value="REMOTE" />
|
|
||||||
<option name="backtrace" value="SHORT" />
|
|
||||||
<envs />
|
|
||||||
<option name="isRedirectInput" value="false" />
|
|
||||||
<option name="redirectInputPath" value="" />
|
|
||||||
<method v="2">
|
|
||||||
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
|
|
||||||
</method>
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
|
@ -12,8 +12,3 @@ pretty_env_logger = "0.4.0"
|
||||||
tokio = { version = "1.18.2", features = ["rt-multi-thread", "macros"] }
|
tokio = { version = "1.18.2", features = ["rt-multi-thread", "macros"] }
|
||||||
rspotify = { version = "0.11.5", features = ["default"]}
|
rspotify = { version = "0.11.5", features = ["default"]}
|
||||||
sentry = "0.26.0"
|
sentry = "0.26.0"
|
||||||
invidious = "0.2.1"
|
|
||||||
chrono = "0.4.19"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
tokio-test = "0.4.2"
|
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
use crate::{spotify, youtube};
|
|
||||||
use chrono::{Date, Utc};
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
pub(crate) enum MusicData {
|
|
||||||
Track(Track),
|
|
||||||
Album(Album),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Track {
|
|
||||||
name: String,
|
|
||||||
authors: Vec<Author>,
|
|
||||||
duration: Duration,
|
|
||||||
album: Vec<Album>,
|
|
||||||
description: String,
|
|
||||||
lyrics: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Album {
|
|
||||||
name: String,
|
|
||||||
authors: Vec<Author>,
|
|
||||||
description: String,
|
|
||||||
year: Date<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Author {
|
|
||||||
name: String,
|
|
||||||
surname: String,
|
|
||||||
date_of_birth: Date<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// The enum holds all the currently supported type of Id which the engine can search for
|
|
||||||
pub(crate) enum ServiceIdKind {
|
|
||||||
Spotify(String),
|
|
||||||
Youtube(String),
|
|
||||||
Automatic(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
// This struct will allow us in the future to search, cache and store data and metadata regarding
|
|
||||||
// tracks, albums and playlists
|
|
||||||
pub(crate) struct MusicEngine {
|
|
||||||
spotify: spotify::Client,
|
|
||||||
youtube: youtube::Client,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MusicEngine {
|
|
||||||
pub(crate) async fn new() -> Self {
|
|
||||||
MusicEngine {
|
|
||||||
spotify: spotify::Client::new().await,
|
|
||||||
youtube: youtube::Client::new().await,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn new_with_dependencies(
|
|
||||||
spotify_client: spotify::Client,
|
|
||||||
youtube_client: youtube::Client,
|
|
||||||
) -> Self {
|
|
||||||
MusicEngine {
|
|
||||||
spotify: spotify_client,
|
|
||||||
youtube: youtube_client,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn search_by_name(&self, name: &str) {
|
|
||||||
todo!("In the future it would be possible to search for all metadata on a record from this call")
|
|
||||||
}
|
|
||||||
pub(crate) async fn search_by_id(&self, id: ServiceIdKind) {
|
|
||||||
match id {
|
|
||||||
ServiceIdKind::Spotify(id) => {
|
|
||||||
let entry_kind = spotify::get_entry_kind(id.as_str());
|
|
||||||
}
|
|
||||||
ServiceIdKind::Youtube(id) => {}
|
|
||||||
ServiceIdKind::Automatic(id) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
39
src/main.rs
39
src/main.rs
|
@ -9,10 +9,8 @@ use spotify::SpotifyKind::Track;
|
||||||
use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast};
|
use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast};
|
||||||
use crate::utils::{human_readable_duration, truncate_with_dots};
|
use crate::utils::{human_readable_duration, truncate_with_dots};
|
||||||
|
|
||||||
mod engine;
|
|
||||||
mod spotify;
|
mod spotify;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod youtube;
|
|
||||||
|
|
||||||
static MAX_ARTISTS_CHARS: usize = 140;
|
static MAX_ARTISTS_CHARS: usize = 140;
|
||||||
|
|
||||||
|
@ -51,11 +49,11 @@ async fn main() {
|
||||||
let text = message.text().and_then(spotify::get_entry_kind);
|
let text = message.text().and_then(spotify::get_entry_kind);
|
||||||
match text {
|
match text {
|
||||||
Some(spotify) => {
|
Some(spotify) => {
|
||||||
let spotify_client = spotify::Client::new().await;
|
let spotify_client = spotify::get_client().await;
|
||||||
match spotify {
|
match spotify {
|
||||||
Track(id) => {
|
Track(id) => {
|
||||||
log::debug!("Parsing spotify song: {}", id);
|
log::debug!("Parsing spotify song: {}", id);
|
||||||
let track_info = spotify_client.get_track(&id).await;
|
let track_info = spotify::get_track(spotify_client, &id).await;
|
||||||
match track_info {
|
match track_info {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
let reply = format!(
|
let reply = format!(
|
||||||
|
@ -77,7 +75,7 @@ async fn main() {
|
||||||
}
|
}
|
||||||
Album(id) => {
|
Album(id) => {
|
||||||
log::debug!("Parsing spotify album: {}", id);
|
log::debug!("Parsing spotify album: {}", id);
|
||||||
let album_info = spotify_client.get_album(&id).await;
|
let album_info = spotify::get_album(spotify_client, &id).await;
|
||||||
match album_info {
|
match album_info {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
let mut reply = format!(
|
let mut reply = format!(
|
||||||
|
@ -104,7 +102,7 @@ async fn main() {
|
||||||
}
|
}
|
||||||
Playlist(id) => {
|
Playlist(id) => {
|
||||||
log::debug!("Parsing spotify playlist: {}", id);
|
log::debug!("Parsing spotify playlist: {}", id);
|
||||||
let playlist_info = spotify_client.get_playlist(&id).await;
|
let playlist_info = spotify::get_playlist(spotify_client, &id).await;
|
||||||
match playlist_info {
|
match playlist_info {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
let reply = format!(
|
let reply = format!(
|
||||||
|
@ -141,3 +139,32 @@ async fn main() {
|
||||||
|
|
||||||
log::info!("Exiting...");
|
log::info!("Exiting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_track_section_for_playlist(tracks: Vec<PlayableKind>, reply: String) -> String {
|
||||||
|
if !tracks.is_empty() {
|
||||||
|
let songs = tracks
|
||||||
|
.iter()
|
||||||
|
.map(|x| match x {
|
||||||
|
PlayableKind::Track(t) => t.name.clone(),
|
||||||
|
PlayableKind::Podcast(e) => e.name.clone()
|
||||||
|
} + "\n")
|
||||||
|
.collect::<String>();
|
||||||
|
reply
|
||||||
|
.clone()
|
||||||
|
.push_str(format!("\n🎶 {} Track(s): {}", tracks.len(), songs).as_str())
|
||||||
|
}
|
||||||
|
reply
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_track_section(tracks: Vec<TrackInfo>, reply: String) -> String {
|
||||||
|
if !tracks.is_empty() {
|
||||||
|
let songs = tracks
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.name.clone() + "\n")
|
||||||
|
.collect::<String>();
|
||||||
|
reply
|
||||||
|
.clone()
|
||||||
|
.push_str(format!("\n🎶 {} Track(s): {}", tracks.len(), songs).as_str())
|
||||||
|
}
|
||||||
|
reply
|
||||||
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ use rspotify::model::PlayableItem::{Episode, Track};
|
||||||
use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId};
|
use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId};
|
||||||
use rspotify::prelude::*;
|
use rspotify::prelude::*;
|
||||||
use rspotify::{ClientCredsSpotify, Credentials};
|
use rspotify::{ClientCredsSpotify, Credentials};
|
||||||
use std::any::Any;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub enum SpotifyKind {
|
pub enum SpotifyKind {
|
||||||
|
@ -47,35 +45,74 @@ pub struct PlaylistInfo {
|
||||||
pub(crate) tracks: Vec<PlayableKind>,
|
pub(crate) tracks: Vec<PlayableKind>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
fn get_id_in_url(url: &str) -> Option<&str> {
|
||||||
pub(crate) struct Client {
|
url.rsplit('/')
|
||||||
client: Arc<ClientCredsSpotify>,
|
.next()
|
||||||
|
.and_then(|x| x.split(' ').next())
|
||||||
|
.and_then(|x| x.split('?').next())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
fn extract_artists_from_tracks(tracks: Vec<FullTrack>) -> Vec<String> {
|
||||||
pub(crate) async fn new() -> Self {
|
tracks
|
||||||
let spotify_creds = Credentials::from_env()
|
.iter()
|
||||||
.expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found.");
|
.flat_map(|t| t.artists.iter().map(|a| a.name.clone()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_entry_kind(url: &str) -> Option<SpotifyKind> {
|
||||||
|
if url.contains("https://open.spotify.com/track/") {
|
||||||
|
let track_id = get_id_in_url(url);
|
||||||
|
return match track_id {
|
||||||
|
Some(id) => Some(SpotifyKind::Track(id.to_string())),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if url.contains("https://open.spotify.com/album/") {
|
||||||
|
let album_id = get_id_in_url(url);
|
||||||
|
return match album_id {
|
||||||
|
Some(id) => Some(SpotifyKind::Album(id.to_string())),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if url.contains("https://open.spotify.com/playlist/") {
|
||||||
|
let playlist_id = get_id_in_url(url);
|
||||||
|
return match playlist_id {
|
||||||
|
Some(id) => Some(SpotifyKind::Playlist(id.to_string())),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if url.contains("https://open.spotify.com/show/") {
|
||||||
|
let playlist_id = get_id_in_url(url);
|
||||||
|
return match playlist_id {
|
||||||
|
Some(id) => Some(SpotifyKind::Podcast(id.to_string())),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if url.contains("https://open.spotify.com/episode/") {
|
||||||
|
let playlist_id = get_id_in_url(url);
|
||||||
|
return match playlist_id {
|
||||||
|
Some(id) => Some(SpotifyKind::Episode(id.to_string())),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_client() -> Box<ClientCredsSpotify> {
|
||||||
|
let spotify_creds =
|
||||||
|
Credentials::from_env().expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found.");
|
||||||
let mut spotify = ClientCredsSpotify::new(spotify_creds);
|
let mut spotify = ClientCredsSpotify::new(spotify_creds);
|
||||||
spotify.request_token().await.unwrap();
|
spotify.request_token().await.unwrap();
|
||||||
Client {
|
Box::new(spotify)
|
||||||
client: Arc::new(spotify),
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn new_with_dependencies(client: ClientCredsSpotify) -> Self {
|
pub async fn get_track(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<TrackInfo> {
|
||||||
Client {
|
let track_id = match TrackId::from_id(id.as_str()) {
|
||||||
client: Arc::new(client),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_track(&self, id: &str) -> Option<TrackInfo> {
|
|
||||||
let track_id = match TrackId::from_id(id) {
|
|
||||||
Ok(track) => track,
|
Ok(track) => track,
|
||||||
Err(_e) => return None,
|
Err(_e) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.client.track(&track_id).await {
|
match spotify.track(&track_id).await {
|
||||||
Ok(track) => Some(TrackInfo {
|
Ok(track) => Some(TrackInfo {
|
||||||
name: track.name,
|
name: track.name,
|
||||||
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
|
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
|
@ -83,15 +120,15 @@ impl Client {
|
||||||
}),
|
}),
|
||||||
Err(_e) => None,
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_album(&self, id: &str) -> Option<AlbumInfo> {
|
pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<AlbumInfo> {
|
||||||
let album_id = match AlbumId::from_id(id) {
|
let album_id = match AlbumId::from_id(id.as_str()) {
|
||||||
Ok(album) => album,
|
Ok(album) => album,
|
||||||
Err(_e) => return None,
|
Err(_e) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.client.album(&album_id).await {
|
match spotify.album(&album_id).await {
|
||||||
Ok(album) => Some(AlbumInfo {
|
Ok(album) => Some(AlbumInfo {
|
||||||
name: album.name,
|
name: album.name,
|
||||||
artists: album.artists.iter().map(|x| x.name.clone()).collect(),
|
artists: album.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
|
@ -109,15 +146,15 @@ impl Client {
|
||||||
}),
|
}),
|
||||||
Err(_e) => None,
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_playlist(&self, id: &String) -> Option<PlaylistInfo> {
|
pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<PlaylistInfo> {
|
||||||
let playlist_id = match PlaylistId::from_id(id.as_str()) {
|
let playlist_id = match PlaylistId::from_id(id.as_str()) {
|
||||||
Ok(playlist) => playlist,
|
Ok(playlist) => playlist,
|
||||||
Err(_e) => return None,
|
Err(_e) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.client.playlist(&playlist_id, None, None).await {
|
match spotify.playlist(&playlist_id, None, None).await {
|
||||||
Ok(playlist) => Some(PlaylistInfo {
|
Ok(playlist) => Some(PlaylistInfo {
|
||||||
name: playlist.name,
|
name: playlist.name,
|
||||||
artists: playlist
|
artists: playlist
|
||||||
|
@ -163,90 +200,4 @@ impl Client {
|
||||||
}),
|
}),
|
||||||
Err(_e) => None,
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_id_in_url(url: &str) -> Option<&str> {
|
|
||||||
url.rsplit('/')
|
|
||||||
.next()
|
|
||||||
.and_then(|x| x.split(' ').next())
|
|
||||||
.and_then(|x| x.split('?').next())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_id_in_uri(uri: &str) -> Option<&str> {
|
|
||||||
uri.rsplit(':').next()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_entry_kind(uri: &str) -> Option<SpotifyKind> {
|
|
||||||
if uri.contains("spotify:track:") {
|
|
||||||
let track_id = get_id_in_uri(uri);
|
|
||||||
return match track_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Track(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("https://open.spotify.com/track/") {
|
|
||||||
let track_id = get_id_in_url(uri);
|
|
||||||
return match track_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Track(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("spotify:album:") {
|
|
||||||
let track_id = get_id_in_uri(uri);
|
|
||||||
return match track_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Album(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("https://open.spotify.com/album/") {
|
|
||||||
let album_id = get_id_in_url(uri);
|
|
||||||
return match album_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Album(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("spotify:playlist:") {
|
|
||||||
let track_id = get_id_in_uri(uri);
|
|
||||||
return match track_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Album(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("https://open.spotify.com/playlist/") {
|
|
||||||
let playlist_id = get_id_in_url(uri);
|
|
||||||
return match playlist_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Playlist(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("spotify:show:") {
|
|
||||||
let track_id = get_id_in_uri(uri);
|
|
||||||
return match track_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Album(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("https://open.spotify.com/show/") {
|
|
||||||
let playlist_id = get_id_in_url(uri);
|
|
||||||
return match playlist_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Podcast(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("spotify:episode:") {
|
|
||||||
let track_id = get_id_in_uri(uri);
|
|
||||||
return match track_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Album(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if uri.contains("https://open.spotify.com/episode/") {
|
|
||||||
let playlist_id = get_id_in_url(uri);
|
|
||||||
return match playlist_id {
|
|
||||||
Some(id) => Some(SpotifyKind::Episode(id.to_string())),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,131 +0,0 @@
|
||||||
use std::error::Error;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
pub(crate) struct VideoSearch {
|
|
||||||
items: Vec<Video>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Video {
|
|
||||||
title: String,
|
|
||||||
videoId: String,
|
|
||||||
author: String,
|
|
||||||
authorId: String,
|
|
||||||
authorUrl: String,
|
|
||||||
lengthSeconds: u64,
|
|
||||||
description: String,
|
|
||||||
descriptionHtml: String,
|
|
||||||
viewCount: u64,
|
|
||||||
published: u64,
|
|
||||||
publishedText: String,
|
|
||||||
live_now: bool,
|
|
||||||
paid: bool,
|
|
||||||
premium: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
type SearchSortBy = str;
|
|
||||||
|
|
||||||
const BY_RELEVANCE: &SearchSortBy = "relevance";
|
|
||||||
const BY_RATING: &SearchSortBy = "rating";
|
|
||||||
const BY_UPLOAD_DATE: &SearchSortBy = "upload_date";
|
|
||||||
const BY_VIEW_COUNT: &SearchSortBy = "view_count";
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub(crate) struct Client {
|
|
||||||
client: Arc<invidious::asynchronous::Client>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Client {
|
|
||||||
pub(crate) async fn new() -> Self {
|
|
||||||
// TODO check for a stable instance
|
|
||||||
let client = invidious::asynchronous::Client::new(String::from("https://vid.puffyan.us"));
|
|
||||||
|
|
||||||
Client {
|
|
||||||
client: Arc::new(client),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn new_with_dependencies(client: invidious::asynchronous::Client) -> Self {
|
|
||||||
Client {
|
|
||||||
client: Arc::new(client),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn search_video(
|
|
||||||
&self,
|
|
||||||
keyword: &str,
|
|
||||||
sort_by: Option<&SearchSortBy>,
|
|
||||||
) -> Result<VideoSearch, Box<dyn Error>> {
|
|
||||||
let mut query = Vec::<String>::new();
|
|
||||||
query.push(format!("{}={}", "q", keyword));
|
|
||||||
match sort_by {
|
|
||||||
Some(sorting_type) => query.push(format!("{}={}", "sort_by", sorting_type)),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
query.push(format!("{}={}", "type", "video"));
|
|
||||||
|
|
||||||
let generated_query = query.join(",");
|
|
||||||
let search = self.client.search(Some(generated_query.as_str())).await?;
|
|
||||||
|
|
||||||
let videos: Vec<Video> = search
|
|
||||||
.items
|
|
||||||
.iter()
|
|
||||||
.map(|search_result| match search_result {
|
|
||||||
invidious::structs::hidden::SearchItem::Video {
|
|
||||||
title,
|
|
||||||
videoId,
|
|
||||||
author,
|
|
||||||
authorId,
|
|
||||||
authorUrl,
|
|
||||||
lengthSeconds,
|
|
||||||
videoThumbnails: _,
|
|
||||||
description,
|
|
||||||
descriptionHtml,
|
|
||||||
viewCount,
|
|
||||||
published,
|
|
||||||
publishedText,
|
|
||||||
liveNow,
|
|
||||||
paid,
|
|
||||||
premium,
|
|
||||||
} => Some(Video {
|
|
||||||
title: title.clone(),
|
|
||||||
videoId: videoId.clone(),
|
|
||||||
author: author.clone(),
|
|
||||||
authorId: authorId.clone(),
|
|
||||||
authorUrl: authorUrl.clone(),
|
|
||||||
lengthSeconds: lengthSeconds.clone(),
|
|
||||||
description: description.clone(),
|
|
||||||
descriptionHtml: descriptionHtml.clone(),
|
|
||||||
viewCount: viewCount.clone(),
|
|
||||||
published: published.clone(),
|
|
||||||
publishedText: publishedText.clone(),
|
|
||||||
live_now: liveNow.clone(),
|
|
||||||
paid: paid.clone(),
|
|
||||||
premium: premium.clone(),
|
|
||||||
}),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.filter(|x| x.is_some())
|
|
||||||
.map(|x| x.unwrap())
|
|
||||||
.filter(|x| !x.premium && !x.paid && !x.live_now)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Result::Ok(VideoSearch { items: videos })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn should_search_properly() {
|
|
||||||
let client = tokio_test::block_on(Client::new());
|
|
||||||
|
|
||||||
let result = tokio_test::block_on(client.search_video(
|
|
||||||
"vfdskvnfdsjklvnfdsjklvnfsdjkldsvmdlfmvkdfslvsdfmklsdvlvfdnjkvnfdsjkvnfsdjk",
|
|
||||||
Some(BY_UPLOAD_DATE),
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(30, result.items.len())
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue