Skip to content

Commit 5830de4

Browse files
committed
Initial working version
0 parents  commit 5830de4

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
**/*.rs.bk
3+
Cargo.lock

Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "python-librespot"
3+
version = "0.1.0"
4+
authors = ["Paul Lietar <paul@lietar.net>"]
5+
6+
[lib]
7+
name = "librespot"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
tokio-core = "0.1.2"
12+
13+
[dependencies.librespot]
14+
git = "https://github.com/plietar/librespot"
15+
16+
[dependencies.cpython]
17+
git = "https://github.com/dgrunwald/rust-cpython.git"

examples/play.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
from librespot import Session, SpotifyId
3+
4+
if len(sys.argv) != 4:
5+
print("Usage: %s USERNAME PASSWORD TRACK" % sys.argv[0])
6+
sys.exit(1)
7+
8+
[_, username, password, trackid] = sys.argv
9+
10+
print("Connecting ...")
11+
session = Session(username, password)
12+
player = session.player()
13+
14+
print("Playing ...")
15+
track = SpotifyId(trackid)
16+
player.play(track)
17+
18+
print("Done")

src/lib.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#[macro_use]
2+
extern crate cpython;
3+
extern crate tokio_core;
4+
extern crate librespot;
5+
6+
use std::cell::RefCell;
7+
use cpython::{PyResult, PyObject};
8+
use tokio_core::reactor::Core;
9+
10+
thread_local! {
11+
pub static CORE: RefCell<Core> = RefCell::new(Core::new().unwrap());
12+
}
13+
14+
py_class!(class Session |py| {
15+
data session : librespot::session::Session;
16+
17+
def __new__(_cls, username: String, password: String) -> PyResult<Session> {
18+
use librespot::session::Config;
19+
use librespot::authentication::Credentials;
20+
21+
CORE.with(|core| {
22+
let mut core = core.borrow_mut();
23+
let handle = core.handle();
24+
25+
let config = Config::default();
26+
let credentials = Credentials::with_password(username, password);
27+
let session = core.run(librespot::session::Session::connect(config, credentials, None, handle)).unwrap();
28+
29+
Session::create_instance(py, session)
30+
})
31+
}
32+
33+
def player(&self) -> PyResult<Player> {
34+
let backend = librespot::audio_backend::find(None).unwrap();
35+
let session = self.session(py).clone();
36+
37+
let player = librespot::player::Player::new(session, None, move || (backend)(None));
38+
Player::create_instance(py, player)
39+
}
40+
});
41+
42+
py_class!(class Player |py| {
43+
data player : librespot::player::Player;
44+
45+
def play(&self, track: SpotifyId) -> PyResult<PyObject> {
46+
CORE.with(|core| {
47+
let mut core = core.borrow_mut();
48+
let player = self.player(py);
49+
let track = *track.id(py);
50+
51+
core.run(player.load(track, true, 0)).unwrap();
52+
53+
Ok(py.None())
54+
})
55+
}
56+
});
57+
58+
py_class!(class SpotifyId |py| {
59+
data id : librespot::util::SpotifyId;
60+
61+
def __new__(_cls, base62: &str) -> PyResult<SpotifyId> {
62+
let id = librespot::util::SpotifyId::from_base62(base62);
63+
SpotifyId::create_instance(py, id)
64+
}
65+
});
66+
67+
py_module_initializer!(librespot, initlibrespot, PyInit_librespot, |py, m| {
68+
m.add_class::<Session>(py)?;
69+
m.add_class::<SpotifyId>(py)?;
70+
Ok(())
71+
});

0 commit comments

Comments
 (0)