Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix For Issue #455 #646

Merged
merged 5 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/yetibot/commands/meme.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
"meme preview <term> # preview an example of the first match for <term>"
{:yb/cat #{:fun :img :meme}}
[{[_ term] :match}]
(if-let [matches (model/search-memes term)]
(if-let [matches (model/scrape-all-memes term 3)]
(urlify (-> matches first :url))
(str "Couldn't find any memes for " term)))

(defn search-cmd
"meme search <term> # query available meme generators"
{:yb/cat #{:fun :img :meme}}
[{[_ term] :match}]
(if-let [matches (model/search-memes term)]
(if-let [matches (model/scrape-all-memes term 3)]
(map :name matches)
(str "Couldn't find any memes for " term)))

Expand Down
18 changes: 15 additions & 3 deletions src/yetibot/models/imgflip.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
(re-find p (s/replace (:name meme) #"\s" ""))))

; todo: append results of search-via-scrape to cached (memes)
(defn search-via-scrape [q]
(defn search-via-scrape [q n]
(info "search via scraping" q)
(let [res (client/get "https://imgflip.com/memesearch" {:query-params {:q q}})]
(let [res (client/get "https://imgflip.com/memesearch" {:query-params {:q q :page n}})]
(->> res
:body
(re-seq #"alt\=\"([^\"]+)\"\s+src\=\'.+imgflip\.com\/([\w\d]+).jpg\'")
Expand All @@ -43,6 +43,18 @@
:url (str "http://i.imgflip.com/" id ".jpg")
:id (parse-base-36-int id)})))))

(defn scrape-all-memes
"Fetch Pages of Memes Until Max Number of Pages is Reached"
([q max-pages]
(let [initial-memes (into [] (search-via-scrape q 1))]
(scrape-all-memes q initial-memes 2 max-pages)))
([q merged-memes page-num max-pages]
(let [new-memes (apply merge
merged-memes (search-via-scrape q page-num))]
(if (< page-num max-pages) ; Set Max Number of Pages to Fetch Here
(scrape-all-memes q new-memes (inc page-num) max-pages)
merged-memes))))

(defn search-memes [query]
(let [ms (-> (memes) :data :memes)
p (re-pattern (str "(?i)" query))
Expand All @@ -51,7 +63,7 @@
(if-not (empty? matching-ms)
matching-ms
; fallback to search-via-scrape if cached results don't contain a match
(search-via-scrape query)))))
(scrape-all-memes query 3)))))

(defn generate-meme [id text0 text1]
(get-json
Expand Down