Class: DisavowTool::ImportedLinks

Inherits:
List
  • Object
show all
Defined in:
lib/disavow_tool/imported_links.rb

Instance Method Summary collapse

Methods inherited from List

#add_domain, #add_url, #comment?, #count, #delete_url, #delete_urls_if_domains, #each, #export, #import, #mass_delete_urls, #new, #restore, #summary, #to_a

Constructor Details

#initialize(import_file = nil) ⇒ ImportedLinks

Returns a new instance of ImportedLinks.



11
12
13
14
15
# File 'lib/disavow_tool/imported_links.rb', line 11

def initialize(import_file=nil)
  @menu_options = {whitelist_domain: "W", whitelist_url: "w", whitelist_all_urls: "a",
                  disavow_domain: "D", disavow_url: "d", open_in_browser: "o", exit: "."}
  super(OPTIONS.import_files)
end

Instance Method Details

#add_url_message(url) ⇒ Object



83
# File 'lib/disavow_tool/imported_links.rb', line 83

def add_url_message(url); "+++ Inserting #{url} in Imported list" end

#analyse(disavowed, white_list) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/disavow_tool/imported_links.rb', line 25

def analyse(disavowed, white_list)
   "Ready to delete analize #{@list.count} remaining links"
   @list.each do |url|
     puts "#{"*"*100}\n*"
     puts "* Analysing url: #{url.on_green}"
     if OPTIONS.network_requests
       print "* "+ "Obtaining website's title...\r".red.blink
       puts "* Website title: #{website_title(url)}".ljust(100)
     end
     puts "* URls with this same domain: #{urls_with_same_domain(url)}"
     puts "*\n#{"*"*100}"

     loop do
       puts menu()
       input = $stdin.getch
       case input
            when @menu_options[:whitelist_url]
              raise "Command run with no whitelist option" if OPTIONS.whitelist == false
              white_list.add_url url
              self.delete_url url

            when @menu_options[:whitelist_domain]
              raise "Command run with no whitelist option" if OPTIONS.whitelist == false
              domain = white_list.add_domain_from_url(url)
              self.delete_url url
              puts "Attempting to remove URLs with the domain #{domain} from imported links to stop anaylsing"
              self.delete_urls_if_domains(domain)

            when @menu_options[:whitelist_all_urls]
              white_list.add_urls_with_same_domain_as url, self

            when @menu_options[:disavow_domain]
              domain = disavowed.add_domain_from_url(url)
              self.delete_url url
              puts "Attempting to remove URLs with the domain #{domain} from imported links to stop anaylsing"
              self.delete_urls_if_domains(domain)

            when @menu_options[:disavow_url]
              disavowed.add_url(url)
              self.delete_url url

            when @menu_options[:open_in_browser]
              open_browser(url)

            when @menu_options[:exit]
              exit

            else
              puts "Incorrect option selected"
       end
       next if input == @menu_options[:open_in_browser]
       break if @menu_options.value?(input) unless input == @menu_options[:open_in_browser]
     end
   puts "\n\n#{@list.count} Remaining links to analize".blue
   end
end

#clean_line!(link) ⇒ Object



92
93
94
# File 'lib/disavow_tool/imported_links.rb', line 92

def clean_line!(link)
  link.gsub!(/\"/, '')  # cleaning some bad links
end

#delete_url_message(url) ⇒ Object



84
# File 'lib/disavow_tool/imported_links.rb', line 84

def delete_url_message(url); "--- Deleting #{url} from imported list" end

#import_message(link) ⇒ Object



85
86
87
# File 'lib/disavow_tool/imported_links.rb', line 85

def import_message(link)
  "Importing #{link} into Imported links"
end

#mass_delete_messageObject



82
# File 'lib/disavow_tool/imported_links.rb', line 82

def mass_delete_message; "Ready to delete the following links" end

#mensaje_sumary_before_exportObject



89
# File 'lib/disavow_tool/imported_links.rb', line 89

def mensaje_sumary_before_export; "Links pending to analyse" end


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/disavow_tool/imported_links.rb', line 97

def menu
  message = ""
  if OPTIONS.whitelist
    message = "[#{@menu_options[:whitelist_domain]}] Whitelist as domain "\
              "[#{@menu_options[:whitelist_url]}] Whitelist URL "\
              "[#{@menu_options[:whitelist_all_urls]}] whitelist as url All urls with this domain\n"
  end
  message += "[#{@menu_options[:disavow_domain]}] Disavow as domain "\
             "[#{@menu_options[:disavow_url]}] Disavow as URL\n"\
             "[#{@menu_options[:open_in_browser]}] to open the URL "\
             "[#{@menu_options[:exit]}] to exit"

end

#message_sumary_importedObject



88
# File 'lib/disavow_tool/imported_links.rb', line 88

def message_sumary_imported; "New links imported" end

#open_browser(link) ⇒ Object



111
112
113
114
115
# File 'lib/disavow_tool/imported_links.rb', line 111

def open_browser(link)
  if Gem.win_platform? then system "start chrome #{URL.escape(link)}" else system "open -a safari #{link}" end
  puts "Opening #{link}...".blue
  return true
end


17
18
19
# File 'lib/disavow_tool/imported_links.rb', line 17

def remove_known_links(known_links)
  mass_delete_urls(known_links)
end


21
22
23
# File 'lib/disavow_tool/imported_links.rb', line 21

def remove_known_links_for_domain(domains)
  delete_urls_if_domains(domains)
end

#urls_with_same_domain(url) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/disavow_tool/imported_links.rb', line 136

def urls_with_same_domain(url)
  domain = URI.parse(URI.escape(url)).host
  counter = 0
  self.each do |link|
    counter += 1 if URI.parse(URI.escape(link)).host == domain
  end
  counter
end

#website_title(url) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/disavow_tool/imported_links.rb', line 117

def website_title(url)
  begin
    Timeout::timeout(SECONDS_TITTLE_REQUEST) do
      page = Nokogiri::HTML(open(URI.escape(url)))
      return "Empty Title" if page.css("title").blank?
      return page.css("title")[0].text
    end
  rescue Timeout::Error => e
    return "Empty Title — Request Time Out: #{e}"
  rescue OpenURI::HTTPError => e
    return "Empty Title. HTTP Error: #{e}"
  rescue SocketError => e
    return "Empty Title. Can't open site: #{e}"
  rescue
    return "Empty Tittle "
  end

end