161 lines
4.7 KiB
Python
161 lines
4.7 KiB
Python
# THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
|
|
# THE FOLLOWING SOURCE CODE I UNDER THE GNU
|
|
# AGPL LICENSE V3 OR ANY LATER VERSION.
|
|
|
|
# This project is not for simple users, but for
|
|
# web-masters and a like, so we are counting on
|
|
# your ability to set it up and running.
|
|
|
|
import os
|
|
import json
|
|
from difflib import SequenceMatcher # checks how similar are two strings
|
|
|
|
def similar(a, b):
|
|
# I guess it simpifies the syntax for SequenceMatcher
|
|
# In the previous version we use Lavenshtain but it made
|
|
# it an issue for some people to install.
|
|
return SequenceMatcher(None, a, b).ratio()
|
|
|
|
def search_app(name):
|
|
|
|
# This function output a json of an app that's the closest
|
|
# match to the requested name.
|
|
|
|
closest = {}
|
|
match = 0
|
|
|
|
all_apps = []
|
|
for i in os.listdir("apps"):
|
|
if i.endswith(".json"):
|
|
|
|
try:
|
|
with open("apps/"+i) as json_file:
|
|
idata = json.load(json_file)
|
|
except Exception as e:
|
|
print("Error!", i, e)
|
|
idata = {}
|
|
|
|
all_apps.append(idata)
|
|
# Round 1. By the name
|
|
|
|
for i in all_apps:
|
|
for n in i.get("names",[]):
|
|
m = similar(n.lower(), name.lower())
|
|
if m > match and m > 0.6:
|
|
closest = i
|
|
match = m
|
|
|
|
if closest:
|
|
return closest, match
|
|
|
|
# If there was no match for the program by name
|
|
# we are saving the name into a special file so
|
|
# the operator of the website could see that there
|
|
# was a name that didn't have a file for it.
|
|
|
|
# We open the 'missing.json' file
|
|
try:
|
|
with open("data/missing.json") as json_file:
|
|
missing = json.load(json_file)
|
|
except:
|
|
missing = {}
|
|
|
|
# There could be a problem with writing so we look into it
|
|
# for a very close match. Up to about 60%. No more.
|
|
|
|
match_missing = 0
|
|
closest_missing = ""
|
|
for i in missing:
|
|
m = similar(i.lower(), name.lower())
|
|
if m > match_missing and m > 0.6:
|
|
closest_missing = i
|
|
match_missing = 0
|
|
write_to = closest_missing
|
|
if not write_to:
|
|
write_to = name
|
|
|
|
# Now we add one number to it's mention
|
|
if write_to not in missing:
|
|
missing[write_to] = 1
|
|
else:
|
|
missing[write_to] += 1
|
|
|
|
# Now we save the file
|
|
with open("data/missing.json", 'w') as f:
|
|
json.dump(missing, f, indent=4, sort_keys=True)
|
|
|
|
|
|
|
|
match = 0
|
|
closest = {}
|
|
|
|
# Round 2. By Generic name
|
|
|
|
for i in all_apps:
|
|
for n in i.get("generic_name",[]):
|
|
m = similar(n.lower(), name.lower())
|
|
if m > match and is_free(i):
|
|
closest = i
|
|
match = m
|
|
|
|
if closest:
|
|
return closest, match
|
|
|
|
def suggest(json_data):
|
|
|
|
# This function will suggest
|
|
|
|
|
|
found = []
|
|
all_apps = []
|
|
for i in os.listdir("apps"):
|
|
if i.endswith(".json"):
|
|
|
|
try:
|
|
with open("apps/"+i) as json_file:
|
|
idata = json.load(json_file)
|
|
except Exception as e:
|
|
idata = {}
|
|
|
|
all_apps.append(idata)
|
|
|
|
for i in all_apps:
|
|
score = 0
|
|
for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]:
|
|
|
|
for b in json_data.get(c, []):
|
|
if b in i.get(c, []):
|
|
score += 1
|
|
# Pass the found datapoint into the renderer
|
|
try:
|
|
i[c][i[c].index(b)] = "*"+b
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
# If software has issues of any kind we move it down.
|
|
if "issues" in i and score:
|
|
score = max(0.1, score - len(i["issues"]))
|
|
|
|
|
|
found.append([score, i])
|
|
|
|
try:
|
|
found.sort(key=lambda x: x[0])
|
|
found = list(reversed(found))
|
|
except Exception as e:
|
|
print("Found problem:", e)
|
|
fount = []
|
|
|
|
return found
|
|
|
|
def is_free(app):
|
|
if "licenses" in app and app["licenses"]:
|
|
with open("data/licenses.json", "r") as data:
|
|
all_licenses = json.load(data)["licenses"]
|
|
for al in all_licenses: # Making longer loop once
|
|
for l in app["licenses"]:
|
|
if l in [al.get("licenseId",""),al.get("name","")]\
|
|
and al.get("isFsfLibre", False):
|
|
return True
|
|
#print("License Error! "+app.get("names",[])[0], "Check with data/licenses.json 'licenseId'")
|
|
return False
|