1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
import os |
4
|
|
|
|
5
|
|
|
# covered by python-dotenv |
6
|
|
|
# noinspection PyPackageRequirements |
7
|
|
|
import dotenv |
8
|
|
|
import pyotp |
9
|
|
|
import requests |
10
|
|
|
|
11
|
|
|
from kuon.api_response import APIResponse |
12
|
|
|
from kuon.bitskins.api.exceptions import * |
13
|
|
|
from kuon.selenium_helper import SeleniumHelper |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class BitSkins(object): |
17
|
|
|
_selenium_helper = None |
18
|
|
|
|
19
|
|
|
def __init__(self, api_key=None, secret=None): |
20
|
|
|
"""Initializing function |
21
|
|
|
According to the API documentation (https://bitskins.com/api/python#totp_code) they |
22
|
|
|
require OTPs and the API key so we verify the API key and the secret and generate the OTPs with a property |
23
|
|
|
|
24
|
|
|
:type api_key: string |
25
|
|
|
:type secret: string |
26
|
|
|
""" |
27
|
|
|
dotenv_path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, '.env') |
28
|
|
|
dotenv.load_dotenv(dotenv_path) |
29
|
|
|
|
30
|
|
|
self._api_key = api_key |
31
|
|
|
self._secret = secret |
32
|
|
|
|
33
|
|
|
if not api_key: |
34
|
|
|
self._api_key = os.environ.get('BITSKINS_API_KEY') |
35
|
|
|
|
36
|
|
|
if not secret: |
37
|
|
|
self._secret = os.environ.get('BITSKINS_2FA_SECRET') |
38
|
|
|
|
39
|
|
|
if not self._api_key: |
40
|
|
|
raise NoAPIKeyProvidedException('Please provide an API key via .env or as argument') |
41
|
|
|
|
42
|
|
|
if not self._secret: |
43
|
|
|
raise NoSecretProvidedException('Please provide a secret to generate One Time Passwords for the API usage') |
44
|
|
|
|
45
|
|
|
self._pyotp = pyotp.TOTP(self._secret) |
46
|
|
|
self.validate_api_key() |
47
|
|
|
|
48
|
|
|
def validate_api_key(self): |
49
|
|
|
"""Validate the api key and the 2 FA secret |
50
|
|
|
|
51
|
|
|
:return: |
52
|
|
|
""" |
53
|
|
|
api_url = "https://bitskins.com/api/v1/get_account_balance/" |
54
|
|
|
payload = { |
55
|
|
|
'api_key': '313d5b35-74dd-4a6f-b107-c25396d3728f', |
56
|
|
|
'code': self.secret |
57
|
|
|
} |
58
|
|
|
link = requests.get(url=api_url, params=payload) |
59
|
|
|
|
60
|
|
|
response = APIResponse(link.text) |
61
|
|
|
if response.status != "success": |
62
|
|
|
raise InvalidOrWrongApiKeyException('Please provide a valid API key and 2 FA secret') |
63
|
|
|
|
64
|
|
|
@property |
65
|
|
|
def selenium_helper(self): |
66
|
|
|
"""Use property to make this lazy since it takes 3-4 seconds to load |
67
|
|
|
|
68
|
|
|
:return: |
69
|
|
|
""" |
70
|
|
|
if not self._selenium_helper: |
71
|
|
|
self._selenium_helper = SeleniumHelper() |
72
|
|
|
return self._selenium_helper |
73
|
|
|
|
74
|
|
|
@property |
75
|
|
|
def secret(self): |
76
|
|
|
"""Getter for One Time Passwords based on the given secret |
77
|
|
|
|
78
|
|
|
:return: |
79
|
|
|
""" |
80
|
|
|
return self._pyotp.now() |
81
|
|
|
|