如何解析来自Google Alerts的数据?

33

首先,除了解析谷歌发送给您的电子邮件消息的文本之外,您如何将Google Alerts信息存入数据库?

似乎没有Google Alerts API。

如果您必须解析文本,您将如何解析出电子邮件消息的相关部分?


我知道这篇文章有点老了,但是经过很多搜索,我终于找到了一个可编程读取、删除和创建谷歌提醒的工作php库:http://coders11.com/googlealertsapi。 - MazarD
3个回答

42

当您创建警报时,请将“发送到”设置为“Feed”,然后您可以像处理其他任何提要一样处理提要XML。这样更容易解析并转换为数据库。


API在哪里?我找不到任何使用Google Alerts API的文档。 - Muneem Habib
这不是一个API,而只是一个提要URL。请参见https://thenextweb.com/google/2013/09/11/google-alerts-regains-rss-delivery-option-it-lost-after-google-readers-demise/,它展示了如何管理您的Google Alert提要RSS源并获取该URL。 - Bill Karwin

12
class googleAlerts{
    public function createAlert($alert){
        $USERNAME = 'XXXXXX@gmail.com';
        $PASSWORD = 'YYYYYY';
        $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>') === false) {
            return false;

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

            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/create');
            curl_setopt($ch, CURLOPT_POST, 0);
            $result = curl_exec($ch);
            //var_dump($result);
            $result = $this->getFormFieldsCreate($result);
            $result['q'] = $alert;
            $result['t'] = '7';
            $result['f'] = '1';
            $result['l'] = '0';
            $result['e'] = 'feed';
            unset($result['PersistentCookie']);

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

            $post_string = substr($post_string, 0, -1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            $result = curl_exec($ch);
            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/manage');
            $result = curl_exec($ch);
            if (preg_match_all('%'.$alert.'(?=</a>).*?<a href=[\'"]http://www.google.com/alerts/feeds/([^\'"]+)%i', $result, $matches)) {
                return ('http://www.google.com/alerts/feeds/'.$matches[1][0]);
            } else {
                return false;
            }


        }
    }

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

            return $inputs;
        } else {
            die('didnt find login form');
        }
    }
    private function getFormFieldsCreate($data)
    {
        if (preg_match('/(<form.*?name=.?.*?<\/form>)/is', $data, $matches)) {
            $inputs = $this->getInputs($matches[1]);

            return $inputs;
        } else {
            die('didnt find login form1');
        }
    }


    private 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;
    }
}
$alert = new googleAlerts;
echo $alert->createAlert('YOUR ALERT');

它将返回您新创建的警报的RSS源链接。

我还没有测试过这段代码 - 但是像你分享创建警报的代码一样,是否也有删除或编辑警报的代码? - Nirav Zaveri
7
供未来读者参考,截止至今日,该解决方案已不再有效。 - Dylan Pierce

0

我在这里找到了一个Google Alerts API 链接。它非常简洁,我还没有测试过。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接