PKsSK÷Vcrapipy/__init__.py""" Clash Royale wrapper for cr-api.com """ __version__ = "0.16" from .client import Client from .client_async import AsyncClient from .exceptions import APITimeoutError, APIClientResponseError, APIError from .url import APIURL PKrQKȴ[ [ crapipy/client.py""" cr-api client for Clash Royale. """ import json import logging import requests from requests.exceptions import HTTPError from .exceptions import APIError from .models import Clan, TopClans, Player, Constants, Tag, TopPlayers from .url import APIURL logger = logging.getLogger('__name__') logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.ERROR) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) class Client: """ API Client. """ def __init__(self): pass def fetch(self, url): """Fetch URL. :param url: URL :return: Response in JSON """ try: r = requests.get(url) if r.status_code != 200: logger.error( "API Error | HTTP status {status} | url: {url}".format( status=r.status_code, url=url ) ) raise APIError data = r.json() except (HTTPError, ConnectionError, json.JSONDecodeError): raise APIError return data def get_clan(self, clan_tag): """Fetch a single clan.""" url = APIURL.clan.format(clan_tag) data = self.fetch(url) return Clan(data) def get_clans(self, clan_tags): """Fetch multiple clans. :param clan_tags: List of clan tags """ url = APIURL.clan.format(','.join(clan_tags)) data = self.fetch(url) return [Clan(d) for d in data] def get_top_clans(self): """Fetch top clans.""" data = self.fetch(APIURL.top_clans) return TopClans(data) def get_profile(self, tag: str): """Get player profile by tag. :param tag: :return: """ ptag = Tag(tag).tag url = APIURL.profile.format(ptag) data = self.fetch(url) return Player(data) def get_profiles(self, tags): """Fetch multiple players from profile API.""" ptags = [Tag(tag).tag for tag in tags] url = APIURL.profile.format(','.join(ptags)) data = self.fetch(url) return [Player(d) for d in data] def get_constants(self, key=None): """Fetch contants. :param key: Optional field. """ url = APIURL.constants data = self.fetch(url) return Constants(data) def get_top_players(self): """Fetch top players.""" url = APIURL.top_players data = self.fetch(url) return TopPlayers(data) PKRK crapipy/client_async.py""" cr-api async client for Clash Royale. """ import asyncio import json import logging import aiohttp from .exceptions import APIError from .models import Clan, Tag, Player, Constants, TopPlayers from .url import APIURL from .util import make_box logger = logging.getLogger('__name__') logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.ERROR) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) class AsyncClient: """ API AsyncClient. """ def __init__(self): pass async def fetch(self, url): """Fetch URL. :param url: URL :return: Response in JSON """ try: async with aiohttp.ClientSession() as session: async with session.get(url) as resp: data = await resp.json() if resp.status != 200: logger.error( "API Error | HTTP status {status} | {errmsg} | url: {url}".format( status=resp.status, errmsg=data.get('error'), url=url ) ) raise APIError except (asyncio.TimeoutError, aiohttp.ClientResponseError, json.JSONDecodeError): raise APIError return data async def get_clan(self, clan_tag): """Fetch a single clan.""" url = APIURL.clan.format(clan_tag) data = await self.fetch(url) if isinstance(data, list): data = data[0] return Clan(data) async def get_clans(self, clan_tags): """Fetch multiple clans. :param clan_tags: List of clan tags """ url = APIURL.clan.format(','.join(clan_tags)) data = await self.fetch(url) return [Clan(d) for d in data] async def get_top_clans(self): """Fetch top clans.""" data = await self.fetch(APIURL.top_clans) return make_box(data) async def get_profile(self, tag: str) -> Player: """Get player profile by tag. :param tag: :return: """ ptag = Tag(tag).tag url = APIURL.profile.format(ptag) data = await self.fetch(url) return Player(data) async def get_profiles(self, tags): """Fetch multiple players from profile API.""" ptags = [Tag(tag).tag for tag in tags] url = APIURL.profile.format(','.join(ptags)) data = await self.fetch(url) return [Player(d) for d in data] async def get_constants(self, key=None): """Fetch contants. :param key: Optional field. """ url = APIURL.constants data = await self.fetch(url) return Constants(data) async def get_top_players(self): """Fetch top players.""" url = APIURL.top_players data = await self.fetch(url) return TopPlayers(data) PKF]K(۠crapipy/exceptions.py""" Wrapper exceptions """ class APIError(Exception): pass class APITimeoutError(APIError): pass class APIClientResponseError(APIError): pass PKQQKi crapipy/models.py""" Data models """ from box import Box class BaseModel(Box): """ Base model. """ def __init__(self, *args, **kwargs): kwargs.update({ "camel_killer_box": True, }) super().__init__(*args, **kwargs) class Clan(BaseModel): """Clan.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) class TopClans(BaseModel): """Clan.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) class Player(BaseModel): """Player profile.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @property def clan_name(self): return self.clan.name @property def clan_role(self): return self.clan.role class TopPlayers(BaseModel): """Top Players""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) class Constants(BaseModel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def get_chest_by_index(self, index): """Return chest by index.""" order = self.chest_cycle.order return order[index % len(order)] def get_card(self, key=None, card_key=None, name=None): """Return card by any property""" for card in self.cards: if card.key == key: return card if card.card_key == card_key: return card if card.name == name: return card return None class Tag: """SuperCell tags.""" TAG_CHARACTERS = "0289PYLQGRJCUV" def __init__(self, tag: str): """Init. Remove # if found. Convert to uppercase. Convert Os to 0s if found. """ if tag.startswith('#'): tag = tag[1:] tag = tag.replace('O', '0') tag = tag.upper() self._tag = tag def __str__(self): return self._tag def __repr__(self): return self._tag @property def tag(self): """Return tag as str.""" return self._tag @property def valid(self): """Return true if tag is valid.""" for c in self.tag: if c not in self.TAG_CHARACTERS: return False return True @property def invalid_chars(self): """Return list of invalid characters.""" invalids = [] for c in self.tag: if c not in self.TAG_CHARACTERS: invalids.append(c) return invalids PK%QK`  crapipy/url.pyclass APIURL: """ API URL """ clan = 'http://api.cr-api.com/clan/{}' top_clans = 'http://api.cr-api.com/top/clans' profile = 'http://api.cr-api.com/profile/{}' constants = 'http://api.cr-api.com/constants' top_players = 'http://api.cr-api.com/top/players' PKL]K#Wcrapipy/util.py""" Utility functions """ from box import Box, BoxList def make_box(data): """Create Box instance from dict.""" return Box(data, camel_killer_box=True) def make_box_list(data): """Create BoxList instance from dict.""" return BoxList(data, camel_killer_box=True) PKаIK` ::"crapipy-0.16.dist-info/LICENSE.txtMIT License Copyright (c) 2017 SML Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H}0RRcrapipy-0.16.dist-info/WHEEL1 0 RZtMDtPI{w<wUnbaKM*A1ѭ g\Ic c~PK!H}&crapipy-0.16.dist-info/METADATAuQn0S@SMdljmVcK79K`;y{P0,;eGsYRJ9h -cnN=AvވkHI(*F!6iLh&' V=ѿ2͸ڏycI@}h~a7hnL8(]Ήp~@Tjy}n:cA޳CG.ks+7%s ND0w~w`x?)R/J6PpttZ76w,%p^:iۣ/OrĤ&w ›Z?C Tw5#hƪ&IW(o2V3LG-apq|N=W |a,i/=<+CyU2Z ¬@$֤)`DG+!̖LclmI0n,؊jBO9Iha ܓ6.Xs%VwvCn]<>-r#=mP;#hJd~O:_K%g:8`:*Ntɯ,?HOzg9V: ѐQH nCs}H1-"%fw_PKsSK÷Vcrapipy/__init__.pyPKrQKȴ[ [ crapipy/client.pyPKRK  crapipy/client_async.pyPKF]K(۠crapipy/exceptions.pyPKQQKi crapipy/models.pyPK%QK`  "crapipy/url.pyPKL]K#W$crapipy/util.pyPKаIK` ::"[%crapipy-0.16.dist-info/LICENSE.txtPK!H}0RR)crapipy-0.16.dist-info/WHEELPK!H}&a*crapipy-0.16.dist-info/METADATAPK!HFY.?,crapipy-0.16.dist-info/RECORDPK .