Newer
Older
#!/usr/bin/env python3
#To import data you need a excel style CSV (";" as seperator) with the first as short url (e.g. kurz.ml/example) and in the seccond row the long url (e.g http://example.com).
#Then run docker -it <container_name> python3 import.py Now you can paste the CSV as text and press enter again at the end
from tqdm import tqdm #If the database size is extreme you will see the progress, but normaly you see direct the 100%.
def table_check(): #Check if database exists
create_table = """
CREATE TABLE WEB_URL(
LONG_URL TEXT NOT NULL,
SHORT_URL TEXT NOT NULL
);
"""
with sqlite3.connect('db/urls.db') as conn:
cursor = conn.cursor()
try: #Try making the database structure, if fails Database was already created.
cursor.execute(create_table)
except sqlite3.OperationalError:
pass
table_check()
with sqlite3.connect('db/urls.db') as conn:
cursor = conn.cursor()
lines = []
print("ready to recive data, please paste your data to import and press ENTER to import all")
while True: #read input line, if line is empty proceed to next step.
line = input()
if line:
lines.append(line)
else:
break
text = lines
for lines in tqdm(text):
SHORT_URL = lines.split(";")[0].replace("\n", "").replace("\r","") #Split the CSV at the ";" then use the first one and replace all linebreaks
LONG_URL = lines.split(";")[1].replace("\n", "").replace("\r","") #Split the CSV at the ";" then use the seccond one and replace all linebreaks
res = cursor.execute( #Insert the data in the SQL table
'INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)',
[LONG_URL, SHORT_URL.lower()] #replace big letters in short url with small letters.