CS Code for Auto Google Image Searching (By URL)

Toyz
by Toyz · 7 posts
12 years ago in .Net (C#, VB, etc)
Posted 12 years ago · Author
The following code will basically let you search google's new API for image searching without ever loading the google page, just note this is still beta and may not compile outside of .net 4.0 sorry D:

Code
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Toyz_Portfoilo.sBin
{
    public class SBIHelper
    {
        private static HttpWebResponse _response;

        public static string SearchByImage(string url)
        {
            var request = WebRequest.Create(String.Format("http://www.google.com/searchbyimage?hl=en&site=search&image_url={0}", url)) as HttpWebRequest;
            var sb = new StringBuilder();
            var buf = new byte[8192];

            if (request != null)
            {
                request.Method = "GET";
                request.ProtocolVersion = HttpVersion.Version11;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0";

                _response = request.GetResponse() as HttpWebResponse;
            }
            if (_response != null)
            {
                var stream = _response.GetResponseStream();

                var count = 0;

                do
                {
                    if (stream != null) count = stream.Read(buf, 0, buf.Length);

                    if (count == 0) continue;
                    var tempString = Encoding.ASCII.GetString(buf, 0, count);

                    sb.Append(tempString);
                } while (count > 0);
            }

            string re1 = "(;)";
            string re2 = "(q)";
            string re3 = "(=)";
            string re4 = "((?:[a-z][a-z]+))";
            string re5 = ".*?";
            string re6 = "(&)";

            var r = new Regex(re1 + re2 + re3 + re4 + re5 + re6, RegexOptions.IgnoreCase | RegexOptions.Singleline);


            var match = r.Match(sb.ToString());
            var imageName = match.Success ? match.ToString().Remove(0, 3).TrimEnd('&').Replace("%20", " ") : null;

            return imageName;
        }
    }
}
Posted 12 years ago · Author
Don Von Free Credits wrote:
I thought google's API was restricted to only allow 100 queries per day, 1,000 if you are logged into an account back in 2004?


Look how mine works, I don't use Google API at all, I just use there official link and send the data like they send it
Posted 12 years ago
When I tried to compile this, I got this error:

Error 1 Program 'c:\Users\User\AppData\Local\Temporary Projects\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe' does not contain a static 'Main' method suitable for an entry point C:\Users\User\AppData\Local\Temporary Projects\ConsoleApplication1\CSC ConsoleApplication1
Posted 12 years ago · Author
Ð▪ℳ wrote:
When I tried to compile this, I got this error:

Error 1 Program 'c:\Users\User\AppData\Local\Temporary Projects\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe' does not contain a static 'Main' method suitable for an entry point C:\Users\User\AppData\Local\Temporary Projects\ConsoleApplication1\CSC ConsoleApplication1

its cause this ismt a standalone file

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT