forked from c00w/bitHopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.py
88 lines (74 loc) · 3.27 KB
/
diff.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
#License#
#bitHopper by Colin Rice is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.
import re, eventlet, logging, ConfigParser
from eventlet.green import threading, socket, urllib2
# Global timeout for sockets in case something leaks
socket.setdefaulttimeout(900)
class Difficulty():
"""
Stores difficulties and automatically updates them
Access difficulties by using this item as a dictionary
"""
def __init__(self, bitHopper):
"""
Sets up coin difficulties and reads in old difficulties
from file.
"""
#Add Coins
self.diff = {}
for attr_coin in bitHopper.altercoins.itervalues():
self.diff[attr_coin['short_name']] = attr_coin['recent_difficulty']
#Store bitHopper for logging
self.bitHopper = bitHopper
#Read in old diffs
cfg = ConfigParser.ConfigParser()
cfg.read(["diffwebs.cfg"])
#Add diff_sites
self.diff_sites = []
for site in cfg.sections():
self.diff_sites.append(dict(cfg.items(site)))
self.lock = threading.RLock()
eventlet.spawn_n(self.update_difficulty)
def __getitem__(self, key):
return self.diff[key]
def updater(self, coin, short_coin):
# Generic method to update the difficulty of a given currency
logging.info('Updating Difficulty of ' + coin)
config_diffcoin = [site for site in self.diff_sites if site['coin'] == short_coin]
#timeout = eventlet.timeout.Timeout(5, Exception(''))
useragent = {'User-Agent': self.bitHopper.config.get('main', 'work_user_agent')}
for site in config_diffcoin:
try:
req = urllib2.Request(site['url'], headers = useragent)
response = urllib2.urlopen(req)
if site['get_method'] == 'direct':
output = response.read()
elif site['get_method'] == 'regexp':
diff_str = response.read()
output = re.search(site['pattern'], diff_str)
output = output.group(1)
elif site['get_method'] == 'json':
pass
self.diff[short_coin] = float(output)
logging.debug('Retrieved Difficulty: ' + str(self[short_coin]))
break
except Exception, e:
logging.debug('Unable to update difficulty for ' + coin + ' from ' + site['url'] + ' : ' + str(e))
finally:
#timeout.cancel()
pass
def update_difficulty(self):
while True:
"Tries to update difficulty from the internet"
with self.lock:
output_diffs = ConfigParser.ConfigParser()
output_diffs.read("whatevercoin.cfg")
for generic_title, attr_coin in self.bitHopper.altercoins.iteritems():
self.updater(attr_coin['long_name'], attr_coin['short_name'])
output_diffs.set(generic_title, 'recent_difficulty', self.diff[attr_coin['short_name']])
output = open("whatevercoin.cfg", 'wb')
output_diffs.write(output)
output.close()
eventlet.sleep(60*10)