search.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if c == "generic_name":
  75. score += 10 # Features matter more than formats
  76. else:
  77. score += 1
  78. # Pass the found datapoint into the renderer
  79. try:
  80. i[c][i[c].index(b)] = "*"+b
  81. except Exception as e:
  82. print(e)
  83. # If software has issues of any kind we move it down.
  84. if "issues" in i and score:
  85. score = max(0.1, score - len(i["issues"]))
  86. found.append([score, i])
  87. try:
  88. found.sort(key=lambda x: x[0])
  89. found = list(reversed(found))
  90. except Exception as e:
  91. print("Found problem:", e)
  92. fount = []
  93. return found
  94. def is_free(app):
  95. if "licenses" in app and app["licenses"]:
  96. with open("data/licenses.json", "r") as data:
  97. all_licenses = json.load(data)["licenses"]
  98. for al in all_licenses: # Making longer loop once
  99. for l in app["licenses"]:
  100. if l in [al.get("licenseId",""),al.get("name","")]\
  101. and al.get("isFsfLibre", False):
  102. return True
  103. #print("License Error! "+app.get("names",[])[0], "Check with data/licenses.json 'licenseId'")
  104. return False