Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
(ns yetibot.commands.scrape | ||
(:require | ||
[taoensso.timbre :refer [debug info warn error]] | ||
[yetibot.core.hooks :refer [cmd-hook]]) | ||
(:import | ||
(org.jsoup Jsoup) | ||
(org.jsoup.select Elements) | ||
(org.jsoup.nodes Element))) | ||
|
||
(defn get-page [url] | ||
(.get (.userAgent (Jsoup/connect url) "curl/7.51.0"))) | ||
|
||
(defn get-elems [page selector] | ||
(.select page selector)) | ||
|
||
(defn get-attr [element attr] | ||
(condp = attr | ||
"text" (.text element) | ||
"html" (.html element) | ||
(.attr element attr))) | ||
|
||
(defn scrape [url selector attr] | ||
(remove nil? | ||
(for [e (-> url | ||
get-page | ||
(get-elems selector))] | ||
(get-attr e attr)))) | ||
|
||
(defn scrape-cmd | ||
"scrape <url> <selector-path> <attr> # scrape a url and select elements' text, html or attributes via jsoup. <attr> can be: | ||
- text - inner text of the element(s) | ||
- html - html of the element(s) | ||
- anything else - an attribute of the element(s)" | ||
{:yb/cat #{:util}} | ||
[{[_ url selector-and-attr] :match}] | ||
(let [[_ selector attr] (re-find | ||
#"(.*)\s(\w+)$" | ||
selector-and-attr)] | ||
(scrape url selector attr))) | ||
|
||
(cmd-hook #"scrape" | ||
#"(\S+)(.*)" scrape-cmd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(ns yetibot.test.commands.scrape | ||
(:require | ||
[clojure.test :refer :all] | ||
[yetibot.commands.scrape :refer :all])) | ||
|
||
(deftest test-scrape | ||
(testing "Imgflip" | ||
(is (not-empty (scrape "https://imgflip.com" ".base-img[src!='']" "src"))))) |