Skip to content

Commit

Permalink
Merge pull request #31 from SenZmaKi/v2.1.5
Browse files Browse the repository at this point in the history
v2.1.5
  • Loading branch information
SenZmaKi authored Mar 9, 2024
2 parents cda52d8 + c4a4789 commit 6cf22b3
Show file tree
Hide file tree
Showing 22 changed files with 514 additions and 88 deletions.
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ test-downloads/
__pycache__/
.ruff_cache/
.pytest_cache
allanime.py
marin.py
setups
build
dist
setups/
build/
dist/
.scraps/
.credentials/

crap.py
.todo
scripts/changelog.md
56 changes: 0 additions & 56 deletions bump_version.py

This file was deleted.

32 changes: 27 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "senpwai"
version = "2.1.4"
version = "2.1.5"
description = "A desktop app for tracking and batch downloading anime"
authors = ["SenZmaKi <[email protected]>"]
license = "GPL v3"
Expand Down Expand Up @@ -63,12 +63,18 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poe.tasks]
release = "python -m scripts.release"
bump_version = "python -m scripts.bump_version"
generate_release_test = [{ ref = "test" }, { ref = "generate_release" }]
generate_release_ddl = [{ ref = "test_ddl" }, { ref = "generate_release" }]
generate_release = [
{ ref = "build_exes" },
{ ref = "compile_installers" },
{ ref = "install" },
{ ref = "run_exes" },
{ ref = "build_pip" },
{ ref = "install_pip" },
{ ref = "run_pip_exes" },
]
test = [{ ref = "test_pahe" }, { ref = "test_gogo" }]
test_pahe = "python -m senpwai.scrapers.test --site pahe all"
Expand All @@ -83,16 +89,32 @@ test_gogo_norm_ddl = "python -m senpwai.scrapers.test --site gogo direct_links"
test_gogo_hls_ddl = "python -m senpwai.scrapers.test --site gogo hls_links --hls"

build_exes = [{ ref = "build_senpwai_exe" }, { ref = "build_senpcli_exe" }]
build_senpwai_exe = "python setup.py build"
build_senpcli_exe = "python setup.py --senpcli build"
build_senpwai_exe = "python -m scripts.setup build"
build_senpcli_exe = "python -m scripts.setup --senpcli build"

compile_installers = [
{ ref = "compile_senpwai_installer" },
{ ref = "compile_senpcli_installer" },
]
compile_senpwai_installer = "iscc setup.iss /Q"
compile_senpcli_installer = "iscc setup_senpcli.iss /Q"
compile_senpwai_installer = "iscc scripts/setup.iss /Q"
compile_senpcli_installer = "iscc scripts/setup_senpcli.iss /Q"

install = [{ ref = "install_senpwai" }, { ref = "install_senpcli" }]
install_senpwai = "setups/Senpwai-setup.exe /verysilent"
install_senpcli = "setups/Senpcli-setup.exe /verysilent"

build_pip = [{ ref = "rb_dist" }, { ref = "poetry_build" }]
rb_dist = "rb dist -r"
poetry_build = "poetry build"
install_pip = "python -m scripts.install_pip"
publish_pip = "poetry publish"
run_all_exes = "python -m scripts.run_exes --all"
run_exes = [{ ref = "run_senpwai_exe" }, { ref = "run_senpcli_exe" }]
run_pip_exes = [
{ ref = "run_senpwai_pip_exe" },
{ ref = "run_senpcli_pip_exe" },
]
run_senpwai_exe = "python -m scripts.run_exes --senpwai"
run_senpcli_exe = "python -m scripts.run_exes --senpcli"
run_senpwai_pip_exe = "python -m scripts.run_exes --senpwai_pip"
run_senpcli_pip_exe = "python -m scripts.run_exes --senpcli_pip"
1 change: 1 addition & 0 deletions scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions scripts/announce/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .main import main # noqa F403
5 changes: 5 additions & 0 deletions scripts/announce/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from announce.main import main
from scripts.utils import get_piped_input, ARGS

if __name__ == "__main__":
main(ARGS[0], get_piped_input())
42 changes: 42 additions & 0 deletions scripts/announce/discord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import requests
import re

from scripts.utils import ARGS, ROOT_DIR, get_piped_input
from .utils import FailedToAnnounce

CHANNEL_URL = "https://discord.com/api/v10/channels/1142774130689720370/messages"
URL_REGEX = re.compile(r"https://\S+")


def remove_embed_url(string: str) -> str:
return URL_REGEX.sub(lambda x: f"<{x.group(0)}>", string)


def main(title: str, release_notes: str) -> None:
with open(ROOT_DIR.joinpath(".credentials/discord.json")) as f:
token = json.load(f)["token"]
headers = {
"Authorization": f"Bot {token}",
"Content-Type": "application/json",
}
nonembed_notes = remove_embed_url(release_notes)
smaller_titles = "\n".join(
[
line.replace("# ", "## ", 1) if line.strip().startswith("# ") else line
for line in nonembed_notes.splitlines()
]
)

everyone = "@everyone\n" if "--ping_everyone" in ARGS else ""
message = f"{everyone}# {title}\n\n" + smaller_titles
payload = {
"content": message,
}
response = requests.post(url=CHANNEL_URL, headers=headers, json=payload)
if not response.ok:
raise FailedToAnnounce("discord", response.json())


if __name__ == "__main__":
main(ARGS[0], get_piped_input())
10 changes: 10 additions & 0 deletions scripts/announce/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from scripts.announce import discord
from scripts.announce import reddit
from scripts.utils import log_info


def main(title: str, release_notes: str) -> None:
log_info("Announcing on Discord")
discord.main(title, release_notes)
log_info("Posting on Reddit")
reddit.main(title, release_notes)
83 changes: 83 additions & 0 deletions scripts/announce/reddit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from typing import Any, cast
from scripts.announce.utils import FailedToAnnounce
from scripts.utils import ARGS, ROOT_DIR, get_piped_input
import json
import requests
import re
from scripts.utils import log_info

ACCESS_TOKEN_URL = "https://www.reddit.com/api/v1/access_token"
SUBMIT_POST_URL = "https://oauth.reddit.com/api/submit"
APPROVE_POST_URL = "https://oauth.reddit.com/api/approve"
POST_ID_REGEX = re.compile(
r"https://www\.reddit\.com/r/Senpwai/comments/([a-zA-Z0-9_]+)/"
)


def validate_response(is_ok: bool, response_json: dict[str, Any]) -> None:
# Only valid in submit_post
success = response_json.get("success", None)
if not is_ok or success is not None and not success:
raise FailedToAnnounce("reddit", response_json)


def fetch_access_token() -> str:
with open(ROOT_DIR.joinpath(".credentials/reddit.json"), "r") as f:
credentials = json.load(f)
data = {
"scope": "*",
"grant_type": "password",
"username": credentials["username"],
"password": credentials["password"],
}
headers = {"User-Agent": "script"}
auth = (credentials["client_id"], credentials["client_secret"])
response = requests.post(
ACCESS_TOKEN_URL, data=data, headers=headers, auth=auth
)
validate_response(response.ok, response.json())
return response.json()["access_token"]


def submit_post(
title: str,
release_notes: str,
access_token: str,
) -> str:
headers = {"Authorization": f"Bearer {access_token}", "User-Agent": "script"}

data = {
"title": title,
"kind": "self",
"sr": "Senpwai",
"resubmit": "true",
"send_replies": "true",
"text": release_notes,
}

response = requests.post(SUBMIT_POST_URL, headers=headers, data=data)
response_json = response.json()
validate_response(response.ok, response_json)
return cast(re.Match, POST_ID_REGEX.search(response.text)).group(1)


def approve_post(post_short_id: str, access_token: str) -> None:
headers = {"Authorization": f"Bearer {access_token}", "User-Agent": "script"}
response = requests.post(
APPROVE_POST_URL, headers=headers, data={"id": f"t3_{post_short_id}"}
)
response_json = response.json()
validate_response(response.ok, response_json)


def main(title: str, release_notes: str) -> None:
log_info("Fetching auth token")
access_token = fetch_access_token()
log_info("Submitting post")
post_short_id = submit_post(title, release_notes, access_token)
log_info("Approving post")
approve_post(post_short_id, access_token)


if __name__ == "__main__":
main(ARGS[0], get_piped_input())
8 changes: 8 additions & 0 deletions scripts/announce/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Any


class FailedToAnnounce(Exception):
def __init__(self, platform: str, response_json: dict[str, Any]) -> None:
super().__init__(
f"Failed to make announcement on {platform}\n\n{response_json}"
)
Loading

0 comments on commit 6cf22b3

Please sign in to comment.