def get_IP(self, get_all=False, num_want=50, max_attempts=3, ignore_wait=False): for each_tracker in self.tracker_list: pre_len = len(self.IPs) if(get_all): new_IPs = each_tracker.get_all_IPs(num_want, ignore_wait=ignore_wait) if(new_IPs): for each_IP in new_IPs: if each_IP not in self.IPs: self.IPs.append(each_IP) ip_location = util.get_geolocation_for_ip(each_IP) if not ip_location: continue loc_key = (ip_location['country'], ip_location['city']) if loc_key not in self.geo_info.keys(): ip_location['n'] = 1 self.geo_info[loc_key] = ip_location else: self.geo_info[loc_key]['n'] += 1 print("Successfully collected " + str(len(self.IPs) - pre_len) + " new IPs from " + each_tracker.URL) else: print("Error: Could not announce to tracker " + each_tracker.URL) else: new_IPs = each_tracker.get_IPs(num_want) if(not (new_IPs == -1)): for each_IP in new_IPs: if each_IP not in self.IPs: self.IPs.append(each_IP) ip_location = util.get_geolocation_for_ip(each_IP) loc_key = (ip_location['country'], ip_location['city']) if not ip_location: continue if loc_key not in self.geo_info.keys(): ip_location['n'] = 1 self.geo_info[loc_key] = ip_location else: self.geo_info[loc_key]['n'] += 1 print("Successfully collected " + str(len(self.IPs) - pre_len) + " new IPs from " + each_tracker.URL) else: print("Error: Could not announce to tracker " + each_tracker.URL) return self.IPs
def get_IPs(self, num_want=50, port=8080): if self.serv_type == "udp" and not self.connected: if(not self._connect_UDP()): return -1 packet_hash = self.info_hash.decode('hex') recvd_IPs = self._announce_UDP(num_want) if (self.serv_type.lower() == "udp") else self._announce_http(num_want) if not recvd_IPs: return -1 unique_IPs = [] for ip in recvd_IPs: if ip not in self.IP: self.IP.append(ip) unique_IPs.append(ip) ip_location = util.get_geolocation_for_ip(ip) if not ip_location: continue loc_key = (ip_location['country'], ip_location['city']) if loc_key not in self.geo_info.keys(): ip_location['n'] = 1 self.geo_info[loc_key] = ip_location else: self.geo_info[loc_key]['n'] += 1 return unique_IPs