Skip to content
Snippets Groups Projects
Commit 7df2c960 authored by Jonas Leder's avatar Jonas Leder
Browse files

Added import and some changes to docker-compose

parent 7dc9ec66
Branches
No related tags found
No related merge requests found
db/urls.db db/urls.db
import.csv
...@@ -12,12 +12,16 @@ LABEL Name=url_shorter Version=1.0.1 ...@@ -12,12 +12,16 @@ LABEL Name=url_shorter Version=1.0.1
EXPOSE 5000 EXPOSE 5000
WORKDIR /app WORKDIR /app
ADD . /app ADD ./static /app/static
ADD ./templates /app/templates
COPY import.py /app/import.py
COPY main.py /app/main.py
#RUN apt update RUN apt update
#RUN apt upgrade -y RUN apt upgrade -y
RUN pip install pipreqs RUN pip install pipreqs
RUN pipreqs . --force RUN pipreqs . --force
RUN python3 -m pip install -r requirements.txt RUN python3 -m pip install -r requirements.txt
RUN date > builddate.txt RUN date > builddate.txt
CMD ["python3", "main.py"]
ENTRYPOINT python3 main.py
import sqlite3
import os
def table_check():
create_table = """
CREATE TABLE WEB_URL(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
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()
try:
file = open("import.csv", "r").readlines()
except:
print("no file for import found")
exit()
entries = len(file)
counter = 1
for lines in file:
print("Importing " + str(counter) + " from " + str(entries))
SHORT_URL = lines.split(";")[0].replace("\n", "").replace("\r","")
LONG_URL = lines.split(";")[1].replace("\n", "").replace("\r","")
res = cursor.execute(
'INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)',
[LONG_URL, SHORT_URL]
)
counter = counter + 1
os.system("exit")
\ No newline at end of file
...@@ -59,7 +59,7 @@ def home(): ...@@ -59,7 +59,7 @@ def home():
except Exception as e: except Exception as e:
pass pass
if not already_used: #If noone used the short link before, insert the data in the Database. if not already_used: #If short link wasn't used before, insert the link in the Database.
res = cursor.execute( res = cursor.execute(
'INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)', 'INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)',
[url, request.form.get('domain') + "/" + request.form.get('short')] [url, request.form.get('domain') + "/" + request.form.get('short')]
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment