bjarneo / Pytify

A CLI application for controlling Spotify
MIT License
741 stars 40 forks source link

how to use pytify inside a python code ? #67

Closed jojo-monk closed 5 years ago

jojo-monk commented 5 years ago

Hello, I would like to use pytify in a python code, but what are the methods, and functions ?

thanks

bjarneo commented 5 years ago

Hi

That you can do. What is your use case? :)

As for controlling Spotify (through cli), you can use this code: https://github.com/bjarneo/Pytify/blob/master/pytify/cli.py#L15, then use next/prev/stop etc like this: https://github.com/bjarneo/Pytify/blob/master/pytify/cli.py#L45

However, if you want to communicate with the spotify API, I would recommend this lib: https://spotipy.readthedocs.io/en/latest/

jojo-monk commented 5 years ago

Thanks

I would like to write a simple recorder for spotify. spotipy is good but it can play only 30 seconds.

but i don't understand, if i try this for example :

`import pytify from pytify import cli

spot = cli.App() spot.play_pause()`

I get this :

`pytify 3.5.2 [https://github.com/bjarneo/Pytify] Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/interface.py", line 383, in run with self.input.raw_mode(): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/input.py", line 69, in raw_mode return raw_mode(self.stdin.fileno()) io.UnsupportedOperation: fileno

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Volumes/Données/jojo/python/test pytify.py", line 4, in spot = cli.App() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytify/cli.py", line 19, in init self.run() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytify/cli.py", line 61, in run self.interaction() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytify/cli.py", line 72, in interaction search_input = custom_prompt(self.pytify.get_current_playing()) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytify/prompt.py", line 63, in custom_prompt style=style File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/shortcuts.py", line 548, in prompt document = cli.run(reset_current_buffer=False) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/interface.py", line 397, in run self._redraw() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/interface.py", line 344, in _redraw self.renderer.render(self, self.layout, is_done=self.is_done) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/renderer.py", line 391, in render size = output.get_size() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/terminal/vt100_output.py", line 352, in get_size rows, columns = _get_size(stdout.fileno()) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prompt_toolkit/terminal/vt100_output.py", line 317, in _get_size fcntl.ioctl(fileno, termios.TIOCGWINSZ, buf, True) OSError: [Errno 19] Operation not supported by device`

excuse me for my bad english, and my beginner's questions ;)

bjarneo commented 5 years ago

@jojo-monk could you try to implement it by doing this?

from pytify.strategy import get_pytify_class_by_platform

spot = get_pytify_class_by_platform()()
spot.play_pause()
jojo-monk commented 5 years ago

Yes, it works, thanks.

another question : is there a method to get the duration of a song with pytify ?

bjarneo commented 5 years ago

We don't have any logic for this I am afraid. However, if you have a look at this line: https://github.com/bjarneo/Pytify/blob/master/pytify/linux.py#L12 it gets the metadata from the current song. It should contain the length. Haven't tried it yet.

This line https://github.com/bjarneo/Pytify/blob/master/pytify/linux.py#L31 get the current playing