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.
156 lines
3.9 KiB
156 lines
3.9 KiB
from getpass import getpass |
|
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 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() - 600} |
|
) |
|
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.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 get_user(): |
|
""" Hämtar användare att kolla upp från lookups """ |
|
cursor = db.aql.execute( |
|
""" |
|
FOR doc IN lookups |
|
RETURN doc |
|
""" |
|
) |
|
try: |
|
doc = cursor.next() |
|
if 'other' not in doc: |
|
doc['other'] = [] |
|
db.collection("lookups").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)
|
|
|