Skip to content

Commit

Permalink
refactor(config): rename to example.goodgle.config.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunam6 committed Apr 12, 2021
1 parent 044f9d4 commit cf98f98
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ For now just run `deno run --allow-net --allow-read --unstable app.ts`
### Contributing

Hi and welcome! Contributing is very welcome! Every type of contribution is welcomed, just remember to follow the [GitHub Community Guidelines](https://docs.github.com/articles/github-community-guidelines).

*PS: if you don't know where to start look at all the "TODO" comments (I recommend Todo Tree for VSCode).*

### License
Expand Down
2 changes: 1 addition & 1 deletion app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Application, Router, helpers, send} from 'https://deno.land/x/oak/mod.ts'
import {DOMParser} from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'
import {existsSync} from 'https://deno.land/std/fs/mod.ts'
import {search} from './src/search.ts'
import {search} from './src/all.ts'

//Default config
let config = {
Expand Down
7 changes: 7 additions & 0 deletions example.goodgle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const config = {
secure: {
certFile: './path/to/certFile',
keyFile: './path/to/keyFile'
},
port: 8000
}
7 changes: 0 additions & 7 deletions goodgle.config.ts

This file was deleted.

File renamed without changes.
113 changes: 113 additions & 0 deletions src/images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {DOMParser} from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'
import {renderFile} from 'https://deno.land/x/mustache_ts/mustache.ts'

export const images = async (params: Record<string, string>) => {
//TODO: change results per page (& handle for start param)
const doc = new DOMParser().parseFromString(
await fetch('https://google.com/search?q=' + params.q + (params.page != undefined ? '&start='+ (parseInt(params.page)-1) + '0' : ''), {
headers: {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41',
'accept-language': 'en-GB,en;q=0.9'
}
}).then(res => res.text()),
'text/html'
)!

interface KnwlPanel {
title: string
subtitle: string
desc: string
infos: Record<string, string>[]
}

let data: {
query: string
firstResult: Record<string, string>
firstResults: Record<string, string>[]
results: Record<string, string>[]
hasKnwlPanel: boolean
knwlPanel: KnwlPanel
relatedSearchs: string[]
} = {
query: params.q,
firstResult: {},
firstResults: [],
results: [
{
title: 'No results',
desc: 'Try searching for something else...'
}
],
hasKnwlPanel: true,
knwlPanel: {
title: '',
subtitle: '',
desc: '',
infos: []
},
relatedSearchs: []
}

doc.querySelectorAll('.LC20lb.DKV0Md').forEach((el, i) => (data.results[i] = {title: el.textContent})) //results title
doc.querySelectorAll('.aCOpRe').forEach((el, i) => (data.results[i].desc = el.textContent)) //results description
let shownLink: string[] = [] //results shown link
doc.querySelectorAll('.iUh30.Zu0yb').forEach(el => shownLink.push(el.textContent))
shownLink = [...new Set(shownLink)]
shownLink.forEach((el, i) => (data.results[i].shownLink = el))
doc.querySelectorAll('.yuRUbf').forEach((el, i) => (data.results[i].link = el.children[0].getAttribute('href')!)) //results link
data.firstResult = data.results.shift()! //first result
doc.querySelectorAll('.l').forEach((el, i) => (data.firstResults[i] = {title: el.textContent})) //first results title
doc.querySelectorAll('.st').forEach((el, i) => (data.firstResults[i].desc = el.textContent)) //first results description
doc.querySelectorAll('.l').forEach((el, i) => (data.firstResults[i].link = el.parentElement!.children[0].getAttribute('href')!)) //first results link
data.firstResults = data.firstResults.slice(0, 4) //Better visibility
if (doc.querySelector('.qrShPb') !== null) {
data.knwlPanel.title = doc.querySelector('.qrShPb')!.textContent //knowledge panel title
data.knwlPanel.subtitle = doc.querySelector('.wwUB2c')!.textContent //knowledge panel subtitle
if (doc.querySelector('.kno-rdesc')! !== null) data.knwlPanel.desc = doc.querySelector('.kno-rdesc')!.children[1].textContent //knowledge panel description
doc.querySelectorAll('.w8qArf').forEach((el, i) => (data.knwlPanel.infos[i] = {title: el.children[0].textContent + ': '})) //knowledge panel infos title
doc.querySelectorAll('.kno-fv').forEach((el, i) => (data.knwlPanel.infos[i].content = el.textContent.replace(' - Disclaimer', '').replace(', MORE', '').replace('%)', '%)\n'))) //knowledge panel infos content
//TODO: remove unwanted elements from knowledge panel infos (eg: q=toyota)
} else data.hasKnwlPanel = false

return await renderFile('./views/index.hbs', data)
}


async function GIS(query: string) {
function fileExt(s: string) {
return ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'].some(imgExt)
function imgExt(ext: string) {
return s.toLowerCase().includes(ext)
}
}
function imgRefs(content: string) {
const refs = []
const re = /\["(http.+?)",(\d+),(\d+)\]/g
let result
while ((result = re.exec(content)) !== null) {
if (result.length > 3 && !result[1].includes('gstatic.com')) {
refs.push({
url: result[1],
width: +result[3],
height: +result[2]
})
}
}
return refs
}

const doc = new DOMParser().parseFromString(
await fetch('http://google.com/search?tbm=isch&q=' + query.replace(' ', '+'), {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}
}).then(res => res.text()),
'text/html'
)!

const scripts = doc.querySelectorAll('script')
const scriptContents: string[] = []
for (let i = 0; i < scripts.length; ++i) if (fileExt(scripts[i].textContent)) scriptContents.push(scripts[i].textContent)

return scriptContents.map(imgRefs)
}

0 comments on commit cf98f98

Please sign in to comment.