This commit is contained in:
2022-03-18 21:25:50 +02:00
commit 2baff4118e
28 changed files with 573 additions and 0 deletions

6
coc/__init__.py Normal file
View File

@@ -0,0 +1,6 @@
import imp
from .user import Player
from .clan import Clan
class ClashOfClans(Player, Clan):
pass

26
coc/base.py Normal file
View File

@@ -0,0 +1,26 @@
import json
import requests
from abc import ABC
class Base(ABC):
def __init__(self, api_token:str, clan_tag:str = None) -> None:
self.headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {api_token}".format(api_token=api_token)
}
self.clan_tag = clan_tag
def get(self, url: str, tag: str):
request = requests.get(
url.format(tag.replace("#", "")),
headers=self.headers
)
return json.loads(request.text)
def post(self, url: str, tag: str, payload: dict):
request = requests.post(
url=url.format(tag.replace("#", "")),
headers=self.headers,
data=json.dumps(payload)
)
return json.loads(request.text)

17
coc/clan.py Normal file
View File

@@ -0,0 +1,17 @@
from .base import Base
class Clan(Base):
def clan_members(self, clan_tag:str = None):
if self.clan_tag is not None:
clan_tag = self.clan_tag
url = "https://api.clashofclans.com/v1/clans/%23{}/members"
return self.get(url, tag=clan_tag)
def get_clan(self, clan_tag:str = None):
if self.clan_tag is not None:
clan_tag = self.clan_tag
url = "https://api.clashofclans.com/v1/clans/%23{}"
return self.get(url, tag=clan_tag)

12
coc/user.py Normal file
View File

@@ -0,0 +1,12 @@
from .base import Base
class Player(Base):
def get_player(self, tag: str):
url = "https://api.clashofclans.com/v1/players/%23{}"
return self.get(url, tag)
def verify_token(self, tag: str, token: str):
payload = {"token":token}
url = "https://api.clashofclans.com/v1/players/%23{}/verifytoken"
return self.post(url, tag, payload)