| Conditions | 13 |
| Total Lines | 134 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 Engine.synclibrary() 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 |
||
| 17 | def synclibrary(self, mode="quick"): |
||
| 18 | """ |
||
| 19 | Fetches the library from Simkl to Kodi |
||
| 20 | Mode can be "quick" or "full" |
||
| 21 | """ |
||
| 22 | xbmc.log("Simkl: Syncing library (Simkl to Kodi)") |
||
| 23 | |||
| 24 | mode = "full" |
||
| 25 | |||
| 26 | if mode == "full": |
||
| 27 | todump = { |
||
| 28 | "jsonrpc": "2.0", |
||
| 29 | "method": "VideoLibrary.GetMovies", |
||
| 30 | "params": { |
||
| 31 | "limits": { |
||
| 32 | "start": 0, |
||
| 33 | "end": 1000 |
||
| 34 | }, |
||
| 35 | "properties": [ |
||
| 36 | "playcount", |
||
| 37 | "imdbnumber", |
||
| 38 | "file", |
||
| 39 | "lastplayed", |
||
| 40 | "year" |
||
| 41 | ], |
||
| 42 | "sort": { |
||
| 43 | "order": "ascending", |
||
| 44 | "method": "playcount", |
||
| 45 | #"ignorearticle": True |
||
| 46 | } |
||
| 47 | }, |
||
| 48 | "id": "libMovies" |
||
| 49 | } |
||
| 50 | kodilibrary = json.loads(xbmc.executeJSONRPC(json.dumps(todump))) |
||
| 51 | if kodilibrary["result"]["limits"]["total"] > 0: |
||
| 52 | xbmc.log(str(kodilibrary)) |
||
| 53 | progress = interface.SyncProgress("movies", "full") |
||
| 54 | each = float(100) / kodilibrary["result"]["limits"]["total"] |
||
| 55 | for movie in kodilibrary["result"]["movies"]: |
||
| 56 | progress.push(each, "{0} ({1})".format(movie["label"], movie["year"])) |
||
| 57 | if movie["playcount"] == 0: |
||
| 58 | ### DOWNLOAD FROM KODI |
||
| 59 | #Separate big list in chunks |
||
| 60 | movie["media"] = "movie" |
||
| 61 | if self.api.check_if_watched(movie): |
||
| 62 | xbmc.log("Simkl: {0}".format(movie)) |
||
| 63 | ret = xbmc.executeJSONRPC(json.dumps({ |
||
| 64 | "jsonrpc": "2.0", |
||
| 65 | "method": "VideoLibrary.SetMovieDetails", |
||
| 66 | "params": { |
||
| 67 | "playcount": 1, |
||
| 68 | #"lastplayed":"", |
||
| 69 | "movieid":movie["movieid"] |
||
| 70 | } |
||
| 71 | })) |
||
| 72 | #xbmc.log(ret) |
||
| 73 | del progress |
||
| 74 | |||
| 75 | todump["method"] = "VideoLibrary.GetTVShows" |
||
| 76 | todump["params"]["properties"] = ["imdbnumber", "title", "watchedepisodes"] |
||
| 77 | #If watchedepisodes > 0 |
||
| 78 | kodilibrary = xbmc.executeJSONRPC(json.dumps(todump)) |
||
| 79 | kodilibrary = json.loads(kodilibrary) |
||
| 80 | |||
| 81 | if kodilibrary["result"]["limits"]["total"] > 0: |
||
| 82 | progress = interface.SyncProgress("TV Shows", "full") |
||
| 83 | each = float(100) / kodilibrary["result"]["limits"]["total"] |
||
| 84 | debug_cnt = 0 |
||
| 85 | for tvshow in kodilibrary["result"]["tvshows"]: |
||
| 86 | #if debug_cnt >= 10: break #I have a lot of TV Shows, only for testing |
||
| 87 | progress.push(each, tvshow["label"]) |
||
| 88 | debug_cnt += 1 |
||
| 89 | |||
| 90 | |||
| 91 | todump["method"] = "VideoLibrary.GetSeasons" |
||
| 92 | #todump["params"]["Library.Id"] = tvshow["tvshowid"] |
||
| 93 | todump["id"] = tvshow["tvshowid"] |
||
| 94 | todump["params"]["properties"] = ["season", "episode", "watchedepisodes", "showtitle"] |
||
| 95 | seasons = xbmc.executeJSONRPC(json.dumps(todump)) |
||
| 96 | xbmc.log(json.dumps(tvshow)) |
||
| 97 | xbmc.log(seasons) |
||
| 98 | seasons = json.loads(seasons) |
||
| 99 | |||
| 100 | |||
| 101 | for season in seasons["result"]["seasons"]: |
||
| 102 | values = [] |
||
| 103 | |||
| 104 | todump["method"] = "VideoLibrary.GetEpisodes" |
||
| 105 | todump["params"]["tvshowid"] = tvshow["tvshowid"] |
||
| 106 | todump["params"]["season"] = season["season"] |
||
| 107 | todump["params"]["properties"] = ["title", "rating", "playcount", |
||
| 108 | "season", "episode", "showtitle", "lastplayed", "tvshowid"] |
||
| 109 | |||
| 110 | episodes = xbmc.executeJSONRPC(json.dumps(todump)) |
||
| 111 | xbmc.log(episodes) |
||
| 112 | episodes = json.loads(episodes) |
||
| 113 | |||
| 114 | if episodes["result"]["limits"]["total"] > 0: |
||
| 115 | for episode in episodes["result"]["episodes"]: |
||
| 116 | values.append({ |
||
| 117 | "type": "tv", |
||
| 118 | "season": episode["season"], |
||
| 119 | "episode": episode["episode"], |
||
| 120 | "title": episode["showtitle"], |
||
| 121 | "tvdb": tvshow["imdbnumber"] |
||
| 122 | }) |
||
| 123 | |||
| 124 | watched = self.api.check_if_watched(values, False) |
||
| 125 | xbmc.log(json.dumps(watched)) |
||
| 126 | |||
| 127 | for i, episode in enumerate(episodes["result"]["episodes"]): |
||
| 128 | toupdate = { |
||
| 129 | "jsonrpc": "2.0", |
||
| 130 | "method": "VideoLibrary.SetEpisodeDetails", |
||
| 131 | "params": { |
||
| 132 | "episodeid":episode["episodeid"], |
||
| 133 | "playcount": int(watched[i]["result"]), |
||
| 134 | }, |
||
| 135 | "id": "libMovies" |
||
| 136 | } |
||
| 137 | try: |
||
| 138 | toupdate["params"]["lastplayed"] = watched[i]["last_watched"] |
||
| 139 | except KeyError: |
||
| 140 | toupdate["params"]["lastplayed"] = "" |
||
| 141 | |||
| 142 | info = xbmc.executeJSONRPC(json.dumps(toupdate)) |
||
| 143 | xbmc.log("Simkl: Info: {0}".format(info)) |
||
| 144 | |||
| 145 | del todump["params"]["tvshowid"] |
||
| 146 | del todump["params"]["season"] |
||
| 147 | del progress |
||
| 148 | |||
| 149 | xbmc.log("Simkl: Finished syncing library") |
||
| 150 | interface.notify("Finished syncing library") |
||
| 151 | |||
| 255 |