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).

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

Author comment

Began life as a copy of #1004999

download  show line numbers  debug dex  old transpilations   

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

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: 277 / 345
Version history: 7 change(s)
Referenced in: [show references]