-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathalmoner.py
425 lines (371 loc) · 13.3 KB
/
almoner.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""
Almoner is a program to determine how many bitcoins to donate to each recipient.
This is meant to be used by bitcoin pools or miners to automatically donate to a list of recipients. With this program, people could simply decide how much to donate, they don't have to also look up bitcoin addresses for each recipient.
==Commands==
===Help===
The -h option or the -help option will print the help, which is this document. The example follows:
python almoner.py -h
===Input===
The -input option sets the input file name. The example follows:
python almoner.py -input bitcoinshare.html
An example of a donation information input file is at:
https://github.com/Unthinkingbit/charity/blob/master/bitcoinshare.html
===Output===
The -output option sets the output. If the output ends with stderr, the output will be sent to stderr If the output ends with stdout or is empty, the output will be sent to stdout. If the output does not end with stderr or stdout, the output will be written to a file by that name. The example follows:
python almoner.py -input bitcoinshare.html
An example of an almoner output file is at:
https://github.com/Unthinkingbit/charity/blob/master/almoner.csv
==Install==
For almoner to run, you need Python 2.x, almoner will probably not run with python 3.x. To check if it is on your machine, in a terminal type:
python
If python 2.x is not on your machine, download the latest python 2.x, which is available from:
http://www.python.org/download/
"""
import cStringIO
import math
import os
import shutil
import sys
import time
import urllib
import zipfile
__license__ = 'MIT'
globalOpenSourceStartWords = 'agpl apache bsd creative gnu gpl mit public unlicense'.split()
def getAddressLines(fileName):
'Get the address lines by the file name.'
if fileName == '':
return []
addressLines = []
for contributor in getContributors(fileName):
addressLines.append(contributor.bitcoinAddress)
return addressLines
def getAddressText(fileName):
'Get the address text by the file name.'
return getTextByLines(getAddressLines(fileName))
def getAlmonerText(contributors, hasName):
'Get the almoner text which consists of lines each of which have a coin address followed by a space then the share.'
almonerText = ''
for contributor in contributors:
almonerText += 'Coin,%s' % contributor.bitcoinAddress
if hasName:
almonerText += '-%s' % getLinkText(contributor.name)
almonerText += ',%s\n' % contributor.share
return almonerText
def getBoolean(arguments, defaultValue, name):
'Get the boolean parameter of the given name from the arguments.'
parameter = getParameter(arguments, defaultValue, name).lower()
if parameter.startswith('1') or parameter.startswith('t') or parameter.startswith('y'):
return True
return False
def getColonDividedWords(text):
'Get the words divided around the colon.'
colonIndex = text.find(':')
if colonIndex < 0:
return [text]
return [text[: colonIndex], text[colonIndex + 1 :]]
def getCommaNumberString(number):
'Get the number string with commas.'
commaNumberString = str(number)
commaIndex = len(commaNumberString) - 3
dotIndex = commaNumberString.find('.')
if dotIndex > -1:
commaIndex = dotIndex - 3
while commaIndex > 0:
commaNumberString = commaNumberString[: commaIndex] + ',' + commaNumberString[commaIndex :]
commaIndex -= 3
return commaNumberString
def getContributors(fileName):
'Get the words divided around the colon.'
return getContributorsByText(getFileText(fileName))
def getContributorsByText(text):
'Get the contributors by text.'
contributor = None
contributors = []
for line in getTextLines(text):
words = getColonDividedWords(line)
if len(words) > 1:
firstLowerSpaceless = getWithoutLeadingStar(words[0].lower().replace(' ', ''))
if len(firstLowerSpaceless) > 0:
if firstLowerSpaceless == 'contributor':
contributor = Contributor()
contributors.append(contributor)
if contributor != None:
contributor.parseLine(line)
for contributorIndex in xrange(len(contributors) - 1, -1, -1):
contributor = contributors[contributorIndex]
if contributor.bitcoinAddress == '':
print('Warning, no bitcoin address for: %s' % contributor.name)
del contributors[contributorIndex]
return contributors
def getFileText(fileName, printWarning=True, readMode='r'):
'Get the entire text of a file.'
try:
file = open(fileName, readMode)
fileText = file.read()
file.close()
return fileText
except IOError:
if printWarning:
print('The file ' + fileName + ' does not exist.')
return ''
def getFloat(defaultValue, text):
'Get float if possible, defaultValue if not.'
try:
return float(text)
except:
pass
return defaultValue
def getInternetText(address):
'Get the entire text of an internet page.'
try:
page = urllib.urlopen(address)
text = page.read()
page.close()
return text
except IOError, theError:
print('The address ' + address + ' could not be opened.')
print(theError)
return ''
def getLinkText(text):
'Get the text part of a text without the tag part.'
isLinkText = True
cLinkText = cStringIO.StringIO()
for character in text:
if character == '<':
isLinkText = False
elif character == '>':
isLinkText = True
elif isLinkText:
cLinkText.write(character)
return cLinkText.getvalue().strip()
def getLocationText(address):
'Get the page by the address, be it a file name or hypertext address.'
if address.startswith('http://') or address.startswith('https://'):
return getInternetText(address)
return getFileText(address)
def getOutput(arguments):
'Get the output according to the arguments.'
fileName = getParameter(arguments, '', 'input')
contributors = getContributors(fileName)
setUtilityValues(contributors)
setShares(contributors)
hasName = False
if getParameter(arguments, '', 'name').lower().startswith('t'):
hasName = True
return getAlmonerText(contributors, hasName)
def getParameter(arguments, defaultValue, name):
'Get the parameter of the given name from the arguments.'
name = '-' + name
if name not in arguments:
return defaultValue
nameIndexNext = arguments.index(name) + 1
if nameIndexNext >= len(arguments):
return defaultValue
return arguments[nameIndexNext]
def getSourceText(address):
'Get the devtome source text for the address.'
text = getInternetText(address)
if '<textarea' not in text:
error508AttemptCount = 0
while ('Error 508.' in text or '>500 Internal Server Error<' in text) and error508AttemptCount < 5:
print('')
print('Warning, server error, attempt: %s' % error508AttemptCount)
print(address)
print('')
error508AttemptCount += 1
time.sleep(10)
text = getInternetText(address)
textAreaTagIndex = text.find('<textarea')
if textAreaTagIndex == -1:
print('')
print('Warning, no textarea tag found for:')
print(address)
print('')
return ''
tagEndIndex = text.find('>', textAreaTagIndex)
if tagEndIndex == -1:
print('')
print('Warning, no tag end found for:')
print(address)
print('')
return ''
textAreaEndTagIndex = text.find('</textarea>', tagEndIndex)
if textAreaEndTagIndex == -1:
print('')
print('Warning, no textarea end tag found for:')
print(address)
print('')
return ''
return text[tagEndIndex + 1 : textAreaEndTagIndex].lstrip()
def getStartsWithWords(text, words):
'Determine if the text starts with a word in the list of words.'
for word in words:
if text.startswith(word):
return True
return False
def getSuffixedFileName(fileName, suffix=''):
'Get the file name with the suffix.'
lastDotIndex = fileName.rfind('.')
if suffix == '' or lastDotIndex == -1:
return fileName
return '%s_%s%s' % (fileName[: lastDotIndex], suffix, fileName[lastDotIndex :])
def getTextByLines(lines):
'Get the text by lines, each ended with a newline.'
text = ''
for line in lines:
text += '%s\n' % line
return text
def getTextLines(text):
'Get the all the stripped lines of text of a text.'
originalLines = text.replace('\r', '\n').replace('\n\n', '\n').split('\n')
textLines = []
for originalLine in originalLines:
originalLineStripped = originalLine.strip()
if originalLineStripped != '':
textLines.append(originalLineStripped)
return textLines
def getWithoutLeadingStar(word):
'Get the word without the leading star, if any.'
if len(word) == 0:
return word
if word[0] == '*':
return word[1 :]
return word
def makeDirectory(folderName):
'Delete the existing directory, if it exists, and make an empty directory.'
if os.path.isdir(folderName):
shutil.rmtree(folderName)
os.makedirs(folderName)
def sendOutputTo(outputTo, text):
'Send output to a file or a standard output.'
if outputTo == '':
return False
if outputTo.endswith('stderr'):
sys.stderr.write(text)
sys.stderr.flush()
return False
if outputTo.endswith('stdout'):
sys.stdout.write(text)
sys.stdout.flush()
return False
return writeFileText(outputTo, text)
def setShares(contributors):
'Set each shares to the utility value divided by the total of the utility values.'
totalUtilityValue = 0.0
for contributor in contributors:
totalUtilityValue += contributor.utilityValue
for contributor in contributors:
contributor.share += contributor.utilityValue / totalUtilityValue
def setUtilityValues(contributors):
'Set the utility values of the contributors.'
numberOfUtilityValues = 0
totalUtilityValue = 0.0
for contributor in contributors:
if contributor.utilityValue != None:
numberOfUtilityValues += 1
totalUtilityValue += contributor.utilityValue
average = 1.0
if numberOfUtilityValues > 0:
average = totalUtilityValue / float(numberOfUtilityValues)
for contributor in contributors:
if contributor.utilityValue == None:
contributor.utilityValue = average
def writeFileText(fileName, fileText, writeMode='w+'):
'Write a text to a file.'
try:
file = open(fileName, writeMode)
file.write(fileText)
file.close()
except IOError:
print('The file ' + fileName + ' can not be written to.')
return False
return True
def writeOutput(arguments):
'Write output.'
if len(arguments) < 2 or '-h' in arguments or '-help' in arguments:
print(__doc__)
return
outputTo = getParameter(arguments, 'almoner.csv', 'output')
if sendOutputTo(outputTo, getOutput(arguments)):
print('The almoner file has been written to:\n%s\n' % outputTo)
def writeTitleValue(cString, title, value):
'Write the title and value line, if the value is not empty.'
if value != '':
cString.write('%s: %s\n' % (title, value))
def writeZipFileByFolder(backupFolder):
'Write zip file from a folder and remove that folder.'
zipNameExtension = backupFolder + '.zip'
if zipNameExtension in os.listdir(os.getcwd()):
os.remove(zipNameExtension)
zipArchive = zipfile.ZipFile(zipNameExtension, 'w', compression=zipfile.ZIP_DEFLATED)
backupFileNames = os.listdir(backupFolder)
for backupFileName in backupFileNames:
zipArchive.write(os.path.join(backupFolder, backupFileName), backupFileName)
zipArchive.close()
print('The zip file has been written to:\n%s\n' % zipNameExtension)
if os.path.isdir(backupFolder):
shutil.rmtree(backupFolder)
class Contributor:
'A class to handle a contributor.'
def __init__(self):
'Make empty contributor.'
self.bitcoinAddress = ''
self.description = ''
self.isOpenSource = False
self.name = ''
self.projectHomepage = ''
self.projectLicense = ''
self.projectType = ''
self.share = 0.0
self.utility = ''
self.utilityValue = None
def __repr__(self):
"Get the string representation of this Contributor."
return self.toString()
def addAlmonerLine(self, cString):
'Add bitcoin address and share to the cString.'
cString.write('%s %s\n' % (self.bitcoinAddress, self.share))
def parseLine(self, line):
'Parse line, splitting into two words around a colon, do nothing if there are less than two words.'
words = getColonDividedWords(line)
if len(words) < 2:
return
firstLowerSpaceless = getWithoutLeadingStar(words[0].lower().replace(' ', ''))
if len(firstLowerSpaceless) < 1:
return
secondWord = words[1].strip()
if 'coinaddress' in firstLowerSpaceless:
self.bitcoinAddress = secondWord
elif firstLowerSpaceless == 'contributor':
self.name = secondWord
elif firstLowerSpaceless == 'description':
self.description = secondWord
elif firstLowerSpaceless == 'projecthomepage':
self.projectHomepage = secondWord
elif firstLowerSpaceless == 'projectlicense':
self.projectLicense = secondWord
self.isOpenSource = getStartsWithWords(getLinkText(secondWord).lower().replace(' ', ''), globalOpenSourceStartWords)
elif firstLowerSpaceless == 'projectype':
self.projectType = secondWord
elif firstLowerSpaceless == 'utility':
self.utility = secondWord
self.utilityValue = getFloat(None, secondWord)
def toString(self):
'Get contributor, bitcoin address, share and utility value as string.'
cString = cStringIO.StringIO()
cString.write('Contributor: %s\n' % self.name)
cString.write('Bitcoin Address: %s\n' % self.bitcoinAddress)
writeTitleValue(cString, 'Description', self.description)
writeTitleValue(cString, 'Open Source', self.isOpenSource)
writeTitleValue(cString, 'Project Homepage', self.projectHomepage)
writeTitleValue(cString, 'Project License', self.projectLicense)
writeTitleValue(cString, 'Project Type', self.projectType)
cString.write('Share: %s\n' % str(round(100.0 * self.share, 1)))
writeTitleValue(cString, 'Utility', self.utility)
cString.write('Utility Value: %s\n' % self.utilityValue)
return cString.getvalue()
def main():
'Write output.'
writeOutput(sys.argv)
if __name__ == '__main__':
main()