<commit_before>GoogleClientLogin = require('googleclientlogin').GoogleClientLogin
request = require 'request'

module.exports = class GoogleContacts
  constructor: (@options = {}) ->
    @options.service = 'contacts'
    @url = "https://google.com/m8/feeds/contacts/#{@options.email}/thin?alt=json&max-results=9999"

  connect: (callback) ->
    @auth = new GoogleClientLogin(@options)
    @auth.on GoogleClientLogin.events.login, =>
      callback?(null)
    @auth.on GoogleClientLogin.events.error, (err) =>
      callback?(err)
    @auth.login()

  getContacts: (callback) ->
    params =
      url: @url
      headers:
        'Authorization': "GoogleLogin auth=#{@auth.getAuthId()}"
    request params, (err, response, body) =>
      if err?
        callback?(err, null)
      else
        page = @_parseBody(body)
        callback?(null, page)

  _parseBody: (body) ->
    (require 'fs').writeFileSync('contacts1.json', body)
    feed = JSON.parse(body).feed
    page =
      startIndex: feed.openSearch$startIndex.$t
      itemsPerPage: feed.openSearch$itemsPerPage.$t
      totalResults: feed.openSearch$totalResults.$t
      contacts: []
    for entry in feed.entry
      page.contacts.push(@_parseEntry(entry))
    page

  _parseEntry: (entry) ->
    {
      id: @_parseId(entry.id.$t),
      name: entry.title.$t
      email: entry.gd$email[0].address
    }<commit_msg>don't crash when there's no email<commit_after>GoogleClientLogin = require('googleclientlogin').GoogleClientLogin
request = require 'request'

module.exports = class GoogleContacts
  constructor: (@options = {}) ->
    @options.service = 'contacts'
    @url = "https://google.com/m8/feeds/contacts/#{@options.email}/thin?alt=json&max-results=9999"

  connect: (callback) ->
    @auth = new GoogleClientLogin(@options)
    @auth.on GoogleClientLogin.events.login, =>
      callback?(null)
    @auth.on GoogleClientLogin.events.error, (err) =>
      callback?(err)
    @auth.login()

  getContacts: (callback) ->
    params =
      url: @url
      headers:
        'Authorization': "GoogleLogin auth=#{@auth.getAuthId()}"
    request params, (err, response, body) =>
      if err?
        callback?(err, null)
      else
        page = @_parseBody(body)
        callback?(null, page)

  _parseBody: (body) ->
    (require 'fs').writeFileSync('contacts1.json', body)
    feed = JSON.parse(body).feed
    page =
      startIndex: feed.openSearch$startIndex.$t
      itemsPerPage: feed.openSearch$itemsPerPage.$t
      totalResults: feed.openSearch$totalResults.$t
      contacts: []
    for entry in feed.entry
      contact = @_parseEntry(entry)
      page.contacts.push(contact) if contact?
    page

  _parseEntry: (entry) ->
    emails = entry.gd$email
    return null if emails.length == 0
    {
      id: @_parseId(entry.id.$t),
      name: entry.title.$t
      email: entry.gd$email[0].address
    }
{'fraction_non_alphanumeric': 0.1436502428868841, 'fraction_numerical': 0.005204718945176961, 'mean_word_length': 3.0605633802816903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4090674', 'n_tokens_mistral': 920, 'n_tokens_neox': 914, 'n_words': 174}