| Conditions | 16 |
| Total Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 2 | Features | 3 |
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 SauceNao.check_files() 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 |
||
| 83 | |||
| 84 | :type file_name: str |
||
| 85 | :return: |
||
| 86 | """ |
||
| 87 | self.logger.info("checking file: {0:s}".format(file_name)) |
||
| 88 | if self._combine_api_types: |
||
| 89 | result = self.check_image(file_name, self.API_HTML_TYPE) |
||
| 90 | sorted_results = self.parse_results_json(result) |
||
| 91 | |||
| 92 | additional_result = self.check_image(file_name, self.API_JSON_TYPE) |
||
| 93 | additional_sorted_results = self.parse_results_json(additional_result) |
||
| 94 | sorted_results = self.merge_results(sorted_results, additional_sorted_results) |
||
| 95 | else: |
||
| 96 | result = self.check_image(file_name, self._output_type) |
||
| 97 | sorted_results = self.parse_results_json(result) |
||
| 98 | |||
| 99 | filtered_results = self.filter_results(sorted_results) |
||
| 100 | return filtered_results |
||
| 101 | |||
| 102 | def check_image(self, file_name: str, output_type: int) -> str: |
||
| 103 | """Check the possible sources for the given file |
||
| 104 | |||
| 105 | :type output_type: int |
||
| 106 | :type file_name: str |
||
| 107 | :return: |
||
| 108 | """ |
||
| 109 | file_path = os.path.join(self._directory, file_name) |
||
| 110 | |||
| 111 | files = {'file': open(file_path, 'rb').read()} |
||
| 112 | headers = { |
||
| 113 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' |
||
| 114 | 'Chrome/63.0.3239.84 Safari/537.36', |
||
| 115 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', |
||
| 116 | 'Accept-Language': 'en-DE,en-US;q=0.9,en;q=0.8', |
||
| 117 | 'Accept-Encoding': 'gzip, deflate, br', |
||
| 118 | 'DNT': '1', |
||
| 119 | 'Connection': 'keep-alive' |
||
| 120 | } |
||
| 121 | params = { |
||
| 122 | 'file': file_path, |
||
| 123 | 'Content-Type': self.mime.guess_type(file_path), |
||
| 124 | # parameters taken from form on main page: https://saucenao.com/ |
||
| 125 | 'url': None, |
||
| 126 | 'frame': 1, |
||
| 127 | 'hide': 0, |
||
| 128 | # parameters taken from API documentation: https://saucenao.com/user.php?page=search-api |
||
| 129 | 'output_type': output_type, |
||
| 130 | 'db': self._databases, |
||
| 131 | } |
||
| 132 | |||
| 133 | if self._api_key: |
||
| 134 | params['api_key'] = self._api_key |
||
| 135 | |||
| 136 | link = requests.post(url=self.SEARCH_POST_URL, files=files, params=params, headers=headers) |
||
| 137 | |||
| 138 | code, msg = http.verify_status_code(link, file_name) |
||
| 139 | |||
| 140 | if code == http.STATUS_CODE_SKIP: |
||
| 141 | self.logger.error(msg) |
||
| 142 | return json.dumps({'results': []}) |
||
| 143 | elif code == http.STATUS_CODE_REPEAT: |
||
| 144 | if not self._previous_status_code: |
||
| 145 | self._previous_status_code = code |
||
| 146 | self.logger.info("Received an unexpected status code, repeating after 10 seconds...") |
||
| 147 | time.sleep(10) |
||
| 148 | return self.check_image(file_name, output_type) |
||
| 149 | else: |
||
| 150 | raise UnknownStatusCodeException(msg) |
||
| 151 | else: |
||
| 152 | self._previous_status_code = None |
||
| 153 | |||
| 154 | if output_type == self.API_HTML_TYPE: |
||
| 274 |