| Conditions | 21 |
| Total Lines | 122 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 435.05 |
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:
Complex classes like Shows.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | 1 | from plugin.core.constants import GUID_SERVICES |
|
| 21 | 1 | @elapsed.clock |
|
| 22 | def run(self): |
||
| 23 | # Retrieve show sections |
||
| 24 | p_sections, p_sections_map = self.sections('show') |
||
| 25 | |||
| 26 | # Fetch episodes with account settings |
||
| 27 | p_shows, p_seasons, p_episodes = self.plex.library.episodes.mapped( |
||
| 28 | p_sections, ([ |
||
| 29 | MetadataItem.library_section |
||
| 30 | ], [], []), |
||
| 31 | account=self.current.account.plex.key, |
||
| 32 | parse_guid=True |
||
| 33 | ) |
||
| 34 | |||
| 35 | # TODO process seasons |
||
| 36 | |||
| 37 | # Calculate total number of episodes |
||
| 38 | pending = {} |
||
| 39 | |||
| 40 | for data in self.get_data(SyncMedia.Episodes): |
||
| 41 | t_episodes = [ |
||
| 42 | (key, se, ep) |
||
| 43 | for key, t_show in self.trakt[(SyncMedia.Episodes, data)].items() |
||
| 44 | for se, t_season in t_show.seasons.items() |
||
| 45 | for ep in t_season.episodes.iterkeys() |
||
| 46 | ] |
||
| 47 | |||
| 48 | if data not in pending: |
||
| 49 | pending[data] = {} |
||
| 50 | |||
| 51 | for key in t_episodes: |
||
| 52 | pending[data][key] = False |
||
| 53 | |||
| 54 | # Task started |
||
| 55 | unsupported_shows = {} |
||
| 56 | |||
| 57 | # Process shows |
||
| 58 | for sh_id, guid, p_show in p_shows: |
||
| 59 | if not guid or guid.service not in GUID_SERVICES: |
||
| 60 | mark_unsupported(unsupported_shows, sh_id, guid) |
||
| 61 | continue |
||
| 62 | |||
| 63 | key = (guid.service, guid.id) |
||
| 64 | |||
| 65 | # Try retrieve `pk` for `key` |
||
| 66 | pk = self.trakt.table('shows').get(key) |
||
| 67 | |||
| 68 | # Store in item map |
||
| 69 | self.current.map.add(p_show.get('library_section'), sh_id, [key, pk]) |
||
| 70 | |||
| 71 | if pk is None: |
||
| 72 | # No `pk` found |
||
| 73 | continue |
||
| 74 | |||
| 75 | # Execute data handlers |
||
| 76 | for data in self.get_data(SyncMedia.Shows): |
||
| 77 | t_show = self.trakt[(SyncMedia.Shows, data)].get(pk) |
||
| 78 | |||
| 79 | # Execute show handlers |
||
| 80 | self.execute_handlers( |
||
| 81 | SyncMedia.Shows, data, |
||
| 82 | key=sh_id, |
||
| 83 | |||
| 84 | p_item=p_show, |
||
| 85 | t_item=t_show |
||
| 86 | ) |
||
| 87 | |||
| 88 | # Process episodes |
||
| 89 | for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in p_episodes: |
||
| 90 | if not guid or guid.service not in GUID_SERVICES: |
||
| 91 | mark_unsupported(unsupported_shows, ids['show'], guid) |
||
| 92 | continue |
||
| 93 | |||
| 94 | key = (guid.service, guid.id) |
||
| 95 | |||
| 96 | # Try retrieve `pk` for `key` |
||
| 97 | pk = self.trakt.table('shows').get(key) |
||
| 98 | |||
| 99 | if pk is None: |
||
| 100 | # No `pk` found |
||
| 101 | continue |
||
| 102 | |||
| 103 | if not ids.get('episode'): |
||
| 104 | # Missing `episode` rating key |
||
| 105 | continue |
||
| 106 | |||
| 107 | for data in self.get_data(SyncMedia.Episodes): |
||
| 108 | t_show = self.trakt[(SyncMedia.Episodes, data)].get(pk) |
||
| 109 | |||
| 110 | if t_show is None: |
||
| 111 | # Unable to find matching show in trakt data |
||
| 112 | continue |
||
| 113 | |||
| 114 | t_season = t_show.seasons.get(season_num) |
||
| 115 | |||
| 116 | if t_season is None: |
||
| 117 | # Unable to find matching season in `t_show` |
||
| 118 | continue |
||
| 119 | |||
| 120 | t_episode = t_season.episodes.get(episode_num) |
||
| 121 | |||
| 122 | if t_episode is None: |
||
| 123 | # Unable to find matching episode in `t_season` |
||
| 124 | continue |
||
| 125 | |||
| 126 | self.execute_handlers( |
||
| 127 | SyncMedia.Episodes, data, |
||
| 128 | key=ids['episode'], |
||
| 129 | |||
| 130 | p_item=p_episode, |
||
| 131 | t_item=t_episode |
||
| 132 | ) |
||
| 133 | |||
| 134 | # Increment one step |
||
| 135 | self.step(pending, data, (pk, season_num, episode_num)) |
||
| 136 | |||
| 137 | # Task checkpoint |
||
| 138 | self.checkpoint() |
||
| 139 | |||
| 140 | # Log details |
||
| 141 | log_unsupported(log, 'Found %d unsupported show(s)\n%s', unsupported_shows) |
||
| 142 | log.debug('Pending: %r', pending) |
||
| 143 |