diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/README-fastapi.md b/README-fastapi.md new file mode 100644 index 0000000..433269a --- /dev/null +++ b/README-fastapi.md @@ -0,0 +1,49 @@ +# thai-lotto-archive-fastapi +Archive of winning Thai lottery numbers since 2007. All data are sourced from kapook.com, sanook.com, and other sources. + +## Install dependencies +
+ +```console +$ pip install fastapi + +---> 100% +``` + +
+ +You will also need an ASGI server, for production such as Uvicorn or Hypercorn. + +
+ +```console +$ pip install "uvicorn[standard]" + +---> 100% +``` + +
+ +### Run it + +Run the server with: + +
+ +```console +$ uvicorn main:app --reload + +INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) +INFO: Started reloader process [28720] +INFO: Started server process [28722] +INFO: Waiting for application startup. +INFO: Application startup complete. +``` + +
+ +### GET it + +- http://127.0.0.1:8000/docs +- http://127.0.0.1:8000/latest +- http://127.0.0.1:8000/date/2022-12-16 diff --git a/main.py b/main.py new file mode 100644 index 0000000..3e0908a --- /dev/null +++ b/main.py @@ -0,0 +1,90 @@ +import os, glob +from fastapi import FastAPI +from fastapi.responses import JSONResponse + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"data": "Hello,World"} + +@app.get("/latest") +def get_lotto_latest(): + files = glob.glob('./lottonumbers/2022-12-30.txt') + files = sorted(files) + rows = [] + + need_dtypes = [ + 'FIRST', + 'TWO', + 'THREE', + 'THREE_FIRST', + 'THREE_LAST', + ] + for file in files: + + date = os.path.basename(file).replace('.txt', '') + + with open(file, 'r') as f: + lines = [l.strip() for l in f.readlines()] + + row = {'date': date} + for line in lines[1:]: + data = line.split() + + dtype = data[0] + if dtype not in need_dtypes: + continue + + dtype = dtype.lower() + if len(data) > 2: + for i, d in enumerate(data[1:]): + row[dtype + '_' + str(i + 1)] = d + else: + row[dtype] = data[1] + + + rows.append(row) + print(rows) + return JSONResponse(content=rows[0]) + +@app.get("/date/{date_id}") +def get_lotto_date(date_id: str): + the_file = """./lottonumbers/{0}.txt""".format(date_id) + files = glob.glob(the_file) + files = sorted(files) + rows = [] + + need_dtypes = [ + 'FIRST', + 'TWO', + 'THREE', + 'THREE_FIRST', + 'THREE_LAST' + ] + for file in files: + + date = os.path.basename(file).replace('.txt', '') + + with open(file, 'r') as f: + lines = [l.strip() for l in f.readlines()] + + row = {'date': date} + for line in lines[1:]: + data = line.split() + + dtype = data[0] + if dtype not in need_dtypes: + continue + + dtype = dtype.lower() + if len(data) > 2: + for i, d in enumerate(data[1:]): + row[dtype + '_' + str(i + 1)] = d + else: + row[dtype] = data[1] + + + rows.append(row) + print(rows) + return JSONResponse(content=rows[0]) \ No newline at end of file