]> git.r.bdr.sh - rbdr/lyricli/blob - Sources/lyricli/sources/spotify_source.swift
863a399f2e68bc13cede3bcada3de8005ae264c1
[rbdr/lyricli] / Sources / lyricli / sources / spotify_source.swift
1 import ScriptingBridge
2
3 // Protocol to obtain the track from Spotify
4 @objc protocol SpotifyTrack {
5 @objc optional var name: String {get}
6 @objc optional var artist: String {get}
7 }
8
9 // Protocol to interact with Spotify
10 @objc protocol SpotifyApplication {
11 @objc optional var currentTrack: SpotifyTrack? {get}
12 }
13
14 extension SBApplication: SpotifyApplication {}
15
16 // Source that reads track artist and name from current Spotify track
17 class SpotifySource: Source {
18
19 // Calls the spotify API and returns the current track
20 var currentTrack: Track? {
21
22 if let spotify: SpotifyApplication = SBApplication(bundleIdentifier: "com.spotify.client") {
23 if let application = spotify as? SBApplication {
24 if !application.isRunning {
25 return nil
26 }
27 }
28
29 // Attempt to fetch the title from a song
30
31 if let currentTrack = spotify.currentTrack {
32 if let track = currentTrack {
33 if let name = track.name {
34 if let artist = track.artist {
35
36 return Track(withName: name, andArtist: artist)
37 }
38 }
39 }
40 }
41 }
42
43 return nil
44 }
45
46 func enable() -> Bool { return true }
47 func disable() -> Bool { return true }
48 func reset() -> Bool { return true }
49 }