Conditions | 16 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
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 Player._detect_item() 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 | #!/usr/bin/python |
||
34 | def _detect_item(self): |
||
35 | self._item = {} |
||
36 | _data = json.loads(xbmc.executeJSONRPC(json.dumps({ |
||
37 | "jsonrpc": "2.0", "method": "Player.GetItem", |
||
38 | "params": { |
||
39 | "properties": ["showtitle", "title", "season", "episode", "file", "tvshowid", "imdbnumber","genre" ,"year"], |
||
40 | "playerid": 1}, |
||
41 | "id": "VideoGetItem"})))["result"]["item"] |
||
42 | is_tv = _data["tvshowid"] != -1 and _data["season"] > 0 and _data["episode"] > 0 |
||
43 | if is_tv: |
||
44 | _data["imdbnumber"] = json.loads(xbmc.executeJSONRPC(json.dumps({ |
||
45 | "jsonrpc": "2.0", "method": "VideoLibrary.GetTVShowDetails", |
||
46 | "params": {"tvshowid": _data["tvshowid"], "properties": ["imdbnumber"]}, |
||
47 | "id": 1 |
||
48 | })))["result"]["tvshowdetails"]["imdbnumber"] |
||
49 | |||
50 | log("Full: {0}".format(_data)) |
||
51 | if ("imdbnumber" not in _data or _data["imdbnumber"] == '') and _data['file']: |
||
52 | _r = self._api.detect_by_file(filename=_data['file']) |
||
53 | if isinstance(_r, dict) and "type" in _r: |
||
54 | if _r["type"] == "episode": |
||
55 | # TESTED |
||
56 | if "episode" in _r: |
||
57 | self._item = { |
||
58 | "type": "episodes", |
||
59 | "title": _r["show"]["title"], |
||
60 | "simkl": _r["episode"]["ids"]["simkl"], |
||
61 | "season": _r["episode"]["season"], |
||
62 | "episode": _r["episode"]["episode"] |
||
63 | } |
||
64 | elif _r["type"] == "movie" and "movie" in _r: |
||
65 | # TESTED |
||
66 | self._item = { |
||
67 | "type": "movies", |
||
68 | "title": _r["movie"]["title"], |
||
69 | "year": _r["movie"]["year"], |
||
70 | "simkl": _r["movie"]["ids"]["simkl"] |
||
71 | } |
||
72 | |||
73 | if not self._item and (_data["title"] or _data["showtitle"]): |
||
74 | if is_tv: |
||
75 | # TESTED |
||
76 | self._item = { |
||
77 | "type": "shows", |
||
78 | "title": _data["showtitle"], |
||
79 | "tvdb": _data["imdbnumber"], |
||
80 | "season": _data["season"], |
||
81 | "episode": _data["episode"] |
||
82 | } |
||
83 | else: |
||
84 | # TESTED |
||
85 | self._item = { |
||
86 | "type": "movies", |
||
87 | "title": _data["title"], |
||
88 | "year": _data["year"], |
||
89 | "imdb": _data["imdbnumber"], |
||
90 | } |
||
91 | |||
92 | if self._item: |
||
93 | self._run_tracker() |
||
94 | |||
166 | return None |