Skip to content

Commit

Permalink
Merge pull request #23 from kyan/feature/drag_in_tracks
Browse files Browse the repository at this point in the history
Drag tracks from Spotify app into KyanBar to add to jukebox playlist
  • Loading branch information
whomwah committed Feb 11, 2015
2 parents 2de33b1 + 28643e9 commit 4ecdd91
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Motion::Project::App.setup do |app|
app.pods do
pod 'SocketRocket'
pod 'MASShortcut'
pod "AFNetworking", "~> 2.0"
pod "AFNetworking"
end

app.sparkle do
Expand Down
36 changes: 36 additions & 0 deletions app/_windows/jukebox_control_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,43 @@ def self.build
).tap do |win|
win.setFrameAutosaveName("MAINWINDOWPositionHeightWidth")
win.build_views

win.registerForDraggedTypes([NSPasteboardTypeString])
end
end

def draggingEntered(sender)
source_drag_mask = sender.draggingSourceOperationMask
pboard = sender.draggingPasteboard

if pboard.types.include?(NSPasteboardTypeString)
if source_drag_mask & NSDragOperationGeneric
return NSDragOperationGeneric
end
end

NSDragOperationNone
end

def draggingUpdated(sender)
NSDragOperationCopy
end

def performDragOperation(sender)
source_drag_mask = sender.draggingSourceOperationMask
pboard = sender.draggingPasteboard

if pboard.types.include?(NSPasteboardTypeString)
str = pboard.stringForType(NSPasteboardTypeString)

# only handle tracks at the moment
# e.g http://open.spotify.com/track/75clRtmtXSkZsPm6SjjQ5j
if str.match(/http:\/\/open\.spotify\.com\/track\/[a-zA-Z0-9]*/)
PlaylistHandler.add!(str)
end
end

true
end

def canBecomeKeyWindow
Expand Down
3 changes: 2 additions & 1 deletion app/config/constants.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
WEBSOCKET_HOST = 'jukebox.local'
WEBSOCKET_URL = "ws://#{WEBSOCKET_HOST}:8081"
VOTE_URL = "http://#{WEBSOCKET_HOST}/external?"
JUKEBOX_URL = "http://#{WEBSOCKET_HOST}"
VOTE_URL = "#{JUKEBOX_URL}/external?"

DEFAULT_MENU_WIDTH=250.0

Expand Down
20 changes: 1 addition & 19 deletions app/lib/jukebox_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,7 @@ def after_update
end

def do_notifications
jukebox.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
KyanBar::Notifier.send!(jukebox.notifications)
end

end
35 changes: 35 additions & 0 deletions app/lib/notifier.rb
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
94 changes: 94 additions & 0 deletions app/lib/playlist_handler.rb
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

0 comments on commit 4ecdd91

Please sign in to comment.