Git Product home page Git Product logo

swspotify's Introduction

SwSpotify

Discord Server Test codecov PyPI Downloads

SwSpotify is a Python library to get the song and artist of the currently playing song from the Spotify application faster and without using the API. It works on Windows, Linux, macOS and even the Spotify Web Player! ๐Ÿฅณ

In order to add support for the Spotify Web Player, the SwagLyrics Chrome Extension needs to be installed. We have plans to extend this for other browsers as well.

If you're a developer using SwSpotify, you can direct your end users to install the extension to automatically make your application work with the Spotify Web Player. The source of the Chrome Extension is open sourced at https://github.com/SwagLyrics/SwagLyrics-Chrome-Extension.

The original repository was spotilib which worked just for Windows and hasn't been updated since a long while when it broke on account of Spotify updating their application.

Originally made for use in SwagLyrics for Spotify.

Installation

Requires Python3. Use pip or pip3 depending on your installation. You might want to use the --user flag on Linux to avoid using pip as root.

pip install SwSpotify

For linux you need dbus which is usually pre-installed.

Usage

Use it in your project by importing it as:

from SwSpotify import spotify

Then you can access the song and artist as:

>>> spotify.song()
'Hello'
>>> spotify.artist()
'Adele'

Since mostly song and artist are used in conjunction, there is a current() method as well.

>>> spotify.current()
('Hello', 'Adele')

This allows you to access song and artist by tuple unpacking as:

>>> song, artist = spotify.current()

A SpotifyNotRunning Exception is raised if Spotify is closed or paused. SpotifyClosed and SpotifyPaused inherit from SpotifyNotRunning, meaning that you can catch both at the same time:

try:
    title, artist = spotify.current()
except SpotifyNotRunning as e:
    print(e)
else:
    print(f"{title} - {artist}")

In case Spotify is closed or paused, that will automatically be reflected in the value of e.

For finer control you can catch SpotifyClosed and SpotifyPaused separately.

Compiling SwSpotify for Development

  • Clone the repo by git clone https://github.com/SwagLyrics/SwSpotify.git or use ssh.
  • cd into the cloned repo.
  • pip install -e . the -e flag installs it locally in editable mode.

Contributing

Sure, improvements/fixes/issues everything is welcome :)

swspotify's People

Contributors

aadibajpai avatar ace314159 avatar dependabot-preview[bot] avatar dependabot[bot] avatar flabbet avatar marioortizmanero avatar matejmecka avatar musabkilic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swspotify's Issues

dbus-python extra requirement

since dbus-python is only necessary for travis, I'm thinking of installing it separately in travis and not as a dependency in SwSpotify since the dbus is pre-installed on linux systems already.

Failed install with pip

Cannot install : `

Command "/usr/bin/python3 -c "import setuptools, tokenize;__file__='/home/maxence/SwSpotify/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" develop --no-deps --user --prefix=" failed with error code 1 in /home/maxence/SwSpotify/

tried pip3 install swspotify and the local pip3 install -e .
`

Werkzeug >=2.1.0 causes the web server to not function.

Describe the bug
When running the SwSpotify web server or using the command line interface with a python-werkzeug package version 2.1.0 or greater, the web server fails with a runtime error stating that it is not running with the werkzeug server.

To Reproduce
Steps to reproduce the behavior:

  1. Have python-werkzeug >= 2.1.0 installed
  2. Run swaglyrics

Expected behavior
The server should come up in a new tab or in a terminal and supply lyrics.

Desktop (please complete the following information):

  • OS: Arch Linux (kernel version: 5.17.7)
  • Browser: Brave
  • Version: 1.2.3 (also tested with the current AUR version, 1.2.1, to no avail)

Additional context
Werkzeug provides a warning when using a 2.0.x versioned package:
/usr/lib/python3.10/site-packages/SwSpotify/web_server.py:48: UserWarning: The 'environ['werkzeug.server.shutdown']' function is deprecated and will be removed in Werkzeug 2.1.

The documentation for werkzeug shows the proper way to do the shutdown: https://werkzeug.palletsprojects.com/en/2.1.x/serving/#shutting-down-the-server

So until a fix is provided the only way to use this is to downgrade workzeug to 2.0.3 and add it to IgnorePkg (on Arch Linux at least, I am not sure about other Linux distros).
The same message regarding deprecation appears on Windows as well, however, pip prevents the upgrade to 2.1.x.

Device - cast - support

Preconditions:
Swag-lyrics turned on - currently playing.

  • Alexa Echo bot.

To Reproduce
Steps to reproduce the behavior:

  1. Click on "Devices Available"
  2. Click on "Connected device"
  3. See error

Expected behavior
Lyrics showed up.

Actual result:
<
Nothing Playing.
Lyrics unavailable for by .

Reads window titles from wrong window

With certain programs running at the same time as Spotify, in my case Jetbrains Toolbox but I'm sure this applies to other embedded browser applications, SwSpotify will read the title from the wrong window and report the song name as eg. Jetbrains Toolbox.

This happened on Windows 10 20H2 with Python 3.9.5, SwSpotify 1.2.1 and Jetbrains Toolbox App 1.20.8352.

Closing Jetbrains Toolbox fixes the problem.

Swspotify no longer works on latest UWP Spotify

Describe the bug
Swspotify no longer works on latest uwp Spotify version.

To Reproduce
Update Spotify UWP to latest version and try using spotify.song() fn.

Proposed Fix
Figured out that replacing "Chrome_WidgetWin_0" with "Chrome_WidgetWin_1" in line 31 of spotify.py fixes the issue. Occasionally Visual studio code or other electron apps do cause issues but that can be fixed with an additional if statement.

spotify.current() throws blocking runtime error if Spotify is not playing

When running

from SwSpotify import spotify
try:
    title, artist = spotify.current()
except:
    print(f"Song lookup timed out")
    return

I get the below error message. Furthermore, the python script will hang until I send a Ctrl-C keyboard interrupt to the console.

Not sure why my try:except isn't catching the error, also not sure why I have to send Ctrl-C to let the python script continue.

[2022-08-16 18:08:53,054] ERROR in app: Exception on /shutdown [POST]
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\SwSpotify\web_server.py", line 40, in shutdown_server
    shutdown()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\SwSpotify\web_server.py", line 47, in shutdown
    raise RuntimeError('Not running with the Werkzeug Server')
RuntimeError: Not running with the Werkzeug Server

[BUG] Doesn't work

Describe the bug
Was working fine. I updated Spotify app a few times, doesn't work anymore

To Reproduce
Steps to reproduce the behavior:

  1. Play any song on newest Spotify app on mac
  2. swaglyrics -c
  3. Doesn't detect artist and song name (even Despacito doesn't work)
$ swaglyrics -c
Updating unsupported.txt from server.
Updated unsupported.txt successfully.
Lyrics unavailable for  by .


(Press Ctrl+C to quit)
^C
Sure boss, exiting.

Expected behavior
should work

Desktop (please complete the following information):

  • MacOS 10.15.4
  • swaglyrics: 1.1.2
  • Spotify app version: 1.1.31.703.g256add22

During startup SwSpotify detects a song called "Spotify"

I saw a few funny requests on the backend:

2019-12-09 14:08:37 getting stripper from Genius for Spotify by
2019-12-09 14:27:23 getting stripper from Genius for Spotify by
2019-12-12 18:29:57 getting stripper from Genius for Spotify by

I noticed this happened when opening the Spotify client on Windows.
I ran this script and then opened the client.

>>> from SwSpotify import spotify, SpotifyNotRunning
>>> while True:
...     try:
...             spotify.current()
...     except SpotifyNotRunning:
...             continue
...

Output:

('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')
('Spotify', '')

Using pydbus instead of python-dbus

I wanted to suggest using pydbus instead of python-dbus. The former is much more pythonic (modern), easy to use, and many users are migrating to it. I've used it myself in my project and it's really easy to use coming from python-dbus. I can help you with it myself and do a PR if you want.

Implementing play/pause and other improvements

(Continuation of vidify/vidify#10)

The main loop is just an endless loop that calls a function you provide with data of events happening when the dbus bus detects a change like pause or a new song (you can check my dbus module to see how it works). I use it in Spotify Music Videos to control the player and know when songs end.

I will be able to do a couple PRs soon-ish but I'm not really sure how you want it done. Like what I have inside my implementation?

Feature: Add support for Spotify Web Player

SwSpotify is cross-platform, that means we support Windows, macOS and Linux. However, we haven't been able to support the Web Player yet, and doing so would make us completely unstoppable :)

There is an existing Chrome extension that is supposed to be integrated: https://swaglyrics.dev/SwagLyrics-Chrome-Extension

However, we haven't been able to completely test it out so some work might be needed on both the extension as well as the library itself.

Any idea how I could get the liked status of current song

I'm sorry I know this does not belong here but I can't find anything related anywhere...and you guys seem to know a lot about it...

I need to get if the currently playing song is liked or not (for a widget). I'm on linux so using MPRIS is totally cool but I don't think there is something like this on the official implementation (idk tho) but I guess the default behaviour could be changed with something like spicetify.

I've seen Spotify remembers if the current song was liked even if I restart it offline so I'm guessing I could read a file every time but I can't seem to locate it. Using the API would be much slower but that would be acceptable too but I could not find such a request in the API Documentation.

I also wanted to be able to toggle it but I'm willing to stick to the keyboard shortcuts extension in spicetify if that's too much of a problem

thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.