I want to expand URLs from Twitter data and (simultaneously-ish) extract their domains. I tried doing this before in Python using requests, but I guess I screwed up somewhere, because the vast majority of the URLs are still in 'short' form (bit.ly, goo.gl, etc.) I've got the Twitter data stored as JSON. I'm using clj-http.client :as client to resolve the URLs. I've got code that looks like this, so far:
(defn expand-urls [urls] (for [url-str urls]
(and url-str (last (:trace-redirects
(client/get url-str))))))
(def ^:dynamic *domain-pat* (re-pattern #"https?://([\w\.]+)/.*"))
(defn get-domains [urls] (for [url urls] (first (filter #(not= url %1)
(re-find *domain-pat* url)))))
I've got the Twitter data formatted [tweet-id [{tweet-data-map} {user-data-map}]], so (get-in json-data [1 0 "urls"] returns the URLs, (get-in json-data [1 0 "domains"]) returns the domains.
When I try something like (update-in (update-in js-line [1 0 "urls"] expand-urls) [1 0 "domains"] get-domains), domains are (nil). I've independently verified that my regex works, so I suspect that the issue is that the lazy sequence returned by expand-urls isn't evaluating by the time get-domains gets called. Frustratingly, (type (doall (expand-urls some-urls))) returns clojure.lang.LazySeq, as does (type (doall (doall (expand-urls some-urls)))). I've tried doall, I've tried adding vec into expand-urls. Neither seem to work.
Is this really a laziness issue, or am I missing something else?
(.getHost (java.net.URL. address))instead of a regex. - noisesmithrealized?to test whether a lazy seq has been realized. - Marsupdate-indoes not modify its argumentjs-line; it returns a distinct structure that reflects the modifications you wanted. Don't expectupdate-into changejs-line. - Marsupdate-into handle. I would expand that into something more complicated usingletfor example, or play with it at the repl, and figure out the problem that way. That should make it easier to figure out what's going on, and you can put it back in the nestedupdate-inform after that. At the very least, that would allow you to simplify the question to one that focuses on a more specific problem. - Mars