-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_wrapper.py
123 lines (103 loc) · 4.28 KB
/
api_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class Telegram:
def __init__(self, token):
self.url = 'https://api.telegram.org/bot{}'.format(token)
def send_message(self, chat_id, text):
url = self.url + '/sendMessage'
payload = {'chat_id': chat_id, 'text': text, 'parse_mode': 'HTML'}
response = requests.post(url, json=payload)
logger.debug('Send message: {}'.format(response.json()))
class Github:
def __init__(self, user, repo, token):
self.user = user
self.repo = repo
self.auth = (user, token)
self.url = 'https://api.github.com/repos/{}/{}/issues'.format(
self.user, self.repo)
def get_issue(self, number):
url = '{}/{}'.format(self.url, number)
response = requests.get(url, auth=self.auth)
logger.debug('Get issue: {}'.format(response))
return response.json(), response.status_code
def comment_issue(self, number, text):
url = '{}/{}/comments'.format(self.url, number)
payload = {'body': text}
response = requests.post(url, json=payload, auth=self.auth)
logger.debug('Comment issue: {}'.format(response))
return response.status_code
def label_issue(self, number, label):
url = '{}/{}/labels'.format(self.url, number)
# Usamos strip porque a Github no le gustan los labels con espacios o
# saltos de lineas al final o al comienzo.
payload = [label.strip()]
response = requests.post(url, json=payload, auth=self.auth)
logger.debug('Label issue: {}'.format(response))
return response.status_code
def close_issue(self, number):
url = '{}/{}'.format(self.url, number)
payload = {'state': 'closed'}
response = requests.patch(url, json=payload, auth=self.auth)
logger.debug('Close issue: {}'.format(response))
return response.status_code
def open_issue(self, number):
url = '{}/{}'.format(self.url, number)
payload = {'state': 'open'}
response = requests.patch(url, json=payload, auth=self.auth)
logger.debug('Open issue: {}'.format(response))
return response.status_code
def get_comments(self, number):
url = '{}/{}/comments'.format(self.url, number)
response = requests.get(url, auth=self.auth)
return response.json()
class CryptoMKT:
@staticmethod
def get_prices(market):
payload = {'market': market}
response = requests.get('https://api.cryptomkt.com/v1/ticker',
params=payload)
response_json = response.json()
if ('status', 'error') in response_json.items():
return None
price_dict = {'bid': float(response_json['data'][0]['bid']),
'ask': float(response_json['data'][0]['ask'])}
return price_dict
class SurBTC:
@staticmethod
def get_prices(market):
fixed_market = market[:3].lower() + '-' + market[3:6].lower()
response = requests.get(
'https://www.surbtc.com/api/v2/markets/{}/ticker.json'
.format(fixed_market))
response_json = response.json()
if 'message' in response_json:
return None
price_dict = {'bid': float(response_json['ticker']['max_bid'][0]),
'ask': float(response_json['ticker']['min_ask'][0])}
return price_dict
class Orionx:
@staticmethod
def get_prices(market):
url = 'http://api.orionx.io/graphql'
query = '''
query getOrderBook($marketCode: ID!) {
orderBook: marketOrderBook(marketCode: $marketCode) {
spread
mid
}
}
'''
request_json = {'query': query,
'operationName': 'getOrderBook',
'variables': {'marketCode': market}}
response = requests.post(url=url, json=request_json)
response_json = response.json()
if 'errors' in response_json:
return None
spread = response_json['data']['orderBook']['spread']
mid = response_json['data']['orderBook']['mid']
price_dict = {'bid': mid - spread / 2,
'ask': mid + spread / 2}
return price_dict