Thread: Google and c

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    38

    Google and c

    I want to write a program that takes the text inputted by the user and then inputs it into google translate. The user chooses a language in the program and it is updated in the translator. It then takes the result back into the program. How would I go about doing something like this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By right, you should use the API, but I see that it is now a paid service.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    I was thinking about formatting the users input to that of the link of the page they want. for example Translating is fun from english to my favourite Norwwegian is this link Google Translate not sure if that would work, oh how to scan info back to the program.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Use a tool like Wireshark to see what google translate is doing when you send it a word.

    Re-create the HTTP(s) request in your C program, send it to google, read and parse the response, and you're done.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    The reading and parsing the response is my problem. I can't seem to get that to work.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    In html it can be found here <span id="result_box" class="short_text" lang="no"><span class="hps">Oversettelse</span> <span class="hps">er</span> <span class="hps">gøy</span></span> which then goes to <span class="hps">Oversettelse</span>
    <span class="hps">er</span>
    <span class="hps">gøy</span>
    <span id="result_box" class="short_text" lang="no"><span class="hps">Oversettelse</span> <span class="hps">er</span> <span class="hps">gøy</span></span>

    My idea was to scan in that code and then remove all extras, but not sure how to program to look through the source code of web sites

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A search brings me to: Translating with Google Translate without API and C# Code.

    Of course, now that the API is a paid service, they may have taken steps to prevent this kind of bypassing of their API. Plus, it might be against the terms of use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Thanks, I went there and I am not quite sure that will work. I will try it out, what I really want to do is have a copy of the inspected element to search through, so I can reformat it into my needs. How do I have a copy of the inspected web page?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered just using the API? Payment may be a pain, but it is a small pain ($20 for a million characters) if your usage is small.

    Quote Originally Posted by Angel45
    what I really want to do is have a copy of the inspected element to search through, so I can reformat it into my needs. How do I have a copy of the inspected web page?
    What do you mean by "inspected element" and "inspected web page"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    @ll: Not sure why you're plugging the API so much.

    Because of how the page works, you send it a request, and it sends a response back with the result. This cannot be a difficult thing to re-create and parse, even if you have to use OpenSSL to handle the connection. And there's no reason to forfeit good coding practice and 20 bucks, just to have Google do it for you.

    @OP: You'll have to be more specific as to what you've already done, a HTML snippet isn't helpful.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    I have not considered using API mostly because I want to learn how to do it this way if possible, so I can use it on other sites in a similar manner. If you are using Google Chrome it allows you to right click and inspect element. This then allows you to see everything about the site you could possibly want. I want to copy what is store in result_box to my program, reformat it and output it to the user. Only step that I don't know is how to copy or get access to the code for the programming. Thanks again.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by memcpy
    Not sure why you're plugging the API so much.
    Because that is the right thing to do. You use something that is documented and specifically intended for programmatic use, so you can get running faster and don't have to worry about what happens if Google changes something that the developers do not regard as being problematic to programmatic use of the service.

    Quote Originally Posted by memcpy
    Because of how the page works, you send it a request, and it sends a response back with the result. This cannot be a difficult thing to re-create and parse, even if you have to use OpenSSL to handle the connection.
    Not quite: if you read the article I linked to, "Google at least changed their input pages to use AJAX callbacks". Thus, it is the page itself that sends a request to the service and sends a response back with the result. You'll need to do the same thing and parse the AJAX response rather than parse the page itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    I understand your point laserlight, Google does change quite often. I still wish to know how to do the process of searching through the inspected element for other purposes. I know that it may only work temporarily but the way they currently have the results displayed are simplified already. And I fully plan on upgrading my program to account for their changes. But you are correct in your saying.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angel45
    I want to copy what is store in result_box to my program
    If the information I have is correct, then that field will simply be empty to you when you make the call to get the page. The idea in that article I linked to is to use the internal API (as used by the AJAX call) to get the (JSON) response with the result. You then parse the response, which can be done through various C libraries for parsing JSON, and display what you want from the result.

    The use of the internal API is apparently just a matter of accessing the page via the get method with the appropriate variables in the URL query string, while setting a known user agent string as a real browser would likely do.

    You should be able to use Chrome's developer tools to inspect the AJAX calls made, and thus determine what is the exact URL to access and what the JSON response really looks like.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Ok I will try that. what is stored in the result_box actually is the result translated split by words per line. It is a little hard to find through reading it but you can find it. I really do not wish to pay for API, might try and find a loop hole. Still trying to find out how to search through an inspected element though. That is my main problem, I believe it has to be possible, not sure how though. Whenever we find out though it will be really beneficial.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The end of Google
    By Mario F. in forum General Discussions
    Replies: 34
    Last Post: 09-22-2010, 12:06 AM
  2. Reading Google Sketchup (.skp) or Google Earth (.kmz)
    By Zeeshan in forum Game Programming
    Replies: 9
    Last Post: 03-07-2008, 05:25 PM
  3. google down
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-28-2004, 03:11 PM

Tags for this Thread