<commit_before>module EvernoteHelper
  #http://discussion.evernote.com/topic/15321-evernote-ruby-thrift-client-error/
  require 'rubygems'
  require 'nokogiri'

  def add_evernote_task(guid)
    cloud_service = CloudService.where( :name => 'evernote' ).first_or_create
    cloud_note = CloudNote.where(:cloud_note_identifier => guid, :cloud_service_id => cloud_service.id).first_or_create
    cloud_note.update_attribute( :dirty, true )
    cloud_note.update_attribute( :sync_retries, 0 )
    # To do the actual updates asynchronously, commented out the following line.
    # Then add a cron job for EvernoteHelper.run_evernote_tasks.
    run_evernote_tasks
    "OK"
  end

  def run_evernote_tasks
    cloud_notes = CloudNote.needs_syncdown
    cloud_notes.each do |cloud_note|
      syncdown_note(cloud_note.cloud_note_identifier)
    end
  end

  def syncdown_note(guid)
    cloud_service = CloudService.where(:name => 'evernote').first_or_create
    auth = cloud_service.auth

    if auth.empty?
      CloudServiceMailer.auth_not_found('evernote').deliver
      logger.error t('notes.sync.rejected.not_authenticated', :provider => 'Evernote', :guid => guid)
    else
      edam_noteStoreUrl = auth.extra.access_token.params[:edam_noteStoreUrl]
      oauth_token = auth.extra.access_token.params[:oauth_token]
      note_store = get_note_store(edam_noteStoreUrl)

      note_metadata = note_store.getNote(oauth_token, guid, false, false, false, false)
      cloud_note = CloudNote.where(:cloud_note_identifier => note_metadata.guid, :cloud_service_id => cloud_service.id).first_or_create

      required_notebooks = Settings.evernote.notebooks.split(' ')
      required_tags = Settings.evernote.tags.split(' ')

      if !required_notebooks.include?(note_metadata.notebookGuid)
        cloud_note.destroy
        logger.info t('notes.sync.rejected.not_in_notebook', :provider => 'Evernote', :guid => guid, :title => note_metadata.title)
      else
        note = Note.where(:id => cloud_note.note_id).first
        note_tags = note_store.getNoteTagNames(oauth_token, guid)<commit_msg>Improves condition for missing auth.<commit_after>module EvernoteHelper
  #http://discussion.evernote.com/topic/15321-evernote-ruby-thrift-client-error/
  require 'rubygems'
  require 'nokogiri'

  def add_evernote_task(guid)
    cloud_service = CloudService.where( :name => 'evernote' ).first_or_create
    cloud_note = CloudNote.where(:cloud_note_identifier => guid, :cloud_service_id => cloud_service.id).first_or_create
    cloud_note.update_attribute( :dirty, true )
    cloud_note.update_attribute( :sync_retries, 0 )
    # To do the actual updates asynchronously, commented out the following line.
    # Then add a cron job for EvernoteHelper.run_evernote_tasks.
    run_evernote_tasks
    "OK"
  end

  def run_evernote_tasks
    cloud_notes = CloudNote.needs_syncdown
    cloud_notes.each do |cloud_note|
      syncdown_note(cloud_note.cloud_note_identifier)
    end
  end

  def syncdown_note(guid)
    cloud_service = CloudService.where(:name => 'evernote').first_or_create
    auth = cloud_service.auth

    if !auth || auth.empty?
      CloudServiceMailer.auth_not_found('evernote').deliver
      logger.error t('notes.sync.rejected.not_authenticated', :provider => 'Evernote', :guid => guid)
    else
      edam_noteStoreUrl = auth.extra.access_token.params[:edam_noteStoreUrl]
      oauth_token = auth.extra.access_token.params[:oauth_token]
      note_store = get_note_store(edam_noteStoreUrl)

      note_metadata = note_store.getNote(oauth_token, guid, false, false, false, false)
      cloud_note = CloudNote.where(:cloud_note_identifier => note_metadata.guid, :cloud_service_id => cloud_service.id).first_or_create

      required_notebooks = Settings.evernote.notebooks.split(' ')
      required_tags = Settings.evernote.tags.split(' ')

      if !required_notebooks.include?(note_metadata.notebookGuid)
        cloud_note.destroy
        logger.info t('notes.sync.rejected.not_in_notebook', :provider => 'Evernote', :guid => guid, :title => note_metadata.title)
      else
        note = Note.where(:id => cloud_note.note_id).first
        note_tags = note_store.getNoteTagNames(oauth_token, guid)
{'fraction_non_alphanumeric': 0.10175939134569663, 'fraction_numerical': 0.0028530670470756064, 'mean_word_length': 4.271929824561403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '792335', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1336, 'n_words': 230}