Skip to content

Commit

Permalink
export scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Affenmilchmann committed May 28, 2024
1 parent fe04cc9 commit 43e6f3e
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
73 changes: 73 additions & 0 deletions api_export_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from pathlib import Path
from time import sleep
from tqdm import tqdm
import requests
import logging
import json
import csv

logging.basicConfig(format='[%(levelname)s]:\t%(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

API = "http://lingconlab.ru/parabible/api"

def call_api(url: str) -> dict:
logger.debug(f"Getting {url}")
resp = requests.get(url)
if not resp.ok:
logger.error(f"Error! {resp.text}")
raise Exception(f"Error! {resp.text}")
return json.loads(resp.text)

def get_book_abbrivs() -> dict:
url = "http://lingconlab.ru/parabible/api/get/book_title_abbrs"
return call_api(url)

def get_chapters(translation_id: int, book_id: int) -> list[int]:
url = f"{API}/get/chapter_ids?mode=all"
url += f"&translation_id={translation_id}"
url += f"&book_id={book_id}"
return call_api(url)['chapters']

def get_verses(translation_id: int, book_id: int, chapter: int) -> list[int]:
url = f"{API}/get/verse_ids?mode=all"
url += f"&translation_id={translation_id}"
url += f"&book_id={book_id}&chapter_id={chapter}"
return call_api(url)['verses']

def get_verse_text(translation_id: int, book_id: int, chapter: int, verse: int) -> str:
url = f"{API}/get/verse?"
url += f"translation_id={translation_id}"
url += f"&book_id={book_id}&chapter={chapter}&verse={verse}"
return call_api(url)['verse']

def form_verse_tag(abbrivs: dict, book_id: int, chapter: int, verse: int) -> str:
return f"{abbrivs[book_id]} {chapter}:{verse}"

def main():
csv_result = Path(__file__).parent.joinpath('misc_scripts/script_data/export.csv')

BOOK_IDS = range(40, 67) # 40 - 66
TRANS_ID = 627
abbrivs = {int(k): v for k, v in get_book_abbrivs().items()}

with open(csv_result, 'w', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(("Verse", "Text"))
for book_id in BOOK_IDS:
logger.info(f"Processing {abbrivs[book_id]}")
chapters = get_chapters(TRANS_ID, book_id)
logger.info(f"{len(chapters)} chapters")
for ch in chapters:
logger.info(f"Chapter {ch}")
verses = get_verses(TRANS_ID, book_id, ch)
logger.info(f"{len(verses)} verses")
for v in tqdm(verses):
verse_text = get_verse_text(TRANS_ID, book_id, ch, v)
verse_tag = form_verse_tag(abbrivs, book_id, ch, v)
writer.writerow((verse_tag, verse_text))
sleep(0.2)

if __name__ == "__main__":
main()
56 changes: 56 additions & 0 deletions db_export_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pathlib import Path
from time import sleep
from tqdm import tqdm
import logging
import json
import csv

logging.basicConfig(format='[%(levelname)s]:\t%(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

from web.app.src.dbmanager import BibleDB
from web.app.src.file_handling import file_data

def get_book_abbrivs() -> dict:
return file_data.get_book_abbrivs()

def get_chapters(db: BibleDB, translation_id: int, book_id: int) -> list[int]:
return db.get_chapters(translation_id, book_id)

def get_verses(db: BibleDB, translation_id: int, book_id: int, chapter: int) -> list[int]:
return db.get_verse_ids(translation_id, book_id, chapter)

def get_verse_text(db: BibleDB, translation_id: int, book_id: int, chapter: int, verse: int) -> str:
return db.get_verse((book_id, chapter, verse), translation_id)

def form_verse_tag(abbrivs: dict, book_id: int, chapter: int, verse: int) -> str:
return f"{abbrivs[book_id]} {chapter}:{verse}"

def main():
db = BibleDB()
csv_result = Path(__file__).parent.joinpath('misc_scripts/script_data/export.csv')

BOOK_IDS = range(40, 67) # 40 - 66
TRANS_ID = 627
abbrivs = {int(k): v for k, v in get_book_abbrivs().items()}

with open(csv_result, 'w', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(("Verse", "Text"))
for book_id in BOOK_IDS:
logger.info(f"Processing {abbrivs[book_id]}")
chapters = get_chapters(db, TRANS_ID, book_id)
logger.info(f"{len(chapters)} chapters")
for ch in chapters:
logger.info(f"Chapter {ch}")
verses = get_verses(db, TRANS_ID, book_id, ch)
logger.info(f"{len(verses)} verses")
for v in tqdm(verses):
verse_text = get_verse_text(db, TRANS_ID, book_id, ch, v)
verse_tag = form_verse_tag(abbrivs, book_id, ch, v)
writer.writerow((verse_tag, verse_text))
sleep(0.2)

if __name__ == "__main__":
main()
File renamed without changes.

0 comments on commit 43e6f3e

Please sign in to comment.