def create_short_url(): ''' Takes long _url as url, custom string(opt), tag(opt) for input and returns short_url ''' if request.method == 'POST': if 'url' in request.args: og_url = request.args['url'] if url_check(og_url) is True: if 'custom' in request.args: token_string = request.args['custom'] if 'tag' in request.args: tag_url = request.args['tag'] else: tag_url = '' else: token_string = random_token() if 'tag' in request.args: tag_url = request.args['tag'] else: tag_url = '' conn = MySQLdb.connect(host, user, passwrd, db) cursor = conn.cursor() check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE" cursor.execute(check_row, (token_string,)) check_fetch = cursor.fetchone() if (check_fetch is None): insert_row = """ INSERT INTO WEB_URL(URL, S_URL, TAG) VALUES( %s, %s, %s) """ cursor.execute(insert_row, (og_url, token_string, tag_url,)) conn.commit() conn.close() short_url = shorty_host + token_string data = jsonify({ 'long_url': og_url, 'short_url': short_url, 'custom': token_string, 'tag': tag_url }) return make_response(data, 200) else: data = jsonify({'error': 'suffix already present'}) return make_response(data, 200) else: data = jsonify({'error': 'URL given is not valid . Enter a valid URL.'}) return make_response(data, 200) else: data = jsonify({'error': 'invalid request'}) return make_response(data, 405) else: data = jsonify({'error': 'Invalid Method Used'}) return make_response(data, 405)
def shorten(): if not request.is_json(): return jsonify({ 'error_message': 'Body should be in JSON format.'}), http.client.BAD_REQUEST body = request.get_json(silent=True) if body is None or body.get('url') is None: return jsonify({ 'error_message': 'Invalid Body' }), http.client.BAD_REQUEST og_url = body.get('url') if not check_prefix(og_url): return jsonify({'error_message': 'URL given is not valid.'}), http.client.BAD_REQUEST token_string = request.args.get('custom', random_token()) tag_url = request.args.get('tag', '') conn = pymysql.connect(db_host , db_user , db_passwrd , db_db) web_url = WebUrl(conn)
def index(): access = False if not config.is_secure: access = True elif 'Auth' in request.headers: if request.headers['Auth'] == config.SECRET_KEY: access = True if not access: return redirect('http://basalam.com') conn = psycopg2.connect(host=host, user=user, password=passwrd, database=db) cursor = conn.cursor() # Return the full table to displat on index. list_sql = "SELECT * FROM WEB_URL;" cursor.execute(list_sql) result_all_fetch = cursor.fetchall() if request.method == 'POST': og_url = request.form.get('url_input') custom_suff = request.form.get('url_custom') tag_url = request.form.get('url_tag') if custom_suff == '': token_string = random_token() else: token_string = custom_suff if og_url != '': if url_check(og_url): # Check's for existing suffix check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE" cursor.execute(check_row, (token_string, )) check_fetch = cursor.fetchone() if check_fetch is None: insert_row = """ INSERT INTO WEB_URL(URL , S_URL , TAG) VALUES( %s, %s , %s) """ result_cur = cursor.execute(insert_row, ( og_url, token_string, tag_url, )) conn.commit() conn.close() e = '' return render_template('index.html', shorty_url=shorty_host + token_string, error=e) else: e = "The Custom suffix already exists." \ " Please use another suffix or leave it blank for random suffix." return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e) else: e = "URL entered doesn't seem valid , Enter a valid URL." return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e) else: e = "Enter a URL." return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e) else: e = '' return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e)
def index(): conn = MySQLdb.connect(host, user, passwrd, db) cursor = conn.cursor() # Return the full table to displat on index. list_sql = "SELECT * FROM WEB_URL;" cursor.execute(list_sql) result_all_fetch = cursor.fetchall() if request.method == 'POST': og_url = request.form.get('url_input') custom_suff = request.form.get('url_custom') tag_url = request.form.get('url_tag') if custom_suff == '': token_string = random_token() else: token_string = custom_suff if og_url != '': if url_check(og_url) == True: # Check's for existing suffix check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE" cursor.execute(check_row, (token_string, )) check_fetch = cursor.fetchone() if (check_fetch is None): insert_row = """ INSERT INTO WEB_URL(URL , S_URL , TAG) VALUES( %s, %s , %s) """ result_cur = cursor.execute(insert_row, ( og_url, token_string, tag_url, )) conn.commit() conn.close() e = '' return render_template('index.html', shorty_url=shorty_host + token_string, error=e) else: e = "The Custom suffix already exists . Please use another suffix or leave it blank for random suffix." return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e) else: e = "URL entered doesn't seem valid , Enter a valid URL." return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e) else: e = "Enter a URL." return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e) else: e = '' return render_template('index.html', table=result_all_fetch, host=shorty_host, error=e)
''' if request.method == 'POST': if 'url' in request.args : og_url = request.args['url'] if check_prefix(og_url): if 'custom' in request.args : token_string = request.args['custom'] if 'tag' in request.args: tag_url = request.args['tag'] else: tag_url = '' else: token_string = random_token() if 'tag' in request.args: tag_url = request.args['tag'] else: tag_url = '' conn = pymysql.connect(db_host , db_user , db_passwrd , db_db) cursor = conn.cursor() check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE" cursor.execute(check_row,(token_string,)) check_fetch = cursor.fetchone() if (check_fetch is None): insert_row = """ INSERT INTO WEB_URL(URL , S_URL , TAG) VALUES( %s, %s , %s)
def index(): page = request.args.get('p', 1) s_url = request.args.get('s_url', False) offset = int(page) * 5 - 5 conn = pymysql.connect(db_host, db_user, db_passwrd, db_db) cursor = conn.cursor() # Return the full table to displat on index. if s_url: list_sql = "SELECT * FROM %s WHERE S_URL = '%s' ORDER BY ID DESC LIMIT 5 OFFSET %s;" % ( db_table, s_url, offset) else: list_sql = "SELECT * FROM %s ORDER BY ID DESC LIMIT 5 OFFSET %s;" % ( db_table, offset) print('query', list_sql) cursor.execute(list_sql) result_all_fetch = cursor.fetchall() if request.method == 'POST': print('post method') og_url = request.form.get('url_input') custom_suff = request.form.get('url_custom') tag_url = request.form.get('url_tag') if custom_suff == '': token_string = random_token() else: token_string = custom_suff if og_url != '': #if url_check(og_url) == True: og_url = check_prefix(og_url) if og_url: # Check's for existing suffix check_row = "SELECT S_URL FROM WEB_URL WHERE S_URL = %s FOR UPDATE" cursor.execute(check_row, (token_string, )) check_fetch = cursor.fetchone() if (check_fetch is None): insert_row = """ INSERT INTO WEB_URL(URL , S_URL , TAG) VALUES( %s, %s , %s) """ result_cur = cursor.execute(insert_row, ( og_url, token_string, tag_url, )) conn.commit() conn.close() e = '' return render_template('index.html', shorty_url=shorty_host + token_string, hostname=SHORTY_URL, offset=offset, error=e) else: e = "The Custom suffix already exists . Please use another suffix or leave it blank for random suffix." return render_template('index.html', table=result_all_fetch, host=shorty_host, hostname=SHORTY_URL, offset=offset, error=e) else: e = "URL entered doesn't seem valid , Enter a valid URL." return render_template('index.html', table=result_all_fetch, host=shorty_host, hostname=SHORTY_URL, offset=offset, error=e) else: e = "Enter a URL." return render_template('index.html', table=result_all_fetch, host=shorty_host, hostname=SHORTY_URL, offset=offset, error=e) else: e = '' return render_template('index.html', table=result_all_fetch, offset=offset, hostname=SHORTY_URL, host=shorty_host, error=e)