|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Visual Basic - Google API
Well,
I have seen alot of code of PHP for googles API, but I came across only one Visual Basic code so here is what i fond. THE PROGRAMMING: OK, you have a pretty form that does nothing. Now we need to add some code to make it fly! First, go to the Declarations section at the top of the code view of your new form, and add the following declarations - </FONT> Option Explicit Dim SOAPRequest As CoEnvelope Dim Transport As HTTPTransport ' Google API SOAP Params Dim GoogleSearchMethodName As String Dim GoogleNameSpaceURI As String Dim GoogleSOAPAction As String Dim GoogleEndPoint As String Dim MyKey As String Dim DupFilter As Boolean Dim FamilyFilter As Boolean You'll notice that I'm using variables, rather than hard-coding some of the SOAP-related information. This makes it easier to understand what the code is doing, or to change it later if needed. Next, add the following initialization code to Form_Load. Make sure that you replace the "put your key here" with the actual value of the API key code you got from Google, or else it won't work! - Private Sub Form_Load() ' Init Vars GoogleSearchMethodName = "doGoogleSearch" GoogleNameSpaceURI = "urn:GoogleSearch" GoogleSOAPAction = "urn:GoogleSearchAction" GoogleEndPoint = "http://api.google.com/search/beta2" MyKey = "put your key here" DupFilter = True FamilyFilter = False ' Init Form vars txtQuery = "" txtURL = "" txtMax = 100 lblCurrent = 0 txtResults = "" End Sub Notice the variables DupFilter and FamilyFilter. These control how the search is done. DupFilter eliminates "very similar" duplicate listings in the results, and limits each domain to 2 listings max, this is the normal setting for Google searches unless you change it. FamilyFilter controls the "Family Safe" filtering of search results. You can change these settings if you wish. (On the subject of settings, you will see below that the API restrict, lr, ie, and oe options are presently set to default, you only need these if you wish to limit searches to a specific country or language, or search with an non-standard character set (like Chinese), see the API doc's if you need to do this.) Next, add the Private Function ValidateForm, and add the following code. This function does simple error-checking on the user's input, you can add to this if you wish - Private Function ValidateForm() as Boolean Dim OKFlag as Boolean OKFlag = True If txtMax = "" Then txtMax = 100 If txtMax > 1000 Then MsgBox "Error - API Cannot scan past 1,000 listings!", _ vbExclamation, "Error - # of Listings too large!" OKFlag = False End If If Trim(txtQuery) = "" Then MsgBox "Error - No Search Query Specified!", _ vbExclamation, "No Search Query Specified!" OKFlag = False End If If Trim(txtURL) = "" Then MsgBox "Error - No URL to Search for!", _ vbExclamation, "No URL to Search for!" OKFlag = False End If ValidateForm = OKFlag End Function Finally, add the following code to your command button's cmdDoSearch_click() event. This is the meat and potatoes of the program, and actually performs the scan and generates the results - Private Sub cmdDoSearch_Click() Dim CurrentOffset As Long Dim RequestSize As Long Dim Listings As Variant Dim CurrentURL As String Dim MyCounter As Long Dim strResults As String Dim MatchFound As Boolean MatchFound = False On Error goto SOAPErr If ValidateForm() Then strResults = "" Set Transport = New HTTPTransport CurrentOffset = 0 RequestSize = 10 If (txtMax < 10) And (txtMax > 0) Then RequestSize = txtMax End If lblCurrent.Caption = "0" APILoop: Set SOAPRequest = New CoEnvelope SOAPRequest.SetMethod GoogleSearchMethodName, _ GoogleNameSpaceURI SOAPRequest.Parameters.Create "key", MyKey SOAPRequest.Parameters.Create "q", Trim(txtQuery) SOAPRequest.Parameters.Create "start", CurrentOffset SOAPRequest.Parameters.Create "maxResults", RequestSize SOAPRequest.Parameters.Create "filter", DupFilter SOAPRequest.Parameters.Create "restrict", "" SOAPRequest.Parameters.Create "safeSearch", FamilyFilter SOAPRequest.Parameters.Create "lr", "" SOAPRequest.Parameters.Create "ie", "" SOAPRequest.Parameters.Create "oe", "" Transport.SOAPAction = GoogleSOAPAction Transport.Send GoogleEndPoint, SOAPRequest.Serialize SOAPRequest.Parse Transport Listings = SOAPRequest.Parameters.Item(0).Nodes.ItemByName("resultElements").Value If (CurrentOffset = 0) Then ' First Time thru, get total "hits" strResults = strResults & "Out of " If (True = SOAPRequest.Parameters.Item(0).Nodes.ItemByName("estimateIsExact").Value) Then strResults = strResults & "Exactly " Else strResults = strResults & "Approxiately " End If strResults = strResults & _ SOAPRequest.Parameters.Item(0).Nodes.ItemByName("estimatedTotalResultsCount").Value & _ " hits" & vbCrLf & vbCrLf txtResults = strResults txtResults.Refresh End If For MyCounter = 0 To UBound(Listings) CurrentURL = Listings(MyCounter).Nodes.ItemByName("URL").Value If (InStr(1, CurrentURL, txtURL, vbTextCompare) > 0) Then ' Match found strResults = strResults & "#" & CStr(CurrentOffset + MyCounter + 1) & " " strResults = strResults & Listings(MyCounter).Nodes.ItemByName("title").Value & vbCrLf strResults = strResults & Listings(MyCounter).Nodes.ItemByName("URL").Value & vbCrLf & vbCrLf txtResults = strResults txtResults.Refresh MatchFound = True End If Next lblCurrent.Caption = CStr(CurrentOffset + UBound(Listings) + 1) lblCurrent.Refresh CurrentOffset = CurrentOffset + 10 If ((txtMax - CurrentOffset) < 10) Then RequestSize = txtMax - CurrentOffset End If If (CurrentOffset < txtMax) And (UBound(Listings) >= 8) _ And Not (MatchFound) Then GoTo APILoop End If strResults = strResults & "Done." & vbCrLf txtResults = strResults Set SOAPRequest = Nothing Set Transport = Nothing End If Exit Sub SOAPErr: MsgBox "Err # " & Err.Number & vbCrLf & vbCrLf & _ Err.Description, vbExclamation, _ "Google API SOAP/XML Error!" Exit Sub End Sub That's it! Look over it for any typos. Now be sure to save the form again so you don't loose anything! [font=verdana][size=2][color=#000000]NEXT - HOW TO USE HOW TO USE: LiGhTen Last edited by dazzlindonna : October 17th, 2004 at 07:23 PM. Reason: no fake sigs allowed |
|
#2
|
||||
|
||||
|
To use this, make sure that you are connected to the Internet, then run the program. Enter the desired search phrase into the txtQuery box, the URL to search for in the txtURL box, change the search depth in the txtMax box if desired, and then click the "Do Search!" button.
The URL can be either an exact page (www.mysite.com/mypage.html), or just a domain name (http://www.mydomain.com), or even a partial domain name! (intel) You can either include the "http://" or not, both ways will work. The program will scan until it finds a listing who's URL contains what you typed. The program will do an automatic looping high-speed scan to whatever search depth you specify, up to the maximum API limit of 1,000 listings, via the txtMax box. It is recommended that you leave this set to the default unless you need to do a deep scan. Very deep scans take longer, use resources, and do you really need to know if your page comes up #935 on some obscure search term? More important, it is very easy to use up you daily maximum API search allotment using this program! At the maximum API search depth, doing only 10 maximum-depth searches could use up you quota for the entire day! So be considerate! In order to increase efficiency and reduce search load, this program is also designed to automatically stop any search the instant that results are found. So if you are set to scan 100 results, and your page is found on #10, the program terminates the scanning at that point. During a scan, lblCurrent will display the # of listings scanned so far. After the first block is read, the txtResults will display the total # of hits for that search phrase, and whether Google indicated that # was exact, or just an estimate (approximate). If your page is found, it will display the ranking, the page title, and the URL. If more than one result is found in the same block of 10 results (such as when you have back-to-back listings like #1 & #2), then both results will be displayed. The scan will terminate when you page is found, or the specified scan depth is reached. A more enhanced version of this program is in process, will keep you informed. If your a Visual Basic programmer, I discovered SOAP/XML can be a lot of fun! You can use the tools I have discussed to "get your feet wet", and maybe invent the next great application for the Google API in the process! Good luck! LiGhTen |
|
#3
|
||||
|
||||
well no one posted any comment nor reply so i will post my self excellent source code . |
![]() |
| Viewing: SEO Chat Forums > Search Engine Strategies > Search Engine Optimization > Visual Basic - Google API |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|