Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate code from pecha.org api #1

Merged
merged 12 commits into from
Dec 25, 2024
Merged
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
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Set up Python 3.8
uses: actions/setup-python@v3
with:
python-version: "3.8"

- name: Install dependencies
run: |
pip install -U pip
pip install .
pip install .[dev]

- name: Test with pytest
run: PYTHONPATH=src pytest

- name: Test Coverage
run: PYTHONPATH=src pytest --cov project_name
run: PYTHONPATH=src pytest --cov pecha_uploader
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ repos:
args: ["--config=setup.cfg"]
additional_dependencies: [flake8-isort]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
hooks:
- id: mypy
additional_dependencies: [types-all]

# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
</h1>

## _Project Name_
_The project name should match its code's capability so that new users can easily understand what it does._
_Pecha Uploader_

## Owner(s)

_Change to the owner(s) of the new repo. (This template's owners are:)_
- [@ngawangtrinley](https://github.com/ngawangtrinley)
- [@mikkokotila](https://github.com/mikkokotila)
- [@evanyerburgh](https://github.com/evanyerburgh)
- [@sandup](https://github.com/lobsam)


## Table of contents
Expand Down Expand Up @@ -56,16 +54,16 @@ Get started with _Project Name_ by _(write the first step a user needs to start


### Install _Project Name_
1. _Write the step here._
1. _Write the step here._

_Explanatory text here_

_Explanatory text here_

_(Optional: Include a code sample or screenshot that helps your users complete this step.)_

2. _Write the step here._
a. _Substep 1_

a. _Substep 1_

b. _Substep 2_


Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "project_name"
name = "pecha_uploader"
version = "0.0.1"
authors = [
{ name="OpenPecha", email="dev@openpecha.org" },
]
description = "A small example package"
description = "This project handles the pecha api in order to upload text/pecha to pecha.org"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jsonpickle
File renamed without changes.
Empty file.
27 changes: 27 additions & 0 deletions src/pecha_uploader/category/extract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import urllib
from urllib.error import HTTPError

from pecha_uploader.config import baseURL, headers


def get_category(category_name: str):
"""
Check full category path for variable `category_name`.
`category_name`: str, example: "Indian Treatises/Madyamika/The way of the bodhisattvas"
"""
url = baseURL + "api/category/" + urllib.parse.quote(category_name)
req = urllib.request.Request(url, method="GET", headers=headers)

try:
response = urllib.request.urlopen(req)
res = response.read().decode("utf-8")
print(res)
if "error" not in res:
return {"status": True}
elif "already exists" in res["error"]:
return {"status": True}
return {"status": False, "error": res}
except HTTPError as e:
print("[categories] Error code: ", e.code)
print(e.read())
return {"status": False, "error": res}
46 changes: 46 additions & 0 deletions src/pecha_uploader/category/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import json
from typing import List
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen

from pecha_uploader.config import PECHA_API_KEY, baseURL, headers


def post_category(en_category_list: List[str], bo_category_list: List[str]):
"""
Post path for article categorizing.
You MUST use post_term() before using post_category().
`pathLIST`: list of str,
if you want to post path = "Indian Treatises/Madyamika/The way of the bodhisattvas"
=> post_category(["Indian Treatises"])
=> post_category(["Indian Treatises", "Madyamika"])
=> post_category(["Indian Treatises", "Madyamika", "The way of the bodhisattvas"])
"""
url = baseURL + "api/category"
category = {
"sharedTitle": list(map(lambda x: x["name"], en_category_list))[-1],
"path": list(map(lambda x: x["name"], en_category_list)),
"enDesc": list(map(lambda x: x["enDesc"], en_category_list))[-1],
"heDesc": list(map(lambda x: x["heDesc"], bo_category_list))[-1],
"enShortDesc": list(map(lambda x: x["enShortDesc"], en_category_list))[-1],
"heShortDesc": list(map(lambda x: x["heShortDesc"], bo_category_list))[-1],
}
input_json = json.dumps(category)
values = {"json": input_json, "apikey": PECHA_API_KEY}

data = urlencode(values)
binary_data = data.encode("ascii")
req = Request(url, binary_data, headers=headers)

try:
response = urlopen(req)
res = response.read().decode("utf-8")
if "error" not in res:
return {"status": True}
elif "already exists" in res:
return {"status": True}
return {"status": True, "error": res}
except HTTPError as e:
print("Error code: ", e, list(map(lambda x: x["name"], en_category_list))[-1])
return {"status": False, "error": e}
13 changes: 13 additions & 0 deletions src/pecha_uploader/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

PECHA_API_KEY = os.getenv("PECHA_API_KEY")
if not PECHA_API_KEY:
raise ValueError("Please set PECHA_API_KEY environment variable")

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" # noqa
}
BASEPATH = os.path.dirname(os.path.abspath(__file__)) # path to `Pecha.org/tools`

# baseURL = "https://staging.pecha.org/"
baseURL = "http://127.0.0.1:8000/"
File renamed without changes.
Empty file.
22 changes: 22 additions & 0 deletions src/pecha_uploader/index/extract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json
import urllib
from urllib.error import HTTPError

from pecha_uploader.config import baseURL, headers


def get_index(index: str):
"""
Get Index information for article name `index`.
`index`: str, article en name
"""
index_url = baseURL + "api/v2/raw/index"
prepare_index_str = index.replace(" ", "_")
url = f"{index_url}/{prepare_index_str}?with_content_counts=1"
req = urllib.request.Request(url, method="GET", headers=headers)
try:
response = urllib.request.urlopen(req)
print(json.loads(response.read().decode("utf-8")))
except HTTPError as e:
print("Error code: ", e.code)
print(e.read().decode("utf-8"))
62 changes: 62 additions & 0 deletions src/pecha_uploader/index/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import urllib
from typing import Dict, List
from urllib.error import HTTPError

from pecha_uploader.config import PECHA_API_KEY, baseURL, headers


def post_index(index_str: str, category_list: List[str], nodes: Dict):
""" "
Post index value for article settings.
`index`: str, article title,
`catLIST`: list of str, category list (see post_category() for example),
`titleLIST`: list of json, title name in different language,
titleLIST = {
"lang": "en/he",
"text": "Your en/he title",
"primary": True (You must have a primary title for each language)
}
"""
url = (
baseURL + "api/v2/raw/index/" + urllib.parse.quote(index_str.replace(" ", "_"))
)

# "titles" : titleLIST,
# "key" : index,
# "nodeType" : "JaggedArrayNode",
# # "lengths" : [4, 50],
# "depth" : 2,
# "sections" : ["Chapter", "Verse"],
# "addressTypes" : ["Integer", "Integer"],

index = {"title": "", "categories": [], "schema": {}}
index["title"] = index_str
index["categories"] = list(map(lambda x: x["name"], category_list))
index["schema"] = nodes

# if text is commentary
if "base_text_mapping" in category_list[-1].keys():
index["base_text_titles"] = category_list[-1]["base_text_titles"]
index["base_text_mapping"] = category_list[-1]["base_text_mapping"]
index["collective_title"] = index_str
index["dependence"] = category_list[-1]["link"]

input_json = json.dumps(index, indent=4, ensure_ascii=False)

values = {
"json": input_json,
"apikey": PECHA_API_KEY,
}
data = urllib.parse.urlencode(values)
binary_data = data.encode("ascii")
req = urllib.request.Request(url, binary_data, headers=headers)
try:
response = urllib.request.urlopen(req)
res = response.read().decode("utf-8")
if "error" in res and "already exists." not in res:
return {"status": False, "error": res}
return {"status": True}
except HTTPError as e:
print("Error code: ", e.code, e.read())
return {"status": False, "error": e.read()}
Loading
Loading