You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
119 lines
3.0 KiB
from datetime import datetime |
|
from getpass import getpass |
|
from time import sleep |
|
import json |
|
|
|
import nacl.secret |
|
import nacl.utils |
|
from arango import ArangoClient |
|
|
|
from config import * |
|
|
|
|
|
def checked_members(): |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN members |
|
FILTER doc.checked == true |
|
RETURN doc._key |
|
""" |
|
) |
|
members_checked = set([doc for doc in cursor]) |
|
return members_checked |
|
|
|
|
|
def count_docs(col): |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN @@col |
|
COLLECT WITH COUNT INTO length |
|
RETURN length |
|
""", |
|
bind_vars={"@col": col} |
|
) |
|
return cursor.next() |
|
|
|
def write_report(users, pictures): |
|
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
db.insert_document({ |
|
'_id':f'reports/{now}', |
|
'users': [user.username for user in users], |
|
'members': count_docs('members'), |
|
'total_picture_reactions':count_docs('picture_reactions'), |
|
'pictures':count_docs('pictures'), |
|
'new_pictures': pictures |
|
}) |
|
|
|
|
|
def get_profile(created=True): |
|
""" Hämtar profil från profiles """ |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN profiles |
|
FILTER doc.in_use == false |
|
FILTER doc.created == @created |
|
RETURN doc |
|
""", |
|
bind_vars={'created': created} |
|
) |
|
return cursor.next() |
|
|
|
|
|
def friends_of_user(user): |
|
"""Returnernar användare som reagerat på user:s bilder""" |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN picture_reactions |
|
FILTER doc._to == @user |
|
RETURN DISTINCT doc._from |
|
""", |
|
bind_vars={"user": "members/" + user}, |
|
) |
|
return [doc[8:] for doc in cursor] |
|
|
|
|
|
def remove_profile(profile): |
|
db.collection("profiles").delete(profile['_key'], silent=True, ignore_missing=True) |
|
print( |
|
f'{profile} blockerad och borttagen {datetime.now().strftime("%Y%m%d_%H:%M:%S")}.' |
|
) |
|
|
|
# TODO #2 Funktion för backup av databasen |
|
|
|
|
|
# Starta koppling till arangodb |
|
# Avkryptera lösen till arango |
|
for i in range(0, 6, 1): |
|
if i == 5: |
|
exit() |
|
try: |
|
key = "sssladnnklja" + getpass() |
|
pwd = ( |
|
nacl.secret.SecretBox(key.encode()) |
|
.decrypt(pwd_arango, encoder=nacl.encoding.HexEncoder) |
|
.decode("utf-8") |
|
) |
|
break |
|
except: |
|
print("Fel lösenord.") |
|
sleep(1) |
|
|
|
|
|
def backup(db): |
|
"""Skapar en json-backup för specificerade collections. |
|
|
|
Args: |
|
db: databaskoppling till aktuell databas |
|
""" |
|
d = {} |
|
for col in ['members', 'pictures', 'picture_reactions', 'profiles']: |
|
l = [] |
|
for doc in db.collection(col).all(): |
|
l.append(doc) |
|
d[col] = l |
|
with open('data/backup.json', 'w') as f: |
|
json.dump(d, f) |
|
print(f'Senaste backup: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}') |
|
|
|
|
|
db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd)
|
|
|