Skip to content

Instantly share code, notes, and snippets.

@aurorapar
Created January 21, 2025 17:39
Show Gist options
  • Save aurorapar/91739a0b00fdafdd714a2251155410e8 to your computer and use it in GitHub Desktop.
Save aurorapar/91739a0b00fdafdd714a2251155410e8 to your computer and use it in GitHub Desktop.
<?php
$post = file_get_contents('php://input');
if($post == null)
{
echo json_encode(array("ERROR" => "No data was passed to search for."));
}
else
{
$url = 'http://192.168.1.XXXX:XXXXX/search';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => json_encode($post)
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
echo json_encode($result);
}
?>
import http
import json
import os
import socketserver
import time
import traceback
from datetime import datetime, timezone
from enum import Enum, auto
from http.server import BaseHTTPRequestHandler, SimpleHTTPRequestHandler
from threading import Thread
from elasticsearch import Elasticsearch
SERVER_ADDRESS =
CERT_PATH =
DOCS_DIR =
DOCS_UPLOADED_FILE_CACHE =
def main():
post_documents = Thread(target=upload_docs)
post_documents.start()
web_server = Thread(target=handle_request)
web_server.start()
web_server.join()
def get_client():
password = os.environ['elasticsearch_password']
client = Elasticsearch("https://localhost:9200", basic_auth=('elastic', password), ca_certs=CERT_PATH)
return client
def upload_docs():
while True:
loaded_documents = []
if os.path.exists(DOCS_UPLOADED_FILE_CACHE):
with open(DOCS_UPLOADED_FILE_CACHE, 'r') as f:
loaded_documents = json.load(f)
all_documents = []
for current_directory, sub_directories, fileNames in os.walk(DOCS_DIR):
for doc in fileNames:
all_documents.append(os.path.join(current_directory, doc))
for doc in [x for x in all_documents if x not in loaded_documents]:
doc_name = os.path.basename(doc)
path = os.path.dirname(doc)
game_doc = GameDocument(title=doc_name, path=path)
try:
game_doc.post_document(get_client(), 'system', DocumentActions.CREATE.name)
loaded_documents.append(doc)
with open(DOCS_UPLOADED_FILE_CACHE, 'w') as f:
json.dump(loaded_documents, f)
print("Posted " + doc)
except Exception as e:
print(traceback.format_exc())
print("Error occurred while processing " + doc)
input()
exit(0)
time.sleep(1000 * 3 * 60 * 60)
class GameDocument:
def __init__(self, title=None, path=None, document_id: int = None):
if not title and not document_id:
raise RuntimeError("Document name or id not supplied")
if title and not path:
raise RuntimeError("Document path was not supplied")
self.index = "game_document"
self.title = title
self.path = path
self.document_id = document_id
self.doc_type = None
if self.title:
self.doc_type = os.path.splitext(os.path.join(self.path, self.title))[-1]
self.modifications = [
# (user, action, time)
]
def get_document(self, client):
if not self.title:
return client.get(index=self.index, id=self.document_id)
return client.search(index=self.index, query={
"term": {
"title": self.title
}
})
def post_document(self, client, user, action: str):
body = {
'title': self.title,
'path': self.path,
'doc_type': self.doc_type,
'modifications': self.modifications
}
body['modifications'].append((user, action, datetime.now(timezone.utc).ctime()))
print(body)
return client.index(index=self.index, body=body)
class DocumentActions(Enum):
CREATE = auto(),
ACCESS = auto(),
MODIFY = auto()
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
return None
def do_POST(self):
print("Got a response")
if self.path != '/search':
print("path wasn't search")
return None
content_length = int(self.headers['Content-Length'])
post_data_bytes = self.rfile.read(content_length)
post_data_str = post_data_bytes.decode("UTF-8")
post_data_dict = json.loads(post_data_str)
while type(post_data_dict) is str:
post_data_dict = json.loads(post_data_dict)
if len(list(post_data_dict.keys())) != 1 or 'search_term' not in list(post_data_dict.keys()):
self.send_response(500)
self.send_header('Content-Type', 'application/text')
self.end_headers()
self.wfile.write(json.dumps({"ERROR": "invalid request"}).encode("utf8"))
return
search_term = post_data_dict['search_term']
client = get_client()
response = client.search(index="game_document", size=15, query={
"multi_match": {
"query": search_term,
"fields": ["title", "path"]
}
})
data = [x['_source'] for x in response['hits']['hits']]
self.send_response(200)
self.send_header('Content-Type', 'application/text')
self.end_headers()
self.wfile.write(json.dumps(data).encode("utf8"))
def handle_request():
try:
handler_object = MyHttpRequestHandler
my_server = socketserver.TCPServer(("", 9201), handler_object)
my_server.serve_forever()
except KeyboardInterrupt:
my_server.server_close()
input("Shutting down...")
if __name__ == "__main__":
main()
var last_search_time = new Date().getTime();
(function() {
var startingTime = new Date().getTime();
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(script);
// Poll for jQuery to come into existance
var checkReady = function(callback) {
if (window.jQuery) {
callback(jQuery);
}
else {
window.setTimeout(function() { checkReady(callback); }, 20);
}
};
// Start polling...
checkReady(function($) {
$(function() {
return;
var endingTime = new Date().getTime();
var tookTime = endingTime - startingTime;
window.alert("jQuery is loaded, after " + tookTime + " milliseconds!");
});
});
})();
async function perform_search()
{
var now = new Date().getTime();
if(now - last_search_time <= 1500)
{
last_search_time = now;
update_output("Please wait before performing another search.", true);
return;
}
last_search_time = now;
const user_input = {"search_term": null};
user_input['search_term'] = get_search_input();
var data_return = "";
update_output("Performing search...", true);
const response = await fetch("https://spawningpool.net/doc_search.php", {
credentials: "same-origin",
mode: "same-origin",
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(user_input),
});
if (!response.ok) {
update_output("Unable to complete search.", true);
}
else
{
const stream = response.body.pipeThrough(new TextDecoderStream());
for await (const value of stream) {
console.log(value);
data_return += value;
}
var data = JSON.parse(JSON.parse(data_return));
if(data.length > 0)
{
var display = '<ul>';
data.forEach((doc, index) => {
var path = doc['path'];
path = path.replace(/\\/g, "/");
var path_parts = path.split("/");
path = "http://spawningpool.net/rpg_stuff";
path_parts.forEach((part, part_index) => {
if(part_index > 3)
path += "/" + part;
});
var title = doc['title'];
path += "/" + title;
display += '<li><a href="' + path + '">' + title + '</a></li>';
});
display += '</ul>';
$("#search_results").html(display);
}
else
{
$("#search_results").html("<br />&nbsp;&nbsp;&nbsp;There were no results.");
}
}
}
function get_search_input()
{
return $("#search_input").val();
}
function update_output(output, html=false)
{
if(html)
$("#search_results").html("<br />&nbsp;&nbsp;&nbsp;" + output + "<br/>");
else
$("#search_results").text(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment