def get_multiple(index, data=None): """ Get multiple records by ID """ try: serverClient = FaunaClient( secret=os.environ.get("FAUNA_SERVER_SECRET")) res_arr = [] if data is None: res = serverClient.query( q.map_(q.lambda_("data", q.get(q.var("data"))), q.paginate(q.match(q.index(index))))) res_arr.extend(res["data"]) elif isinstance(data, list): for x in data: res = serverClient.query( q.map_(q.lambda_("data", q.get(q.var("data"))), q.paginate(q.match(q.index(index), q.casefold(x))))) res_arr.extend(res["data"]) else: res = serverClient.query( q.map_(q.lambda_("data", q.get(q.var("data"))), q.paginate(q.match(q.index(index), q.casefold(data))))) res_arr.extend(res["data"]) arr = [] for x in res_arr: x["data"]["ref_id"] = x["ref"].id() arr.append(x["data"]) return arr except Exception as ex: raise ex
def delete(collection, id): try: serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) serverClient.query(q.delete(q.ref(q.collection(collection), id))) return True except Exception as ex: raise ex
def main(argv): # # Create an admin client. This is the client we will use to create the database. # scheme = "http" domain = "127.0.0.1" port = "8443" secret = "secret" adminClient = FaunaClient(secret=secret, domain=domain, scheme=scheme, port=port) # # If you are using the the FaunaDB-Cloud use these lines to create the connection. # Change the secret to your value and comment out the lines above # # secret = "Your Secret Goes here" # adminClient = FaunaClient(secret=secret) dbName = "TestDB" # # Call to Create the database # res = adminClient.query(q.create_database({"name": dbName})) print('DB {0} created: {1}'.format(dbName, res)) # # Call to check to see if database exists and to delete it id it does. # res = adminClient.query( q.if_(q.exists(q.database(dbName)), q.delete(q.database(dbName)), True)) print('DB {0} deleted: {1}'.format(dbName, res))
async def get_pypi_data(package_name: str) -> dict: console.log(f"Requesting PyPi data for {package_name}.") request_url = f"https://pypi.org/pypi/{package_name}/json" response = requests.get(request_url) if response.status_code == 404: raise PyPiPackageNotFound assert response.status_code == 200 package_data = response.json() package_data["normalized_name"] = normalize(package_data["info"]["name"]) console.log(f"Logging PyPi data for {package_name} to FaunaDB.") client = FaunaClient(secret=settings.FAUNADB_KEY.get_secret_value()) try: client.query( q.create( q.collection("packages"), {"data": package_data}, )) except BadRequest: package = await get_package_by_name(package_name) ref = package["ref"] client.query(q.update(ref, {"data": package_data})) return package_data
def create_database(scheme, domain, port, secret, db_name): # # Create an admin client. This is the client we will use to create the database. # # If you are using the the FaunaDB-Cloud you will need to replace the value of the # 'secret' in the command below with your "secret". # adminClient = FaunaClient(secret=secret, domain=domain, scheme=scheme, port=port) print("Connected to FaunaDB as admin!") # # The code below creates the Database that will be used for this example. Please note that # the existence of the database is evaluated, deleted if it exists and recreated with a single # call to the Fauna DB. # res = adminClient.query( q.if_( q.exists(q.database(db_name)), [q.delete(q.database(db_name)), q.create_database({"name": db_name})], q.create_database({"name": db_name})) ) print('DB {0} created:'.format(db_name)) pprint.pprint(res) # # Create a key specific to the database we just created. We will use this to # create a new client we will use in the remainder of the examples. # res = adminClient.query(q.select(["secret"], q.create_key({"database": q.database(db_name), "role": "server"}))) print('DB {0} secret: {1}'.format(db_name, res)) return res
def create_server_client(): """ create server client, collections and indexes :return: server client """ client = FaunaClient(secret=os.environ.get('FAUNA_SERVER_SECRET')) client.query(q.create_collection({"name": "users"})) client.query(q.create_index( { "name": "users_by_username", "source": q.collection("users"), "permissions": {"read": "public"}, "terms": [{"field": ["data", "username"]}], "unique": True } )) client.query(q.create_collection({"name": "contacts"})) client.query(q.create_index( { "name": "contacts_by_username", "source": q.collection("contacts"), "terms": [{"field": ["data", "username"]}] } )) return client
def update(collection, id, data): try: serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) res = serverClient.query(q.update(q.ref(q.collection(collection), id), {"data": data})) res["data"]["id"] = res["ref"].id() return res["data"] except Exception as ex: raise ex
def get(index, data): try: serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) res = serverClient.query(q.get(q.match(q.index(index), data))) res["data"]["id"] = res["ref"].id() return res["data"] except Exception as ex: raise ex
class FaunaDB: def __init__(self, database="Balthus", collection="domains"): self.database = database self.collection = collection FAUNA_SECRET = os.getenv("FAUNA_SECRET") self.client = FaunaClient(secret=FAUNA_SECRET) try: self.client.query(q.create_database({"name": database})) except: print("Database: {} already exist".format(database)) ref = self.client.query( q.create_key({ "database": q.database(database), "role": "server" })) self.client = FaunaClient(secret=ref['secret']) try: self.client.query(q.create_collection({"name": collection})) except: print("Collection: {} already exist".format(collection)) def fill_collection(self, records): self.client.query( q.map_( lambda data: q.create(q.collection(self.collection), {"data": data}), records))
def create_server_client(): client = FaunaClient(secret=os.environ.get('FAUNA_SECRET')) client.query(q.create_collection({"name": "users"})) client.query( q.create_index({ "name": "users_by_username", "source": q.collection("users"), "permissions": { "read": "public" }, "terms": [{ "field": ["data", "username"] }], "unique": True })) client.create_collection()
class FaunaWrapper: def __init__(self): url = os.getenv("FAUNA_KEY") self.client = FaunaClient(secret=url) # initialize faunadb client pass def get_documents_in_index(self, index="unique_news_items", size=100000): """Assume index name exists Validation not needed personal script unique_halts, unique_news, unique_short_news """ documents = self.client.query( q.paginate(q.match(q.index(index)), size=100000)) return documents # Have a faunadb class with a refer to the client def create_document_in_collection(self, collection, collection_data): """Assumes that collection name exists collections are halts and news and short_news Validation not needed, personal project <3 """ try: result = self.client.query( q.create(q.collection(collection), {"data": collection_data})) print(result) return True except BadRequest as error: # get properties of bad request # print(dir(bad)) if hasattr(error, "_get_description"): if error._get_description() == "document is not unique.": ticker = collection_data.get("ticker") print(f"skipping {ticker} since doc is not unique") return False # unknown error, stop everything except Exception as error: print(collection_data) # raise Exception(error) pass
def logout(user, logout_type): if logout_type == "all": all_tokens = True else: all_tokens = False user_client = FaunaClient(secret=session["user_secret"]) result = user_client.query(q.logout(all_tokens)) session.clear() return redirect(url_for("index"))
def decorated(*args, **kwargs): if "user_secret" in session: try: user_client = FaunaClient(secret=session["user_secret"]) result = user_client.query(q.current_identity()) except Unauthorized as e: session.clear() return redirect(url_for("login")) else: return redirect(url_for("login")) return f(result, *args, **kwargs)
def get_by_ref_id(collection, id): """ Get record by ID """ try: serverClient = FaunaClient( secret=os.environ.get("FAUNA_SERVER_SECRET")) res = serverClient.query(q.get(q.ref(q.collection(collection), id))) res["data"]["ref_id"] = res["ref"].id() return res["data"] except Exception as ex: raise ex
def create(collection, data): """ Create new Record """ try: serverClient = FaunaClient( secret=os.environ.get("FAUNA_SERVER_SECRET")) res = serverClient.query( q.create(q.collection(collection), {"data": data})) res["data"]["ref_id"] = res["ref"].id() return res["data"] except Exception as ex: raise ex
def do_GET(self): query = self.path.split('?', 1) params = parse_qs(query[1]) theme = params.get('theme', '')[0] m = pymorphy2.MorphAnalyzer() client = FaunaClient(secret=os.environ.get('DBSECRET')) futr_news = [] # futr_news = self.getNews('world', client, m) + self.getNews('nation', client, m) + self.getNews('scitech', client, m) futr_news = self.getNews(theme, client, m) if len(futr_news) > 0: client.query( q.map_( lambda post: q.create(q.collection('NewsPast'), {'data': post}), futr_news)) self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(futr_news).encode()) return
def login(): body = request.json client = FaunaClient(secret=FAUNA_SECRET) try: result = client.query( q.login(q.match(q.index("Users_by_username"), body["username"]), {"password": body["password"]})) return {"secret": result['secret']} except faunadb.errors.BadRequest as exception: error = exception.errors[0] return {"code": error.code, "description": error.description}, 401
def things(): userSecret = request.headers.get('fauna-user-secret') client = FaunaClient(secret=userSecret) try: result = client.query( q.map_(q.lambda_("ref", q.get(q.var("ref"))), q.paginate(q.documents(q.collection("Things"))))) things = map( lambda doc: { "id": doc["ref"].id(), "name": doc["data"]["name"], "color": doc["data"]["color"] }, result["data"]) return {"things": list(things)} except faunadb.errors.Unauthorized as exception: error = exception.errors[0] return {"code": error.code, "description": error.description}, 401
def signup(): body = request.json client = FaunaClient(secret=FAUNA_SECRET) try: result = client.query( q.create( q.collection("Users"), { "data": { "username": body["username"] }, "credentials": { "password": body["password"] } })) return {"userId": result['ref'].id()} except faunadb.errors.BadRequest as exception: error = exception.errors[0] return {"code": error.code, "description": error.description}, 409
from faunadb.client import FaunaClient from tqdm import tqdm import pprint import json f = open(".faunarc", "r") testDatabaseKey = f.readline()[10:] f.close() # print(testDatabaseKey) client = FaunaClient(testDatabaseKey) # indexes = client.query(q.paginate(q.indexes())) # pprint.pprint(indexes) with open("classes.txt", "r") as f: classes_dict = json.load(f) # print(list(classes_dict.keys())) for c in tqdm(list(classes_dict.keys())[5:]): data_dict = { "identifier": classes_dict[c]["course_abbrev"], "course_num": classes_dict[c]["course_number"], "full_identifier": c, "department": classes_dict[c]["department"], "description": classes_dict[c]["description"], "credit_hours": classes_dict[c]["credit_hours"] } client.query(q.create(q.collection("Class"), {"data": data_dict}))
async def get_package_names() -> List[str]: client = FaunaClient(secret=settings.FAUNADB_KEY.get_secret_value()) return client.query(q.paginate(q.match(q.index("package_names"))))["data"]
from faunadb import query as q from faunadb.objects import Ref from faunadb.client import FaunaClient from dotenv import dotenv_values from munch import AutoMunch from pprint import pprint cfg = AutoMunch(dotenv_values()) # pprint(cfg) # exit() fdb = FaunaClient(secret=cfg.FAUNASECRET) if __name__ == "__main__": indexes = fdb.query(q.paginate(q.collections())) pprint(indexes)
#------------------------------------------------------------------------------- # Imports import face_recognition import cv2 import numpy as np import re from faunadb import query as q from faunadb.objects import Ref from faunadb.client import FaunaClient import array #------------------------------------------------------------------------------- # Variables & Setup video_capture = cv2.VideoCapture(0) # Get a reference to the webcam client = FaunaClient(secret="YOUR_SECRET_HERE") # Connection To Fauna indexdata = client.query(q.paginate(q.match(q.index('YOUR_INDEX_HERE')))) idlist = [indexdata['data']] result = re.findall('\d+', str(idlist)) # print (result) known_face_encodings = [] known_face_names = [] for i in range(0, len(result)): # print(result[i]) currentuserid = result[i] userdetails = client.query( q.get(q.ref(q.collection('YOUR_COLLECTION_HERE'), currentuserid))) details_list = [userdetails['data']]
# © Dr. Takeyuki UEDA # ### import os from faunadb.client import FaunaClient from faunadb.objects import Ref from faunadb import query as q client = FaunaClient(secret=os.environ['FAUNADB_SECRET']) # CRUD examples ## Databases ### Create a database client.query(q.create_database({"name": "annuvin"})) ### Paginate all databases client.query(q.paginate(q.databases())) ### Get a database client.query(q.get(q.database("annuvin"))) ### Rename a database client.query(q.update(q.database("annuvin"), {"name": "llyr"})) ### Annotate a database client.query(q.update(q.database("llyr"), {"data": {"env": "test"}})) ### Delete a database client.query(q.delete(q.database("llyr")))
from decouple import config from faunadb import query as q from faunadb.client import FaunaClient from faunadb.objects import Ref from faunadb.errors import BadRequest, NotFound from faker import Faker from models import TweetCategoryEnum tweetCategories = [e.value for e in TweetCategoryEnum] fake = Faker() # Initialize the FaunaDB client FAUNA_SECRET_KEY = config('FAUNA_SECRET_KEY') client = FaunaClient(secret=FAUNA_SECRET_KEY) indexes = client.query(q.paginate(q.indexes())) # Show FaunaDB indexes # print(indexes) class User: """ Description:- Helper functions for User model (CRUD) """ def __init__(self): self.collection = 'users' def register_user(self, data: dict): """ CREATE - Register a new user """ new_user = client.query(
def do_GET(self): code = os.environ.get('VKTOKEN') app = os.environ.get('VKAPPID') secret = os.environ.get('VKSECRET') vk_session = vk_api.VkApi(token=code, app_id=app, client_secret=secret) try: vk = vk_session.get_api() except vk_api.AuthError as error_msg: print(error_msg) return # CORE RADIO group groups = ['-23314431'] posts = [] client = FaunaClient(secret=os.environ.get('DBSECRET')) for group in groups: remote_wall = vk.wall.get(count=10, owner_id=group) for post in remote_wall['items']: postid = post['id'] date = datetime.fromtimestamp(post['date']).isoformat() text = [t for t in post['text'].split('\n') if len(t) > 1] if len(text) > 2: title = text[0] country = text[2] genre = text[1] search = client.query( q.paginate(q.match(q.index("titles"), title))) if not search['data']: if ('2021' in title) and (('Metalcore' in genre) or ('Deathcore' in genre) or ('Post-Hardcore' in genre)): img = [ img for img in post['attachments'][0]['photo'] ['sizes'] if img['type'] == 'x' ][0]['url'] links = [ link for link in post['attachments'] if link['type'] == 'link' ] if len(links) > 0: url = links[0]['link']['url'] if 'Post-Hardcore' in genre: style = 'Post-Hardcore' elif 'Deathcore' in genre: style = 'Deathcore' else: style = 'Metalcore' posts.append({ 'title': title, 'date': date, 'img': img, 'country': country, 'genre': genre, # 'style': style, 'groupid': group, 'postid': postid, 'url': url }) if len(posts) > 0: client.query( q.map_( lambda post: q.create(q.collection("AlbumEntry"), {"data": post}), posts)) self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps({'posts': posts}).encode()) return
from faunadb import query as q from faunadb.objects import Ref from faunadb.client import FaunaClient client = FaunaClient(secret="secret", domain="localhost", scheme="http", port="8443") # create db client.query(q.create_database({"name": "my_app"})) # Accessing the database # Create an initial server key by using an admin key. The server key has unrestricted access to a single database; # in this case, the server key will only access the blog post database we just created. client.query(q.create_key({ "database": q.database("my_app"), "role": "server" })) # Set up a collection # Create a collection using the CreateCollection function with a param_object containing the name of the collection. # We shall name our collection "posts": client.query(q.create_collection({"name": "posts"})) # Create an index # The customary way to access documents within a collection is by specifying a criteria for one of the fields. # To enable criteria-based searches, we need to first create an index using the path of the field within the document. client.query(
# industry_classified = list(set([item[2] for item in result.values.tolist()])) # print(industry_classified) with open("./data/stocks.json", "r") as in_file: data = json.load(in_file) code_list = [[ "{}{}".format(stock[0].split(".")[-1].lower(), stock[1]), stock[2], stock[4] ] for stock in data] # code_list = code_list[:100] with concurrent.futures.ThreadPoolExecutor(max_workers=500) as executor: futures = [executor.submit(analyze_item, item) for item in code_list] for future in concurrent.futures.as_completed(futures): pass df = pd.DataFrame(analyze_data) df = df.join(pd.DataFrame(df["gain"].to_dict()).T) index_names = df[(df["long"] < 0) | (df["short"] < 0)].index df.drop(index_names, inplace=True) df = df.sort_values(["score"], ascending=[False]) top_10 = df.head(10).values.tolist() ret = server_client.query( q.create(q.collection("analyze"), {"data": { "result": top_10 }})) # with open("result.json", "w") as result_json: # json.dump(analyze_data, result_json)
def delete_database(stack_name): client = FaunaClient(secret=os.environ['FAUNADB_SECRET']) client.query(q.delete(q.database(stack_name)))
from faunadb import query as q from faunadb.objects import Ref from faunadb.client import FaunaClient import pprint f = open(".faunarc", "r") testDatabaseKey = f.readline()[10:] f.close() print(testDatabaseKey) client = FaunaClient(testDatabaseKey) indexes = client.query(q.paginate(q.indexes())) allPeopleAllFields = client.query(q.paginate(q.match( q.index("all_people"))))["data"] for person in allPeopleAllFields: print(client.query(q.get(person))["data"])