{"id":697,"date":"2012-12-07T19:24:03","date_gmt":"2012-12-07T19:24:03","guid":{"rendered":"https:\/\/trouble.org\/?p=697"},"modified":"2013-01-29T06:36:31","modified_gmt":"2013-01-29T06:36:31","slug":"one-ring-to-bind-them-one-line-to-find-them","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=697","title":{"rendered":"one ring to bind them, one line to find them"},"content":{"rendered":"<p>I guess if you import enough libraries just about anything can be made into a one liner&#8230; if you have imported BeautifulSoup, re, requests, and sys, in python3 you can simply do:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">print(re.sub(r'^.*imgurl=([^&amp;]+)&amp;.*$', r'\\1', str(BeautifulSoup(requests.get(&quot;http:\/\/images.google.com\/search?num=50&amp;hl=en&amp;safe=off&amp;site=&amp;tbm=isch&amp;source=hp&amp;biw=1744&amp;bih=1279&amp;q=%s&amp;oq=&quot; % sys.argv[1]).text).find(href=re.compile(&quot;imgurl&quot;)))))<\/div><\/div>\n<p>To find the first hit on a google image search with argv[1]. Google will probably change their URL images later today and it&#8217;ll stop working, but I wanted this for a random task&#8230;.<\/p>\n<p>E.g. &#8220;foo.py one+ring+to+bind+them&#8221; currently yields the URL for this beauty:<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/trouble.org\/wp-content\/uploads\/2012\/12\/one-ring1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-703\" title=\"one-ring\" src=\"https:\/\/trouble.org\/wp-content\/uploads\/2012\/12\/one-ring1.jpg\" alt=\"\" width=\"600\" height=\"183\" srcset=\"https:\/\/trouble.org\/wp-content\/uploads\/2012\/12\/one-ring1.jpg 600w, https:\/\/trouble.org\/wp-content\/uploads\/2012\/12\/one-ring1-300x91.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Or you can wimp out and do it the ez way.<\/p>\n<div class=\"codecolorer-container python blackboard\" style=\"overflow:auto;white-space:nowrap;height:800px;\"><div class=\"python codecolorer\"><span class=\"co1\">#!\/usr\/local\/bin\/python3<\/span><br \/>\n<br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># search google images for the first match to a word (optionally more than<\/span><br \/>\n<span class=\"co1\"># one, put together by quotes; returns the URL of the first match.<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># Usage: $0 name-to-search-for<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># Google image URLs currently look like (for a search for &quot;monkey+breath&quot;):<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># &lt;a href=&quot;\/imgres?imgurl=http:\/\/amirobyn.com\/blog\/wp-content\/uploads\/2009\/07\/monkeybreath03.jpg&amp;imgrefurl=http:\/\/amirobyn.com\/blog\/%3Fp%3D16&amp;usg=__y39gYotHzJkeYQ2RhxJAkQIbLf4=&amp;h=318&amp;w=620&amp;sz=59&amp;hl=en&amp;start=1&amp;zoom=1&amp;tbnid=bUjKYBdrdgvHSM:&amp;tbnh=70&amp;tbnw=136&amp;ei=kTrCUMzzCs_siQLd0oDwBQ&amp;prev=\/search%3Fq%3Dmonkey%2Bbreath%26hl%3Den%26safe%3Doff%26biw%3D1744%26bih%3D1279%26ie%3DUTF-8%26tbm%3Disch&amp;itbs=1&quot;&gt;&lt;img src=&quot;http:\/\/t2.gstatic.com\/images?q=tbn:ANd9GcS19-iKGCVUWOdUwzdighrxyDpU3HWLpDPiAcmdPHVDIgDG7U2Y5GAVX70L&quot; alt=&quot;&quot; width=&quot;136&quot; height=&quot;70&quot; \/&gt;&lt;\/a&gt;<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># a quick dispatch to beautiful soup and a substitution and the real URL is yours -<\/span><br \/>\n<span class=\"co1\"># in this case, at this time:<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># http:\/\/amirobyn.com\/blog\/wp-content\/uploads\/2009\/07\/monkeybreath03.jpg<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<br \/>\n<span class=\"kw1\">from<\/span> bs4 <span class=\"kw1\">import<\/span> BeautifulSoup<br \/>\n<br \/>\n<span class=\"kw1\">import<\/span> <span class=\"kw3\">re<\/span><br \/>\n<span class=\"kw1\">import<\/span> requests<br \/>\n<span class=\"kw1\">import<\/span> <span class=\"kw3\">sys<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"kw2\">len<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">==<\/span> <span class=\"nu0\">1<\/span> <span class=\"kw1\">or<\/span> <span class=\"kw2\">len<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&gt;<\/span> <span class=\"nu0\">2<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Usage: %s image-name-to-search-(can-use-pluses-between-multi-words)&quot;<\/span> % <span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co1\"># You can do it in one monster line... monster line... monster line....<\/span><br \/>\n<span class=\"co1\"># print(re.sub(r'^.*imgurl=([^&amp;]+)&amp;.*$', r'\\1', str(BeautifulSoup(requests.get(&quot;http:\/\/images.google.com\/search?num=50&amp;hl=en&amp;safe=off&amp;site=&amp;tbm=isch&amp;source=hp&amp;biw=1744&amp;bih=1279&amp;q=%s&amp;oq=&quot; % sys.argv[1]).text).find(href=re.compile(&quot;imgurl&quot;)))))<\/span><br \/>\n<br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># or have a prayer of understanding it the usual way<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\nurl <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;http:\/\/images.google.com\/search?num=50&amp;hl=en&amp;safe=off&amp;site=&amp;tbm=isch&amp;source=hp&amp;biw=1744&amp;bih=1279&amp;q=%s&amp;oq=&quot;<\/span> % <span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Searching for %s&quot;<\/span> % <span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\nr <span class=\"sy0\">=<\/span> requests.<span class=\"me1\">get<\/span><span class=\"br0\">&#40;<\/span>url<span class=\"br0\">&#41;<\/span><br \/>\nsoup <span class=\"sy0\">=<\/span> BeautifulSoup<span class=\"br0\">&#40;<\/span>r.<span class=\"me1\">text<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co1\"># find urls that have the imgurl<\/span><br \/>\n<br \/>\nbig_link <span class=\"sy0\">=<\/span> soup.<span class=\"me1\">find<\/span><span class=\"br0\">&#40;<\/span>href<span class=\"sy0\">=<\/span><span class=\"kw3\">re<\/span>.<span class=\"kw2\">compile<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;imgurl&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\nreal_link <span class=\"sy0\">=<\/span> <span class=\"kw3\">re<\/span>.<span class=\"me1\">sub<\/span><span class=\"br0\">&#40;<\/span>r<span class=\"st0\">'^.*imgurl=([^&amp;]+)&amp;.*$'<\/span><span class=\"sy0\">,<\/span> r<span class=\"st0\">'<span class=\"es0\">\\1<\/span>'<\/span><span class=\"sy0\">,<\/span> <span class=\"kw2\">str<\/span><span class=\"br0\">&#40;<\/span>big_link<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span>real_link<span class=\"br0\">&#41;<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I guess if you import enough libraries just about anything can be made into a one liner&#8230; if you have imported BeautifulSoup, re, requests, and sys, in python3 you can simply do: print(re.sub(r&#8217;^.*imgurl=([^&amp;]+)&amp;.*$&#8217;, r&#8217;\\1&#8242;, str(BeautifulSoup(requests.get(&quot;http:\/\/images.google.com\/search?num=50&amp;hl=en&amp;safe=off&amp;site=&amp;tbm=isch&amp;source=hp&amp;biw=1744&amp;bih=1279&amp;q=%s&amp;oq=&quot; % sys.argv[1]).text).find(href=re.compile(&quot;imgurl&quot;))))) To find the first hit on a google image search with argv[1]. Google will probably change their URL [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,146,172,87],"tags":[182,179,181,180,335],"class_list":["post-697","post","type-post","status-publish","format-standard","hentry","category-code","category-hack","category-python","category-web","tag-dont-touch-that-ring","tag-hackasaurus","tag-lotr","tag-one-liner","tag-python"],"_links":{"self":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=697"}],"version-history":[{"count":8,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions\/722"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}