YahooExtract.cs.svn-base
上传用户:asz878
上传日期:2020-04-01
资源大小:189k
文件大小:6k
源码类别:

WEB邮件程序

开发平台:

Visual C++

  1. //Code by Gnilly (http://gnillydev.blogspot.com)
  2. //Offical site: http://opencontacts.sourceforge.net
  3. //This program is free software: you can redistribute it and/or modify
  4. //it under the terms of the GNU Lesser General Public License as published by
  5. //the Free Software Foundation, either version 3 of the License, or
  6. //(at your option) any later version.
  7. //This program is distributed in the hope that it will be useful,
  8. //but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. //GNU Lesser General Public License for more details.
  11. //You should have received a copy of the GNU Lesser General Public License
  12. //along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.Specialized;
  16. using System.Net;
  17. using System.Text;
  18. using System.Text.RegularExpressions;
  19. namespace OpenContactsNet
  20. {
  21.     public class YahooExtract : IMailContactExtract
  22.     {
  23.         private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv";
  24.         private const string _authUrl = "https://login.yahoo.com/config/login?";
  25.         private const string _loginPage = "https://login.yahoo.com/config/login";
  26.         private const string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
  27.         #region IMailContactExtract Members
  28.         public bool Extract( NetworkCredential credential, out MailContactList list )
  29.         {
  30.             bool result = false;
  31.             list = new MailContactList();
  32.             try
  33.             {
  34.                 WebClient webClient = new WebClient();
  35.                 webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
  36.                 webClient.Encoding = Encoding.UTF8;
  37.                 byte[] firstResponse = webClient.DownloadData( _loginPage );
  38.                 string firstRes = Encoding.UTF8.GetString( firstResponse );
  39.                 NameValueCollection postToLogin = new NameValueCollection();
  40.                 Regex regex = new Regex( "type="hidden" name="(.*?)" value="(.*?)"", RegexOptions.IgnoreCase );
  41.                 Match match = regex.Match( firstRes );
  42.                 while ( match.Success )
  43.                 {
  44.                     if ( match.Groups[ 0 ].Value.Length > 0 )
  45.                     {
  46.                         postToLogin.Add( match.Groups[ 1 ].Value, match.Groups[ 2 ].Value );
  47.                     }
  48.                     match = regex.Match( firstRes, match.Index + match.Length );
  49.                 }
  50.                 postToLogin.Add( ".save", "Sign In" );
  51.                 postToLogin.Add( ".persistent", "y" );
  52.                 string login = credential.UserName.Split( '@' )[ 0 ];
  53.                 postToLogin.Add( "login", login );
  54.                 postToLogin.Add( "passwd", credential.Password );
  55.                 webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
  56.                 webClient.Headers[ HttpRequestHeader.Referer ] = _loginPage;
  57.                 webClient.Encoding = Encoding.UTF8;
  58.                 webClient.Headers[ HttpRequestHeader.Cookie ] = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];
  59.                 webClient.UploadValues( _authUrl, postToLogin );
  60.                 string cookie = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];
  61.                 if ( string.IsNullOrEmpty( cookie ) )
  62.                 {
  63.                     return false;
  64.                 }
  65.                 string newCookie = string.Empty;
  66.                 string[] tmp1 = cookie.Split( ',' );
  67.                 foreach ( string var in tmp1 )
  68.                 {
  69.                     string[] tmp2 = var.Split( ';' );
  70.                     newCookie = String.IsNullOrEmpty( newCookie ) ? tmp2[ 0 ] : newCookie + ";" + tmp2[ 0 ];
  71.                 }
  72.                 // set login cookie
  73.                 webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
  74.                 byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
  75.                 string thirdRes = Encoding.UTF8.GetString( thirdResponse );
  76.                 string crumb = string.Empty;
  77.                 Regex regexCrumb = new Regex( "type="hidden" name="\.crumb" id="crumb1" value="(.*?)"", RegexOptions.IgnoreCase );
  78.                 match = regexCrumb.Match( thirdRes );
  79.                 if ( match.Success && match.Groups[ 0 ].Value.Length > 0 )
  80.                 {
  81.                     crumb = match.Groups[ 1 ].Value;
  82.                 }
  83.                 NameValueCollection postDataAB = new NameValueCollection();
  84.                 postDataAB.Add( ".crumb", crumb );
  85.                 postDataAB.Add( "vcp", "import_export" );
  86.                 postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );
  87.                 webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
  88.                 webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;
  89.                 byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
  90.                 string csvData = Encoding.UTF8.GetString( FourResponse );
  91.                 string[] lines = csvData.Split( 'n' );
  92.                 foreach ( string line in lines )
  93.                 {
  94.                     string[] items = line.Split( ',' );
  95.                     if ( items.Length < 5 )
  96.                     {
  97.                         continue;
  98.                     }
  99.                     string email = items[ 4 ];
  100.                     string name = items[ 3 ];
  101.                     if ( !string.IsNullOrEmpty( email ) && !string.IsNullOrEmpty( name ) )
  102.                     {
  103.                         email = email.Trim( '"' );
  104.                         name = name.Trim( '"' );
  105.                         if ( !email.Equals( "Email" ) && !name.Equals( "Nickname" ) )
  106.                         {
  107.                             MailContact mailContact = new MailContact();
  108.                             mailContact.Name = name;
  109.                             mailContact.Email = email;
  110.                             list.Add( mailContact );
  111.                         }
  112.                     }
  113.                 }
  114.                 result = true;
  115.             }
  116.             catch
  117.             {
  118.             }
  119.             return result;
  120.         }
  121.         #endregion
  122.     }
  123. }