Passed
Push — develop ( 456c9f...9a88f4 )
by Dean
03:13
created

Base.build_request()   D

Complexity

Conditions 9

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90
Metric Value
cc 9
dl 0
loc 50
ccs 0
cts 26
cp 0
crap 90
rs 4
1
from plugin.core.filters import Filters
0 ignored issues
show
Bug introduced by
The name filters does not seem to exist in module plugin.core.
Loading history...
Configuration introduced by
Unable to import 'plugin.core.filters' (invalid syntax (<string>, line 196))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
2
from plugin.core.helpers.variable import merge
3
from plugin.core.identifier import Identifier
4
from plugin.managers.session.base import UpdateSession
0 ignored issues
show
Bug introduced by
The name base does not seem to exist in module plugin.managers.session.
Loading history...
Configuration introduced by
Unable to import 'plugin.managers.session.base' (invalid syntax (<string>, line 63))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6
from plex.objects.library.metadata.episode import Episode
7
from plex.objects.library.metadata.movie import Movie
8
from plex_metadata import Metadata, Guid
9
import logging
10
11
log = logging.getLogger(__name__)
12
13
14
class Base(object):
15
    name = None
16
17
    @classmethod
18
    def build_request(cls, session, rating_key=None, view_offset=None):
19
        # Retrieve metadata for session
20
        if rating_key is None:
21
            rating_key = session.rating_key
22
23
        # Retrieve metadata
24
        metadata = Metadata.get(rating_key)
25
26
        # Queue a flush for the metadata cache
27
        Metadata.cache.flush_queue()
28
29
        # Validate metadata
30
        if not metadata:
31
            log.warn('Unable to retrieve metadata for rating_key %r', rating_key)
32
            return None
33
34
        if metadata.type not in ['movie', 'episode']:
35
            log.info('Ignoring session with type %r for rating_key %r', metadata.type, rating_key)
36
            return None
37
38
        # Apply library/section filter
39
        if not Filters.is_valid_metadata_section(metadata):
40
            return None
41
42
        # Parse guid
43
        guid = Guid.parse(metadata.guid)
44
45
        # Build request from guid/metadata
46
        if type(metadata) is Movie:
47
            result = cls.build_movie(metadata, guid)
48
        elif type(metadata) is Episode:
49
            result = cls.build_episode(metadata, guid)
50
        else:
51
            return None
52
53
        if not result:
54
            return None
55
56
        # Retrieve media progress
57
        if view_offset is not None:
58
            # Calculate progress from `view_offset` parameter
59
            progress = UpdateSession.get_progress(metadata.duration, view_offset)
60
        else:
61
            # Use session progress
62
            progress = session.progress
63
64
        # Merge progress into request
65
        return merge(result, {
66
            'progress': progress
67
        })
68
69
    @staticmethod
70
    def build_episode(episode, guid):
71
        ids = Identifier.get_ids(guid, strict=False)
72
73
        if not ids:
74
            return None
75
76
        return {
77
            'show': {
78
                'title': episode.show.title,
79
                'year': episode.year,
80
81
                'ids': ids
82
            },
83
            'episode': {
84
                'title': episode.title,
85
86
                'season': episode.season.index,
87
                'number': episode.index
88
            }
89
        }
90
91
    @staticmethod
92
    def build_movie(movie, guid):
93
        ids = Identifier.get_ids(guid, strict=False)
94
95
        if not ids:
96
            return None
97
98
        return {
99
            'movie': {
100
                'title': movie.title,
101
                'year': movie.year,
102
103
                'ids': ids
104
            }
105
        }
106
107
    @staticmethod
108
    def session_jumped(session, view_offset):
109
        if session.view_offset is None or view_offset is None:
110
            return False
111
112
        view_delta = view_offset - session.view_offset
113
114
        jump_offset = session.duration - session.view_offset - view_delta
115
        jump_perc = float(view_delta) / session.duration
116
117
        if jump_perc >= 0.98 and jump_offset < 1000:
118
            log.info('Session jumped: %r -> %r (delta: %r, jump_offset: %r, jump_perc: %r)', session.view_offset, view_offset, view_delta, jump_offset, jump_perc)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (162/120).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
119
            return True
120
121
        return False
122