Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sheiun committed Oct 18, 2020
0 parents commit 5e99a36
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 0 deletions.
141 changes: 141 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# 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

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__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/

# Custom
.vscode/
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Plurk Dice

## Install

```bash
pip install plurk-dice
```

## Usage

```python
>>> from plurk_dice import Dice

# Roll a dice directly

>>> Dice(20).roll()
{'result': 7, 'url': 'https://s.plurk.com/ff94b39b3f0927042f8479fac0fd92d1.png'}

>>> Dice("bzz").roll(base64=True)
{'result': 'B', 'url': 'https://s.plurk.com/e3481a0219283c49455d8af6012980ea.png', 'base64': 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAARxJREFUeNqs1bFLAmEYx/H3Ui6c2sR0jByV3EWE2gMRkyJpcDOXBsF2JynrDwhqUf8MHaWWtKWtocBwDhzCvhePcYOHd+/bC5/hOR5+vHfv3XOWrb6V5krgEEOMlxc3lP6KoYEbpP8jMIU48riTWjswigpCUmdwiz3dwCpyrnqOJLJhjbB91Fz1APeY4CNo4A5aciCf6Mjzm/51OK+NT1voYYERDlb1+Q0L4VrC+tj16vUTZuESX7iSnSqTwDpmaMJe178u7AxvOJedKpPAE7ygEuDgPANLeEI5SJhX4BEeUQwatirQuc1nnZ0tOV/KJiIooI42urojKCyz7BinuMCDwUj7HV/veJUPvK8MlyW/AGdQbsvEmJsE/ggwAMem4bIle0IVAAAAAElFTkSuQmCC'}

# Roll dices parse from text

>>> Dice.parse("(dice20)(dice8)")
[{'result': 2, 'url': 'https://s.plurk.com/27866de1cbed77d98cd8a886205c9dcb.png'}, {'result': 6, 'url': 'https://s.plurk.com/17c9123ed084f917ede14447afdfabdf.png'}]
>>> Dice.parse("(dice20)(dice8)", base64=True)
...
```

## Run tests

```bash
python -m unittest
```
3 changes: 3 additions & 0 deletions plurk_dice/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .dice import Dice

__all__ = ["Dice"]
139 changes: 139 additions & 0 deletions plurk_dice/dice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import re
from base64 import b64encode
from random import choice
from typing import Dict, Union, List

import requests

DOMAIN = "https://s.plurk.com/"

DICE_NAME = {
"dice4": 4,
"dice": 6,
"dice8": 8,
"dice10": 10,
"dice12": 12,
"dice20": 20,
"draw": "lots",
"lot": "lots",
"bzzz": "bzz",
}
DICE = {
4: { # YELLOW
1: "8119f8d3c6530e1f96f5ae683641dfc3.png",
2: "ab7b1d70a2989e4411276da40aab8cd4.png",
3: "b68892fd60fbada8fe730ebaa51d93a1.png",
4: "a1fe8924e7dc4a4252b8e2c89fc729a4.png",
},
6: { # DICE
1: "dde5171bb9139e692053f9ac66cbd564.png",
2: "e7e1ede2d97726bf418a3abf9064869c.png",
3: "c3e6d94310590416f656079db8ded155.png",
4: "8329e14c55b7176a1030bf2be1c734dd.png",
5: "ac7f20875fca82628e66933742ff5c36.png",
6: "7c9a7af9caf0bcbadc74ec87400eb66d.png",
},
8: { # PINK
1: "c7909a13f2a1d82f6a8ec88806cedd5f.png",
2: "8e09eec35e1ae9a9c506260f46f5fa80.png",
3: "d7366d5512962df2cf1038627b3387f9.png",
4: "e61038f50e60112b6cd029604a2e0c6a.png",
5: "375320099fed5bcd480a33ceea294eb7.png",
6: "17c9123ed084f917ede14447afdfabdf.png",
7: "7cbe3526bf770399d9bed3e5217cc21f.png",
8: "7b990c34dc8f63d90a06a67b8eaf56aa.png",
},
10: { # BLUE
1: "c7b659f9a40b3aab4467416bac70743f.png",
2: "9693e838340f3214072694beb7f6666b.png",
3: "77b92b78a34a41fbe5482ea5a3ad79b9.png",
4: "469377ad967761959d56e0db98bf0b48.png",
5: "b4c493ae25e627b9af85750de47a98b4.png",
6: "865614d05bcd6c9cd100684c5990c5c8.png",
7: "e2af8a8d37c81179d61d38b2117cf625.png",
8: "d96522745618c7861c15d3ceb56a0176.png",
9: "5c201b2947b36c83a701537ce3b8cbc9.png",
10: "001b8fae9b328c7d36a9e7f69d8fa922.png",
},
12: { # WHITE
1: "fdcbab46fbd767ad21ad4f8392622aa8.png",
2: "16a33566c4e19dd18f1de9bf64c827c7.png",
3: "659cc49e9386c3a804cdbe8b3e79aa9e.png",
4: "83275472ae5574dff8cbff064e91a372.png",
5: "128b9b64556f85d794770fa5523013b0.png",
6: "e9863814d8baa64a10afaa0e740853d8.png",
7: "ada656fad2ff0a7b0dd2a9c02c48bbd9.png",
8: "31c19af18e7083b34a83011fa559d29f.png",
9: "1397312c8b92664cb843663c03405f86.png",
10: "8a18f029b74f50c318c881bf66f68562.png",
11: "07c3b99baf30d0858d515c9fe52bef30.png",
12: "4694141aad1682240b2d3718beeccf67.png",
},
20: { # ORANGE
1: "a517007a048ca131f7f1d778d1af9684.png",
2: "27866de1cbed77d98cd8a886205c9dcb.png",
3: "1a5cc269b657e7a814aca8aba27fa64f.png",
4: "dcdec54c7152053492e01800b81e0e15.png",
5: "bdd7628d6c87271ac349c84a24b5f138.png",
6: "33203dae72cad566e2a140b531f802f2.png",
7: "ff94b39b3f0927042f8479fac0fd92d1.png",
8: "ad61d499e90390b525bcb1315e6f8e5c.png",
9: "d305cc0e3cdf2ec56e6c9baa7c684f4a.png",
10: "8f36fda06d7b86c7f612974b3ea52bdd.png",
11: "d305cc0e3cdf2ec56e6c9baa7c684f4a.png",
12: "3f8817112e4c988c89d8467e424f4feb.png",
13: "73ca275f83000a6ddf34f98ab8759acc.png",
14: "565fefd777bff763bf53bb2cf8f6f0f0.png",
15: "611615a7152225c461050a3569ea1a2d.png",
16: "1a33f859f00b5d98944892ee9333eb7c.png",
17: "eacfe781bac35c429c2d747f3fd1ba94.png",
18: "c7907086d6db450a140f5192baa88f37.png",
19: "cf737a26669f491dd38e534007ea5dc4.png",
20: "22ecc6bed8b99c6a111d5dbc65dc08fd.png",
},
"bzz": {
"R": "129b757f2346a6e5ea782c79f0337ba9.png",
"G": "5a2a63fa773e68797ec69a1303bfa3b9.png",
"B": "e3481a0219283c49455d8af6012980ea.png",
"K": "7bd4d66256fc805da4dd178b9fdbe3ce.png",
},
"lots": {
"大吉": "469ccf8828b6eb697bbc55b35ed84202.png",
"吉": "ed13a8c77dc92dd28996680c13f2fd92.png",
"中吉": "a6f7de6aae635f616b67505261bc5291.png",
"小吉": "3f1a1cc77b0a248ce7352509645efa87.png",
"小凶": "c12ee0dbd02bf5329682919b39ebdeb3.png",
"凶": "a3e3b6789eff8173ac192af1ed3f243a.png",
"大凶": "bfe98f4effe412f4e5c9199cc27f6888.png",
},
}


class Dice:
def __init__(self, side: Union[int, str]):
self.dice = DICE[side]

def __getitem__(self, key: Union[str, int]) -> str:
return DOMAIN + self.dice[key]

def roll(self, base64=False) -> Dict[str, Union[str, int]]:
"""Roll the dice.
Returns:
Tuple[Union[str, int], str]: (key, url) pair
"""
key = choice(list(self.dice.keys()))
dice = {"result": key, "url": self[key]}
if base64:
dice["base64"] = to_base64(dice["url"])
return dice

@classmethod
def parse(cls, text: str, **kwargs) -> List[Dict[str, Union[str, int]]]:
results = re.findall(r"\((.*?)\)", text)
return [cls(DICE_NAME.get(r, r)).roll(**kwargs) for r in results]


def to_base64(url: str) -> str:
res = requests.get(url)
return b64encode(res.content).decode()
15 changes: 15 additions & 0 deletions plurk_dice/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import requests

from dice import DICE, DOMAIN

if __name__ == "__main__":
for k, d in DICE.items():
for k2, v in d.items():
name = f"{k}_{k2}.png"
r = requests.get(DOMAIN + v, stream=True)
if r.status_code == 200:
with open(f"imgs/{name}", "wb") as f:
for chunk in r:
f.write(chunk)
else:
print(name, "failed!")
Empty file added requirements.txt
Empty file.
24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from setuptools import find_packages, setup

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setup(
name="plurk-dice",
version="0.0.1",
author="SheiUn",
author_email="[email protected]",
description="A third-party Python API for Plurk Dice Emojis",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Plurk-Project/plurk-dice",
packages=find_packages(),
install_requires=["requests"],
test_suite="tests",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.7",
)
13 changes: 13 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest import TestCase

from plurk_dice import Dice


class Test(TestCase):
def test_roll(self):
dice = Dice(20).roll()
self.assertTrue(20 >= dice["result"] >= 1)

def test_base64(self):
dice = Dice("bzz").roll(base64=True)
self.assertIn("base64", dice)

0 comments on commit 5e99a36

Please sign in to comment.