Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import telnetlib as tn
- import re, urllib.request, urllib.parse
- def smart_truncate(content, length=100, suffix='...'):
- if len(content) <= length:
- return content
- else:
- return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix
- def getwiki(qstr):
- qstr = urllib.parse.quote(qstr,':/')
- url = "http://en.wikipedia.org/w/api.php?action=query&titles={}&prop=extracts&exintro=True&format=json".format(qstr)
- try:
- wintro = urllib.request.urlopen(url).read().decode()
- except:
- return "\"Sorry, I couldn't connect to Wikipedia."
- if re.search(r"\"pages\":\{\"-1\"", wintro) != None:
- return "\"Sorry, Wikipedia doesn't have a page that matches {}.".format(re.sub(r"%","%%",qstr))
- wintro = re.search(r"\"extract\":\"([^\}]+)",wintro).group(1)[:-1]
- wintro = re.sub(r"\\u2013","-",wintro)
- wintro = re.sub(r"<[^>]+>","",wintro)
- wintro = re.sub("\\\\n","",wintro)
- wintro = re.sub("\\\\u.{4}","",wintro)
- wintro = re.sub("\\\\\"","\"",wintro)
- wintro = re.sub("\/[^\/]+\/","",wintro)
- wintro = re.sub("\[[^\]]+\]","",wintro)
- wintrosm = smart_truncate(wintro, 1000)
- return "quote {}".format(wintrosm)
- wikir = re.compile(b"\[to Elbot\]: wiki (.+)")
- t = tn.Telnet(<host>,<port>)
- t.read_until(b"\"news\"")
- t.write(<connection string>)
- while True:
- try:
- g = t.expect([wikir])
- if g[0] == 0:
- t.write(bytes("\"You asked for {}. Hang on a second.\n".format(g[1].group(1).decode()),"UTF-8"))
- o = getwiki(g[1].group(1))
- out = bytes("{}\n".format(o),"UTF-8")
- t.write(out)
- print("Sent text.")
- except KeyboardInterrupt:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement