2011-03-21 2 views
1

C#을 사용하여 프로그래밍 방식으로 Google 알리미 (http://www.google.com/alerts)를 만들고 asp.net 애플리케이션에 피드를 소비 할 수 있습니까? Google은 API를 제공하지 않는다는 것을 알고 있습니다. 나는 당신의 제안/아이디어가 필요합니다. 사전에프로그래밍 방식으로 Google 알리미를 작성하는 방법

감사합니다, 그 이후

+1

의 중복 가능성 [? Google 알리미 API (http://stackoverflow.com/questions/860442/google-alerts-api) – jgauffin

+1

그 질문은 경고를 생성하고 파싱 만하는 것에 관해 묻지 않습니다. 나는 그것을 정확한 멍청이로 보지 않는다. –

+0

이것은 관련이있을 수 있습니다 [질문] (http://stackoverflow.com/questions/860442/google-alerts-api) – Richard

답변

4

는 프로그래밍 같은 일을 할 수 http://www.google.com/alerts/create?gl=us&hl=en (물론 옆 현지화)에 게시물 단지 HTML 양식입니다. (언급 한대로) 이에 대한 공개 API는 없으며 해당 페이지/양식/URL/등의 일부입니다. 언제든지 바뀔 수 있으며 신청을 어 기게 될 수 있습니다.

+0

예,하지만 게시 작업 전 (해당 페이지에서 언급 한 것처럼 : http://stackoverflow.com/questions/26857/how-do- 프로그래밍 방식으로 채우기 양식 및 게시물 웹 페이지/26881 # 26881) 내 Google 계정에 로그인해야합니다. 내가 봤어? 내 코드가 작동하는 컴퓨터에서 "내 기억"으로 내 Google 인증을 설정하는 것으로 충분합니까? – anilca

+0

아니오; 귀하의 응용 프로그램은 모든 게시물과 함께 세션 쿠키를 보내야 할 것입니다. –

+0

매개 변수를 보내야하는 형식을 어떻게 알 수 있습니까? 스니퍼 사용? – anilca

3

죄송합니다, 이것은 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; 
    } 
+0

대신 PHP 질문을 찾아 볼 수 있습니다. 질문과 다른 언어로 답변을 게시해서는 안됩니다. –

+1

약간 다른 언어로 된 실제 코드가 "이 접근법이 효과적일지도 모릅니다, 행운을 비네!"보다 낫다고 생각하지 않습니까? 필자의 의견으로는 기존 코드를 번역하는 것보다 훨씬 쉽습니다. 나는이 문제에 대한 해답을 찾고있는 동안이 질문을 발견했다. 그리고 C# 코드가 있다면, 그 코드를 시작점으로 사용하고 행복한 사람이되었을 것이다. –

관련 문제