def registration_handler(args): if argument_checker(args, ['email', 'password'], ['name', 'surname', 'patronymic', 'sex']): email = args['email'] password = args['password'] name = 'Not indicated' surname = 'Not indicated' patronymic = 'Not indicated' sex = 'U' if 'name' in args.keys(): name = args['name'] if 'surname' in args.keys(): surname = args['surname'] if 'patronymic' in args.keys(): patronymic = args['patronymic'] if 'sex' in args.keys(): sex = args['sex'] try: db_conn, cursor = db_connector.connect() cursor.execute('SELECT uuid_generate_v4()') token = cursor.fetchone()[0] try: cursor.execute( 'INSERT INTO Users(email, password, name, surname, patronymic, sex, token) VALUES (%s,%s,%s,%s,%s,%s,%s);', (email, password, name, surname, patronymic, sex, token)) db_conn.commit() db_conn.close() except: return jsonify(form_response_dictionary(1)) return jsonify(form_response_dictionary(0)) except: return jsonify(form_response_dictionary(4)) else: return jsonify(form_response_dictionary(3))
class Database: _cursor, _db = connect() @staticmethod def base_find(id, table_name): Database._cursor.execute(f"Select * from {table_name} where id = {id}") result = Database._cursor.fetchone() return result @staticmethod def base_get(table_name): Database._cursor.execute(f"Select * from {table_name}") return Database._cursor.fetchall()
def search(driver, userid, database, search_in=0): if search_in == 0: userInput = input( "Enter any item you would like to search for on Amazon : ") else: userInput = search_in # Add the search to user's search history in the database if database != 0: if userid != 1: shop_db = db_connector.connect() query = "INSERT INTO History(UserId, RecentSearches) VALUES (\'%s', '%s')" % ( userid, str(userInput)) shop_db.commit() driver.get('https://www.amazon.com') driver.implicitly_wait(5) search_box = driver.find_element_by_id("twotabsearchtextbox") search_box.send_keys(userInput) search_box.send_keys(Keys.RETURN) if (search_in == 0): print() # get info of first item name = getItemName(driver) price = getItemPrice(driver) output = "" if search_in == 0: print("Item: " + name[0]) print("Price: " + price[0] + "." + price[1]) print("Item: " + name[1]) print("Price: " + price[2] + "." + price[3]) print("Item: " + name[2]) print("Price: " + price[4] + "." + price[5]) output += "Item: " + name[0] + "\n" output += "Price: " + price[0] + "." + price[1] + "\n\n" output += "Item: " + name[1] + "\n" output += "Price: " + price[2] + "." + price[3] + "\n\n" output += "Item: " + name[2] + "\n" output += "Price: " + price[4] + "." + price[5] + "\n\n" priceAsDouble = int(price[0]) + (int(price[1]) / 100) return output
resortStr = resort['href'].replace("/", "") if "http" not in resortStr: print "found resort " + resortStr + " -> now load snow" bergfexparser.snow_height(resortStr, region, country) print "sleep 10 sek" time.sleep(10) # main part start = datetime.now() print "start bergfex parser" # set string formating to UTF reload(sys) sys.setdefaultencoding('utf-8') country = "oesterreich" region_array = [ "tirol", "steiermark", "salzburg", "kaernten", "oberoesterreich", "niederoesterreich", "vorarlberg" ] db_connector.connect() for region in region_array: load_resort_for_region(region, country) #bergfexparser.snow_height("tauplitz", "steiermark", country) db_connector.close() print "finished execution, runtime ", (datetime.now() - start)
def save_to_db(): db_connector.connect() for lang, rss_records in RSS_STACK.items(): for rss_record in rss_records: db_connector.insert(rss_record, "prod" if WRITE_TO_PROD_TABLE else "test")
import requests import db_connector as db import random # connect to db db.connect() # get configuration from db config = db.get_configurations('testing') user_name = config['user_name'] rest_url = config['rest_gateway_url'] try: while True: # go throw loop while getting id already exist error user_id = str(random.randint(1, 100)) # generate random user_id post_res = requests.post(rest_url + user_id, json={"user_name": user_name}) # send request to create new user # check response code and response reason if not post_res.status_code == 200 and post_res.json()['reason'] == 'id already exists': continue # generated id already exist in db, try again in loop # check if there are another error if not post_res.status_code == 200 and not post_res.json()['reason'] == 'id already exists': raise Exception("test failed") # there are another error during creating new user, test failed else: break # stop loop because user was created successfully get_res = requests.get(rest_url + user_id) # send get request to get created user # check response code and response user_name if not get_res.status_code == 200 or not get_res.json()['user_name'] == user_name: # if the response code is not 200 or user_name is different test failed raise Exception("test failed") user_data = db.get_user(user_id) # get user from db by user_id
def save_to_db(stack): db_connector.connect() for rss_record in stack: db_connector.insert(rss_record)
from db_connector import connect cursor, mydb = connect() def setup(): cursor.execute('create database if not exists shop_inventory') cursor.execute('use shop_inventory') cursor.execute( 'create table if not exists category(id int auto_increment, type text, name text, primary key(id))' ) cursor.execute( 'create table if not exists stock(id int auto_increment, name text,type text, stock_in_qty int, sale_price double, stock_out_qty int, purchase_price double, primary key(id))' ) setup() def addCategories(): item_type = input("Enter the type of item: ") item_name = input("Enter the name of item: ") cursor.execute('insert into category (type,name) values(%s,%s)', [item_type, item_name]) mydb.commit() def viewCatagories(): cursor.execute('select * from category') for category in cursor.fetchall(): print(
class Database: _cursor, _db = connect()
def login(driver): username, password = prompts() # Flag to set if mysql in installed or not use_database = 0; driver.get('https://www.amazon.com') driver.implicitly_wait(10) signin = driver.find_element_by_id("nav-link-accountList") signin.click() driver.implicitly_wait(3) email_box = driver.find_element_by_id("ap_email") email_box.send_keys(username) email_box.send_keys(Keys.RETURN) # Incorrect username driver.implicitly_wait(5) alert = driver.find_elements_by_class_name("a-alert-heading") if len(alert) >= 1 and alert[0].text.lower() == "there was a problem": print("Error: Incorrect Password or Email, try again") print() return login(driver) # Try again passwd_box = driver.find_element_by_id("ap_password") passwd_box.send_keys(password) passwd_box.send_keys(Keys.RETURN) # Incorrect Password driver.implicitly_wait(5) alert = driver.find_elements_by_class_name("a-alert-heading") if len(alert) >= 1 and (alert[0].text.lower() == "there was a problem" or alert[0].text.lower() == "important message!"): print("Error: Incorrect Password or Email, try again") print() return login(driver) # Try again # Requires 2FA driver.implicitly_wait(3) alert = driver.find_elements_by_xpath("//*[@class='a-row a-spacing-small']") if len(alert) >= 1 and (alert[0].text.lower() == "authentication required" or alert[0].text.lower() == "important message!"): print("Error, 2FA is not supported. Please disable or continue without login") return 1 # DATABASE if use_db: shop_db = db_connector.connect() userid = getId(username); cursor = shop_db.cursor() cursor.execute("INSERT INTO User (UserId, Email, Password) VALUES (%s, %s, %s)", (userid, username, password)); #Commit new entry into the database shop_db.commit() return userid