Skip to content

Commit

Permalink
use external IDs from new agent if available (only for scrobbling) (#571
Browse files Browse the repository at this point in the history
)

* use external IDs from new agent if available (only for scrobbling for now)

* create taggings DB model (prep for sync)

* create tags DB model (prep for sync)

* (sync support) first attempt to make DB use external IDs, fallback to guid

* some fixes (still broken for sync)

* Sync now working for movies (TV groundwork still pending)

* Finished sync preparations (hopefully) for Native TV agent

* Update README to include min reqs for native agents
  • Loading branch information
rg9400 authored Aug 31, 2020
1 parent f9dc5ec commit aeb0bfb
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Trakt.tv (for Plex)
Now compatible with the Native Plex Movie Agent (should also be compatible with the upcoming Native Plex TV Agent). If you use the new agents, you require at least PMS version 1.20.1.3213 or higher, and you have to do a metadata refresh on the entire library after upgrading to this version. This plugin should be fully backwards compatible with the earlier supported agents.

I cannot support other issues with the plugin outside of the new agent support.

[![](https://img.shields.io/badge/license-GPLv3-blue.svg?style=flat-square)][license] [![](https://img.shields.io/requires/github/fuzeman/Plex-Trakt-Scrobbler.svg?style=flat-square)][requires.io] [![](https://img.shields.io/scrutinizer/build/g/fuzeman/Plex-Trakt-Scrobbler.svg?style=flat-square)][scrutinizer] [![](https://img.shields.io/scrutinizer/g/fuzeman/Plex-Trakt-Scrobbler.svg?style=flat-square)][scrutinizer] [![](https://img.shields.io/scrutinizer/coverage/g/fuzeman/Plex-Trakt-Scrobbler.svg?style=flat-square)][scrutinizer]
[![](https://img.shields.io/gitter/room/trakt/Plex-Trakt-Scrobbler.svg?style=social)][gitter.im]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from plex.objects.core.base import Descriptor, Property


class Guid(Descriptor):
id = Property(type=str)

@classmethod
def from_node(cls, client, node):
return cls.construct(client, cls.helpers.find(node, 'Guid'), child=True)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from plex.objects.core.base import Property
from plex.objects.library.extra.guid import Guid
from plex.objects.library.metadata.season import Season
from plex.objects.library.metadata.show import Show
from plex.objects.library.metadata.base import Metadata
Expand All @@ -11,6 +12,15 @@
class Episode(Video, Metadata, PlaylistItemMixin, RateMixin, ScrobbleMixin):
show = Property(resolver=lambda: Episode.construct_show)
season = Property(resolver=lambda: Episode.construct_season)
guids = Property(resolver=lambda: Guid.from_node)
agent_guid = Property('guid')

@property
def guid(self):
try:
return self.guids.id
except:
return self.agent_guid

index = Property(type=int)
absolute_index = Property('absoluteIndex', int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from plex.objects.library.extra.country import Country
from plex.objects.library.extra.genre import Genre
from plex.objects.library.extra.role import Role
from plex.objects.library.extra.guid import Guid
from plex.objects.library.metadata.base import Metadata
from plex.objects.library.video import Video
from plex.objects.mixins.playlist_item import PlaylistItemMixin
Expand All @@ -13,6 +14,15 @@ class Movie(Video, Metadata, PlaylistItemMixin, RateMixin, ScrobbleMixin):
country = Property(resolver=lambda: Country.from_node)
genres = Property(resolver=lambda: Genre.from_node)
roles = Property(resolver=lambda: Role.from_node)
guids = Property(resolver=lambda: Guid.from_node)
agent_guid = Property('guid')

@property
def guid(self):
try:
return self.guids.id
except:
return self.agent_guid

def __repr__(self):
return '<Movie %r (%s)>' % (self.title, self.year)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from plex.objects.core.base import Property
from plex.objects.library.extra.guid import Guid
from plex.objects.library.container import ChildrenContainer
from plex.objects.library.metadata.show import Show
from plex.objects.library.metadata.base import Metadata
Expand All @@ -11,6 +12,16 @@ class Season(Directory, Metadata, RateMixin):

index = Property(type=int)

guids = Property(resolver=lambda: Guid.from_node)
agent_guid = Property('guid')

@property
def guid(self):
try:
return self.guids.id
except:
return self.agent_guid

banner = Property
theme = Property

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from plex.objects.core.base import Property
from plex.objects.library.extra.guid import Guid
from plex.objects.directory import Directory
from plex.objects.library.container import LeavesContainer, ChildrenContainer
from plex.objects.library.metadata.base import Metadata
Expand All @@ -8,6 +9,15 @@
class Show(Directory, Metadata, RateMixin):
index = Property(type=int)
duration = Property(type=int)
guids = Property(resolver=lambda: Guid.from_node)
agent_guid = Property('guid')

@property
def guid(self):
try:
return self.guids.id
except:
return self.agent_guid

banner = Property
theme = Property
Expand Down
100 changes: 84 additions & 16 deletions Trakttv.bundle/Contents/Libraries/Shared/plex_database/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,24 @@ def mapped(self, sections, fields=None, account=None, where=None, parse_guid=Fal
if fields is None:
fields = []

# Build subquery
subq = (Taggings
.select(
Tags.tag_type,
Tags.tag,
Taggings.metadata_item
)
.join(Tags, on=(Tags.id == Taggings.tag).alias('taggings'))
.where(Tags.tag_type == 314)
.order_by(Tags.id.asc())
.switch(Taggings)
.alias('subq')
)

fields = [
MetadataItem.id,
MetadataItem.guid,
subq.c.tag,

MediaPart.duration,
MediaPart.file,
Expand All @@ -304,6 +319,8 @@ def mapped(self, sections, fields=None, account=None, where=None, parse_guid=Fal

# Build query
query = (MetadataItem.select(*fields)
.join(subq, JOIN_LEFT_OUTER, on=(subq.c.metadata_item_id == MetadataItem.id).alias('tags'))
.switch(MetadataItem)
.join(MediaItem, on=(MediaItem.metadata_item == MetadataItem.id).alias('media'))
.join(MediaPart, on=(MediaPart.media_item == MediaItem.id).alias('part'))
.switch(MetadataItem)
Expand All @@ -318,7 +335,9 @@ def mapped(self, sections, fields=None, account=None, where=None, parse_guid=Fal
query = self._join(query, models, account, [
MetadataItemSettings,
MediaItem,
MediaPart
MediaPart,
Taggings,
Tags
])

# Apply `WHERE` filter
Expand All @@ -331,13 +350,15 @@ def mapped(self, sections, fields=None, account=None, where=None, parse_guid=Fal

def movies_iterator():
for row in self._tuple_iterator(query):
id, guid, movie = self._parse(fields, row, offset=2)
id, guid, tag, movie = self._parse(fields, row, offset=3)

# Parse `guid` (if enabled, and not already parsed)
if parse_guid:
if id not in guids:
guids[id] = Guid.parse(guid, strict=True)

if tag:
guids[id] = Guid.parse(tag, strict=True)
else:
guids[id] = Guid.parse(guid, strict=True)
guid = guids[id]

# Return item
Expand All @@ -351,23 +372,66 @@ def __call__(self, sections, fields=None, account=None, where=None):
# Set default `select()` fields
if fields is None:
fields = []

subq = (Taggings
.select(
Tags.tag_type,
Tags.tag,
Taggings.metadata_item
)
.join(Tags, on=(Tags.id == Taggings.tag).alias('taggings'))
.where(Tags.tag_type == 314)
.order_by(Tags.id.asc())
.switch(Taggings)
.alias('subq')
)

fields = [
MetadataItem.id,
MetadataItem.guid
MetadataItem.guid,
subq.c.tag
] + fields

# Build query
query = self.query(
sections,
fields=fields,
account=account,
where=where
query = (MetadataItem.select(*fields)
.join(subq, JOIN_LEFT_OUTER, on=(subq.c.metadata_item_id == MetadataItem.id).alias('tags'))
.switch(MetadataItem)
)

if account and type(account) is Account:
account = account.id

# Map `Section` list to ids
section_ids = [id for (id, ) in sections]

# Build `where()` query
if where is None:
where = []

where += [
MetadataItem.library_section << section_ids,
MetadataItem.metadata_type == MetadataItemType.Show
]

# Join settings
query = self._join_settings(query, account, MetadataItem)

# Join extra models
models = self._models(fields, account)

query = self._join(query, models, account, [
MetadataItemSettings,
Taggings,
Tags
])

# Apply `WHERE` filter
query = query.where(
*where
)

# Parse rows
return [
self._parse(fields, row, offset=2)
self._parse(fields, row, offset=3)
for row in self._tuple_iterator(query)
]

Expand Down Expand Up @@ -627,12 +691,16 @@ def mapped_shows(self, sections, fields=None, account=None):

# Retrieve shows
shows = Library.shows(sections, fields, account)
output = {}
for (id, guid, tag, show) in shows:
if id not in output:
if tag:
output[id] = (tag, show)
else:
output[id] = (guid, show)

# Map shows by `id`
return dict([
(id, (guid, show))
for (id, guid, show) in shows
])
return output

def mapped_seasons(self, sections, fields=None, account=None):
# Parse `fields`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from plex_database.models.media_part import MediaPart
from plex_database.models.metadata_item import MetadataItem, MetadataItemType
from plex_database.models.metadata_item_settings import MetadataItemSettings
from plex_database.models.tags import Tags
from plex_database.models.taggings import Taggings

# Model aliases
Season = MetadataItem.alias()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from plex_database.core import db

from peewee import *
from plex_database.models import MetadataItem
from plex_database.models import Tags


class Taggings(Model):
class Meta:
database = db
db_table = 'taggings'

metadata_item = ForeignKeyField(MetadataItem, null=True, related_name='taggings')
tag = ForeignKeyField(Tags, null=True, related_name='taggings')

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from plex_database.core import db

from peewee import *


class Tags(Model):
class Meta:
database = db
db_table = 'tags'

tag = CharField(null=True)
tag_type = IntegerField(null=True)

Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@
'type': int
},

#
# Native Agents
#

'imdb': {
'media': ['movie', 'show', 'season', 'episode'],
'service': 'imdb'
},

'tmdb': {
'media': ['movie', 'show', 'season', 'episode'],
'service': 'tmdb',
'type': int
},

'tvdb': {
'media': ['show', 'season', 'episode'],
'service': 'tvdb',
'type': int
},

#
# Misc
#
Expand Down

0 comments on commit aeb0bfb

Please sign in to comment.