Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 2.21 KB | None | 0 0
  1. NR<=2 { next }
  2. /^ *dict entry\(/ { ++l; next }
  3. /^ *\)$/ {
  4.     if (typeof(b[l][2]) == "unassigned") {
  5.         for (k in b[l+1])
  6.             b[l][b[l][1]][k] = b[l+1][k]
  7.         delete b[l+1]
  8.     }
  9.     else if (isarray(b[l][2]))
  10.         for (k in b[l][2])
  11.             b[l][b[l][1]][k] = b[l][2][k]
  12.     else
  13.         b[l][b[l][1]]=b[l][2];
  14.     delete b[l][1]
  15.     delete b[l][2]
  16.     n[l] = 0
  17.     --l
  18.     next
  19. }
  20. /^ *\]$/ { n[l] = 0; --l; next }
  21. /^ *variant/ { gsub(/^ *variant/, "") }
  22. {
  23.     match($0, / *(object path|[^ ]+) +(.*)/, a);
  24.     switch (a[1]) {
  25.     case "array": ++l; delete b[l][0]; next
  26.     case "string":
  27.     case "object path":
  28.         strings_to_decode[++sdl][0]=gensub(/"(.*)"/,"\\1",1,a[2])
  29.             v="\a"sdl
  30.         break
  31.     default:
  32.             v=a[2]
  33.     }
  34.     b[l][++n[l]] = v
  35. }
  36. function id(s) { return s }
  37. function decode_string(s) {
  38.     return typeof(s) == "string" && substr(s,1,1) == "\a" \
  39.         ? strings_to_decode[strtonum(substr(s,2))][1] \
  40.         : s
  41. }
  42. function cp(a, b, k, f) {
  43.     if (isarray(a)) {
  44.         b[k][0] = 0; delete b[k][0]
  45.         for (k_ in a)
  46.             cp(a[k_], b[k], @f(k_), f)
  47.     } else
  48.         b[k] = @f(a)
  49. }
  50. function p(i, a, o) {
  51.     if (isarray(a)) {
  52.         if (!o) print ":"
  53.         for (k in a) {
  54.             printf "%s[%s]", i, k
  55.             p("\t"i, a[k])
  56.         }
  57.     }
  58.     else
  59.         printf " [%s]\n", a
  60. }
  61. function decode_strings() { if (length(strings_to_decode) == 0) return
  62.     decode_cmd = "bash -c 'while IFS= read -r s; do printf %b\\\\n \"${s//%/\\\\x}\"; done'"
  63.     for (i in strings_to_decode)
  64.         print strings_to_decode[i][0] |& decode_cmd
  65.     close(decode_cmd, "to")
  66.     i = 0
  67.     while ((decode_cmd |& getline s) > 0)
  68.         strings_to_decode[++i][1] = s
  69.     close(decode_cmd)
  70. }
  71. END {
  72.     decode_strings()
  73.     cp(b[1], dbus, 0, "decode_string")
  74.  
  75.     # uncomment this to see what dbus gives you
  76.     #p("", dbus, 1)
  77.     # uncomment this to see what mediainfo gives you
  78.         #p("", mediainfo, 1)
  79.  
  80.     # example
  81.    
  82.     title = dbus[0]["xesam:title"]
  83.     artist = dbus[0]["xesam:artist"][1]
  84.         rating = dbus[0]["xesam:userRating"] == "1" ? "luv" : dbus[0]["xesam:userRating"]
  85.     MAX_TITLE_LENGTH = 40
  86.     # TRUNCATION_STRING = "..."
  87.     TRUNCATION_STRING = "…"
  88.     if (length(title) > MAX_TITLE_LENGTH)
  89.            title = substr(title, 1, MAX_TITLE_LENGTH - length(TRUNCATION_STRING)) TRUNCATION_STRING
  90.     printf "%s  /  %s  /  %s\n", title, artist, rating
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement