Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Iwakura Lain committed May 15, 2020
0 parents commit f2c61db
Show file tree
Hide file tree
Showing 43 changed files with 1,592 additions and 0 deletions.
27 changes: 27 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from smspva_wrapper import Client, Backends, Services, Countries
from smspva_wrapper.types import country_dict, service_dict

SIMSMS_API_KEY = ''
SMSPVA_API_KEY = ''

c = Client(api_key=SMSPVA_API_KEY, backend=Backends.SMSPVA)


def cheapest_price_finder():
prices = []
for country in country_dict:
a = c.get_service_price(Services.TELEGRAM, country)
item = (a.price, a.country)
prices.append(item)

prices.sort()
for item in prices:
print(f"{item[1]}: {item[0]}")


if '__main__' == __name__:
print(c.name)
print(c.balance)
print(c.karma)
print(c.name)
cheapest_price_finder()
3 changes: 3 additions & 0 deletions smspva_wrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from smspva_wrapper.client import Client
from smspva_wrapper.errors import Errors
from smspva_wrapper.types import Countries, Services, Backends, Operators
1 change: 1 addition & 0 deletions smspva_wrapper/caller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .caller import caller as caller
52 changes: 52 additions & 0 deletions smspva_wrapper/caller/caller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import requests
from smspva_wrapper.errors import Errors


def caller(query: str) -> dict:
"""Low-level function for calling API
Args:
query (str): API link to request
Returns:
json_dict (dict): containing dictionary of parsed json from API
Raises:
InvalidAPIKeyError
InsufficientFundsError
TooShortIntervalError
RepeatRequestLaterError
RequestSyntaxError
APIGeneralError
NetworkingError
UnknownAPIError
"""
r = requests.get(query)
print("Called API")

if r.status_code == 200:
try:
json_dict: dict = r.json()
return json_dict
except ValueError:
error = r.content.decode()
if 'API KEY не получен!' == error:
raise Errors.InvalidAPIKeyError(error)
elif 'API KEY не найден!' == error:
raise Errors.InvalidAPIKeyError(error)
elif 'API KEY not received!' == error:
raise Errors.InvalidAPIKeyError(error)
elif 'Недостаточно средств!' == error:
raise Errors.InsufficientFundsError(error)
elif 'Превышено количество попыток!' == error:
raise Errors.TooShortIntervalError(error)
elif 'Произошла неизвестная ошибка.' == error:
raise Errors.RepeatRequestLaterError(error)
elif 'Неверный запрос.' == error:
raise Errors.RequestSyntaxError(error)
elif 'Произошла внутренняя ошибка сервера.' == error:
raise Errors.RepeatRequestLaterError(error)
else:
raise Errors.UnknownAPIError(error)
else:
raise Errors.NetworkingError("status code: ", r.status_code)
142 changes: 142 additions & 0 deletions smspva_wrapper/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from typing import Union

from smspva_wrapper.types import Backends, Services, Countries, Containers
from smspva_wrapper.methods import Methods


# noinspection PyCallByClass
class Client:
def __init__(self, api_key: str, backend: Backends):
self._api_base = f'{backend}apikey={api_key}'
self.name = self.me.name

@property
def api_base(self) -> str:
return str(self._api_base)

@property
def me(self) -> Containers.UserInfo:
return self.get_userinfo()

@property
def balance(self) -> float:
return float(self.me.balance)

@property
def karma(self) -> float:
return float(self.me.karma)

def get_balance(self) -> Containers.Balance:
return Containers.Balance(
Methods.get_balance(
api_base=self.api_base
)
)

def get_userinfo(self) -> Containers.UserInfo:
return Containers.UserInfo(
Methods.get_userinfo(
api_base=self.api_base
)
)

def get_count_new(self, service: Union[Services, str], country: Union[Countries, str]) -> Containers.CountNew:
return Containers.CountNew(
Methods.get_count_new(
api_base=self.api_base,
service=str(service),
country=str(country)
)
)

def get_service_price(self, service: Union[Services, str], country: Union[Countries, str]) -> Containers.ServicePrice:
return Containers.ServicePrice(
Methods.get_service_price(
api_base=self.api_base,
service=str(service),
country=str(country)
)
)

def get_number(self, service: Union[Services, str], country: Union[Countries, str]) -> Containers.Number:
return Containers.Number(
Methods.get_number(
api_base=self.api_base,
service=str(service),
country=str(country)
)
)

def ban(self, service: Union[Services, str], _id: str) -> Containers.Ban:
return Containers.Ban(
Methods.ban(
api_base=self.api_base,
service=str(service),
_id=_id
)
)

def get_sms(self, service: Union[Services, str], country: Union[Countries, str], _id: str) -> Containers.SMS:
return Containers.SMS(
Methods.get_sms(
api_base=self.api_base,
service=str(service),
country=str(country),
_id=_id
)
)

def denial(self, service: Union[Services, str], country: Union[Countries, str], _id: str) -> Containers.Denial:
return Containers.Denial(
Methods.denial(
api_base=self.api_base,
service=str(service),
country=str(country),
_id=_id
)
)

def get_proveka(self, service: Union[Services, str], number: str) -> Containers.Proverka:
return Containers.Proverka(
Methods.get_proverka(
api_base=self.api_base,
service=str(service),
number=number
)
)

def balance_sim(self, service: Union[Services, str], _id: str) -> Containers.BalanceSIM:
return Containers.BalanceSIM(
Methods.balance_sim(
api_base=self.api_base,
service=str(service),
_id=_id
)
)

def redirect(self, service: Union[Services, str], number_redirect: str, _id: str) -> Containers.Redirect:
return Containers.Redirect(
Methods.redirect(
api_base=self.api_base,
service=str(service),
_id=_id,
number_redirect=number_redirect
)
)

def get_2fa(self, secret: str) -> Containers.TwoFA:
return Containers.TwoFA(
Methods.get_2fa(
api_base=self.api_base,
secret=str(secret)
)
)

def get_clearsms(self, service: Union[Services, str], _id: str) -> Containers.ClearSMS:
return Containers.ClearSMS(
Methods.get_clearsms(
api_base=self.api_base,
service=str(service),
_id=_id
)
)
25 changes: 25 additions & 0 deletions smspva_wrapper/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Errors:
from .errors import GeneralError
from .errors import NetworkingError
from .errors import APIError
from .errors import UnknownAPIError
from .errors import MaxRequestsPerMinuteError
from .errors import NegativeKarmaError
from .errors import MaxConcurrentStreamsError
from .errors import InvalidAPIKeyError
from .errors import InsufficientFundsError
from .errors import TooShortIntervalError
from .errors import RepeatRequestLaterError
from .errors import RequestSyntaxError
from .errors import UserInfoError
from .errors import NumberAlreadyTakenError
from .errors import BanError
from .errors import GetSMSError
from .errors import DenialFailedError
from .errors import ClearSMSError
from .errors import BalanceSIMError
from .errors import ProverkaNumberForServiceError
from .errors import ProverkaGSMBusyError
from .errors import ProverkaNumberError
from .errors import RedirectFailError
from .errors import RedirectInvalidNumberFormat
106 changes: 106 additions & 0 deletions smspva_wrapper/errors/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
class GeneralError(Exception):
"""Base class for exceptions in this module."""


class NetworkingError(GeneralError):
"""Networking Error"""


# API errors
class APIError(GeneralError):
"""Base class for API error"""


class UnknownAPIError(APIError):
"""Unknown API error"""


# Return code errors
class MaxRequestsPerMinuteError(APIError):
"""You have exceeded the number of requests per minute"""


class NegativeKarmaError(APIError):
"""You will be banned for 10 minutes, because scored negative karma"""


class MaxConcurrentStreamsError(APIError):
"""You have exceeded the number of concurrent streams. SMS Wait from previous orders"""


class InvalidAPIKeyError(APIError):
"""Invalid API KEY has been entered"""


class InsufficientFundsError(APIError):
"""Insufficient funds"""


class TooShortIntervalError(APIError):
"""Set a longer interval between calls to API server"""


class RepeatRequestLaterError(APIError):
"""Try to repeat your request later."""


class RequestSyntaxError(APIError):
"""Check the request syntax and the list of parameters used (can be found on the page with method description)."""


# Method errors
# get_balance, get_userinfo
class UserInfoError(APIError):
"""Not ID or user balance"""


# get_number
class NumberAlreadyTakenError(APIError):
"""Number is already taken"""


# ban
class BanError(APIError):
"""Ban request has failed"""


# get_sms
class GetSMSError(APIError):
"""No such SMS or invalid request ID or SMS waiting time has expired (no more than 10 minutes)"""


# denial
class DenialFailedError(APIError):
"""Denial request has failed"""


# clearsms
class ClearSMSError(APIError):
"""ClearSMS error"""


# balance_sim
class BalanceSIMError(APIError):
"""SIM card balance info hasn't been received"""


# get_proverka errors
class ProverkaNumberForServiceError(APIError):
"""You didn't order a number for this service"""


class ProverkaGSMBusyError(APIError):
"""GSM module is busy try to repeat your request 5 minutes later"""


class ProverkaNumberError(APIError):
"""There's no such number in the system any longer"""


# redirect errors
class RedirectFailError(APIError):
"""Redirecting failed"""


class RedirectInvalidNumberFormat(APIError):
"""Invalid number format for redirecting"""
14 changes: 14 additions & 0 deletions smspva_wrapper/methods/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Methods:
from .balance_sim import balance_sim
from .ban import ban
from .denial import denial
from .get_2fa import get_2fa
from .get_balance import get_balance
from .get_clearsms import get_clearsms
from .get_count_new import get_count_new
from .get_number import get_number
from .get_proverka import get_proverka
from .get_service_price import get_service_price
from .get_sms import get_sms
from .get_userinfo import get_userinfo
from .redirect import redirect
Loading

0 comments on commit f2c61db

Please sign in to comment.