diff --git a/facebook/arangodb.py b/facebook/arangodb.py index 9a0122b..5dac7b3 100644 --- a/facebook/arangodb.py +++ b/facebook/arangodb.py @@ -1,4 +1,5 @@ from getpass import getpass +from random import randint from time import sleep import json @@ -12,7 +13,7 @@ from config import * # 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: + with open("password_arango.txt") as f: pwd = f.readline() except FileNotFoundError: for i in range(0, 6, 1): @@ -34,55 +35,68 @@ db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, passwor 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( - """ + cursor = db.aql.execute( + """ FOR doc IN @@col COLLECT WITH COUNT INTO length RETURN length """, - bind_vars={"@col": col} - ) + 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) + 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) + 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() + 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): @@ -99,29 +113,54 @@ def friends_of_user(user): 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()}.' + + 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) + 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 get_user(): +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 lookups + """ + FOR doc IN @@col RETURN doc - """ - ) + """, + bind_vars={"@col": collection}, + ) try: doc = cursor.next() - if 'other' not in doc: - doc['other'] = [] - db.collection("lookups").delete(doc['_key']) + if "other" not in doc: + doc["other"] = [] + db.collection(collection).delete(doc["_key"]) except StopIteration: doc = None @@ -135,22 +174,24 @@ def backup(db): db: databaskoppling till aktuell databas """ d = {} - for col in ['members', 'pictures', 'picture_reactions', 'profiles']: + 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: + with open("data/backup.json", "w") as f: json.dump(d, f) - print(f'Senaste backup: {now()}') + 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)