changeset 1:bd2abcd7190a

mostly done
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Sep 2022 19:40:39 -0600
parents 6b17b5030868
children 81c73ce6541c
files .hgignore data/build.luan serve.sh src/index.html.luan src/lib/Db.luan src/lib/Shared.luan src/site.css
diffstat 7 files changed, 279 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Sep 19 21:25:22 2022 -0600
+++ b/.hgignore	Tue Sep 20 19:40:39 2022 -0600
@@ -2,3 +2,5 @@
 
 pages/
 .DS_Store
+err
+lucene/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/build.luan	Tue Sep 20 19:40:39 2022 -0600
@@ -0,0 +1,109 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local type = Luan.type or error()
+local stringify = Luan.stringify or error()
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
+local print = Io.print or error()
+local String = require "luan:String.luan"
+local starts_with = String.starts_with or error()
+local Html = require "luan:Html.luan"
+local html_parse = Html.parse or error()
+local Table = require "luan:Table.luan"
+local copy = Table.copy or error()
+
+function Io.schemes.site(path)
+	return uri( "file:../src"..path )
+end
+
+local Db = require "site:/lib/Db.luan"
+
+Db.delete_all()
+
+
+local function get_user_htmls(html)
+	local rtn = {}
+	local n = #html
+	local i = 1
+	local el
+	while true do
+		while true do
+			if i > n then
+				return rtn
+			end
+			el = html[i]
+			if type(el)=="table" and el.type=="tag" and el.name=="div" and el.attributes.userid~=nil then
+				break
+			end
+			i = i + 1
+		end
+		local start = i
+		local nesting = 0
+		repeat
+			if type(el)=="table" and el.type=="tag" then
+				if el.name=="div" then
+					nesting = nesting + 1
+				elseif el.name=="/div" then
+					nesting = nesting - 1
+				end
+			end
+			i = i + 1
+			el = html[i]
+		until nesting==0
+		rtn[#rtn+1] = copy(html,start,i-1)
+	end
+end
+
+local function process_user_html(html)
+--print(stringify(html[1]))
+	local user_id = html[1].attributes.userid or error()
+	if Db.count("user_id:"..user_id) > 0 then
+		return
+	end
+	local doc = {}
+	doc.user_id = user_id
+	for i, el in ipairs(html) do
+		if not (type(el)=="table" and el.type=="tag") then
+			continue
+		end
+		local name = el.name
+		local attributes = el.attributes
+		local class = attributes.class
+		if name=="span" and class=="organization" then
+			local user_name = html[i+1]
+			if type(user_name)=="table" and user_name.type=="tag" and user_name.name=="/span" then
+				user_name = ""
+			end
+			doc.user_name = user_name
+		elseif name=="a" and class~=nil and starts_with(class,"afftag ") then
+			doc.category = doc.category or {}
+			doc.category[#doc.category+1] = html[i+1]
+		elseif name=="section" and class=="description" then
+			doc.description = html[i+1]
+		elseif name=="a" and class=="aff-website" then
+			doc.websites = doc.websites or {}
+			doc.websites[#doc.websites+1] = attributes.href
+		end
+	end
+	Db.save(doc)
+end
+
+local pages_dir = uri "file:pages"
+
+for _, dir in ipairs(pages_dir.children()) do
+	if starts_with( dir.name(), "." ) then
+		continue
+	end
+	--print(dir.name())
+	for _, file in ipairs(dir.children()) do
+		local text = file.read_text()
+		local html = html_parse(text)
+		local user_htmls = get_user_htmls(html)
+		print(file.name().." "..#user_htmls)
+		for _, user_html in ipairs(user_htmls) do
+			process_user_html(user_html)
+		end
+		--break
+	end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serve.sh	Tue Sep 20 19:40:39 2022 -0600
@@ -0,0 +1,1 @@
+luan luan:http/serve.luan src 2>&1 | tee err
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/index.html.luan	Tue Sep 20 19:40:39 2022 -0600
@@ -0,0 +1,89 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
+local Table = require "luan:Table.luan"
+local concat = Table.concat or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Db = require "site:/lib/Db.luan"
+local Shared = require "site:/lib/Shared.luan"
+local to_list = Shared.to_list or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "index.html"
+
+
+return function()
+	local query = Http.request.parameters.query or ""
+	local results, n = Db.search(query,1,100)
+	--logger.info("#results = "..#results)
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+		<meta name="viewport" content="width=device-width, initial-scale=1">
+		<style>
+			@import "/site.css";
+
+			input {
+				display: block;
+				width: 100%;
+			}
+			td {
+				vertical-align: top;
+				padding-right: 8px;
+			}
+			td:first-child {
+				padding-right: 8px;
+			}
+		</style>
+		<title>Search ShareASale Affiliates</title>
+	</head>
+	<body>
+		<h2>Search ShareASale Affiliates</h2>
+		<form>
+			<input name=query value="<%= query %>" autofocus>
+		</form>
+		<p><a href="https://lucene.apache.org/core/4_9_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package_description">search syntax</a></p>
+		<p><%= #results %> of <%= n %></p>
+		<hr>
+<%
+	for _, doc in ipairs(results) do
+%>
+		<table>
+			<tr>
+				<td>user_id:</td>
+				<td><a href="https://account.shareasale.com/m-recruiting.cfm?mode=affiliate&userid=<%= doc.user_id %>"><%= doc.user_id %></a></td>
+			</tr>
+			<tr>
+				<td>user_name:</td>
+				<td><%= doc.user_name %></td>
+			</tr>
+			<tr>
+				<td>category:</td>
+				<td><%= concat( to_list(doc.category), ", " ) %></td>
+			</tr>
+			<tr>
+				<td>description:</td>
+				<td><%= html_encode(doc.description or "") %></td>
+			</tr>
+			<tr>
+				<td>websites:</td>
+				<td>
+<%		for _, s in ipairs(to_list(doc.websites)) do %>
+					<a href="<%=s%>"><%=s%></a><br>
+<%		end %>
+				</td>
+			</tr>
+		</table>
+		<hr>
+<%
+	end
+%>
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Db.luan	Tue Sep 20 19:40:39 2022 -0600
@@ -0,0 +1,40 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
+local Table = require "luan:Table.luan"
+local concat = Table.concat or error()
+local Lucene = require "luan:lucene/Lucene.luan"
+local Shared = require "site:/lib/Shared.luan"
+local to_list = Shared.to_list or error()
+
+
+local function supplementer(doc)
+	local t = {}
+	t[#t+1] = doc.user_name
+	if doc.category ~= nil then
+		t[#t+1] = concat(to_list(doc.category)," ")
+	end
+	t[#t+1] = doc.description
+	return {
+		free = concat(t," ")
+	}
+end
+
+local dir = uri("site:/lucene")
+
+local options = {
+	supplementer = supplementer
+	default_type = Lucene.type.english
+	default_fields = {"free"}
+}
+
+local Db = Lucene.index(dir,options)
+
+Db.indexed_fields.free = Lucene.type.english
+Db.indexed_fields.user_id = Lucene.type.string
+Db.indexed_fields.user_name = Lucene.type.english
+Db.indexed_fields.category = Lucene.type.english
+Db.indexed_fields.description = Lucene.type.english
+
+return Db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Shared.luan	Tue Sep 20 19:40:39 2022 -0600
@@ -0,0 +1,18 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local type = Luan.type or error()
+
+
+local Shared = {}
+
+function Shared.to_list(input)
+	if input == nil then
+		return {}
+	elseif type(input) == "table" then
+		return input
+	else
+		return {input}
+	end
+end
+
+return Shared
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/site.css	Tue Sep 20 19:40:39 2022 -0600
@@ -0,0 +1,20 @@
+* {
+ 	box-sizing: border-box;
+}
+
+body {
+	font-family: Sans-Serif;
+	margin-left: 3%;
+	margin-right: 3%;
+}
+
+a {
+	text-decoration: none;
+}
+a:hover {
+	text-decoration: underline;
+}
+
+table {
+	border-collapse: collapse;
+}