Passed
Push — master ( aeb165...d9ae97 )
by Dean
03:04
created

PlexBasicCredential.refresh_details()   D

Complexity

Conditions 8

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 63.2889
Metric Value
dl 0
loc 40
ccs 1
cts 21
cp 0.0476
rs 4
cc 8
crap 63.2889
1 1
from plugin.core.environment import Environment
2 1
from plugin.models.m_plex.account import PlexAccount
0 ignored issues
show
Bug introduced by
The name account does not seem to exist in module plugin.models.m_plex.
Loading history...
Configuration introduced by
Unable to import 'plugin.models.m_plex.account' (invalid syntax (<string>, line 139))

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...
3 1
from plugin.models.core import db
4
5 1
from playhouse.apsw_ext import *
0 ignored issues
show
Coding Style introduced by
The usage of wildcard imports like playhouse.apsw_ext should generally be avoided.
Loading history...
6 1
from xml.etree import ElementTree
7 1
import logging
8 1
import requests
9
10 1
log = logging.getLogger(__name__)
11
12
13 1
class PlexBasicCredential(Model):
14 1
    class Meta:
15 1
        database = db
16 1
        db_table = 'plex.credential.basic'
17
18 1
    account = ForeignKeyField(PlexAccount, 'basic_credentials', unique=True)
19
20 1
    password = CharField(null=True)
21
22
    # Authorization
23 1
    token_plex = CharField(null=True)
24 1
    token_server = CharField(null=True)
25
26 1
    @property
27
    def state(self):
28
        if self.token_plex is not None and self.token_server is not None:
29
            return 'valid'
30
31
        if self.password is not None:
32
            return 'warning'
33
34
        return 'empty'
35
36 1
    def refresh(self, save=True):
37
        # Refresh account details
38
        if not self.refresh_details():
39
            return False
40
41
        # Store changes in database
42
        if save:
43
            self.save()
44
45
        return True
46
47 1
    def refresh_details(self):
48
        if self.token_plex is None:
49
            # Missing token
50
            return False
51
52
        if self.token_server is not None:
53
            # Already authenticated
54
            return True
55
56
        if self.token_plex == 'anonymous':
57
            return self.refresh_anonymous()
58
59
        # Fetch server token
60
        response = requests.get('https://plex.tv/api/resources?includeHttps=1', headers={
61
            'X-Plex-Token': self.token_plex
62
        })
63
64
        if not (200 <= response.status_code < 300):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after not.
Loading history...
65
            # Invalid response
66
            return False
67
68
        devices = ElementTree.fromstring(response.content)
69
70
        # Find server
71
        server = None
72
73
        for s in devices.findall('Device'):
74
            if s.attrib.get('clientIdentifier') != Environment.platform.machine_identifier:
75
                continue
76
77
            server = s
78
79
        if server is None:
80
            log.warn('Unable to find server with identifier: %r', Environment.platform.machine_identifier)
81
            return False
82
83
        # Update `token_server`
84
        self.token_server = server.attrib.get('accessToken')
85
86
        return True
87
88 1
    def refresh_anonymous(self):
89
        log.debug('Refreshing anonymous plex credential')
90
91
        self.token_server = 'anonymous'
92
93
        return True
94
95 1
    def to_json(self, account):
96
        result = {
97
            'state': self.state,
98
99
            'username': account.username
100
        }
101
102
        if self.password:
103
            result['password'] = '*' * len(self.password)
104
        elif self.token_plex:
105
            result['password'] = '*' * 8
106
107
        return result
108