searches.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const bplist = require('bplist-parser')
  2. const fs = require('fs')
  3. // Derive filenames based on domain + file path
  4. const fileHash = require('../../../util/backup_filehash')
  5. const database = fileHash('Library/Preferences/com.spotify.client.plist', 'AppDomain-com.spotify.client')
  6. module.exports.name = 'spotify.searches'
  7. module.exports.description = 'List associated Spotify account and its usage information'
  8. // Specify this reporter requires a backup.
  9. // The second parameter to func() is now a backup instead of the path to one.
  10. module.exports.requiresBackup = true
  11. // Specify this reporter supports the promises API for allowing chaining of reports.
  12. module.exports.usesPromises = true
  13. module.exports.func = function (program, backup, resolve, reject) {
  14. spotifyReport(backup)
  15. .then((items) => {
  16. var result = program.formatter.format(items, {
  17. program: program,
  18. columns: {
  19. 'Username': el => el.username,
  20. 'Type': el => el.placeholderIconIdentifier ? el.placeholderIconIdentifier.toLowerCase() : 'song',
  21. 'Title': el => el.title,
  22. 'Subtitle': el => el.subtitle
  23. }
  24. })
  25. resolve(result)
  26. })
  27. .catch(reject)
  28. }
  29. const spotifyReport = (backup) => {
  30. return new Promise((resolve, reject) => {
  31. var filename = backup.getFileName(database)
  32. try {
  33. let spotifyData = bplist.parseBuffer(fs.readFileSync(filename))[0]
  34. let spotifyResult = []
  35. // console.log('spotifyData', spotifyData)
  36. // Get spotify username
  37. if (Object.keys(spotifyData).some((key) => ~key.indexOf('.com.spotify'))) {
  38. const keys = Object.keys(spotifyData).filter((key) => ~key.indexOf('.com.spotify'))
  39. const username = keys[0].split('.com.spotify')[0]
  40. // Get spotify search history
  41. const searchHistory = spotifyData[username + '.com.spotify.feature.search.com.spotify.search.fancyRecents']
  42. searchHistory.forEach(element => {
  43. element.username = username
  44. })
  45. spotifyResult = searchHistory
  46. }
  47. resolve(spotifyResult)
  48. } catch (e) {
  49. reject(e)
  50. }
  51. })
  52. }