forked from andraghetti/OutFromViresBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
79 lines (68 loc) · 2.45 KB
/
scraper.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
import logging
import threading
import time
import requests
from bs4 import BeautifulSoup
import notify
class Scraper(threading.Thread):
def __init__(self, name, url, delay=15, amount_threshold=100):
threading.Thread.__init__(self)
self.name = name
self.url = url
self.txs = []
self.killsig = False
self.delay = delay
self.amount_threshold = amount_threshold
# Initial fetch
lines = self.fetch_page_lines()
for line in lines:
tx = self.parse_line(line)
if tx is not None and line not in self.txs:
self.txs.append(line)
def fetch_page_lines(self):
html = requests.get(self.url).text
text = BeautifulSoup(html, 'html.parser').text
return text.split('\n')
def parse_line(self, line):
# TODO better parsing
splitted = line.split()
if line == '' or len(splitted) < 10:
return None
tx_date, tx_time, id, _, _, _, _, type, amount, currency = splitted[:10]
if len(splitted) > 10:
if splitted[10] != 'invoke':
currency = f'{currency} {splitted[10]}'
# TODO convert all the currencies to short name
currency = 'WBTC' if currency == 'WBTC' else currency
currency = 'VIRESBTCLP' if currency == 'VIRES_BTC_LP' else currency
parsed = {
'date': tx_date + ' ' + tx_time,
'id': int(id[1:-1]),
'type': type[:-2],
'amount': float(amount),
'currency': currency,
}
return parsed
def check_transactions(self):
lines = self.fetch_page_lines()
for line in lines:
try:
tx = self.parse_line(line)
if tx is not None and line not in self.txs:
self.txs.append(line)
notify.send(self, tx)
except Exception as error:
logging.critical(f'Could not parse: {line}: {error}')
def run(self):
while not self.killsig:
logging.debug(f'Fetching transactions from: {self.url}')
try:
self.check_transactions()
time.sleep(self.delay)
except Exception:
logging.exception(f'Failed to scrape: {self.url}')
def cancel(self):
logging.debug(f'Shutting: {self.name}')
notify.send(self, tx=None)
self.shut()
self.close()