search.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
  2. # THE FOLLOWING SOURCE CODE I UNDER THE GNU
  3. # AGPL LICENSE V3 OR ANY LATER VERSION.
  4. # This project is not for simple users, but for
  5. # web-masters and a like, so we are counting on
  6. # your ability to set it up and running.
  7. import os
  8. import json
  9. from difflib import SequenceMatcher # checks how similar are two strings
  10. def similar(a, b):
  11. # I guess it simpifies the syntax for SequenceMatcher
  12. # In the previous version we use Lavenshtain but it made
  13. # it an issue for some people to install.
  14. return SequenceMatcher(None, a, b).ratio()
  15. def search_app(name):
  16. # This function output a json of an app that's the closest
  17. # match to the requested name.
  18. closest = {}
  19. match = 0
  20. all_apps = []
  21. for i in os.listdir("apps"):
  22. if i.endswith(".json"):
  23. try:
  24. with open("apps/"+i) as json_file:
  25. idata = json.load(json_file)
  26. except Exception as e:
  27. print("Error!", i, e)
  28. idata = {}
  29. all_apps.append(idata)
  30. # Round 1. By the name
  31. for i in all_apps:
  32. for n in i.get("names",[]):
  33. m = similar(n.lower(), name.lower())
  34. if m > match and m > 0.6:
  35. closest = i
  36. match = m
  37. if closest:
  38. return closest, match
  39. match = 0
  40. closest = {}
  41. # Round 2. By Generic name
  42. for i in all_apps:
  43. for n in i.get("generic_name",[]):
  44. m = similar(n.lower(), name.lower())
  45. if m > match and is_free(i):
  46. closest = i
  47. match = m
  48. if closest:
  49. return closest, match
  50. def suggest(json_data):
  51. # This function will suggest
  52. found = []
  53. all_apps = []
  54. for i in os.listdir("apps"):
  55. if i.endswith(".json"):
  56. try:
  57. with open("apps/"+i) as json_file:
  58. idata = json.load(json_file)
  59. except Exception as e:
  60. idata = {}
  61. all_apps.append(idata)
  62. for i in all_apps:
  63. score = 0
  64. for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]:
  65. for b in json_data.get(c, []):
  66. if b in i.get(c, []):
  67. score += 1
  68. # Pass the found datapoint into the renderer
  69. try:
  70. i[c][i[c].index(b)] = "*"+b
  71. except Exception as e:
  72. print(e)
  73. # If software has issues of any kind we move it down.
  74. if "issues" in i and score:
  75. score = max(0.1, score - len(i["issues"]))
  76. found.append([score, i])
  77. try:
  78. found.sort(key=lambda x: x[0])
  79. found = list(reversed(found))
  80. except Exception as e:
  81. print("Found problem:", e)
  82. fount = []
  83. return found
  84. def is_free(app):
  85. if "licenses" in app and app["licenses"]:
  86. with open("data/licenses.json", "r") as data:
  87. all_licenses = json.load(data)["licenses"]
  88. for al in all_licenses: # Making longer loop once
  89. for l in app["licenses"]:
  90. if l in [al.get("licenseId",""),al.get("name","")]\
  91. and al.get("isFsfLibre", False):
  92. return True
  93. print("License Error! "+app.get("names",[])[0], "Check with data/licenses.json 'licenseId'")
  94. return False