Skip to content

Commit

Permalink
Spotify and pruning updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Pe8er committed Aug 5, 2019
1 parent bc2fdc7 commit 500b256
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 50 deletions.
Binary file modified Playbox.widget.zip
Binary file not shown.
31 changes: 14 additions & 17 deletions Playbox.widget/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ options =
horizontalPosition : "left" # left | right | center

# Choose widget size.
widgetVariant: "large" # large | medium | small
widgetVariant: "medium" # large | medium | small

# Choose color theme.
widgetTheme: "dark" # dark | light
Expand All @@ -19,7 +19,7 @@ options =
# Stick the widget in the corner? Set to *true* if you're using it with Sidebar widget, set to *false* if you'd like to give it some breathing room and a drop shadow.
stickInCorner: false # true | false

command: "osascript 'Playbox.widget/lib/Get Current Track.applescript'"
command: "osascript 'Playbox.widget/lib/get current track.applescript'"
refreshFrequency: '1s'

style: """
Expand All @@ -45,7 +45,7 @@ style: """
bgColor08 = rgba(bgColor,0.7)
bgColor05 = rgba(bgColor,0.5)
bgColor02 = rgba(bgColor,0.2)
blurProperties = blur(10px) brightness(bgBrightness) contrast(100%) saturate(140%)
blurProperties = blur(20px) brightness(bgBrightness) contrast(100%) saturate(140%)
// Next, let's sort out positioning.
Expand Down Expand Up @@ -83,9 +83,7 @@ style: """
transform-style preserve-3d
-webkit-transform translate3d(0px, 0px, 0px)
mainDimension = 176px
width auto
min-width 200px
max-width mainDimension
width @mainDimension
overflow hidden
white-space nowrap
background-color bgColor02
Expand Down Expand Up @@ -201,17 +199,17 @@ style: """
overflow hidden
.text
// Blurred background is turned off because of insane WebKit glitches :(
//-webkit-backdrop-filter blurProperties
-webkit-backdrop-filter blurProperties
position absolute
bottom 0
left 0
margin 0
padding 8px
color fColor1
background-color bgColor08
width mainDimension * Scale
max-width @width
padding 8px
border-radius 0 0 5px 5px
"""

options : options
Expand Down Expand Up @@ -242,14 +240,13 @@ afterRender: (domEl) ->
if @options.metaPosition is 'inside' and @options.widgetVariant isnt 'small'
meta.delay(3000).fadeOut(500)

div.click(
=>
meta.stop(true,false).fadeIn(250).delay(3000).fadeOut(500)
if @options.stickInCorner is false
div.stop(true,true).animate({zoom: '0.99', boxShadow: '0 0 2px rgba(0,0,0,1.0)'}, 200, 'swing')
div.stop(true,true).animate({zoom: '1.0', boxShadow: '0 20px 40px 0px rgba(0,0,0,0.6)'}, 300, 'swing')
# div.find('.wrapper').stop(true,true).addClass('pushed')
# div.find('.wrapper').stop(true,true).removeClass('pushed')
div.on 'click', =>
(
meta.fadeIn(250).delay(3000).fadeOut(500)
# if @options.stickInCorner is false
# div.stop(true,true).animate({zoom: '0.9', boxShadow: '0 0 2px rgba(0,0,0,1.0)'}, 300, 'swing')
# div.stop(true,true).animate({zoom: '1.0', boxShadow: '0 20px 40px 0px rgba(0,0,0,0.6)'}, 500, 'swing')
# Update
)

# Update the rendered output.
Expand Down
87 changes: 54 additions & 33 deletions Playbox.widget/lib/Get Current Track.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set songMetaFile to (mypath & "songMeta.plist" as string)


if isMusicPlaying() is true then
my pruneCovers()
getSongMeta()
writeSongMeta({"currentPosition" & "##" & currentPosition})

Expand Down Expand Up @@ -135,7 +136,7 @@ on grabCover()
end if
end tell
else if musicapp is "Spotify" then
my getLastfmArt()
my getSpotifyArt()
end if
on error e
logEvent(e)
Expand All @@ -145,7 +146,6 @@ on grabCover()
end grabCover

on getLocaliTunesArt()
do shell script "rm -rf " & readSongMeta({"oldFilename"}) -- delete old artwork
tell application "iTunes" to tell artwork 1 of current track -- get the raw bytes of the artwork into a var
set srcBytes to raw data
if format is Çclass PNG È then -- figure out the proper file extension
Expand All @@ -164,37 +164,50 @@ on getLocaliTunesArt()
set currentCoverURL to getPathItem(currentCoverURL)
end getLocaliTunesArt

on getLastfmArt()
set coverDownloaded to false
set rawXML to ""
set currentCoverURL to "NA"
repeat 5 times
try
set rawXML to (do shell script "curl 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&artist=" & quoted form of (my encodeText(artistName, true, false, 1)) & "&album=" & quoted form of (my encodeText(albumName, true, false, 1)) & "&api_key=" & apiKey & "'")
delay 1
on error e
my logEvent(e & return & rawXML)
end try
if rawXML is not "" then
try
set AppleScript's text item delimiters to "extralarge\">"
set processingXML to text item 2 of rawXML
set AppleScript's text item delimiters to "</image>"
set currentCoverURL to text item 1 of processingXML
set AppleScript's text item delimiters to ""
if currentCoverURL is "" then
my logEvent("Cover art unavailable." & return & rawXML)
set currentCoverURL to "NA"
set coverDownloaded to true
end if
on getSpotifyArt()
try
tell application "Spotify" to set currentCoverURL to artwork url of current track
on error e
my logEvent(e)
set coverDownloaded to false
set rawXML to ""
set currentCoverURL to "NA"
repeat 5 times
try
set rawXML to (do shell script "curl 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&artist=" & my textReplace(artistName, space, "+") & "&album=" & my textReplace(albumName, space, "+") & "&api_key=" & apiKey & "'")
delay 1
on error e
my logEvent(e & return & rawXML)
end try
set coverDownloaded to true
end if
if coverDownloaded is true then exit repeat
end repeat
end getLastfmArt
if rawXML is not "" then
try
set AppleScript's text item delimiters to "extralarge\">"
set processingXML to text item 2 of rawXML
set AppleScript's text item delimiters to "</image>"
set currentCoverURL to text item 1 of processingXML
set AppleScript's text item delimiters to ""
if currentCoverURL is "" then
my logEvent("Cover art unavailable." & return & rawXML)
set currentCoverURL to "NA"
set coverDownloaded to true
end if
on error e
my logEvent(e & return & rawXML)
end try
set coverDownloaded to true
end if
if coverDownloaded is true then exit repeat
end repeat
end try
end getSpotifyArt

on pruneCovers()
try
do shell script "rm '" & readSongMeta({"oldFilename"}) & "'"
on error e
my logEvent(e)
end try
end pruneCovers

on getPathItem(aPath)
set AppleScript's text item delimiters to "/"
Expand Down Expand Up @@ -314,6 +327,15 @@ on number_to_string(this_number)
end if
end number_to_string

on textReplace(sourceText, searchText, replaceText)
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, searchText}
set textItems to every text item of sourceText
set AppleScript's text item delimiters to replaceText
set changedText to textItems as string
set AppleScript's text item delimiters to TID
return changedText
end textReplace

on encodeText(this_text, encode_URL_A, encode_URL_B, method)
--http://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
Expand Down Expand Up @@ -357,9 +379,8 @@ end checkFile
on logEvent(e)
if enableLogging is true then
set e to e as string
tell application "Finder" to set myName to (name of file (path to me))
do shell script "echo '" & (current date) & space & quoted form of e & "' >> ~/Library/Logs/" & quoted form of myName & ".log"
do shell script "echo '" & (current date) & space & e & "' >> ~/Library/Logs/CurrentTrack.log"
else
return
end if
end logEvent
end logEvent

0 comments on commit 500b256

Please sign in to comment.