Skip to content

Commit

Permalink
Update provider discovery to fix binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmirabito committed Nov 17, 2019
1 parent ecc6308 commit eb52308
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 81 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Please fork this repository, push to your fork, and open a pull request to contr

### Adding a Provider

Providers must implement a uniquely-named subclass of `BalanceCheckProvider` in the `balance_check.providers` module. If your provider has been successfully registered, it will appear in the usage message as a supported provider.
Providers must implement a uniquely-named subclass of `BalanceCheckProvider` in the `balance_check.providers` module. You must then add it to the `__all__` list in `balance_check.providers.__init__`. If your provider has been successfully registered, it will appear in the usage message as a supported provider.

Your provider must implement `check_balance(self, **kwargs)` which will accept a keyword argument for each column in the input spreadsheet. You may optionally define a [Cerberus schema](http://docs.python-cerberus.org/en/stable/validation-rules.html) on `self.schema` and invoke `self.validate` with any fields you would like to validate. This is recommended to ensure your provider will not send requests with bad card data. A built-in schema generator for prepaid cards is provided in `balance_check.validators.credit_card` and convenience functions for solving CAPTCHAs are provided on `balance_check.captcha_solver`.

Expand Down
8 changes: 6 additions & 2 deletions balance-check.spec
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- mode: python -*-

from PyInstaller.utils.hooks import collect_submodules

block_cipher = None

a = Analysis(['balance_check/cli.py'],
a = Analysis(['balance_check/__main__.py'],
pathex=['balance_check'],
binaries=None,
datas=None,
hiddenimports=None,
hiddenimports=(
collect_submodules('balance_check.providers')
),
hookspath=None,
runtime_hooks=None,
excludes=None,
Expand Down
12 changes: 9 additions & 3 deletions balance_check/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import colorlog
from balance_check import config
from balance_check.utils.logging import configure_logger
from typing import Mapping
from balance_check.version import __version__

logger = colorlog.getLogger()
configure_logger(logger)

from balance_check.provider import BalanceCheckProvider
from balance_check.providers import *

# Instantiate each provider module and populate the available providers
providers: Mapping[str, BalanceCheckProvider] = {
cls.__name__.lower(): cls() for cls in BalanceCheckProvider.__subclasses__()
}
42 changes: 22 additions & 20 deletions balance_check/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,40 @@
from concurrent.futures import ThreadPoolExecutor, as_completed
from argparse import ArgumentParser, RawTextHelpFormatter
from tqdm import tqdm
from balance_check import __version__, logger, config
from balance_check.providers import providers
from balance_check import __version__, logger, config, providers
from balance_check.utils.logging import configure_logger


def main():
configure_logger(logger)

providers_help = "\n".join([" - {}".format(p_name) for p_name in providers.keys()])

parser = ArgumentParser(
formatter_class=RawTextHelpFormatter,
description=f"""Check gift card balances for a variety of providers.
Supported providers:
{providers_help}
Supported providers:
{providers_help}
Requires an Anti-CAPTCHA API key for providers with CAPTCHAs.
Get one here: https://anti-captcha.com
Configure your key by setting the ANTI_CAPTCHA_KEY environment variable.
Requires an Anti-CAPTCHA API key for providers with CAPTCHAs.
Get one here: https://anti-captcha.com
Configure your key by setting the ANTI_CAPTCHA_KEY environment variable.
Your INPUT_CSV should be formatted as follows:
- A header row is required
- Each column should contain a parameter required by
the specified provider
Your INPUT_CSV should be formatted as follows:
- A header row is required
- Each column should contain a parameter required by
the specified provider
Example (for the 'blackhawk' provider):
-------------------------------------------------
| card_number | exp_month | exp_year | cvv |
|------------------|-----------|----------|-----|
| 4111111111111111 | 12 | 24 | 999 |
-------------------------------------------------
Example (for the 'blackhawk' provider):
-------------------------------------------------
| card_number | exp_month | exp_year | cvv |
|------------------|-----------|----------|-----|
| 4111111111111111 | 12 | 24 | 999 |
-------------------------------------------------
If you find this tool useful, consider buying a coffee for the author:
https://stevenmirabito.com/kudos""",
If you find this tool useful, consider buying a coffee for the author:
https://stevenmirabito.com/kudos""",
)

parser.add_argument(
Expand Down Expand Up @@ -95,7 +97,7 @@ def main():
if provider_allows_chunks:
_chunk.append(card_data)
if (
i + 1
i + 1
) % provider.max_simultaneous: # If end of chunk, send to schedule...
# Schedule balance check
future = executor.submit(provider.check_balance, _chunk)
Expand Down
31 changes: 31 additions & 0 deletions balance_check/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from cerberus import Validator
from balance_check import logger


class BalanceCheckProvider:
def __init__(self):
self.schema = None

def validate(self, args):
if self.schema:
validator = Validator(self.schema)
if not validator.validate(args):
msg = "Invalid card data provided:\n"

for field, errors in validator.errors.items():
msg += "- {}:".format(field)

if len(errors) == 1:
msg += " {}\n".format(errors[0])
elif len(errors) > 1:
msg += "\n"
for error in errors:
msg += " - {}\n".format(error)

logger.error(msg)
return False

return True

def check_balance(self, **kwargs):
raise NotImplementedError("Implement in subclass")
57 changes: 12 additions & 45 deletions balance_check/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import os
from pkgutil import iter_modules
from importlib import import_module
from typing import Mapping
from cerberus import Validator
from balance_check import logger


class BalanceCheckProvider:
def __init__(self):
self.schema = None

def validate(self, args):
if self.schema:
validator = Validator(self.schema)
if not validator.validate(args):
msg = "Invalid card data provided:\n"

for field, errors in validator.errors.items():
msg += "- {}:".format(field)

if len(errors) == 1:
msg += " {}\n".format(errors[0])
elif len(errors) > 1:
msg += "\n"
for error in errors:
msg += " - {}\n".format(error)

logger.error(msg)
return False

return True

def check_balance(self, **kwargs):
raise NotImplementedError("Implement in subclass")


# Import all provider modules
for _, name, _ in iter_modules([os.path.dirname(__file__)]):
import_module("." + name, __package__)

# Instantiate each provider module and populate the available providers
providers: Mapping[str, BalanceCheckProvider] = {
cls.__name__.lower(): cls() for cls in BalanceCheckProvider.__subclasses__()
}
__all__ = [
"bestbuy",
"blackhawk",
"gamestop",
"guitarcenter",
"happy",
"homedepot",
"nike",
"onevanilla",
"prepaidgiftbalance",
"spafinder",
]
2 changes: 1 addition & 1 deletion balance_check/providers/bestbuy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import time
from balance_check import logger, config
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.gift_card import Merchant, GiftCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/blackhawk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bs4 import BeautifulSoup
from balance_check import logger, config
from balance_check.utils.captcha import CaptchaSolver
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.credit_card import Issuer, CreditCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/gamestop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from balance_check import logger, config
from balance_check.utils import deep_get
from balance_check.utils.captcha import CaptchaSolver
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.gift_card import Merchant, GiftCardSchema

HEADERS = {
Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/guitarcenter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import lxml.html
from balance_check import logger, config
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.gift_card import Merchant, GiftCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/happy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bs4 import BeautifulSoup
from balance_check import logger, config
from balance_check.utils.captcha import CaptchaSolver
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.credit_card import Issuer, CreditCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/homedepot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from balance_check import logger, config
from balance_check.utils import deep_get
from balance_check.utils.captcha import CaptchaSolver
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.gift_card import Merchant, GiftCardSchema

HEADERS = {
Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/nike.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
from balance_check import logger
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.gift_card import Merchant, GiftCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/onevanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from balance_check import logger
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.credit_card import Issuer, CreditCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/prepaidgiftbalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from balance_check import logger, config
from balance_check.utils.captcha import CaptchaSolver
from balance_check.utils.browser import get_image_b64_by_id
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.credit_card import Issuer, CreditCardSchema


Expand Down
2 changes: 1 addition & 1 deletion balance_check/providers/spafinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bs4 import BeautifulSoup
from balance_check import logger, config
from balance_check.utils.captcha import CaptchaSolver
from balance_check.providers import BalanceCheckProvider
from balance_check.provider import BalanceCheckProvider
from balance_check.validators.credit_card import Issuer, CreditCardSchema


Expand Down

0 comments on commit eb52308

Please sign in to comment.