Browse Source

ffpb_findnode(): use fuzzy matching if no exact match has been found

Helge Jung 9 years ago
parent
commit
a68cbf6c05
1 changed files with 16 additions and 3 deletions
  1. 16 3
      modules/ffpb.py

+ 16 - 3
modules/ffpb.py

@@ -3,6 +3,7 @@ from __future__ import print_function
 import willie
 
 import datetime
+import difflib
 from email.utils import mktime_tz
 import git
 import netaddr
@@ -154,6 +155,8 @@ def ffpb_findnode(name):
 
 	name = str(name).strip()
 
+	names = {}
+
 	# try to match MAC
 	m = re.search("^([0-9a-fA-F][0-9a-fA-F]:){5}[0-9a-fA-F][0-9a-fA-F]$", name)
 	if (not m is None):
@@ -173,11 +176,14 @@ def ffpb_findnode(name):
 							return node
 
 	# look through the ALFRED peers
-	possible_matches = []
 	for nodeid in alfred_data:
 		node = alfred_data[nodeid]
-		if "hostname" in node and node["hostname"].lower() == name.lower():
-			return node
+		if 'hostname' in node:
+			h = node['hostname']
+			if h.lower() == name.lower():
+				return node
+			else:
+				names[h] = nodeid
 
 	# still not found -> try peers_repo
 	if not peers_repo is None:
@@ -201,6 +207,13 @@ def ffpb_findnode(name):
 		if not (peer_mac is None):
 			return { "hostname": peer_name, "network": { "addresses": [ mac2ipv6(peer_mac, "fdca:ffee:ff12:132:") ], "mac": peer_mac } }
 
+	# do a similar name lookup in the ALFRED data
+	possibilities = difflib.get_close_matches(name, [ x for x in names ], cutoff=0.8)
+	print('findnode: Fuzzy matching \'{0}\' got {1} entries: {2}'.format(name, len(possibilities), ', '.join(possibilities)))
+	if len(possibilities) == 1:
+		# if we got exactly one candidate that might be it
+		return alfred_data[names[possibilities[0]]]
+
 	return None
 
 def ffpb_findnode_from_botparam(bot, name, ensure_recent_alfreddata = True):