Thursday, October 25, 2007

Thanks to Nikie for Visual Basic .NET version on Yahoo! importing code!


Imports System
Imports System.Collections.Specialized
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Imports MailContact

Imports System.Collections

Namespace Gnilly.Syndication.Mail
Public Class YahooExtract
Private Const _addressBookUrl As String = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv"
Private Const _authUrl As String = "https://login.yahoo.com/config/login?"
Private Const _loginPage As String = "https://login.yahoo.com/config/login"
Private Const _userAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"
Public Function Extract(ByVal uname As String, ByVal upass As String) As ArrayList
Dim result As Boolean = False
Dim myarray As New ArrayList

Try
Dim webClient As New WebClient()
webClient.Headers(HttpRequestHeader.UserAgent) = _userAgent
webClient.Encoding = Encoding.UTF8

Dim firstResponse As Byte() = webClient.DownloadData(_loginPage)
Dim firstRes As String = Encoding.UTF8.GetString(firstResponse)


Dim postToLogin As New NameValueCollection()
Dim regex As New Regex("type=""hidden"" name=""(.*?)"" value=""(.*?)""", RegexOptions.IgnoreCase)
Dim match As Match = regex.Match(firstRes)
While match.Success
If match.Groups(0).Value.Length > 0 Then
postToLogin.Add(match.Groups(1).Value, match.Groups(2).Value)
End If
match = regex.Match(firstRes, match.Index + match.Length)
End While


postToLogin.Add(".save", "Sign In")
postToLogin.Add(".persistent", "y")

'Dim login As String = credential.UserName.Split("@"c)(0)
Dim login As String = uname.Split("@")(0)

postToLogin.Add("login", login)
postToLogin.Add("passwd", upass)

webClient.Headers(HttpRequestHeader.UserAgent) = _userAgent
webClient.Headers(HttpRequestHeader.Referer) = _loginPage
webClient.Encoding = Encoding.UTF8
webClient.Headers(HttpRequestHeader.Cookie) = webClient.ResponseHeaders(HttpResponseHeader.SetCookie)

webClient.UploadValues(_authUrl, postToLogin)
Dim cookie As String = webClient.ResponseHeaders(HttpResponseHeader.SetCookie)

'If String.IsNullOrEmpty(cookie) Then
'Return False
'End If

Dim newCookie As String = String.Empty
Dim tmp1 As String() = cookie.Split(","c)
For Each var As String In tmp1
Dim tmp2 As String() = var.Split(";"c)
newCookie = IIf([String].IsNullOrEmpty(newCookie), tmp2(0), newCookie + ";" + tmp2(0))
Next

' set login cookie
webClient.Headers(HttpRequestHeader.Cookie) = newCookie
Dim thirdResponse As Byte() = webClient.DownloadData(_addressBookUrl)
Dim thirdRes As String = Encoding.UTF8.GetString(thirdResponse)

Dim crumb As String = String.Empty
Dim regexCrumb As New Regex("type=""hidden"" name=""\.crumb"" id=""crumb1"" value=""(.*?)""", RegexOptions.IgnoreCase)
match = regexCrumb.Match(thirdRes)
If match.Success AndAlso match.Groups(0).Value.Length > 0 Then
crumb = match.Groups(1).Value
End If


Dim postDataAB As New NameValueCollection()
postDataAB.Add(".crumb", crumb)
postDataAB.Add("vcp", "import_export")
postDataAB.Add("submit[action_export_yahoo]", "Export Now")

webClient.Headers(HttpRequestHeader.UserAgent) = _userAgent
webClient.Headers(HttpRequestHeader.Referer) = _addressBookUrl

Dim FourResponse As Byte() = webClient.UploadValues(_addressBookUrl, postDataAB)
Dim csvData As String = Encoding.UTF8.GetString(FourResponse)

Dim lines As String() = csvData.Split(Chr(10))
'Dim list1 As Hashtable()

For Each line As String In lines
Dim items As String() = line.Split(","c)
If items.Length < 5 Then
Continue For
End If
Dim email As String = items(4)
Dim name As String = items(3)
If Not String.IsNullOrEmpty(email) AndAlso Not String.IsNullOrEmpty(name) Then
email = email.Trim(""""c)
name = name.Trim(""""c)
If Not email.Equals("Email") AndAlso Not name.Equals("Nickname") Then
Dim mailContact As New MailContact()
mailContact.Name = name
mailContact.Email = email
myarray.Add(email)
'list.Add(mailContact)


End If
End If
Next

result = True
Catch
End Try
Return myarray
End Function
End Class
End Namespace

Public Class MailContact
Private _email As String = String.Empty
Private _name As String = String.Empty

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public Property Email() As String
Get
Return _email
End Get
Set(ByVal value As String)
_email = value
End Set
End Property

Public ReadOnly Property FullEmail() As String
Get
Return Email
End Get
End Property
End Class

4 comments:

silly said...

Hi Gnilly,

I have been working the same code for gmail had got a php code for scraping the same.I am trying to create a replica of the php to .net.In php they have used curl functions to retrive the cookies.Please refer http://svetlozar.net/page/Import-Gmail-Addresses.html
The prob is using .net we can only get 3 values of Set-Cookie from the header wherein there are 21 of the values and the prominent one is GMAIL-AT = (sumthing) If we could retrive this.We can directly assign it to a hidden field value named "at" = "GMAIL-AT(value)" and we can login to the contacts page.However i can only retrive 3 values.Could you help me out.

gnilly boy said...

Don't implement it independently!

Check my latest post: Import GMail

Ashish Patel said...

Please help me. How to fetch contact or address list fetching form any email account using their EmailID and Password programmatically??
I want to fetch from atleast following Mail Providers:

Hotmail
Rediffmail
yahoomail
Gmail

Please any idea or help??

Ashish Patel said...

Please help me. How to fetch contact or address list fetching form any email account using their EmailID and Password programmatically??
I want to fetch from atleast following Mail Providers:

Hotmail
Rediffmail
yahoomail
Gmail

Please any idea or help??