|
2 | 2 | from plexapi import media, utils |
3 | 3 | from plexapi.exceptions import BadRequest, NotFound |
4 | 4 | from plexapi.base import Playable, PlexPartialObject |
5 | | -from plexapi.compat import quote_plus |
| 5 | +from plexapi.compat import quote_plus, urlencode |
6 | 6 | import os |
7 | 7 |
|
8 | 8 |
|
@@ -126,6 +126,82 @@ def posters(self): |
126 | 126 |
|
127 | 127 | return self.fetchItems('%s/posters' % self.key, cls=media.Poster) |
128 | 128 |
|
| 129 | + def optimize(self, title=None, target="", targetTagID=None, locationID=-1, policyScope='all', |
| 130 | + policyValue="", policyUnwatched=0, videoQuality=None, deviceProfile=None): |
| 131 | + """ Optimize item |
| 132 | +
|
| 133 | + locationID (int): -1 in folder with orginal items |
| 134 | + 2 library path |
| 135 | +
|
| 136 | + target (str): custom quality name. |
| 137 | + if none provided use "Custom: {deviceProfile}" |
| 138 | +
|
| 139 | + targetTagID (int): Default quality settings |
| 140 | + 1 Mobile |
| 141 | + 2 TV |
| 142 | + 3 Original Quality |
| 143 | +
|
| 144 | + deviceProfile (str): Android, IOS, Universal TV, Universal Mobile, Windows Phone, |
| 145 | + Windows, Xbox One |
| 146 | +
|
| 147 | + Example: |
| 148 | + Optimize for Mobile |
| 149 | + item.optimize(targetTagID="Mobile") or item.optimize(targetTagID=1") |
| 150 | + Optimize for Android 10 MBPS 1080p |
| 151 | + item.optimize(deviceProfile="Android", videoQuality=10) |
| 152 | + Optimize for IOS Original Quality |
| 153 | + item.optimize(deviceProfile="IOS", videoQuality=-1) |
| 154 | +
|
| 155 | + * see sync.py VIDEO_QUALITIES for additional information for using videoQuality |
| 156 | + """ |
| 157 | + tagValues = [1, 2, 3] |
| 158 | + tagKeys = ["Mobile", "TV", "Original Quality"] |
| 159 | + tagIDs = tagKeys + tagValues |
| 160 | + |
| 161 | + if targetTagID not in tagIDs and (deviceProfile is None or videoQuality is None): |
| 162 | + raise BadRequest('Unexpected or missing quality profile.') |
| 163 | + |
| 164 | + if isinstance(targetTagID, str): |
| 165 | + tagIndex = tagKeys.index(targetTagID) |
| 166 | + targetTagID = tagValues[tagIndex] |
| 167 | + |
| 168 | + if title is None: |
| 169 | + title = self.title |
| 170 | + |
| 171 | + backgroundProcessing = self.fetchItem('/playlists?type=42') |
| 172 | + key = '%s/items?' % backgroundProcessing.key |
| 173 | + params = { |
| 174 | + 'Item[type]': 42, |
| 175 | + 'Item[target]': target, |
| 176 | + 'Item[targetTagID]': targetTagID if targetTagID else '', |
| 177 | + 'Item[locationID]': locationID, |
| 178 | + 'Item[Policy][scope]': policyScope, |
| 179 | + 'Item[Policy][value]': policyValue, |
| 180 | + 'Item[Policy][unwatched]': policyUnwatched |
| 181 | + } |
| 182 | + |
| 183 | + if deviceProfile: |
| 184 | + params['Item[Device][profile]'] = deviceProfile |
| 185 | + |
| 186 | + if videoQuality: |
| 187 | + from plexapi.sync import MediaSettings |
| 188 | + mediaSettings = MediaSettings.createVideo(videoQuality) |
| 189 | + params['Item[MediaSettings][videoQuality]'] = mediaSettings.videoQuality |
| 190 | + params['Item[MediaSettings][videoResolution]'] = mediaSettings.videoResolution |
| 191 | + params['Item[MediaSettings][maxVideoBitrate]'] = mediaSettings.maxVideoBitrate |
| 192 | + params['Item[MediaSettings][audioBoost]'] = '' |
| 193 | + params['Item[MediaSettings][subtitleSize]'] = '' |
| 194 | + params['Item[MediaSettings][musicBitrate]'] = '' |
| 195 | + params['Item[MediaSettings][photoQuality]'] = '' |
| 196 | + |
| 197 | + titleParam = {'Item[title]': title} |
| 198 | + section = self._server.library.sectionByID(self.librarySectionID) |
| 199 | + params['Item[Location][uri]'] = 'library://' + section.uuid + '/item/' + \ |
| 200 | + quote_plus(self.key + '?includeExternalMedia=1') |
| 201 | + |
| 202 | + data = key + urlencode(params) + '&' + urlencode(titleParam) |
| 203 | + return self._server.query(data, method=self._server._session.put) |
| 204 | + |
129 | 205 | def sync(self, videoQuality, client=None, clientId=None, limit=None, unwatched=False, title=None): |
130 | 206 | """ Add current video (movie, tv-show, season or episode) as sync item for specified device. |
131 | 207 | See :func:`plexapi.myplex.MyPlexAccount.sync()` for possible exceptions. |
|
0 commit comments