| Conditions | 8 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 72 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from plugin.core.helpers.variable import to_integer, merge, resolve |
||
| 63 | def to_dict(self, obj, info, fetch=False): |
||
| 64 | fetch = resolve(fetch, obj, info) |
||
| 65 | |||
| 66 | view_offset = to_integer(info.get('viewOffset')) |
||
| 67 | |||
| 68 | result = { |
||
| 69 | 'rating_key': to_integer(info.get('ratingKey')), |
||
| 70 | 'view_offset': view_offset, |
||
| 71 | |||
| 72 | 'updated_at': datetime.utcnow() |
||
| 73 | } |
||
| 74 | |||
| 75 | if not fetch: |
||
| 76 | # Return simple update |
||
| 77 | return merge(result, { |
||
| 78 | 'progress': self.get_progress(obj.duration, view_offset) |
||
| 79 | }) |
||
| 80 | |||
| 81 | # Retrieve session key |
||
| 82 | session_key = to_integer(info.get('sessionKey')) |
||
| 83 | |||
| 84 | if not session_key: |
||
| 85 | log.info('Missing session key, unable to fetch session details') |
||
| 86 | return result |
||
| 87 | |||
| 88 | # Retrieve active sessions |
||
| 89 | log.debug('Fetching details for session #%s', session_key) |
||
| 90 | |||
| 91 | p_sessions = Plex['status'].sessions() |
||
| 92 | |||
| 93 | if not p_sessions: |
||
| 94 | log.info('Unable to retrieve active sessions') |
||
| 95 | return result |
||
| 96 | |||
| 97 | # Find session matching `session_key` |
||
| 98 | p_item = p_sessions.get(session_key) |
||
| 99 | |||
| 100 | if not p_item: |
||
| 101 | log.info('Unable to find session with key %r', session_key) |
||
| 102 | return result |
||
| 103 | |||
| 104 | # Retrieve metadata and guid |
||
| 105 | p_metadata, guid = self.get_metadata(p_item.rating_key) |
||
| 106 | |||
| 107 | if not p_metadata: |
||
| 108 | log.info('Unable to retrieve metadata for rating_key %r', p_item.rating_key) |
||
| 109 | return result |
||
| 110 | |||
| 111 | if not guid: |
||
| 112 | return merge(result, { |
||
| 113 | 'duration': p_metadata.duration, |
||
| 114 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
| 115 | }) |
||
| 116 | |||
| 117 | try: |
||
| 118 | # Create/Retrieve `Client` for session |
||
| 119 | result['client'] = ClientManager.get.or_create( |
||
| 120 | p_item.session.player, |
||
| 121 | |||
| 122 | fetch=True, |
||
| 123 | match=True, |
||
| 124 | filtered_exception=True |
||
| 125 | ) |
||
| 126 | |||
| 127 | # Create/Retrieve `User` for session |
||
| 128 | result['user'] = UserManager.get.or_create( |
||
| 129 | p_item.session.user, |
||
| 130 | |||
| 131 | fetch=True, |
||
| 132 | match=True, |
||
| 133 | filtered_exception=True |
||
| 134 | ) |
||
| 135 | |||
| 136 | # Pick account from `client` or `user` objects |
||
| 137 | result['account'] = self.get_account(result) |
||
| 138 | except FilteredException: |
||
| 139 | log.debug('Activity has been filtered') |
||
| 140 | |||
| 141 | result['client'] = None |
||
| 142 | result['user'] = None |
||
| 143 | |||
| 144 | result['account'] = None |
||
| 145 | |||
| 146 | return merge(result, { |
||
| 147 | 'duration': p_metadata.duration, |
||
| 148 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
| 149 | }) |
||
| 157 |