Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
49 changes: 49 additions & 0 deletions README-fastapi.md
Original file line number Diff line number Diff line change
@@ -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
<div class="termy">

```console
$ pip install fastapi

---> 100%
```

</div>

You will also need an ASGI server, for production such as <a href="https://www.uvicorn.org" class="external-link" target="_blank">Uvicorn</a> or <a href="https://github.com/pgjones/hypercorn" class="external-link" target="_blank">Hypercorn</a>.

<div class="termy">

```console
$ pip install "uvicorn[standard]"

---> 100%
```

</div>

### Run it

Run the server with:

<div class="termy">

```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.
```

</div>

### 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
90 changes: 90 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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])