Formatted everything with black
This commit is contained in:
@@ -4,18 +4,20 @@ from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
|
||||
|
||||
app = FastAPI(title="END PLAY Photos", docs_url=None, redoc_url=None, version="0.1")
|
||||
|
||||
|
||||
@app.get("/docs", include_in_schema=False)
|
||||
async def custom_swagger_ui_html():
|
||||
return get_swagger_ui_html(
|
||||
openapi_url=app.openapi_url, # type: ignore
|
||||
openapi_url=app.openapi_url, # type: ignore
|
||||
title=app.title + " - Documentation",
|
||||
swagger_favicon_url="/favicon.ico"
|
||||
swagger_favicon_url="/favicon.ico",
|
||||
)
|
||||
|
||||
|
||||
@app.get("/redoc", include_in_schema=False)
|
||||
async def custom_redoc_html():
|
||||
return get_redoc_html(
|
||||
openapi_url=app.openapi_url, # type: ignore
|
||||
openapi_url=app.openapi_url, # type: ignore
|
||||
title=app.title + " - Documentation",
|
||||
redoc_favicon_url="/favicon.ico"
|
||||
)
|
||||
redoc_favicon_url="/favicon.ico",
|
||||
)
|
||||
|
@@ -4,18 +4,16 @@ from pymongo import MongoClient, GEOSPHERE
|
||||
db_config = configGet("database")
|
||||
|
||||
if db_config["user"] is not None and db_config["password"] is not None:
|
||||
con_string = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(
|
||||
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
|
||||
db_config["user"],
|
||||
db_config["password"],
|
||||
db_config["host"],
|
||||
db_config["port"],
|
||||
db_config["name"]
|
||||
db_config["name"],
|
||||
)
|
||||
else:
|
||||
con_string = 'mongodb://{0}:{1}/{2}'.format(
|
||||
db_config["host"],
|
||||
db_config["port"],
|
||||
db_config["name"]
|
||||
con_string = "mongodb://{0}:{1}/{2}".format(
|
||||
db_config["host"], db_config["port"], db_config["name"]
|
||||
)
|
||||
|
||||
db_client = MongoClient(con_string)
|
||||
@@ -35,4 +33,4 @@ col_videos = db.get_collection("videos")
|
||||
col_tokens = db.get_collection("tokens")
|
||||
col_emails = db.get_collection("emails")
|
||||
|
||||
col_photos.create_index([("location", GEOSPHERE)])
|
||||
col_photos.create_index([("location", GEOSPHERE)])
|
||||
|
@@ -1,5 +1,6 @@
|
||||
from exif import Image
|
||||
|
||||
|
||||
def decimal_coords(coords: float, ref: str) -> float:
|
||||
"""Get latitude/longitude from coord and direction reference
|
||||
|
||||
@@ -9,12 +10,13 @@ def decimal_coords(coords: float, ref: str) -> float:
|
||||
|
||||
### Returns:
|
||||
* float: Decimal degrees
|
||||
"""
|
||||
"""
|
||||
decimal_degrees = coords[0] + coords[1] / 60 + coords[2] / 3600
|
||||
if ref == "S" or ref == "W":
|
||||
decimal_degrees = -decimal_degrees
|
||||
return round(decimal_degrees, 5)
|
||||
|
||||
|
||||
def extract_location(filepath: str) -> dict:
|
||||
"""Get location data from image
|
||||
|
||||
@@ -23,15 +25,11 @@ def extract_location(filepath: str) -> dict:
|
||||
|
||||
### Returns:
|
||||
* dict: `{ "lng": float, "lat": float, "alt": float }`
|
||||
"""
|
||||
"""
|
||||
|
||||
output = {
|
||||
"lng": 0.0,
|
||||
"lat": 0.0,
|
||||
"alt": 0.0
|
||||
}
|
||||
output = {"lng": 0.0, "lat": 0.0, "alt": 0.0}
|
||||
|
||||
with open(filepath, 'rb') as src:
|
||||
with open(filepath, "rb") as src:
|
||||
img = Image(src)
|
||||
|
||||
if img.has_exif is False:
|
||||
@@ -44,4 +42,4 @@ def extract_location(filepath: str) -> dict:
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
return output
|
||||
return output
|
||||
|
@@ -1,13 +1,14 @@
|
||||
from importlib.util import module_from_spec, spec_from_file_location
|
||||
from os import getcwd, path, walk
|
||||
|
||||
#=================================================================================
|
||||
# =================================================================================
|
||||
|
||||
|
||||
# Import functions
|
||||
# Took from https://stackoverflow.com/a/57892961
|
||||
def get_py_files(src):
|
||||
cwd = getcwd() # Current Working directory
|
||||
py_files = []
|
||||
cwd = getcwd() # Current Working directory
|
||||
py_files = []
|
||||
for root, dirs, files in walk(src):
|
||||
for file in files:
|
||||
if file.endswith(".py"):
|
||||
@@ -18,18 +19,21 @@ def get_py_files(src):
|
||||
def dynamic_import(module_name, py_path):
|
||||
try:
|
||||
module_spec = spec_from_file_location(module_name, py_path)
|
||||
module = module_from_spec(module_spec) # type: ignore
|
||||
module_spec.loader.exec_module(module) # type: ignore
|
||||
module = module_from_spec(module_spec) # type: ignore
|
||||
module_spec.loader.exec_module(module) # type: ignore
|
||||
return module
|
||||
except SyntaxError:
|
||||
print(f"Could not load extension {module_name} due to invalid syntax. Check logs/errors.log for details.", flush=True)
|
||||
print(
|
||||
f"Could not load extension {module_name} due to invalid syntax. Check logs/errors.log for details.",
|
||||
flush=True,
|
||||
)
|
||||
return
|
||||
except Exception as exp:
|
||||
print(f"Could not load extension {module_name} due to {exp}", flush=True)
|
||||
return
|
||||
|
||||
|
||||
def dynamic_import_from_src(src, star_import = False):
|
||||
def dynamic_import_from_src(src, star_import=False):
|
||||
my_py_files = get_py_files(src)
|
||||
for py_file in my_py_files:
|
||||
module_name = path.split(py_file)[-1][:-3]
|
||||
@@ -44,4 +48,5 @@ def dynamic_import_from_src(src, star_import = False):
|
||||
print(f"Successfully loaded {module_name} extension", flush=True)
|
||||
return
|
||||
|
||||
#=================================================================================
|
||||
|
||||
# =================================================================================
|
||||
|
@@ -4,55 +4,68 @@ from numpy.typing import NDArray
|
||||
from scipy import spatial
|
||||
import cv2
|
||||
|
||||
|
||||
def hash_array_to_hash_hex(hash_array):
|
||||
# convert hash array of 0 or 1 to hash string in hex
|
||||
hash_array = np.array(hash_array, dtype = np.uint8)
|
||||
hash_str = ''.join(str(i) for i in 1 * hash_array.flatten())
|
||||
return (hex(int(hash_str, 2)))
|
||||
# convert hash array of 0 or 1 to hash string in hex
|
||||
hash_array = np.array(hash_array, dtype=np.uint8)
|
||||
hash_str = "".join(str(i) for i in 1 * hash_array.flatten())
|
||||
return hex(int(hash_str, 2))
|
||||
|
||||
|
||||
def hash_hex_to_hash_array(hash_hex) -> NDArray:
|
||||
# convert hash string in hex to hash values of 0 or 1
|
||||
hash_str = int(hash_hex, 16)
|
||||
array_str = bin(hash_str)[2:]
|
||||
return np.array([i for i in array_str], dtype = np.float32)
|
||||
# convert hash string in hex to hash values of 0 or 1
|
||||
hash_str = int(hash_hex, 16)
|
||||
array_str = bin(hash_str)[2:]
|
||||
return np.array([i for i in array_str], dtype=np.float32)
|
||||
|
||||
|
||||
def get_duplicates_cache(album: str) -> dict:
|
||||
output = {}
|
||||
for photo in col_photos.find( {"album": album} ):
|
||||
for photo in col_photos.find({"album": album}):
|
||||
output[photo["filename"]] = [photo["_id"].__str__(), photo["hash"]]
|
||||
return output
|
||||
|
||||
|
||||
async def get_phash(filepath: str) -> str:
|
||||
img = cv2.imread(filepath)
|
||||
# resize image and convert to gray scale
|
||||
img = cv2.resize(img, (64, 64))
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
img = np.array(img, dtype = np.float32)
|
||||
# calculate dct of image
|
||||
img = np.array(img, dtype=np.float32)
|
||||
# calculate dct of image
|
||||
dct = cv2.dct(img)
|
||||
# to reduce hash length take only 8*8 top-left block
|
||||
# to reduce hash length take only 8*8 top-left block
|
||||
# as this block has more information than the rest
|
||||
dct_block = dct[: 8, : 8]
|
||||
dct_block = dct[:8, :8]
|
||||
# caclulate mean of dct block excluding first term i.e, dct(0, 0)
|
||||
dct_average = (dct_block.mean() * dct_block.size - dct_block[0, 0]) / (dct_block.size - 1)
|
||||
dct_average = (dct_block.mean() * dct_block.size - dct_block[0, 0]) / (
|
||||
dct_block.size - 1
|
||||
)
|
||||
# convert dct block to binary values based on dct_average
|
||||
dct_block[dct_block < dct_average] = 0.0
|
||||
dct_block[dct_block != 0] = 1.0
|
||||
# store hash value
|
||||
return hash_array_to_hash_hex(dct_block.flatten())
|
||||
|
||||
|
||||
async def get_duplicates(hash: str, album: str) -> list:
|
||||
duplicates = []
|
||||
cache = get_duplicates_cache(album)
|
||||
for image_name in cache.keys():
|
||||
try:
|
||||
distance = spatial.distance.hamming(
|
||||
hash_hex_to_hash_array(cache[image_name][1]),
|
||||
hash_hex_to_hash_array(hash)
|
||||
hash_hex_to_hash_array(cache[image_name][1]),
|
||||
hash_hex_to_hash_array(hash),
|
||||
)
|
||||
except ValueError:
|
||||
continue
|
||||
# print("{0:<30} {1}".format(image_name, distance), flush=True)
|
||||
if distance <= 0.1:
|
||||
duplicates.append({"id": cache[image_name][0], "filename": image_name, "difference": distance})
|
||||
return duplicates
|
||||
duplicates.append(
|
||||
{
|
||||
"id": cache[image_name][0],
|
||||
"filename": image_name,
|
||||
"difference": distance,
|
||||
}
|
||||
)
|
||||
return duplicates
|
||||
|
@@ -20,8 +20,7 @@ try:
|
||||
logWrite(f"Initialized SMTP TLS connection")
|
||||
else:
|
||||
mail_sender = SMTP(
|
||||
configGet("host", "mailer", "smtp"),
|
||||
configGet("port", "mailer", "smtp")
|
||||
configGet("host", "mailer", "smtp"), configGet("port", "mailer", "smtp")
|
||||
)
|
||||
mail_sender.ehlo()
|
||||
logWrite(f"Initialized SMTP connection")
|
||||
@@ -31,9 +30,8 @@ except Exception as exp:
|
||||
|
||||
try:
|
||||
mail_sender.login(
|
||||
configGet("login", "mailer", "smtp"),
|
||||
configGet("password", "mailer", "smtp")
|
||||
configGet("login", "mailer", "smtp"), configGet("password", "mailer", "smtp")
|
||||
)
|
||||
logWrite(f"Successfully initialized mailer")
|
||||
except Exception as exp:
|
||||
logWrite(f"Could not login into provided SMTP account due to: {exp}")
|
||||
logWrite(f"Could not login into provided SMTP account due to: {exp}")
|
||||
|
@@ -1,3 +1,3 @@
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
|
||||
scheduler = AsyncIOScheduler()
|
||||
scheduler = AsyncIOScheduler()
|
||||
|
@@ -52,7 +52,7 @@ oauth2_scheme = OAuth2PasswordBearer(
|
||||
"photos.write": "Modify photos.",
|
||||
"videos.list": "List videos.",
|
||||
"videos.read": "View videos.",
|
||||
"videos.write": "Modify videos."
|
||||
"videos.write": "Modify videos.",
|
||||
},
|
||||
)
|
||||
|
||||
@@ -66,8 +66,13 @@ def get_password_hash(password):
|
||||
|
||||
|
||||
def get_user(user: str):
|
||||
found_user = col_users.find_one( {"user": user} )
|
||||
return UserInDB(user=found_user["user"], email=found_user["email"], disabled=found_user["disabled"], hash=found_user["hash"])
|
||||
found_user = col_users.find_one({"user": user})
|
||||
return UserInDB(
|
||||
user=found_user["user"],
|
||||
email=found_user["email"],
|
||||
disabled=found_user["disabled"],
|
||||
hash=found_user["hash"],
|
||||
)
|
||||
|
||||
|
||||
def authenticate_user(user_name: str, password: str):
|
||||
@@ -79,19 +84,22 @@ def authenticate_user(user_name: str, password: str):
|
||||
return user
|
||||
|
||||
|
||||
def create_access_token( data: dict, expires_delta: Union[timedelta, None] = None ):
|
||||
def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None):
|
||||
to_encode = data.copy()
|
||||
if expires_delta:
|
||||
expire = datetime.now(tz=timezone.utc) + expires_delta
|
||||
else:
|
||||
expire = datetime.now(tz=timezone.utc) + timedelta(days=ACCESS_TOKEN_EXPIRE_DAYS)
|
||||
expire = datetime.now(tz=timezone.utc) + timedelta(
|
||||
days=ACCESS_TOKEN_EXPIRE_DAYS
|
||||
)
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
async def get_current_user( security_scopes: SecurityScopes, token: str = Depends(oauth2_scheme) ):
|
||||
|
||||
async def get_current_user(
|
||||
security_scopes: SecurityScopes, token: str = Depends(oauth2_scheme)
|
||||
):
|
||||
if security_scopes.scopes:
|
||||
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
|
||||
else:
|
||||
@@ -112,7 +120,7 @@ async def get_current_user( security_scopes: SecurityScopes, token: str = Depend
|
||||
token_data = TokenData(scopes=token_scopes, user=user)
|
||||
except (JWTError, ValidationError):
|
||||
raise credentials_exception
|
||||
|
||||
|
||||
user = get_user(user=token_data.user)
|
||||
|
||||
if user is None:
|
||||
@@ -128,7 +136,9 @@ async def get_current_user( security_scopes: SecurityScopes, token: str = Depend
|
||||
return user
|
||||
|
||||
|
||||
async def get_current_active_user( current_user: User = Security(get_current_user, scopes=["me"]) ):
|
||||
async def get_current_active_user(
|
||||
current_user: User = Security(get_current_user, scopes=["me"])
|
||||
):
|
||||
if current_user.disabled:
|
||||
raise HTTPException(status_code=400, detail="Inactive user")
|
||||
return current_user
|
||||
return current_user
|
||||
|
@@ -2,12 +2,14 @@ from typing import Any, Union
|
||||
from ujson import loads, dumps, JSONDecodeError
|
||||
from traceback import print_exc
|
||||
|
||||
|
||||
# Print to stdout and then to log
|
||||
def logWrite(message: str, debug: bool = False) -> None:
|
||||
# save to log file and rotation is to be done
|
||||
# logAppend(f'{message}', debug=debug)
|
||||
print(f"{message}", flush=True)
|
||||
|
||||
|
||||
def jsonLoad(filepath: str) -> Any:
|
||||
"""Load json file
|
||||
|
||||
@@ -16,34 +18,40 @@ def jsonLoad(filepath: str) -> Any:
|
||||
|
||||
### Returns:
|
||||
* `Any`: Some json deserializable
|
||||
"""
|
||||
with open(filepath, "r", encoding='utf8') as file:
|
||||
"""
|
||||
with open(filepath, "r", encoding="utf8") as file:
|
||||
try:
|
||||
output = loads(file.read())
|
||||
except JSONDecodeError:
|
||||
logWrite(f"Could not load json file {filepath}: file seems to be incorrect!\n{print_exc()}")
|
||||
logWrite(
|
||||
f"Could not load json file {filepath}: file seems to be incorrect!\n{print_exc()}"
|
||||
)
|
||||
raise
|
||||
except FileNotFoundError:
|
||||
logWrite(f"Could not load json file {filepath}: file does not seem to exist!\n{print_exc()}")
|
||||
logWrite(
|
||||
f"Could not load json file {filepath}: file does not seem to exist!\n{print_exc()}"
|
||||
)
|
||||
raise
|
||||
file.close()
|
||||
return output
|
||||
|
||||
|
||||
def jsonSave(contents: Union[list, dict], filepath: str) -> None:
|
||||
"""Save contents into json file
|
||||
|
||||
### Args:
|
||||
* contents (`Union[list, dict]`): Some json serializable
|
||||
* filepath (`str`): Path to output file
|
||||
"""
|
||||
"""
|
||||
try:
|
||||
with open(filepath, "w", encoding='utf8') as file:
|
||||
with open(filepath, "w", encoding="utf8") as file:
|
||||
file.write(dumps(contents, ensure_ascii=False, indent=4))
|
||||
file.close()
|
||||
except Exception as exp:
|
||||
logWrite(f"Could not save json file {filepath}: {exp}\n{print_exc()}")
|
||||
return
|
||||
|
||||
|
||||
def configGet(key: str, *args: str) -> Any:
|
||||
"""Get value of the config key
|
||||
|
||||
@@ -53,23 +61,25 @@ def configGet(key: str, *args: str) -> Any:
|
||||
|
||||
### Returns:
|
||||
* `Any`: Value of provided key
|
||||
"""
|
||||
"""
|
||||
this_dict = jsonLoad("config.json")
|
||||
this_key = this_dict
|
||||
for dict_key in args:
|
||||
this_key = this_key[dict_key]
|
||||
return this_key[key]
|
||||
|
||||
|
||||
def apiKeyInvalid(obj):
|
||||
obj.send_response(401)
|
||||
obj.send_header('Content-type', 'application/json; charset=utf-8')
|
||||
obj.send_header("Content-type", "application/json; charset=utf-8")
|
||||
obj.end_headers()
|
||||
obj.wfile.write(b'{"code":401, "message": "Invalid API key"}')
|
||||
return
|
||||
|
||||
|
||||
def apiKeyExpired(obj):
|
||||
obj.send_response(403)
|
||||
obj.send_header('Content-type', 'application/json; charset=utf-8')
|
||||
obj.send_header("Content-type", "application/json; charset=utf-8")
|
||||
obj.end_headers()
|
||||
obj.wfile.write(b'{"code":403, "message": "API key expired"}')
|
||||
return
|
||||
return
|
||||
|
Reference in New Issue
Block a user