python-plexapi/plexapi/sync.py

93 lines
2.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2017-01-02 21:06:40 +00:00
2014-12-29 03:21:58 +00:00
import requests
from plexapi import utils
2014-12-29 03:21:58 +00:00
from plexapi.exceptions import NotFound
class SyncItem(object):
2017-01-02 21:06:40 +00:00
"""Summary
2017-01-02 21:06:40 +00:00
Attributes:
device (TYPE): Description
id (TYPE): Description
location (TYPE): Description
machineIdentifier (TYPE): Description
MediaSettings (TYPE): Description
metadataType (TYPE): Description
policy (TYPE): Description
rootTitle (TYPE): Description
servers (TYPE): Description
status (TYPE): Description
title (TYPE): Description
version (TYPE): Description
"""
2014-12-29 03:21:58 +00:00
def __init__(self, device, data, servers=None):
2017-01-02 21:06:40 +00:00
"""Summary
Args:
device (TYPE): Description
data (TYPE): Description
servers (None, optional): Description
"""
2014-12-29 03:21:58 +00:00
self.device = device
self.servers = servers
self.id = utils.cast(int, data.attrib.get('id'))
self.version = utils.cast(int, data.attrib.get('version'))
2014-12-29 03:21:58 +00:00
self.rootTitle = data.attrib.get('rootTitle')
self.title = data.attrib.get('title')
self.metadataType = data.attrib.get('metadataType')
self.machineIdentifier = data.find('Server').get('machineIdentifier')
self.status = data.find('Status').attrib.copy()
self.MediaSettings = data.find('MediaSettings').attrib.copy()
self.policy = data.find('Policy').attrib.copy()
self.location = data.find('Location').attrib.copy()
def __repr__(self):
2017-01-02 21:06:40 +00:00
"""Summary
Returns:
TYPE: Description
"""
return '<%s:%s>' % (self.__class__.__name__, self.id)
2014-12-29 03:21:58 +00:00
def server(self):
2017-01-02 21:06:40 +00:00
"""Summary
Returns:
TYPE: Description
Raises:
NotFound: Description
"""
server = list(filter(lambda x: x.machineIdentifier ==
self.machineIdentifier, self.servers))
2014-12-29 03:21:58 +00:00
if 0 == len(server):
2017-01-02 21:06:40 +00:00
raise NotFound('Unable to find server with uuid %s' %
self.machineIdentifier)
2014-12-29 03:21:58 +00:00
return server[0]
def getMedia(self):
2017-01-02 21:06:40 +00:00
"""Summary
Returns:
TYPE: Description
"""
2014-12-29 03:21:58 +00:00
server = self.server().connect()
items = utils.listItems(server, '/sync/items/%s' % self.id)
2014-12-29 03:21:58 +00:00
return items
def markAsDone(self, sync_id):
2017-01-02 21:06:40 +00:00
"""Summary
Args:
sync_id (TYPE): Description
Returns:
TYPE: Description
"""
2014-12-29 03:21:58 +00:00
server = self.server().connect()
2017-01-02 21:06:40 +00:00
url = '/sync/%s/%s/files/%s/downloaded' % (
self.device.clientIdentifier, server.machineIdentifier, sync_id)
server.query(url, method=requests.put)