|
| 1 | +use librespot; |
| 2 | +use cpython::{PyResult, Python, PythonObject}; |
| 3 | +use pyfuture::PyFuture; |
| 4 | +use futures; |
| 5 | +use SpotifyId; |
| 6 | + |
| 7 | +py_class!(pub class Track |py| { |
| 8 | + data session : librespot::session::Session; |
| 9 | + data track : librespot::metadata::Track; |
| 10 | + |
| 11 | + def id(&self) -> PyResult<SpotifyId> { |
| 12 | + SpotifyId::new(py, self.track(py).id) |
| 13 | + } |
| 14 | + |
| 15 | + def name(&self) -> PyResult<String> { |
| 16 | + Ok(self.track(py).name.clone()) |
| 17 | + } |
| 18 | + |
| 19 | + def album(&self) -> PyResult<PyFuture> { |
| 20 | + let session = self.session(py).clone(); |
| 21 | + let album = self.track(py).album; |
| 22 | + |
| 23 | + Album::get(py, session, album) |
| 24 | + } |
| 25 | + |
| 26 | + def artists(&self) -> PyResult<PyFuture> { |
| 27 | + let session = self.session(py).clone(); |
| 28 | + let artists = self.track(py).artists.clone(); |
| 29 | + Artist::get_all(py, session, artists) |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +py_class!(pub class Album |py| { |
| 34 | + data session : librespot::session::Session; |
| 35 | + data album : librespot::metadata::Album; |
| 36 | + |
| 37 | + def id(&self) -> PyResult<SpotifyId> { |
| 38 | + SpotifyId::new(py, self.album(py).id) |
| 39 | + } |
| 40 | + |
| 41 | + def name(&self) -> PyResult<String> { |
| 42 | + Ok(self.album(py).name.clone()) |
| 43 | + } |
| 44 | + |
| 45 | + def artists(&self) -> PyResult<PyFuture> { |
| 46 | + let session = self.session(py).clone(); |
| 47 | + let artists = self.album(py).artists.clone(); |
| 48 | + Artist::get_all(py, session, artists) |
| 49 | + } |
| 50 | + |
| 51 | + def tracks(&self) -> PyResult<PyFuture> { |
| 52 | + let session = self.session(py).clone(); |
| 53 | + let artists = self.album(py).tracks.clone(); |
| 54 | + Track::get_all(py, session, artists) |
| 55 | + } |
| 56 | +}); |
| 57 | + |
| 58 | +py_class!(pub class Artist |py| { |
| 59 | + data _session : librespot::session::Session; |
| 60 | + data artist : librespot::metadata::Artist; |
| 61 | + |
| 62 | + def id(&self) -> PyResult<SpotifyId> { |
| 63 | + SpotifyId::new(py, self.artist(py).id) |
| 64 | + } |
| 65 | + |
| 66 | + def name(&self) -> PyResult<String> { |
| 67 | + Ok(self.artist(py).name.clone()) |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +fn get<T, F, O>(py: Python, |
| 72 | + session: librespot::session::Session, |
| 73 | + id: librespot::util::SpotifyId, |
| 74 | + create_instance: F) -> PyResult<PyFuture> |
| 75 | + where |
| 76 | + T: librespot::metadata::MetadataTrait + Send, |
| 77 | + F: FnOnce(Python, librespot::session::Session, T) -> PyResult<O> + Send + 'static, |
| 78 | + O: PythonObject |
| 79 | +{ |
| 80 | + let future = session.metadata().get::<T>(id); |
| 81 | + PyFuture::new(py, future, |py, result| { |
| 82 | + create_instance(py, session, result.unwrap()) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +fn get_all<T, F, O, I>(py: Python, |
| 87 | + session: librespot::session::Session, |
| 88 | + ids: I, |
| 89 | + create_instance: F) -> PyResult<PyFuture> |
| 90 | + where |
| 91 | + T: librespot::metadata::MetadataTrait + Send, |
| 92 | + F: Fn(Python, librespot::session::Session, T) -> PyResult<O> + Send + 'static, |
| 93 | + O: PythonObject, |
| 94 | + I: IntoIterator<Item = librespot::util::SpotifyId>, |
| 95 | + I::IntoIter: 'static |
| 96 | +{ |
| 97 | + let session_ = session.clone(); |
| 98 | + let futures = ids.into_iter().map(move |id| { |
| 99 | + session_.metadata().get::<T>(id) |
| 100 | + }); |
| 101 | + |
| 102 | + let future = futures::future::join_all(futures); |
| 103 | + |
| 104 | + PyFuture::new(py, future, move |py, result| { |
| 105 | + let objects = result.unwrap(); |
| 106 | + let objects = objects.into_iter().map(|artist| { |
| 107 | + create_instance(py, session.clone(), artist) |
| 108 | + }).collect::<PyResult<Vec<_>>>()?; |
| 109 | + |
| 110 | + Ok(objects) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +impl Track { |
| 115 | + pub fn get(py: Python, |
| 116 | + session: librespot::session::Session, |
| 117 | + id: librespot::util::SpotifyId) -> PyResult<PyFuture> |
| 118 | + { |
| 119 | + get(py, session, id, Track::create_instance) |
| 120 | + } |
| 121 | + |
| 122 | + pub fn get_all<I>(py: Python, |
| 123 | + session: librespot::session::Session, |
| 124 | + ids: I) -> PyResult<PyFuture> |
| 125 | + where I: IntoIterator<Item = librespot::util::SpotifyId>, |
| 126 | + I::IntoIter: 'static |
| 127 | + { |
| 128 | + get_all(py, session, ids, Track::create_instance) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl Album { |
| 133 | + pub fn get(py: Python, |
| 134 | + session: librespot::session::Session, |
| 135 | + id: librespot::util::SpotifyId) -> PyResult<PyFuture> |
| 136 | + { |
| 137 | + get(py, session, id, Album::create_instance) |
| 138 | + } |
| 139 | + |
| 140 | + pub fn get_all<I>(py: Python, |
| 141 | + session: librespot::session::Session, |
| 142 | + ids: I) -> PyResult<PyFuture> |
| 143 | + where I: IntoIterator<Item = librespot::util::SpotifyId>, |
| 144 | + I::IntoIter: 'static |
| 145 | + { |
| 146 | + get_all(py, session, ids, Album::create_instance) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl Artist { |
| 151 | + pub fn get(py: Python, |
| 152 | + session: librespot::session::Session, |
| 153 | + id: librespot::util::SpotifyId) -> PyResult<PyFuture> |
| 154 | + { |
| 155 | + get(py, session, id, Artist::create_instance) |
| 156 | + } |
| 157 | + |
| 158 | + pub fn get_all<I>(py: Python, |
| 159 | + session: librespot::session::Session, |
| 160 | + ids: I) -> PyResult<PyFuture> |
| 161 | + where I: IntoIterator<Item = librespot::util::SpotifyId>, |
| 162 | + I::IntoIter: 'static |
| 163 | + { |
| 164 | + get_all(py, session, ids, Artist::create_instance) |
| 165 | + } |
| 166 | +} |
0 commit comments