search.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 modules import missing
  10. from difflib import SequenceMatcher # checks how similar are two strings
  11. def similar(a, b):
  12. # I guess it simpifies the syntax for SequenceMatcher
  13. # In the previous version we use Lavenshtain but it made
  14. # it an issue for some people to install.
  15. return SequenceMatcher(None, a, b).ratio()
  16. def search_app(name):
  17. # This function output a json of an app that's the closest
  18. # match to the requested name.
  19. closest = {}
  20. match = 0
  21. all_apps = []
  22. for i in os.listdir("apps"):
  23. if i.endswith(".json"):
  24. try:
  25. with open("apps/"+i) as json_file:
  26. idata = json.load(json_file)
  27. except Exception as e:
  28. print("Error!", i, e)
  29. idata = {}
  30. all_apps.append(idata)
  31. # Round 1. By the name
  32. for i in all_apps:
  33. for n in i.get("names",[]):
  34. m = similar(n.lower(), name.lower())
  35. if m > match and m > 0.6:
  36. closest = i
  37. match = m
  38. if closest:
  39. missing.remove(closest["names"][0])
  40. return closest, match
  41. # If there was no match for the program by name
  42. # we are saving the name into a special file so
  43. # the operator of the website could see that there
  44. # was a name that didn't have a file for it.
  45. missing.add(name)
  46. match = 0
  47. closest = {}
  48. # Round 2. By Generic name
  49. for i in all_apps:
  50. for n in i.get("generic_name",[]):
  51. m = similar(n.lower(), name.lower())
  52. if m > match and is_free(i):
  53. closest = i
  54. match = m
  55. if closest:
  56. return closest, match
  57. def suggest(json_data):
  58. # This function will suggest
  59. found = []
  60. all_apps = []
  61. for i in os.listdir("apps"):
  62. if i.endswith(".json"):
  63. try:
  64. with open("apps/"+i) as json_file:
  65. idata = json.load(json_file)
  66. except Exception as e:
  67. idata = {}
  68. all_apps.append(idata)
  69. for i in all_apps:
  70. score = 0
  71. for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]:
  72. for b in json_data.get(c, []):
  73. if b in i.get(c, []):
  74. score += 1
  75. # Pass the found datapoint into the renderer
  76. try:
  77. i[c][i[c].index(b)] = "*"+b
  78. except Exception as e:
  79. print(e)
  80. # If software has issues of any kind we move it down.
  81. if "issues" in i and score:
  82. score = max(0.1, score - len(i["issues"]))
  83. found.append([score, i])
  84. try:
  85. found.sort(key=lambda x: x[0])
  86. found = list(reversed(found))
  87. except Exception as e:
  88. print("Found problem:", e)
  89. fount = []
  90. return found
  91. def is_free(app):
  92. if "licenses" in app and app["licenses"]:
  93. with open("data/licenses.json", "r") as data:
  94. all_licenses = json.load(data)["licenses"]
  95. for al in all_licenses: # Making longer loop once
  96. for l in app["licenses"]:
  97. if l in [al.get("licenseId",""),al.get("name","")]\
  98. and al.get("isFsfLibre", False):
  99. return True
  100. #print("License Error! "+app.get("names",[])[0], "Check with data/licenses.json 'licenseId'")
  101. return False