Skip to content

Commit

Permalink
Automate csv generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Guvalif committed Sep 25, 2024
1 parent d84bd5d commit e3a3f4a
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 34 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Getting Started
-------------------------------------------------------------------------------

In **git-root** :

```sh
npm ci
```

In **sdk/** :

```sh
python3 -m venv .
source ./bin/activate
pip install -r requirements.txt
```


How to Use
-------------------------------------------------------------------------------

In **sdk/** :

```sh
source ./bin/activate
python3 generate_stocks_csv.py {{ KEYCHAIN_LABEL }}
```

In **git-root** :

```sh
npm run start
```
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"version": "0.0.1",
"private": true,
"scripts": {
"csv": "python3 sdk/add_url_column.py",
"start": "node --loader ts-node/esm src/index.mts"
"start": "node --loader ts-node/esm src/index.mts sdk/stocks.csv"
},
"author": "TAKASE Kazuyuki (@Guvalif)",
"license": "UNLICENSED",
Expand Down
167 changes: 167 additions & 0 deletions sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Project specific ignores
bin/
include/
pyvenv.cfg
32 changes: 0 additions & 32 deletions sdk/add_url_column.py

This file was deleted.

63 changes: 63 additions & 0 deletions sdk/generate_stocks_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import subprocess
from xml.etree.ElementTree import fromstring as xml_fromstring
from json import loads as json_loads
from gspread import authorize
from urllib.parse import quote
from google.oauth2.service_account import Credentials
from csv import writer


SPREADSHEET_ID = '1N4yco9qmRAMu7uS5pa-_vH55D2ZCahHIC-4wiFwxqGI'
WORKSHEET_NAME = 'STOCKS'


def get_service_account_from_keychain(label):
result = subprocess.run(
[ 'security', 'find-generic-password', '-l', label, '-w' ],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)

hex_data = result.stdout.strip()
raw_data = bytes.fromhex(hex_data).decode('utf-8')
xml = xml_fromstring(raw_data)
json_str = xml.find('dict/string').text
json = json_loads(json_str)

return json


def get_latest_stocks(credentials):
client = authorize(credentials)
spreadsheet = client.open_by_key(SPREADSHEET_ID)
worksheet = spreadsheet.worksheet(WORKSHEET_NAME)

header, *body = worksheet.get_all_values()

yield [ 'century', 'rarity', 'number', 'color', 'stock', 'name', 'memo', 'url' ]

for row in body:
url = f"https://www.wikihouse.com/dim0wiki/index.php?{quote(row[5].encode('euc-jp'))}"

yield [ *row, url ]


def main(label):
credentials = Credentials.from_service_account_info(
get_service_account_from_keychain(label)
).with_scopes([
'https://www.googleapis.com/auth/spreadsheets',
])

stocks = get_latest_stocks(credentials)

with open('stocks.csv', 'w') as csv_file:
csv_writer = writer(csv_file)
csv_writer.writerows(stocks)


if __name__ == '__main__':
from sys import argv

main(argv[1])
14 changes: 14 additions & 0 deletions sdk/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cachetools==5.5.0
certifi==2024.8.30
charset-normalizer==3.3.2
google-auth==2.35.0
google-auth-oauthlib==1.2.1
gspread==6.1.2
idna==3.10
oauthlib==3.2.2
pyasn1==0.6.1
pyasn1_modules==0.4.1
requests==2.32.3
requests-oauthlib==2.0.0
rsa==4.9
urllib3==2.2.3

0 comments on commit e3a3f4a

Please sign in to comment.