Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #1022472 - googleImageSearch_new

JavaX fragment (include) [tags: use-pretranspiled]

Uses 616K of libraries. Click here for Pure Java version (2935L/19K).

scope googleImageSearch_new.

lib 1002510 // jsoup

import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

static S #userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
static int #timeout = 10*1000;

static new ThreadLocal<Bool> #nsfw;

static BufferedImage googleImageSearch_new(S q) {
  S html = loadPage_cached(q);
  
  S group = regexpFirstGroup("image/jpeg;base64,([a-zA-Z0-9/+]*)={0,2}", html);
  ret group == null ? null
    : imageFromBytes(base64decode(dropLast(l(group) & 3, group)));
}

sS #loadPage_cached(S q) {
  File f = googleImageSearch_htmlCacheFile(q);
  if (fileSize(f) > 0) ret loadTextFile(f);
  S html = str(loadPage(q));
  saveTextFile(f, html);
  ret html;
}

static Document #loadPage(S q) ctex {
  S googleUrl = "https://www.google.com/search?tbm=isch"
    + (!isTrue(nsfw!) ? "&safe=active" : "")
    + "&q=" + urlencode(q);
  print("Googling " + quote(q));
  ret Jsoup.connect(googleUrl).userAgent(#userAgent).timeout(#timeout).get();
}

end scope

Author comment

Began life as a copy of #1004999

download  show line numbers  debug dex   

Travelled to 5 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pzhvpgtvlbxg, tvejysmllsmz

No comments. add comment

Snippet ID: #1022472
Snippet name: googleImageSearch_new
Eternal ID of this version: #1022472/8
Text MD5: 29b41cad1c5e22d8470a2b84708afd13
Transpilation MD5: a38e94b89f6422a90a63e852dfb96788
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-25 23:57:01
Source code size: 1143 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 95 / 135
Version history: 7 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1022472 & http://1022472.tinybrain.de