| Conditions | 15 |
| Total Lines | 68 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 libs.analytics.Analytics.analyze() 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 | import ipaddress |
||
| 114 | async def analyze(self, data: dict): |
||
| 115 | """ |
||
| 116 | Do analysis from URL sent by message with databases |
||
| 117 | :param data: dict from message decoded |
||
| 118 | :return: dict to response |
||
| 119 | """ |
||
| 120 | url = url_normalize(data.get("url")) |
||
| 121 | url_hash = sha256(url.encode("utf-8")).hexdigest() |
||
| 122 | |||
| 123 | try: |
||
| 124 | response = requests.get(url) |
||
| 125 | except requests.exceptions.ConnectionError as e: |
||
| 126 | return { |
||
| 127 | "status": 403, |
||
| 128 | "reason": str(e) |
||
| 129 | } |
||
| 130 | |||
| 131 | if response.status_code != 200: |
||
| 132 | return { |
||
| 133 | "status": 404, |
||
| 134 | "http_code": response.status_code |
||
| 135 | } |
||
| 136 | |||
| 137 | if "text/html" not in response.headers["content-type"]: |
||
| 138 | return { |
||
| 139 | "status": 405 |
||
| 140 | } |
||
| 141 | |||
| 142 | url = response.url |
||
| 143 | |||
| 144 | host = urlparse(url).hostname if urlparse(url).hostname != "localhost" else "127.0.0.1" |
||
| 145 | if (validators.ipv4(host) or validators.ipv6(host)) and ipaddress.ip_address(host).is_private: |
||
| 146 | return { |
||
| 147 | "status": 403, |
||
| 148 | "reason": "forbidden" |
||
| 149 | } |
||
| 150 | |||
| 151 | cache = self.data_control.find_result_cache_by_url_hash(url_hash) |
||
| 152 | |||
| 153 | if cache is not None: |
||
| 154 | score = cache |
||
| 155 | |||
| 156 | elif self.data_control.check_trustlist(url): |
||
| 157 | score = 1 |
||
| 158 | |||
| 159 | elif self.data_control.check_trust_domain(host): |
||
| 160 | score = 1 |
||
| 161 | |||
| 162 | elif self.data_control.check_blacklist(url): |
||
| 163 | score = 0 |
||
| 164 | |||
| 165 | elif self.data_control.check_warnlist(url): |
||
| 166 | score = 0.5 |
||
| 167 | |||
| 168 | elif self.safe_browsing.lookup([url]): |
||
| 169 | score = 0 |
||
| 170 | self.data_control.mark_as_blacklist(url) |
||
| 171 | |||
| 172 | else: |
||
| 173 | score = await self._deep_analyze(data, url) |
||
| 174 | |||
| 175 | if cache is None: |
||
| 176 | self.data_control.upload_result_cache(url_hash, score) |
||
| 177 | |||
| 178 | return { |
||
| 179 | "status": 200, |
||
| 180 | "url": url, |
||
| 181 | "trust_score": score |
||
| 182 | } |
||
| 251 |