Skip to content

Commit

Permalink
fix the order of results
Browse files Browse the repository at this point in the history
  • Loading branch information
xyb committed Nov 16, 2020
1 parent e60da13 commit cc37c64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
path: ~/.nimble
key: ${{ runner.os }}-nimble-stable
- uses: jiro4989/setup-nim-action@v1.0.2
- uses: jiro4989/setup-nim-action@v1
with:
nim-version: 'stable'
- name: Build and test
Expand Down
20 changes: 13 additions & 7 deletions src/googlesearch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,26 @@ iterator search*(query: string, maxResults = 10): SearchResult =
let html = queryHtml(query, total)
let xml = parseHtml(newStringStream(html))

let links = xml.querySelectorAll("div.g")
if len(links) == 0:
let items = xml.querySelectorAll("div.g")
if len(items) == 0:
break
let snippets = xml.querySelectorAll("div>div>span>span")

for (link, snip) in zip(links, snippets):
for item in items:
var sr: SearchResult
for a in link.querySelectorAll("a"):
for a in item.querySelectorAll("a"):
sr.url = a.attr("href")
break
for h3 in link.querySelectorAll("h3"):
if sr.url.len < 10:
continue

for h3 in item.querySelectorAll("h3"):
sr.title = h3.innerText()
break
sr.snippet = snip.innerText()

let snippets = item.querySelectorAll("div>div>span>span")
sr.snippet = ""
for snip in snippets:
sr.snippet &= $snip.innerText()

yield sr

Expand Down

0 comments on commit cc37c64

Please sign in to comment.