2022-03-30 16:54:49 +02:00
|
|
|
# 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
|
2022-03-30 19:52:36 +02:00
|
|
|
from difflib import SequenceMatcher # checks how similar are two strings
|
2022-03-30 16:54:49 +02:00
|
|
|
|
|
|
|
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())
|
2022-04-02 09:25:54 +02:00
|
|
|
if m > match and m > 0.6:
|
2022-03-30 16:54:49 +02:00
|
|
|
closest = i
|
|
|
|
match = m
|
|
|
|
|
|
|
|
if closest:
|
2022-04-01 21:29:15 +02:00
|
|
|
return closest, match
|
2022-03-30 16:54:49 +02:00
|
|
|
|
2022-04-01 21:59:32 +02:00
|
|
|
match = 0
|
|
|
|
closest = {}
|
2022-03-30 16:54:49 +02:00
|
|
|
|
|
|
|
# Round 2. By Generic name
|
|
|
|
|
|
|
|
for i in all_apps:
|
|
|
|
for n in i.get("generic_name",[]):
|
|
|
|
m = similar(n.lower(), name.lower())
|
2022-04-01 21:59:32 +02:00
|
|
|
if m > match and is_free(i):
|
2022-03-30 16:54:49 +02:00
|
|
|
closest = i
|
|
|
|
match = m
|
|
|
|
|
|
|
|
if closest:
|
2022-04-01 21:29:15 +02:00
|
|
|
return closest, match
|
2022-03-30 16:54:49 +02:00
|
|
|
|
|
|
|
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
|
2022-03-30 19:52:36 +02:00
|
|
|
|
2022-03-30 16:54:49 +02:00
|
|
|
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
|
2022-04-01 21:59:32 +02:00
|
|
|
|
2022-04-02 00:25:53 +02:00
|
|
|
def is_free(app):
|
|
|
|
if "licenses" in app and app["licenses"]:
|
2022-04-02 09:18:05 +02:00
|
|
|
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'")
|
2022-04-02 00:25:53 +02:00
|
|
|
return False
|