Permalink
Browse files

Scraping StackOverflow pages with cheerio

  • Loading branch information...
NickTikhonov committed Aug 18, 2016
1 parent 896d160 commit 039a1e1e976d029f7d6b061b4c0dac3eb4a3b5d2
Showing with 16 additions and 1 deletion.
  1. +15 −1 lib/sourcefetch.js
  2. +1 −0 package.json
View
@@ -2,6 +2,7 @@
import { CompositeDisposable } from 'atom'
import request from 'request'
import cheerio from 'cheerio'
export default {
@@ -21,16 +22,29 @@ export default {
fetch() {
let editor
let self = this
if (editor = atom.workspace.getActiveTextEditor()) {
let selection = editor.getSelectedText()
this.download(selection).then((html) => {
editor.insertText(html)
let answer = self.scrape(html)
if (answer === '') {
atom.notifications.addWarning('No answer found :(')
} else {
editor.insertText(answer)
}
}).catch((error) => {
console.log(error)
atom.notifications.addWarning(error.reason)
})
}
},
scrape(html) {
$ = cheerio.load(html)
return $('div.accepted-answer pre code').text()
},
download(url) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
View
@@ -13,6 +13,7 @@
"atom": ">=1.0.0 <2.0.0"
},
"dependencies": {
"cheerio": "^0.20.0",
"request": "^2.73.0"
}
}

0 comments on commit 039a1e1

Please sign in to comment.