2009-05-13 22 views
31

Parece que no hay API de Google Alerts.Google Alerts API?

En primer lugar, ¿cómo obtendría la información de Google Alerts en una base de datos que no sea para analizar el texto del mensaje de correo electrónico que Google le envía?

Si debe analizar texto, ¿cómo haría para analizar las piezas relevantes del mensaje de correo electrónico?

Respuesta

40

Cuando crea la alerta, establezca "Entregar a" a "Feed" y luego puede consumir XML como lo haría con cualquier otra fuente. Esto es mucho más fácil de analizar y resumir en una base de datos.

+0

¿Dónde está la API? No puedo encontrar ningún documento de uso de las alertas de Google api –

+0

No es tanto una API como solo una URL de fuente. Consulte https://thenextweb.com/google/2013/09/11/google-alerts-regains-rss-delivery-option-it-lost-after-google-readers-demise/ que muestra cómo administrar su RSS de Google Alerts. alimentar y obtener la URL. –

2

Sé que este post es un poco viejo, pero después de buscar mucho, por fin encontré una biblioteca PHP de trabajo para leer mediante programación, eliminar y crear alertas de Google:

http://coders11.com/googlealertsapi

+6

Esa API ya no está disponible de forma gratuita. FWIW. –

+0

Pero sigue funcionando, no obstante, – MazarD

12
class googleAlerts{ 
    public function createAlert($alert){ 
     $USERNAME = '[email protected]'; 
     $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'); 

Será enlace de retorno al feed RSS de su alerta de nueva creación

+0

No he probado el código. Pero como ha compartido el código para crear una alerta, ¿hay algún código para eliminar o editar alertas también? –

+4

Para futuros lectores, a partir de hoy, esta solución ya no funciona. –

+0

¿Alguien tiene una actualización de este código? – MarcoZen

1

también podría probar este C# library

+1

El enlace está muerto. Versión en caché de Google: http://webcache.googleusercontent.com/search?q=cache:cYyxb6oQ3oYJ:www.frickingnutz.com/%3Fq%3Dnode/51+&cd=6&hl=en&ct=clnk&gl=ie – Seany84