-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBRIAN.py
238 lines (228 loc) · 7.13 KB
/
BRIAN.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
# -*- coding: cp1252 -*-
import os
import random
import math
import time
import sys
from findtext import scour
import urllib2
import sys
import platform
ver = "19.11"
#botname = "BRIAN"
#update = "yes"
try:
tempfile = open("settings.pts", "r");
settings = file.read(tempfile);
tempfile.close();
except IOError:
tempfile = open("settings.pts", "w");
tempfile.write("botname = BRIAN\nupdate = yes")
tempfile.close();
tempfile = open("settings.pts", "r");
settings = file.read(tempfile);
tempfile.close();
c = 0
while c < len(settings.split("\n")):
try:
if settings.split("\n")[c].split(" = ")[0] in "botname":
botname = settings.split("\n")[c].split(" = ")[1]
if settings.split("\n")[c].split(" = ")[0] in "autoupdate":
update = settings.split("\n")[c].split(" = ")[1]
if settings.split("\n")[c].split(" = ")[0] in "tts":
tts = settings.split("\n")[c].split(" = ")[1]
if settings.split("\n")[c].split(" = ")[0] in "static":
static = settings.split("\n")[c].split(" = ")[1]
except IndexError:
c = c + 1
c = c + 1
st = ""
try:
botname
st = st + "botname = " + botname + "\n"
except NameError:
botname = "BRIAN"
st = st + "botname = BRIAN\n"
try:
update
st = st + "update = " + update + "\n"
except NameError:
update = "yes"
st = st + "update = yes\n"
try:
tts
st = st + "tts = " + tts + "\n"
except NameError:
tts = "on"
st = st + "tts = on\n"
try:
static
st = st + "static = no\n"
except NameError:
update = "yes"
st = st + "static = yes\n"
#print st
#print botname
#print update
#print tts
tempfile = open("settings.pts", "w");
tempfile.write(st)
tempfile.close();
def autoupdate():
import urllib2
l = urllib2.urlopen("https://raw.githubusercontent.com/notanewbie/BRIAN/master/latest?nocache=" + str(random.randint(0,1000000000000))).read().replace("\n", "")
ldl = "/~https://github.com/notanewbie/BRIAN/archive/" + l + ".zip"
if l in ver:
#print l
#print ver
print botname + " is up to date."
print "Version: " + ver;
else:
print "Updating to " + l + "..."
f = open(sys.argv[0], "w")
f.write(urllib2.urlopen("https://raw.githubusercontent.com/notanewbie/BRIAN/" + l + "/BRIAN.py").read())
file.close(f)
t = sys.argv[0]
print "Update installed."
#print "Restarting..."
raw_input("Please restart inact changes. Press enter to continue.")
sys.exit(0)
#execfile(t)
if "no" in update:
c = c
else:
try:
import urllib2
l = urllib2.urlopen("https://raw.githubusercontent.com/notanewbie/BRIAN/master/latest?nocache=" + str(random.randint(0,1000000000000))).read().replace("\n", "")
autoupdate()
except urllib2.URLError:
print "Unable to reach CDN server to check for updates. Please connect to the internet. If the problem persists, please email notanewbie at admin@notanewbie.com."
print "Version: " + ver;
#This is BRIAN, a Bot Really Intelligent And Nice.
#
#
#This code is still undergoing Beta testing.
#Function to parse harmful characters.
def harshChars(t):
t = t.replace("'", "")
t = t.replace('"', "")
return t
def parseChars(t):
t = t.replace("?", "")
t = t.replace('"', "")
t = t.replace('PH/', "")
t = t.replace('\n', "")
t = t.replace('<', "")
t = t.replace('>', "")
t = t.replace('/', "")
t = t.replace(':', "")
t = t.replace('*', "")
t = t.replace('\\', "")
return t
#Log my intelligence upon startup.
#Find out how many phrases I know.
kno = os.listdir("DB/")
i = 0
for item in kno:
i = i + 1
kno = i * 1.0
i = 0
#Find out how many phrases I understand.
und = os.listdir("PH/")
for item in und:
i = i + 1
und = i * 1.0
i = 0
#Calculate my relative knowledge of phrases known to phrases understood.
try:
Think = und/kno
Think = Think * 100
Think = str(Think) + "%"
#Save the log.
filename = time.strftime("%Y.%m.%d - %H-%M-%S.txt")
Logz = open(filename, "w");
Logz.write("Knows " + str(kno) + " phrases. \n Understands " + str(und) + " phrases. \n Knowledge level: " + Think + ".");
file.close(Logz);
except ZeroDivisionError:
#Save the log.
filename = time.strftime("%Y.%m.%d - %H-%M-%S.txt")
Logz = open(filename, "w");
Logz.write("Is brand new. Knows nothing.");
file.close(Logz);
#Make a new function. This will be the engine.
def Initiate(user):
#Look in the database for a match to "User" so I can respond.
#Do I know how to respond?
USER = parseChars(user)
exists = os.path.isfile("PH/" + USER + ".ph");
#If I do
if exists is True:
#Load the entry
Respo = open("PH/" + USER + ".ph", "r");
respo = file.read(Respo);
Respo.close();
#reply
if "off" in tts:
exists = exists
else:
Speak(respo)
Input = raw_input(botname + ": " + respo + "\nYOU: ");
#If I don't
if exists is False:
#Choose a similar response:
PHDB = os.listdir("PH/");
T = open("PH/" + scour(user, PHDB)[0], "r");
respo = file.read(T);
T.close();
if "off" in tts:
exists = exists
else:
Speak(respo)
Input = raw_input(botname + ": " + respo + "\nYOU: ");
INPUT = parseChars(Input)
RESPO = parseChars(respo)
try:
if "no" in static:
DBup = open("DB/" + INPUT[:200] + ".ph", "w");
DBup.write(Input);
file.close(DBup);
PHup = open("PH/" + RESPO[:200] + ".ph", "w");
PHup.write(Input);
file.close(PHup);
except NameError:
DBup = open("DB/" + INPUT[:200] + ".ph", "w");
DBup.write(Input);
file.close(DBup);
PHup = open("PH/" + RESPO[:200] + ".ph", "w");
PHup.write(Input);
file.close(PHup);
Initiate(Input)
start = raw_input("YOU: ")
#Speak() from pySpeak
def Speak(words):
#print "Speaking"
if "win32" in sys.platform and '10' not in platform.release():
os.system('mshta vbscript:Execute("CreateObject(""SAPI.SpVoice"").Speak(""' + parseChars(words) + '"")(window.close)")')
elif "win64" in sys.platform and '10' in platform.release():
os.system('PowerShell -Command "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(' + "'" + harshChars(parseChars(words)) + "'" + ');"');
elif "win32" in sys.platform and '10' in platform.release():
os.system('PowerShell -Command "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(' + "'" + harshChars(parseChars(words)) + "'" + ');"');
if "darwin" in sys.platform:
os.system("say " + words)
#DEMO CODE:
#
#Speak("I'm eighteen years old but I'm not done yet. I'm not Molly and I'm not Percocet.")
Initiate(start);
'''
____________________
| ________________ |
| |________________| |
| |
| ________________ |
| | | |
| | __________ | |
| | | | | |
| |__| |__| |
|____________________|
notanewbie made this.
'''