added in_use() and check_for_member() erc

pull/5/head
Lasse Edfast 5 years ago
parent 654dab46eb
commit 4c69f682a8
  1. 113
      facebook/arangodb.py

@ -1,4 +1,5 @@
from getpass import getpass from getpass import getpass
from random import randint
from time import sleep from time import sleep
import json import json
@ -12,7 +13,7 @@ from config import *
# Avkryptera lösen till arango # Avkryptera lösen till arango
try: try:
# Om scriptet körs på Macbook finns löseordet i en fil # 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() pwd = f.readline()
except FileNotFoundError: except FileNotFoundError:
for i in range(0, 6, 1): 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 from helpers import now, _print, nowstamp
def checked_members(): def checked_members():
cursor = db.aql.execute( cursor = db.aql.execute(
""" """
FOR doc IN members FOR doc IN members
FILTER doc.checked == true FILTER doc.checked == true
RETURN doc._key RETURN doc._key
""" """
) )
members_checked = set([doc for doc in cursor]) members_checked = set([doc for doc in cursor])
return members_checked return members_checked
def update_inuse(profile):
db.collection("profiles").update(profile["doc"]["id"])
def count_docs(col): def count_docs(col):
cursor = db.aql.execute( cursor = db.aql.execute(
""" """
FOR doc IN @@col FOR doc IN @@col
COLLECT WITH COUNT INTO length COLLECT WITH COUNT INTO length
RETURN length RETURN length
""", """,
bind_vars={"@col": col} bind_vars={"@col": col},
) )
return cursor.next() return cursor.next()
def report_blocked(profile, users): def report_blocked(profile, users):
db.insert_document('reports', { db.insert_document(
'_key':now(), "reports",
'profile': profile.name, {
'users': [user.username for user in users], "_key": now(),
}, overwrite=True) "profile": profile.name,
"users": [user.username for user in users],
},
overwrite=True,
)
def write_report(users): def write_report(users):
db.insert_document('reports', { db.insert_document(
'_key':now(), "reports",
'users': [user.username for user in users] {"_key": now(), "users": [user.username for user in users]},
}, overwrite=True) overwrite=True,
)
def get_profile(db=db): def get_profile(db=db):
""" Hämtar profil från profiles """ """ Hämtar profil från profiles """
cursor = db.aql.execute( cursor = db.aql.execute(
""" """
FOR doc IN profiles FOR doc IN profiles
FILTER doc.in_use < @inuse FILTER doc.in_use < @inuse
RETURN doc RETURN doc
""", """,
bind_vars={'inuse': nowstamp() - 600} bind_vars={"inuse": nowstamp() - 900},
) )
return cursor.next() profiles = [profile for profile in cursor]
profile = profiles[randint(0, len(profiles) - 1)]
return profile
def friends_of_user(user): def friends_of_user(user):
@ -100,28 +114,53 @@ def friends_of_user(user):
def remove_profile(profile): def remove_profile(profile):
db.collection("profiles").delete(profile.doc['_key'], silent=True, ignore_missing=True) db.collection("profiles").delete(
_print(profile, None, f'{profile.name} blockerad och borttagen {now()}.' 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 # TODO #2 Bättre funktion för backup av databasen
def arango_connect(pwd): 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 """ """ Hämtar användare att kolla upp från lookups """
cursor = db.aql.execute( cursor = db.aql.execute(
""" """
FOR doc IN lookups FOR doc IN @@col
RETURN doc RETURN doc
""" """,
) bind_vars={"@col": collection},
)
try: try:
doc = cursor.next() doc = cursor.next()
if 'other' not in doc: if "other" not in doc:
doc['other'] = [] doc["other"] = []
db.collection("lookups").delete(doc['_key']) db.collection(collection).delete(doc["_key"])
except StopIteration: except StopIteration:
doc = None doc = None
@ -135,22 +174,24 @@ def backup(db):
db: databaskoppling till aktuell databas db: databaskoppling till aktuell databas
""" """
d = {} d = {}
for col in ['members', 'pictures', 'picture_reactions', 'profiles']: for col in ["members", "pictures", "picture_reactions", "profiles"]:
l = [] l = []
for doc in db.collection(col).all(): for doc in db.collection(col).all():
l.append(doc) l.append(doc)
d[col] = l d[col] = l
with open('data/backup.json', 'w') as f: with open("data/backup.json", "w") as f:
json.dump(d, f) json.dump(d, f)
print(f'Senaste backup: {now()}') print(f"Senaste backup: {now()}")
def used_servers(): def used_servers():
cursor = db.aql.execute( cursor = db.aql.execute(
""" """
FOR doc IN profiles FOR doc IN profiles
RETURN doc._key RETURN doc._key
""" """
) )
return [doc for doc in cursor] return [doc for doc in cursor]
db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd) db = ArangoClient(hosts=host_arango).db(db_arango, username=user_arango, password=pwd)

Loading…
Cancel
Save