| Conditions | 7 |
| Total Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 56 |
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.warn('Missing session key, unable to fetch session details') |
||
| 86 | return result |
||
| 87 | |||
| 88 | # Retrieve session details |
||
| 89 | log.debug('Fetching details for session #%s', session_key) |
||
| 90 | |||
| 91 | p_item = Plex['status'].sessions().get(session_key) |
||
| 92 | |||
| 93 | if not p_item: |
||
| 94 | log.warn('Unable to find session with key %r', session_key) |
||
| 95 | return result |
||
| 96 | |||
| 97 | # Retrieve metadata and guid |
||
| 98 | p_metadata, guid = self.get_metadata(p_item.rating_key) |
||
| 99 | |||
| 100 | if not p_metadata: |
||
| 101 | log.warn('Unable to retrieve metadata for rating_key %r', p_item.rating_key) |
||
| 102 | return result |
||
| 103 | |||
| 104 | if not guid: |
||
| 105 | return merge(result, { |
||
| 106 | 'duration': p_metadata.duration, |
||
| 107 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
| 108 | }) |
||
| 109 | |||
| 110 | try: |
||
| 111 | # Create/Retrieve `Client` for session |
||
| 112 | result['client'] = ClientManager.get.or_create( |
||
| 113 | p_item.session.player, |
||
| 114 | |||
| 115 | fetch=True, |
||
| 116 | match=True, |
||
| 117 | filtered_exception=True |
||
| 118 | ) |
||
| 119 | |||
| 120 | # Create/Retrieve `User` for session |
||
| 121 | result['user'] = UserManager.get.or_create( |
||
| 122 | p_item.session.user, |
||
| 123 | |||
| 124 | fetch=True, |
||
| 125 | match=True, |
||
| 126 | filtered_exception=True |
||
| 127 | ) |
||
| 128 | |||
| 129 | # Pick account from `client` or `user` objects |
||
| 130 | result['account'] = self.get_account(result) |
||
| 131 | except FilteredException: |
||
| 132 | log.debug('Activity has been filtered') |
||
| 133 | |||
| 134 | result['client'] = None |
||
| 135 | result['user'] = None |
||
| 136 | |||
| 137 | result['account'] = None |
||
| 138 | |||
| 139 | return merge(result, { |
||
| 140 | 'duration': p_metadata.duration, |
||
| 141 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
| 142 | }) |
||
| 150 |