-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drag track from spotify into KyanBar
- Loading branch information
Showing
5 changed files
with
168 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module KyanBar | ||
class Notifier | ||
attr_reader :notifications | ||
|
||
def initialize(notifications=[]) | ||
@notifications = notifications | ||
end | ||
|
||
def self.send!(notifications) | ||
new(notifications).process! | ||
end | ||
|
||
def process! | ||
notifications.each do |message| | ||
gcdq = Dispatch::Queue.new('com.kyan.kyanbar') | ||
gcdq.async do | ||
notification = NSUserNotification.new | ||
notification.title = message.heading | ||
notification.subtitle = message.subtitle | ||
notification.informativeText = message.description | ||
|
||
if !message.artwork_url.nil? | ||
url = NSURL.URLWithString(message.artwork_url) | ||
if url | ||
artwork_image = NSImage.alloc.initWithContentsOfURL(url) | ||
notification.contentImage = artwork_image | ||
end | ||
end | ||
|
||
NSUserNotificationCenter.defaultUserNotificationCenter.scheduleNotification(notification) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
class PlaylistHandler | ||
|
||
attr_reader :data_as_json | ||
|
||
def initialize(track) | ||
@data_as_json = BW::JSON.generate({'filenames' => [track]}) | ||
end | ||
|
||
def self.add!(track) | ||
handler = new(track) | ||
handler.add | ||
end | ||
|
||
def add | ||
if valid? | ||
AFMotion::HTTP.get(dest_url) do |response| | ||
if response.body.to_s.include?('login details incorrect') | ||
alert_creds_wrong | ||
else | ||
if response.success? | ||
parse_json(response.body) | ||
do_notification | ||
end | ||
end | ||
end | ||
else | ||
alert_no_creds | ||
false | ||
end | ||
end | ||
|
||
private | ||
|
||
def dest_url | ||
json_encode = data_as_json.to_url_encoded | ||
encoded_str = "user=#{username}&password=#{password}&argument=#{json_encode}" | ||
"#{JUKEBOX_URL}/run/bulk_add_to_playlist?#{encoded_str}" | ||
end | ||
|
||
def valid? | ||
username && password | ||
end | ||
|
||
def username | ||
Persistence.get("jukeboxUsername") | ||
end | ||
|
||
def password | ||
Persistence.get("jukeboxPassword") | ||
end | ||
|
||
def alert_creds_wrong | ||
oops( | ||
'Doh?', | ||
'Your jukebox username or password appear to be wrong!' | ||
) | ||
end | ||
|
||
def alert_no_creds | ||
oops( | ||
'Want to vote huh?', | ||
'You need to set your username and password in preferences' | ||
) | ||
end | ||
|
||
def oops(title, message) | ||
alert = NSAlert.new | ||
alert.messageText = title | ||
alert.informativeText = message | ||
alert.alertStyle = NSInformationalAlertStyle | ||
alert.addButtonWithTitle("Ok") | ||
response = alert.runModal | ||
end | ||
|
||
def parse_json(str) | ||
e = Pointer.new(:object) | ||
json = NSJSONSerialization.JSONObjectWithData( String(str).to_data, | ||
options:0, | ||
error: e | ||
) | ||
@track = json.first | ||
end | ||
|
||
def do_notification | ||
track = KyanJukebox::Track.new | ||
track.title = 'Successfully added to Jukebox' | ||
track.artist = "#{@track["title"]} - #{@track["artist"]}" | ||
track.album = @track["album"] | ||
track.artwork_url = @track["artwork_url"] | ||
|
||
KyanBar::Notifier.send!([track]) | ||
end | ||
|
||
end |