Skip to content

Commit

Permalink
Merge pull request #75 from astropenguin/#74-release-v0.5.0
Browse files Browse the repository at this point in the history
Release v0.5.0
  • Loading branch information
astropenguin committed Oct 31, 2021
2 parents 5789bec + b30d813 commit c85a56a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Translate and post arXiv articles to various apps

```shell
$ pip install arxiv-post
$ playwright install chromium
```

## Usage
Expand Down
5 changes: 2 additions & 3 deletions arxiv_post/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# flake8: noqa
# type: ignore
__author__ = "Akio Taniguchi"
__version__ = "0.4.0"
__version__ = "0.5.0"


# submodules
from . import article
from . import apps
from . import article
from . import cli
from . import constants
from . import search
Expand Down
1 change: 0 additions & 1 deletion arxiv_post/apps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# flake8: noqa
# type: ignore


# submodules
Expand Down
4 changes: 2 additions & 2 deletions arxiv_post/apps/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def post(articles: Sequence[Article], webhook_url: str, dry_run: bool) -> None:
if not dry_run:
_post(webhook_url, json=payload)

logger.debug(f"Posted an article ({article.arxiv_url})")
logger.debug(f"Posted an article: {article.arxiv_url}")
except TOMLDecodeError:
logger.warn(f"Failed to post an article ({article.arxiv_url})")
logger.warn(f"Failed to post an article: {article.arxiv_url}")


def to_payload(article: Article) -> Dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion arxiv_post/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def arxiv_pdf_url(self) -> str:
def from_arxiv_result(cls, result: Result) -> "Article":
return Article(
title=result.title,
authors=[a.name for a in result.authors], # type: ignore
authors=[a.name for a in result.authors],
summary=result.summary,
arxiv_url=str(result),
)
Expand Down
8 changes: 4 additions & 4 deletions arxiv_post/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


# dependencies
from fire import Fire # type: ignore
from fire import Fire


# submodules
Expand All @@ -19,8 +19,8 @@
N_CONCURRENT,
TIMEOUT,
)
from .translate import translate
from .search import search
from .translate import translate


# logger
Expand Down Expand Up @@ -80,7 +80,7 @@ def cmd_slack(
keywords = keywords.split(",")

for name, value in locals().items():
logger.debug(f"{name}={value!r}")
logger.debug(f"{name}: {value!r}")

articles = search(
categories=categories,
Expand All @@ -99,6 +99,6 @@ def cmd_slack(
return slack.post(translated, webhook_url, dry_run)


def cli() -> None:
def main() -> None:
"""Entry point of command line interface."""
Fire(dict(slack=cmd_slack))
9 changes: 8 additions & 1 deletion arxiv_post/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@


# standard library
from logging import getLogger
from typing import Generator, Sequence


# dependencies
from arxiv import Search # type: ignore
from arxiv import Search
from dateparser import parse


Expand All @@ -19,6 +20,10 @@
ARXIV_DATE_FORMAT = "%Y%m%d%H%M%S"


# logger
logger = getLogger(__name__)


# runtime functions
def search(
categories: Sequence[str] = CATEGORIES,
Expand Down Expand Up @@ -51,6 +56,8 @@ def search(
sub = " OR ".join(f'abs:"{kwd}"' for kwd in keywords)
query += f" AND ({sub})"

logger.debug(f"Searched articles by: {query!r}")

for result in Search(query).results():
yield Article.from_arxiv_result(result)

Expand Down
24 changes: 15 additions & 9 deletions arxiv_post/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# standard library
from asyncio import gather, sleep, run
from enum import Enum, auto
from logging import getLogger
from textwrap import shorten
from typing import Iterable, List, Protocol, TypeVar, Union


Expand Down Expand Up @@ -64,8 +66,11 @@ def __str__(self) -> str:
return self.to_str()


# logger
logger = getLogger(__name__)


# type hints
S = TypeVar("S")
T = TypeVar("T")


Expand All @@ -79,17 +84,17 @@ def __str__(self) -> str:
...


U = TypeVar("U", bound=Translatable)
TL = TypeVar("TL", bound=Translatable)


# runtime functions
def translate(
translatables: Iterable[U],
translatables: Iterable[TL],
language_to: Union[Language, str] = LANGUAGE_TO,
language_from: Union[Language, str] = LANGUAGE_FROM,
n_concurrent: int = N_CONCURRENT,
timeout: float = TIMEOUT,
) -> List[U]:
) -> List[TL]:
"""Translate objects written in one language to another.
Args:
Expand All @@ -115,12 +120,12 @@ def translate(


async def async_translate(
translatables: Iterable[U],
translatables: Iterable[TL],
language_to: Union[Language, str],
language_from: Union[Language, str],
n_concurrent: int,
timeout: float,
) -> List[U]:
) -> List[TL]:
"""Async version of the translate function."""
if isinstance(language_to, str):
language_to = Language.from_str(language_to)
Expand All @@ -136,13 +141,13 @@ async def async_translate(
context = await browser.new_context()
context.set_default_timeout(1e3 * timeout)

async def div_translate(trs: Iterable[U]) -> List[U]:
async def div_translate(tls: Iterable[TL]) -> List[TL]:
page = await context.new_page()
url = f"{DEEPL_TRANSLATOR}#{language_from}/{language_to}/"

try:
await page.goto(url)
return [await _translate(tr, page, timeout) for tr in trs]
return [await _translate(tl, page, timeout) for tl in tls]
finally:
await page.close()

Expand All @@ -153,7 +158,7 @@ async def div_translate(trs: Iterable[U]) -> List[U]:
await browser.close()


async def _translate(translatable: U, page: Page, timeout: float) -> U:
async def _translate(translatable: TL, page: Page, timeout: float) -> TL:
"""Translate an object by a translator page."""
if not (original := str(translatable)):
return translatable
Expand All @@ -172,4 +177,5 @@ async def _translate(translatable: U, page: Page, timeout: float) -> U:

return translatable.replace(original, content)

logger.warn(f"Failed to translate: {shorten(original, 50)!r}")
return translatable
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "arxiv-post"
version = "0.4.0"
version = "0.5.0"
description = "Translate and post arXiv articles to various apps"
authors = ["Akio Taniguchi <taniguchi@a.phys.nagoya-u.ac.jp>"]
keywords = ["arxiv", "slack", "translation"]
Expand Down Expand Up @@ -30,7 +30,7 @@ pytest = "^6.2"
sphinx = "^4.2"

[tool.poetry.scripts]
arxiv-post = "arxiv_post.cli:cli"
arxiv-post = "arxiv_post.cli:main"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# constants
AUTHOR = "Akio Taniguchi"
VERSION = "0.4.0"
VERSION = "0.5.0"


# test functions
Expand Down

0 comments on commit c85a56a

Please sign in to comment.