search.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. print("Error!", i, e)
  61. idata = {}
  62. all_apps.append(idata)
  63. for i in all_apps:
  64. score = 0
  65. for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]:
  66. for b in json_data.get(c, []):
  67. if b in i.get(c, []):
  68. score += 1
  69. found.append([score, i])
  70. try:
  71. found.sort(key=lambda x: x[0])
  72. found = list(reversed(found))
  73. except Exception as e:
  74. print("Found problem:", e)
  75. fount = []
  76. return found
  77. def is_free(app):
  78. if "licenses" in app and app["licenses"]:
  79. with open("data/licenses.json", "r") as data:
  80. all_licenses = json.load(data)["licenses"]
  81. for al in all_licenses: # Making longer loop once
  82. for l in app["licenses"]:
  83. if l in [al.get("licenseId",""),al.get("name","")]\
  84. and al.get("isFsfLibre", False):
  85. return True
  86. print("License Error! "+app.get("names",[])[0], "Check with data/licenses.json 'licenseId'")
  87. return False