| Conditions | 12 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 API.watched() 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 |
||
| 141 | def watched(self, item, duration, date=time.strftime('%Y-%m-%d %H:%M:%S'), cnt=0): #OR IDMB, member: only works with movies |
||
| 142 | filename = item["file"].replace("\\", "/") |
||
| 143 | if self.is_user_logged() and not self.is_locked(filename): |
||
| 144 | try: |
||
| 145 | con = httplib.HTTPSConnection("api.simkl.com") |
||
| 146 | mediadict = {"movie": "movies", "episode":"shows", "show":"shows"} |
||
| 147 | |||
| 148 | if item["imdbnumber"] != "": |
||
| 149 | if item["type"] == "movie": toappend = {"ids":{"imdb": item["imdbnumber"]}, "watched_at":date} |
||
| 150 | elif item["type"] == "episode": |
||
| 151 | toappend = {"ids":{"tvdb":item["imdbnumber"]}, "watched_at":date, |
||
| 152 | "seasons":[{ |
||
| 153 | "number":item["season"], |
||
| 154 | "episodes":[{"number":item["episode"]}]}]} |
||
| 155 | media = mediadict[item["type"]] |
||
| 156 | else: |
||
| 157 | xbmc.log("Simkl: Filename - {0}".format(filename)) |
||
| 158 | values = json.dumps({"file":filename}) |
||
| 159 | xbmc.log("Simkl: Query: {0}".format(values)) |
||
| 160 | con.request("GET", "/search/file/", body=values, headers=headers) |
||
| 161 | r1 = con.getresponse().read()#.decode("utf-8") |
||
| 162 | xbmc.log("Simkl: Response: {0}".format(r1)) |
||
| 163 | r = json.loads(str(r1)) |
||
| 164 | self.lastwatched = r |
||
| 165 | if r == []: |
||
| 166 | xbmc.log("Simkl: Couldn't scrobble: Null Response") |
||
| 167 | return 0 |
||
| 168 | media = mediadict[r["type"]] |
||
| 169 | toappend = {"ids": r[r["type"]]["ids"], "watched_at":date} |
||
| 170 | |||
| 171 | tosend = {} |
||
| 172 | tosend[media] = [] |
||
| 173 | tosend[media].append(toappend) |
||
| 174 | tosend = json.dumps(tosend) |
||
| 175 | |||
| 176 | xbmc.log("Simkl: values {0}".format(tosend)) |
||
| 177 | con.request("GET", "/sync/history/", body=tosend, headers=headers) |
||
| 178 | r = con.getresponse().read().decode("utf-8") |
||
| 179 | xbmc.log("Simkl: {0}".format(r)) |
||
| 180 | |||
| 181 | success = max(json.loads(r)["added"].values()) |
||
| 182 | if success: |
||
| 183 | self.scrobbled_dict |
||
| 184 | self.lock(filename, duration) |
||
| 185 | return success |
||
| 186 | |||
| 187 | except httplib.BadStatusLine: |
||
| 188 | xbmc.log("Simkl: {0}".format("ERROR: httplib.BadStatusLine")) |
||
| 189 | except SSLError: #Fix #8 |
||
| 190 | xbmc.log("Simkl: ERROR: SSLError, retrying?") |
||
| 191 | if cnt == 0: interface.notify(getstr(32029).format(cnt+1)) |
||
| 192 | if cnt <= 3: |
||
| 193 | self.watched(filename, mediatype, duration, date=date, cnt=cnt+1) |
||
| 194 | else: interface.notify("SSLError") |
||
| 195 | |||
| 196 | else: |
||
| 197 | xbmc.log("Simkl: Can't scrobble. User not logged in or file locked") |
||
| 198 | return 0 |
||
| 199 | |||
| 205 |