/
HexBot.py
executable file
·91 lines (73 loc) · 2.53 KB
/
HexBot.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
#!/bin/env python
from sys import argv, exit, platform, stdout
from platform import machine as arch
from os import environ, path, pathsep
import subprocess, getpass, argparse
base = path.dirname(path.realpath(__file__))
parser = None
bots = {k:v for k,v in {
'bing': 'Bing.coffee',
'imgur': 'Imgur.coffee',
'test-coffee': 'Test.coffee',
'test-js': 'Test.js'
}.items() if path.isfile(path.join(base, 'bots', v))}
def updateEnviron():
oper = platform
if oper.startswith('win32'):
oper = 'windows'
elif oper.startswith('darwin'):
oper = 'macosx'
elif oper.startswith('linux'):
oper = 'linux-' + ('amd64' if arch() == 'x86_64' else 'i386')
libs = path.join(base, 'libs')
casper = path.join(libs, 'casperjs', ('bin' if oper is not 'windows' else 'batchbin'))
phantom = path.join(libs, 'phantomjs')
phantomBin = path.join(phantom, 'bin')
if oper.startswith('cygwin'):
oper = 'windows'
phantomBinOS = path.join(phantomBin, oper)
environ['PATH'] += pathsep + pathsep.join([casper, phantomBinOS, phantom, phantomBin])
def getBotFile(botName):
botFile = bots.get(botName.lower())
if botFile != None:
return path.join(base, 'bots', botFile)
parser.print_help()
print('Bot not found: %s' % botName.lower())
exit()
def parseArgs():
global parser
parser = argparse.ArgumentParser(
epilog='Valid bots: %s' % sorted(list(bots.keys()))
)
parser.add_argument('botName', metavar='botName', type=str,
help='Bot name (valid bots below)')
parser.add_argument('botArgs', type=str, nargs='*',
help='Arguments that will be passed on to the bot')
parser.add_argument('-p', '--pw', dest='botPassword', action='store_true',
help='Prompt for password and sent it on to the bot')
parser.add_argument('-t', '--test', dest='botTest', action='store_true',
help='Launch CasperJS in test mode')
return parser.parse_known_args()
def createProcess():
(args, unknown) = parseArgs()
botFile = getBotFile(args.botName)
print('Executing %s' % args.botName.title())
cmdExec = ['casperjs', '--ignore-ssl-errors=true', botFile]
if unknown:
cmdExec.extend(unknown)
if args.botTest:
cmdExec.insert(1, 'test')
cmdExec.extend(args.botArgs)
if args.botPassword:
cmdExec.append('--password=' + getpass.getpass())
updateEnviron()
p = subprocess.call(' '.join(cmdExec), env=environ, shell=True)
if __name__ == '__main__':
try:
if len(bots) is 0:
print('No bots available in: %s' % path.join(base, 'bots'))
else:
createProcess()
except KeyboardInterrupt:
print()
pass