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.
197 lines
4.7 KiB
197 lines
4.7 KiB
from getpass import getpass |
|
from random import randint |
|
from time import sleep |
|
import json |
|
|
|
import nacl.secret |
|
import nacl.utils |
|
from arango import ArangoClient |
|
|
|
from config import * |
|
|
|
# Starta koppling till arangodb |
|
# Avkryptera lösen till arango |
|
try: |
|
# Om scriptet körs på Macbook finns löseordet i en fil |
|
with open("password_arango.txt") as f: |
|
pwd = f.readline() |
|
except FileNotFoundError: |
|
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) |
|
|
|
db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd) |
|
|
|
from helpers import now, _print, nowstamp |
|
|
|
|
|
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 update_inuse(profile): |
|
db.collection("profiles").update(profile["doc"]["id"]) |
|
|
|
|
|
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 report_blocked(profile, users): |
|
db.insert_document( |
|
"reports", |
|
{ |
|
"_key": now(), |
|
"profile": profile.name, |
|
"users": [user.username for user in users], |
|
}, |
|
overwrite=True, |
|
) |
|
|
|
|
|
def write_report(users): |
|
db.insert_document( |
|
"reports", |
|
{"_key": now(), "users": [user.username for user in users]}, |
|
overwrite=True, |
|
) |
|
|
|
|
|
def get_profile(db=db): |
|
""" Hämtar profil från profiles """ |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN profiles |
|
FILTER doc.in_use < @inuse |
|
RETURN doc |
|
""", |
|
bind_vars={"inuse": nowstamp() - 900}, |
|
) |
|
profiles = [profile for profile in cursor] |
|
profile = profiles[randint(0, len(profiles) - 1)] |
|
return profile |
|
|
|
|
|
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.doc["_key"], silent=True, ignore_missing=True |
|
) |
|
_print(profile, None, f"{profile.name} blockerad och borttagen {now()}.") |
|
|
|
|
|
# TODO #2 Bättre funktion för backup av databasen |
|
|
|
|
|
def arango_connect(pwd): |
|
return ArangoClient(hosts=host_arango).db( |
|
db_arango, username=user_arango, password=pwd |
|
) |
|
|
|
|
|
def check_for_user(username): |
|
""" Checks if a user exist in db and if it's checked """ |
|
# TODO Skulle kunna kolla ex mode också |
|
if db.collection("members").has(username): |
|
if db.collection('members').get(username)['checked'] == True: |
|
checked = True |
|
else: |
|
checked = False |
|
else: |
|
checked = False |
|
return checked |
|
|
|
|
|
def check_for_picture(id): |
|
""" Checks if a picture exist in db """ |
|
return db.collection("pictures").has(id) |
|
|
|
|
|
def get_user(collection="lookups"): |
|
""" Hämtar användare att kolla upp från lookups """ |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN @@col |
|
RETURN doc |
|
""", |
|
bind_vars={"@col": collection}, |
|
) |
|
try: |
|
doc = cursor.next() |
|
if "other" not in doc: |
|
doc["other"] = [] |
|
db.collection(collection).delete(doc["_key"]) |
|
except StopIteration: |
|
doc = None |
|
|
|
return doc |
|
|
|
|
|
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: {now()}") |
|
|
|
|
|
def used_servers(): |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN profiles |
|
RETURN doc._key |
|
""" |
|
) |
|
return [doc for doc in cursor] |
|
|
|
|
|
db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd)
|
|
|