1

Can I create a Google alert ( http://www.google.com/alerts ) programmatically using C# and consume the feed to show in an asp.net application? I know that Google does not provide an api to do that. I need your suggestions / ideas.

Thanks in advance,

xkcd
  • 2,538
  • 11
  • 59
  • 96
  • 1
    possible duplicate of [Google Alerts API?](http://stackoverflow.com/questions/860442/google-alerts-api) – jgauffin Mar 21 '11 at 14:46
  • 1
    That question does not ask about creating alerts, only parsing them. I don't see it as an exact dup. – Matt Ball Mar 21 '11 at 14:47
  • This may be related to this [question](http://stackoverflow.com/questions/860442/google-alerts-api) – Richard Mar 21 '11 at 14:45
  • Related to http://stackoverflow.com/questions/13528747/how-to-create-new-google-alert-delivering-it-to-feed-using-php-curl – Jeremy Warne Feb 18 '13 at 22:50

2 Answers2

4

Sorry, this isn't C#, but should be a help to anyone who wants to implement something like this. This PHP code creates the feed and returns the RSS URL for consumption.

Based on code from this page:

Login to Google with PHP and Curl, Cookie turned off?

function createAlert($search) {

        $USERNAME = 'EMAIL_ADDRESS';
        $PASSWORD = 'PASSWORD';
        $COOKIEFILE = 'cookies.txt';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);

        curl_setopt($ch, CURLOPT_URL,
        'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
        $data = curl_exec($ch);

        $formFields = $this->getFormFields($data);

        $formFields['Email']  = $USERNAME;
        $formFields['Passwd'] = $PASSWORD;
        unset($formFields['PersistentCookie']);

        $post_string = '';
        foreach($formFields as $key => $value) {
        $post_string .= $key . '=' . urlencode($value) . '&';
        }

        $post_string = substr($post_string, 0, -1);

        curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

        $result = curl_exec($ch);

        if (strpos($result, '<title>Redirecting') === false) {
            var_dump($result);
            die("Login failed");
        } else {
            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts');
            curl_setopt($ch, CURLOPT_POST, 0);
            curl_setopt($ch, CURLOPT_POSTFIELDS, null);

            $result = curl_exec($ch);

            // Create alert

            preg_match('/<input type="hidden" name="x" value="([^"]+)"/', $result, $matches);

            $post = array(
                "x" => $matches[1],     // anti-XSRF key
                "q" => $search,     // Search term  
                "t" => 7,       // Result type (everything)
                "f" => 1,       // Frequency (once a day)
                "l" => 1,       // How many (all results)
                "e" => "feed"       // Type of delivery (RSS)
            );

            $post_string = '';

            foreach($post as $key => $value) {
                $post_string .= $key . '=' . urlencode($value) . '&';
            }

            $post_string = substr($post_string, 0, -1);

            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/create');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

            $result = curl_exec($ch);
            $matches = array();
            preg_match('#<a href="(http://www.google.com/alerts/feeds/[\d/]+)"#', $result, $matches);

            $top_alert = $matches[1];

            return $top_alert;
        }
    }


    function getFormFields($data)
    {
        if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
            $inputs = $this->getInputs($matches[1]);

            return $inputs;
        } else {
            die("didn't find login form");
        }
    }

    function getInputs($form)
    {
        $inputs = array();

        $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

        if ($elements > 0) {
            for($i = 0; $i < $elements; $i++) {
                $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

                if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                    $name  = $name[1];
                    $value = '';

                    if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                        $value = $value[1];
                    }

                    $inputs[$name] = $value;
                }
            }
        }

        return $inputs;
    }
Community
  • 1
  • 1
Jeremy Warne
  • 3,437
  • 2
  • 30
  • 28
  • You might want to look for a PHP question instead. You should not post an answer that is in a different language than the question requires. – Andrew Barber Feb 16 '13 at 01:26
  • 4
    You don't think that actual code in a slightly different language is better than "oh this approach might work, lots of luck!"? In my opinion it's much easier to translate existing code than to write your own. I found this question while looking for the answer to this problem, and if it had had C# code in it, I'd have used that as a starting point and been a happy man. – Jeremy Warne Feb 16 '13 at 05:48
4

Since that's just an HTML form which posts to http://www.google.com/alerts/create?gl=us&hl=en (localization aside, of course), you could programmatically do the same thing. Since (as you mentioned) there's no public API for this, any part of that page/form/URL/etc. could change at any time, and break your application.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Yes but before the post operation, ( as mentioned in that page: http://stackoverflow.com/questions/26857/how-do-you-programmatically-fill-in-a-form-and-post-a-web-page/26881#26881 ) I have to login to my google account. Didn't I? Is it enough to set my google authentication as "remember me" in the machine that my code will work? – xkcd Mar 21 '11 at 14:56
  • No; your application would have to send the session cookie with every post. – Matt Ball Mar 21 '11 at 14:58
  • How can I find out in what format I have to send parameters? Using a sniffer? – xkcd Mar 21 '11 at 15:24
  • What do you mean by "format?" It's just an HTTP post of a form. Your C# code just needs to send HTTP posts that look the same as the browser's. I suppose that Firebug or Fiddle might help, but I'm not sure of what you're asking... – Matt Ball Mar 21 '11 at 15:26
  • Ok I got it. Thanks for your helpful kickstart – xkcd Mar 22 '11 at 15:33