## 2018-04-30 #Plex

### Playing with Polymorphic identifiers and https://www.plex.tv/ 

- build a PolymorphicIdentifier for [plex](search://plex-scheme.js)
- hacked my way around the missing "?" operator 
  ```javascript
   function getDeepProperty(obj, pathString) {
          var path = pathString.split(".");
          var next;
          var result = obj;
          while (next = path.shift()) {
            var nextResult = result[next];
            if (!nextResult) return; // could not resolve path
            result = nextResult;
          }
          return result;
        }
  ```
- this allows to access deeply nested data
  ```javascript
  getDeepProperty(movies, "children.3.media.0.part.0.file")
  ```


### Creating a Movie List

```javascript
import {getDeepProperty} from "utils"
var movies
var list
(async () => {
  movies = await fetch("plex://library/sections/1/all", {
    method: "GET",
    headers: {
      "content-type": "application/json"
    }
  }).then(r => r.json())
        
  list = movies.children.map(ea => {
    var obj = {
      title: ea.title,
      rating: ea.rating,
      file: getDeepProperty(ea, "media.0.part.0.file")
    }
    return obj
  })
})()
```