Skip to content

Instantly share code, notes, and snippets.

@lmzach09
lmzach09 / style.css
Created September 1, 2019 17:11
css style for chatbot html page
html, body {
margin: 0px;
width: 100vw;
height: 100vh;
font-family: arial;
overflow: hidden;
}
.chatbotImage {
margin-top: 40px;
@lmzach09
lmzach09 / index.html
Created September 1, 2019 17:09
web page for a chatbot
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="./style.css">
<title>Google Based ChatBot</title>
</head>
<body>
<img class="chatbotImage" src="./chatbot.png" />
<span id="chatbotOutput" class="chatbotOutput">Talk to me.</span>
@lmzach09
lmzach09 / server.py
Created September 1, 2019 05:22
HTTP server for a ChatBot API that queries Google for its ChatBot answers
import http.server
import socketserver
from google_search import chatbot_query
PORT = 8080
DIRECTORY = 'public'
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
@lmzach09
lmzach09 / server.py
Created September 1, 2019 05:18
snippet from web server in python 3
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def do_POST(self):
self.send_response(200)
content_length = int(self.headers['Content-Length'])
post_body = self.rfile.read(content_length)
self.end_headers()
print('user query', post_body)
@lmzach09
lmzach09 / server.py
Created September 1, 2019 05:15
beginning of a web server in python 3
import http.server
import socketserver
from google_search import chatbot_query
PORT = 8080
DIRECTORY = 'public'
@lmzach09
lmzach09 / google_search.py
Created September 1, 2019 05:08
A method for doing a ChatBot query to Google Search. Returns the first sentence in the first <p> of the first URL of the search result.
import requests
import string
from lxml import html
from googlesearch import search
from bs4 import BeautifulSoup
# to search
# print(chatbot_query('how old is samuel l jackson'))
def chatbot_query(query, index=0):
@lmzach09
lmzach09 / get_url.py
Created September 1, 2019 04:59
Do a Google search and get the page in the first result with Python 3
import requests
from lxml import html
from googlesearch import search
from bs4 import BeautifulSoup
query = 'how old is samuel l jackson'
## Google Search query results as a Python List of URLs
search_result_list = list(search(query, tld="co.in", num=10, stop=3, pause=1))
@lmzach09
lmzach09 / google_search.py
Created September 1, 2019 04:53
Do a Google Search in Python
from googlesearch import search
query = 'how old is samuel l jackson'
## Google Search query results as a Python List of URLs
search_result_list = list(search(query, tld="co.in", num=10, stop=3, pause=1))
@lmzach09
lmzach09 / for-each-with-promise.js
Last active September 2, 2019 17:42
Implementation of a for-each loop in JavaScript that works with callbacks. The next iteration in the loop does not start until the previous iteration's callback gets called.
// request method implementation is in a GitHub Gist linked below
const array = [1, 2, 3, 4, 5];
array.forEachWithCallback((el, i, next) => {
request({
method: 'GET',
hostname: 'httpbin.org',
path: '/get?myArg=' + el
}).then((res) => {
const responseBody = JSON.parse(res.body);
@lmzach09
lmzach09 / 1-for-each-with-promise.js
Last active September 28, 2022 16:29
Implementation of a for-each loop in JavaScript that works with callbacks. The next iteration in the loop does not start until the previous iteration's callback gets called.
const array = [1, 2, 3, 4, 5];
array.forEachWithCallback((el, i, next) => {
request({
method: 'GET',
hostname: 'httpbin.org',
path: '/get?myArg=' + el
}).then((res) => {
const responseBody = JSON.parse(res.body);
console.log(responseBody.args.myArg);