This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The app had troubles parsing JSON when song's title/artist's name contained quotation marks. Closed #5 .
- Loading branch information
Showing
7 changed files
with
3,367 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
node_modules/ | ||
*.DS_Store* | ||
CatalinaScrobbler-darwin-x64/ | ||
CatalinaScrobbler-cli | ||
|
||
# Logs | ||
logs | ||
|
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,154 @@ | ||
const fs = require('fs'); | ||
var osascript = require('node-osascript'); | ||
|
||
osascript.execute(`try | ||
tell application "Finder" to get application file id "com.apple.Music" | ||
set appExists to true | ||
on error | ||
set appExists to false | ||
end try | ||
return appExists`, function(err, result) { | ||
if (result == true) { | ||
var app = "Music"; | ||
} else if (result == false) { | ||
var app = "iTunes" | ||
} else { // Just in case something does not work. | ||
var app = "Music"; | ||
} | ||
fs.writeFile("/tmp/CurrentPlaying.scpt", `on run | ||
set info to "" | ||
tell application id "com.apple.systemevents" | ||
set num to count (every process whose bundle identifier is "com.apple.` + app + `") | ||
end tell | ||
if num > 0 then | ||
tell application id "com.apple.` + app + `" | ||
if player state is playing then | ||
set track_name to name of current track | ||
set track_artist to the artist of the current track | ||
set track_album to the album of the current track | ||
end if | ||
end tell | ||
end if | ||
return "{\\"artist\\":\\"" & track_artist & "\\", \\"track\\":\\"" & track_name & "\\", \\"album\\":\\"" & track_album & "\\"}" | ||
end run`, function(err) { | ||
if (err) { | ||
return log(err); | ||
} | ||
}); | ||
}); | ||
|
||
var LastfmAPI = require('lastfmapi'); | ||
var Lastfm = require('simple-lastfm'); | ||
const { exec } = require('child_process'); | ||
var Prompt = require('prompt-password'); | ||
var readline = require('readline'); | ||
const dJSON = require('dirty-json'); | ||
|
||
global.timeline = 0 | ||
global.songCount = 0; | ||
|
||
var rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
rl.stdoutMuted = true; | ||
rl.question('Login: ', (login) => { | ||
var prompt = new Prompt({ | ||
type: 'password', | ||
message: 'Password: ', | ||
name: 'password' | ||
}); | ||
prompt.run() | ||
.then(function(password) { | ||
lastFMLogin(login, password); | ||
rl.close(); | ||
}); | ||
}); | ||
|
||
function lastFMLogin(login, pass){ | ||
var lfm = new LastfmAPI({ | ||
'api_key' : '21779adaae15c5fa727a08cd75909df2', | ||
'secret' : '8e2cfe09e0aac01bdc8474c2595d5e68' | ||
}); | ||
var lastfm = new Lastfm({ | ||
api_key: '21779adaae15c5fa727a08cd75909df2', | ||
api_secret: '8e2cfe09e0aac01bdc8474c2595d5e68', | ||
username: login, | ||
password: pass | ||
}); | ||
lastfm.getSessionKey(function(result) { | ||
//console.log("session key = " + result.session_key); | ||
if(result.success) { | ||
lfm.setSessionCredentials(login, result.session_key); | ||
update(lfm); | ||
} else { | ||
console.log("Error: " + result.error); | ||
} | ||
}); | ||
} | ||
|
||
function update(lfm) { | ||
|
||
setInterval(function(){ | ||
console.log("=============================="); | ||
global.timeline += 1; | ||
console.log("Loop: "+global.timeline); | ||
exec('osascript /tmp/CurrentPlaying.scpt', (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`exec error: ${error}`); | ||
console.log("Is music playing?") | ||
global.timeline -= 1; //pause cause not playing anything | ||
return; | ||
} | ||
console.log(`Now Playing: ${stdout}`); | ||
//console.log(`stderr: ${stderr}`); | ||
var obj = dJSON.parse(stdout); | ||
|
||
if (stdout != undefined && stdout != global.playing){ | ||
//song changed | ||
global.previousTime = global.timeline; | ||
global.timeline = 0; | ||
global.songCount += 1; | ||
global.previous = global.playing; | ||
console.log("SongCount: "+global.songCount); | ||
if (global.songCount > 1){ | ||
console.log("Scrobble Previous"); | ||
console.log("Previous: "+global.previous); | ||
var previousSong = dJSON.parse(global.previous); | ||
if (global.previousTime > 12){ //if listened for more than 60 seconds | ||
lfm.track.scrobble({ | ||
'artist' : previousSong.artist, | ||
'track' : previousSong.track, | ||
'timestamp' : Math.floor(Date.now() / 1000) | ||
}, function (err, scrobbles) { | ||
if (err) { return console.log('We\'re in trouble', err); } | ||
|
||
console.log('We have just scrobbled:', scrobbles); | ||
}); | ||
} | ||
global.playing = stdout; | ||
} | ||
else { | ||
//first run not scrobbling | ||
global.playing = stdout; | ||
} | ||
} | ||
|
||
if (stdout != undefined){ | ||
lfm.track.updateNowPlaying({ | ||
'artist' : obj.artist, | ||
'track' : obj.track, | ||
'album' : obj.album | ||
|
||
}, function(err, nowPlaying){ | ||
console.log(nowPlaying); | ||
}) | ||
} | ||
}); | ||
}, 5000); | ||
} | ||
|
||
process.on('SIGINT', function() { | ||
console.log("Caught interrupt signal"); | ||
process.exit(); | ||
}); |
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
Oops, something went wrong.