【问题标题】:How to creating a Google Alert programmatically如何以编程方式创建 Google 快讯
【发布时间】:2011-07-19 18:02:07
【问题描述】:

我可以使用 C# 以编程方式创建 Google 警报 (http://www.google.com/alerts) 并使用提要以在 asp.net 应用程序中显示吗?我知道谷歌没有提供 api 来做到这一点。我需要你的建议/想法。

提前致谢,

【问题讨论】:

标签: c# asp.net alert


【解决方案1】:

由于这只是一个发布到http://www.google.com/alerts/create?gl=us&hl=en 的 HTML 表单(当然,本地化除外),您可以通过编程方式执行相同的操作。由于(正如您所提到的)没有为此的公共 API,因此该页面/表单/URL/等的任何部分。随时可能更改,并破坏您的应用程序。

【讨论】:

  • 是的,但在发布操作之前,(如该页面所述:stackoverflow.com/questions/26857/…)我必须登录我的谷歌帐户。我不是吗?在我的代码可以工作的机器中将我的谷歌身份验证设置为“记住我”就足够了吗?
  • 否;您的应用程序必须在每个帖子中发送会话 cookie。
  • 我怎样才能知道我必须以什么格式发送参数?使用嗅探器?
  • “格式”是什么意思?它只是一个表单的 HTTP 帖子。您的 C# 代码只需要发送看起来与浏览器相同的 HTTP 帖子。我想 Firebug 或 Fiddle 可能会有所帮助,但我不确定你在问什么......
  • 好的,我知道了。感谢您的帮助启动
【解决方案2】:

抱歉,这不是 C#,但应该对任何想要实现这样的东西的人有所帮助。此 PHP 代码创建提要并返回 RSS URL 以供使用。

基于此页面的代码:

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;
    }

【讨论】:

  • 您可能想寻找一个 PHP 问题。您不应发布与问题要求的语言不同的答案。
  • 你不认为用稍微不同的语言编写的实际代码比“哦,这种方法可能有效,祝你好运!”更好?在我看来,翻译现有代码比编写自己的代码要容易得多。我在寻找这个问题的答案时发现了这个问题,如果里面有 C# 代码,我会以此为起点,做一个快乐的人。
猜你喜欢
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多