#!/usr/bin/env python

import requests
from jobs import AbstractJob
from lxml import etree


class Plex(AbstractJob):

    def __init__(self, conf):
        self.interval = conf['interval']
        self.movies = conf['movies']
        self.shows = conf['shows']
        self.timeout = conf.get('timeout')

    def _parse_movies(self, xml):
        tree = etree.fromstring(xml)
        movies = []
        for movie in tree.xpath('/MediaContainer/Video'):
            movies.append({
                'title': movie.get('title'),
                'year': movie.get('year')
            })
        return movies

    def _parse_shows(self, xml):
        tree = etree.fromstring(xml)
        shows = []
        for show in tree.xpath('/MediaContainer/Video'):
            shows.append({
                'name': show.get('grandparentTitle'),
                'title': show.get('title'),
                'episode': show.get('index').zfill(2),
                'season': show.get('parentIndex').zfill(2)
            })
        return shows

    def get(self):
        try:
            r = requests.get(self.movies, timeout=self.timeout)
            movies = self._parse_movies(r.content)

            r = requests.get(self.shows)
            shows = self._parse_shows(r.content)

            return {'movies': movies, 'shows': shows}
        except requests.exceptions.ConnectionError:
            return {}

@@ -65,31 +65,8 @@
 Job%0A
-from lxml import etree%0A
 %0A%0Acl
@@ -296,35 +296,36 @@
 se_movies(self, 
-xml
+data
 ):%0A        tree 
@@ -323,159 +323,17 @@
     
-t
 re
-e = etree.fromstring(xml)%0A        movies = %5B%5D%0A        for movie in tree.xpath('/MediaContainer/Video'):%0A            movies.append(%7B%0A                
+turn %5B%7B
 'tit
@@ -334,28 +334,24 @@
 %5B%7B'title': m
-ovie
 .get('title'
@@ -369,16 +369,17 @@
         
+ 
 'year': 
@@ -379,20 +379,16 @@
 year': m
-ovie
 .get('ye
@@ -391,16 +391,17 @@
 ('year')
+%7D
 %0A       
@@ -409,32 +409,39 @@
     
-%7D)%0A
     
-    return movies
+for m in data%5B'_children'%5D%5D
 %0A%0A  
@@ -465,19 +465,20 @@
 s(self, 
-xml
+data
 ):%0A     
@@ -484,140 +484,90 @@
     
-tree = etree.fromstring(xml)%0A        shows = %5B%5D%0A        for show in tree.xpath('/MediaContainer/Video'):%0A            shows.append(%7B%0A
+return %5B%7B'title': s.get('title'),%0A                 'year': s.get('year'),%0A        
     
@@ -571,23 +571,16 @@
         
-       
 'name': 
@@ -576,27 +576,24 @@
    'name': s
-how
 .get('grandp
@@ -626,163 +626,312 @@
     
-'title': show.get('title'),%0A                'episode': show.get('index').zfill(2),%0A                'season': show.get('parentIndex').zfill(2)%0A            %7D
+ 'episode': '%7B0:02d%7D'.format(s.get('index')),%0A                 'season': '%7B0:02d%7D'.format(s.get('parentIndex'))%7D%0A                for s in data%5B'_children'%5D%5D%0A%0A    def _get_json(self, url):%0A        headers = %7B'Accept': 'application/json'%7D%0A        r = requests.get(url, headers=headers, timeout=self.timeout
 )%0A  
@@ -943,21 +943,24 @@
  return 
-shows
+r.json()
 %0A%0A    de
@@ -1001,58 +1001,41 @@
     
-r = requests.get(self.movies, timeout=self.timeout
+data = self._get_json(self.movies
 )%0A  
@@ -1072,25 +1072,20 @@
 _movies(
-r.content
+data
 )%0A%0A     
@@ -1091,32 +1091,37 @@
         
-r = requests.get
+data = self._get_json
 (self.sh
@@ -1167,17 +1167,12 @@
 ows(
-r.content
+data
 )%0A%0A 

Use JSON API