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

32
LINES

< > BotCompany Repo | #1027024 // googleImageSearch_new2 (ditching jsoup)

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

Libraryless. Click here for Pure Java version (3925L/27K).

1  
scope googleImageSearch_new2.
2  
3  
static S #userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
4  
static int #timeout = 10*1000;
5  
6  
static new ThreadLocal<Bool> #nsfw;
7  
8  
static BufferedImage googleImageSearch_new2(S q) {
9  
  S html = loadPage_cached(q);
10  
  
11  
  S group = regexpFirstGroup("image/jpeg;base64,([a-zA-Z0-9/+]*)={0,2}", html);
12  
  ret group == null ? null
13  
    : imageFromBytes(base64decode(dropLast(l(group) & 3, group)));
14  
}
15  
16  
sS #loadPage_cached(S q) {
17  
  File f = googleImageSearch_htmlCacheFile(q);
18  
  if (fileSize(f) > 0) ret loadTextFile(f);
19  
  S html = loadPage(q);
20  
  saveTextFile(f, html);
21  
  ret html;
22  
}
23  
24  
sS #loadPage(S q) ctex {
25  
  S googleUrl = "https://www.google.com/search?tbm=isch"
26  
    + (!isTrue(nsfw!) ? "&safe=active" : "")
27  
    + "&q=" + urlencode(q);
28  
  print("Googling " + quote(q));
29  
  ret loadPageWithUserAgentAndTimeout(googleUrl, userAgent, timeout);
30  
}
31  
32  
end scope

Author comment

Began life as a copy of #1022472

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027024
Snippet name: googleImageSearch_new2 (ditching jsoup)
Eternal ID of this version: #1027024/6
Text MD5: f2e565215f9e6b166820c271afdfd718
Transpilation MD5: 6db0ca96b6f8dfb6446a27a81b77b6aa
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-10 19:07:41
Source code size: 943 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 181 / 274
Version history: 5 change(s)
Referenced in: [show references]