web.csv is csv file containing urls and their titles + image urls as presented in html files
search
search example, multiple words "download debian"
image search
(searchimg searches from web.csv, *tmp commands search from web-tmp.csv)
crawl
update web.csv
install
- add script to file: webcsv
source code
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 | function webcsv_crawl() {
mkdir -p ~/tmp
eval 'wget2 -P ~/tmp -H -r --filter-mime-type "text/html,text/plain" --protocol-directories '$(webcsv_argstr "$@")' -l 3'
}
function webcsv_argstr() {
for i in $(seq 1 20); do
val=$(eval 'echo -n '$(echo -n '$'$i'" "'))
if [ ${#val} -lt 2 ]; then
break
fi
if [ "${val:0:2}" == "--" ]; then
echo -n "$val"
else
eval 'echo -n \"'$(echo '$'$i'\"" "')
fi
done
}
function webcsv_parse() {
sd=$PWD
cd ~/tmp
cp web.csv web-tmp.csv
files=$(find . -type f | grep -vP "./web(-tmp|).csv")
i=0
len=$(echo -e "$files" | wc -l)
echo -e "$files" | while read f; do
let i++
echo "processing $i/$len $f"
protocol=$(echo "$f" | awk -F "/" '{ print $2 }')
proto_offset=$(( ${#protocol} + 2 ))
url=$(echo "$protocol://${f:$proto_offset}" | webcsv_encodeuri)
images=$(cat "$f" | webcsv_parseimages)
#title=$(cat "$f" | htmlq title -t | perl -pe "s/\n/ /g" | perl -pe "s/\"/”/g")
title=$(cat "$f" | { input=$(cat); m=$(echo "$input" | grep -o '<title>.*</title>'); echo "${m:7:-8}"; })
date=$(date +%s)
echo '"'"$url"'";"'"$title"'";"'"$images"'";'"$date"'' >> web-tmp.csv
done
cat web-tmp.csv | sort | uniq > web.csv
rm web-tmp.csv
cd $sd
}
function webcsv() {
cat ~/tmp/web.csv
}
function webcsv_encodeuri() {
s=$(cat | urlencode -c '|"')
echo ${s::-3}
}
function webcsv_simg() {
grep -i "$1" | grep -oP "http[^ ]+\.(jpg|jpeg|png|webp|svg)" | sort | uniq
#grep -i "$1" | grep -P "http[^ ]+\.(jpg|jpeg|png|webp|svg)" | sort | uniq | awk -F"|" '!_[$1]++ ' | awk -F"|" '!_[$2]++ '
#| awk -F";" '{ print $1,$3 }'
}
function webcsv_imgui() {
echo -e "$(cat <<EOF
<!DOCTYPE html>
<html>
<body>
<style>
img {
max-width: calc(33vw - 1em);
max-height: 50vh;
}
body > div {
position: relative;
display: inline-block;
}
p {
text-align: center;
position: fixed;
bottom: 0;
width: 100%;
margin: 0;
}
p > a {
display: inline-block;
margin: .5em;
background: #ddd;
padding: .3em;
}
div > a {
position: absolute;
border: 1em solid red;
border-width: .3em 0 0 0;
width: 50%;
height: 100%;
opacity: .1;
}
div > a:hover {
opacity: 1;
background: rgba(0, 0, 0, .1)
}
div > a:nth-child(2) {
right: 0;
}
</style>
<script>
let index = 0
let count = 20
function render_gallery(){
links = [...document.querySelectorAll("body > div")];
links.forEach(link => link.style.display = "inline-block");
[...links.slice(0, index), ...links.slice(index + count, 500)].forEach(link => {
link.style.display = "none"
});
window.scrollTo(0, 0)
}
onload = function(){
let nav = document.createElement("p")
let prev = document.createElement("a")
let next = document.createElement("a")
prev.href = next.href = "#"
prev.innerText = "< prev"
next.innerText = "next >"
prev.onclick = function(e){
e.preventDefault()
index = Math.max(0, index - 10)
render_gallery()
}
next.onclick = function(e){
e.preventDefault()
index = Math.min(links.length - count, index + 10)
render_gallery()
}
nav.append(prev)
nav.append(next)
document.body.append(nav)
render_gallery()
}
</script>
EOF
)" > ~/tmp/searchimg.html
searchres=$(cat)
echo $(echo -e "$searchres" | while read entry; do
parts=$(echo "$entry" | perl -pe "s/\|/\n/")
link=$(echo "$parts" | head -n 1)
img=$(echo "$parts" | tail -n 1)
imgsource=$(webcsv | grep -F "$link" | awk -F";" '{ print $1 }' | head -n 1)
echo '<div><a href="'"$link"'"></a><a class="link" href='"$imgsource"'></a><img src="'$img'"/></div>'
done) >> ~/tmp/searchimg.html
echo '</body></html>' >> ~/tmp/searchimg.html
firefox ~/tmp/searchimg.html
}
function webcsv_parseimages() {
links=$(cat | htmlq "a" | grep img | grep -oP "http[^ \"]+")
c=$(echo "$links" | wc -l)
for i in $(seq 1 2 $c); do
link=$(echo -e "$links" | head -n $i | tail -n 1 | webcsv_encodeuri)
img=$(echo -e "$links" | head -n $(( $i + 1 )) | tail -n 1 | webcsv_encodeuri)
echo -n "$link|$img "
done
}
|